-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
docs.js
173 lines (147 loc) · 5.6 KB
/
docs.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
/**
Script used for creating docs. Nothing to see here
*/
const fs = require('fs')
const path = require('path')
const markdownMagic = require('markdown-magic')
const globby = require('markdown-magic').globby
const toTitleCase = (str) => { // eslint-disable-line
return str.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
}
const formatName = (string) => { // eslint-disable-line
return toTitleCase(string.replace(/-/g, ' '));
}
const cleanMatches = (matches) => {
return matches.map((m) => {
return m
.replace(/^\s+|\s+$/g,'')
.replace(/\/\*/g, '') // remove js comments
.replace(/\*\//g, '') // remove js comments
.replace(/^#/g, '') // remove # comments
.replace(/#$/g, '') // remove # comments
.replace(/^\<\!--/g, '') // remove html comments
.replace(/--\>$/g, '') // remove html comments
.replace(/^\s+|\s+$/g,'')
//.replace(/^!$/g,'') // remove trailing !
.replace(/\n#\s+/g,'\n')
.replace(/^Step\s+/g, '')
})
}
const config = {
transforms: {
README_BOTTOM(content, options, instance) {
console.log('instance.outputDir', instance.outputDir)
const repoBase = 'https://github.com/DavidWells/serverless-workshop/tree/master'
const baseLink = `${repoBase}/${instance.outputDir}`
let answersLink = baseLink.replace(/lessons/g, 'lessons-code-complete');
answersLink = answersLink.replace(/\_instructor/g, 'lessons-code-complete');
const link = `## Complete code
If you need help or get stuck refer to the completed code of this lesson
[View Complete Code](${answersLink})`;
return link;
},
// Build lessons list from _instructor directory
GENERATE_LESSONS_LIST(content, options) {
const lessonsPath = path.join(__dirname, '_instructor')
const files = fs.readdirSync(lessonsPath)
const data = files.filter((file) => {
const filePath = path.join(lessonsPath, file)
const stat = fs.statSync(filePath)
// return all directories
return stat && stat.isDirectory()
}).map((file) => {
const filePath = path.join(lessonsPath, file)
const examples = globby.sync([`${filePath}/**/**.md`]);
let md = `### ${formatName(file)}
${generateTable(examples)}
`
return md
})
return data.join('\n');
},
GENERATE_LESSONS_STEPS(content, options, instance) {
//console.log('instance.outputDir', instance.outputDir)
// debug single file
// if (instance.outputDir !== "_instructor/events/step-functions") {
// return content
// }
const lessonFiles = globby.sync(['**', '!node_modules'], {
cwd: instance.outputDir
})
const jsRegex = /\/\* Step([\s\S]*?)\*\//g
const ymlRegex = / *?# Step([\s\S]*?) #\n*?/g
const htmlRegex = / *?\<\!-- Step([\s\S]*?) ?--\>\n*?/g
var matches = []
if (lessonFiles) {
lessonFiles.map((f) => {
const filePath = path.join(instance.outputDir, f)
if (fs.lstatSync(filePath).isDirectory()) {
// skip dirs
return;
}
const fileContents = fs.readFileSync(filePath, 'utf8')
const fileType = path.extname(f)
var regex = jsRegex
if (fileType === '.js') {
regex = jsRegex
} else if (fileType === '.yml' || fileType === '.yaml') {
regex = ymlRegex
} else if (fileType === '.md') {
regex = htmlRegex
}
const hasMatch = fileContents.match(regex)
if (hasMatch) {
const formatMatch = hasMatch.map((match) => {
return match.replace('this_file', `\`${f}\``)
})
matches = matches.concat(formatMatch)
}
})
}
// console.log('matches', matches)
const steps = cleanMatches(matches)
const sortedSteps = steps.reduce((accumulator, currentValue, currentIndex, array) => {
const number = currentValue.match(/^[0-9]{1,3}/)[0]
// console.log(number)
// console.log(parseInt(number, 10))
accumulator[currentIndex] = {
step: parseInt(number, 10),
value: currentValue
}
return accumulator
}, []).sort(function(a,b){
return a.step - b.step
}).map((item) => {
return item.value
})
return sortedSteps.join('\n\n')
}
}
}
// build lessons table
function generateTable(examples) {
let md = '| Lesson | Final Code |\n';
md += '|:--------------------------- |:-----|\n';
examples.forEach((example) => {
console.log(example)
const contents = fs.readFileSync(example, 'utf8')
const dirname = path.basename(path.dirname(example))
const parentDir = path.basename(path.dirname(path.dirname(example)))
console.log('dirname', dirname)
const repoBase = 'https://github.com/DavidWells/serverless-workshop/tree/master'
const baseLink = `${repoBase}/_instructor/${parentDir}/${dirname}`
const lessonLink = baseLink.replace(/_instructor/g, 'lessons');
const answersLink = baseLink.replace(/_instructor/g, 'lessons-code-complete');
//console.log(content)
const heading = contents.match(/^# (.*)/g)
console.log('heading', heading)
const description = (heading && heading[0]) ? heading[0].replace("# ", '') : '';
// add table rows
md += `| [${formatName(dirname)}](${lessonLink}) <br/> ${description} | [Complete Code](${answersLink}) |\n`;
// md += baseLink
});
return md
}
markdownMagic(['README.md', '_instructor/**/**.md'], config, () => {
console.log('Docs updated!'); // eslint-disable-line
})