generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate-github-skills-readme-from-lab-config.js
176 lines (136 loc) · 5.22 KB
/
generate-github-skills-readme-from-lab-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const fs = require('fs').promises;
const path = require('path');
const { parse } = require('yaml');
function ytVideoMdTemplate(title, ytVideoId) {
return `[](https://www.youtube.com/watch?v=${ytVideoId} "${title}")`;
}
function readmeTemplate(title, description, courseDetailsFile, helpUrl, helpText) {
return `
# ${title}
${description}
## Course Details
${courseDetailsFile}
### Help
${helpUrl && helpText ? `[${helpText}](${helpUrl})` : `[Post on the #academy Slack channel](https://wix.slack.com/archives/CE2AFKXEK)`}
## Steps
`;
}
function extractYTVidfromUrl(videoUrl) {
return videoUrl && videoUrl.includes('youtube.com/embed/') && videoUrl.split('embed/')[1].split('/')[0]
}
function stepTemplate(index, stepsCount, title, description, video, link, {
mdFileContent,
courseStep,
noLink
}) {
const stepTitle = `${(index + 1).toString().padStart(2, '0')} - ${title}`;
const ytVideoId = extractYTVidfromUrl(video);
return `
<details id=${index === stepsCount - 1 ? 'X' : index} ${index === 0 ? 'open' : ''}>
<summary><h2>${courseStep ? 'COURSE: ' : ''}${stepTitle}</h2></summary>
${description}
${ytVideoId ?
`
#### Watch the video
${ytVideoMdTemplate(title, ytVideoId)}
` : ''}
${mdFileContent ? mdFileContent+'\n' : ''}
${!noLink && link ? `👉 [${title}](${link})` : ''}
</details>
`;
}
function isPathtoRelativeMdFile(path) {
return path.endsWith('.md') && !path.startsWith('https://') && RegExp('(./)?[a-zA-Z0-9/]*.md').test(path)
}
async function fetchCourseDetails(course, {octokit, reporter}) {
let [owner, repo] = course.split('/');
if (!repo) {
repo = course;
owner = process.env.GITHUB_REPOSITORY_OWNER;
}
try {
const courseConfigResponse = await octokit.rest.repos.getContent({
owner,
repo,
path: 'config.yml'
});
const courseConfig = parse(Buffer.from(courseConfigResponse.data.content, 'base64').toString());
const link = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}`;
const title = courseConfig.title;
const description = courseConfig.description;
return { link, title, description };
} catch (error) {
reporter.info(`failed to fetch course details for ${course}`)
reporter.error(error);
return null;
}
}
async function generateReadmeFromConfig(
configPath='config.yml',
courseDetailsPath='course-details.md',
readmePath='./README.md',
rootPath='./',
octokit = null,
reporter = console,
// ADDON: Options
{
inlineMDlinks
} = {}) {
configPath = path.resolve(rootPath, configPath);
courseDetailsPath = path.resolve(rootPath, courseDetailsPath);
readmePath = path.resolve(rootPath, readmePath);
const yamlFile = await fs.readFile(configPath, 'utf8');
const labConfig = parse(yamlFile);
const { title, description, helpText, helpUrl } = labConfig;
const courseDetailsFile = await fs.readFile(courseDetailsPath, 'utf8');
let _readmeTemplate = readmeTemplate(title, description, courseDetailsFile, helpUrl, helpText);
let labConfigSteps = labConfig.steps;
// ADDON: files & inline md links
labConfigSteps = await Promise.all(labConfigSteps.map(async (step, index) => {
const { link, file, course, inlineMDlink } = step;
let mdFileContent = null;
let courseStep = false;
if (course) {
const courseDetails = await fetchCourseDetails(course, { octokit, reporter });
if (courseDetails) {
const { link: courseLink, title: courseTitle, description: courseDescription } = courseDetails;
step.link ||= courseLink;
step.title ||= courseTitle;
step.description ||= courseDescription;
courseStep = true;
}
} else {
if (((inlineMDlinks || inlineMDlink) && (link && isPathtoRelativeMdFile(link))) || file) {
const filePath = file || (inlineMDlinks || inlineMDlink) && link;
mdFileContent = await fs.readFile(path.resolve(rootPath, filePath), 'utf8');
}
}
// TODO: YouTube thumbnail fetcher
return {...step, index, mdFileContent, courseStep, noLink: (inlineMDlinks || inlineMDlink)}
}))
labConfigSteps.sort((a, b) => a.index - b.index);
try {
labConfigSteps.forEach((step, index) => {
const { title, description, video, link, ...addonProps } = step;
const mdTemplate = stepTemplate(index, labConfigSteps.length, title, description, video, link,
// ADDON: Properties
{
...addonProps,
inlineMDlinks,
})
_readmeTemplate = _readmeTemplate.concat(mdTemplate);
});
await fs.writeFile(readmePath, _readmeTemplate)
return _readmeTemplate;
} catch (error) {
reporter.error('README.md GitHub Skill format file creating error: ', error);
throw error;
}
}
module.exports = {
generateReadmeFromConfig,
isPathtoRelativeMdFile,
extractYTVidfromUrl,
readmeTemplate,
stepTemplate
}