-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ts
171 lines (154 loc) · 4.41 KB
/
build.ts
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
import { load } from 'js-yaml'
import { join } from 'path'
import { writeFile, readFile } from 'fs'
import { promisify } from 'util'
import { parse } from './utils/parse'
import { getArg } from './utils/args'
import { getCommits, CommitLogObject } from './utils/commits'
import skeletonSchema from './schema/skeleton'
import tutorialSchema from './schema/tutorial'
import { validateSchema } from './utils/validateSchema'
import { validateMarkdown } from './utils/validateMarkdown'
import * as T from '../typings/tutorial'
const write = promisify(writeFile)
const read = promisify(readFile)
export type BuildConfigOptions = {
text: string // text document from markdown
config: T.Tutorial // yaml config file converted to json
commits: CommitLogObject // an object of tutorial positions with a list of commit hashes
}
type BuildArgs = {
dir: string
markdown: string
yaml: string
output: string
validate: boolean
}
async function build (args: string[]) {
let options: BuildArgs
try {
// dir - default .
const dir = !args.length || args[0].match(/^-/) ? '.' : args[0]
// -m --markdown - default TUTORIAL.md
const markdown =
getArg(args, { name: 'markdown', alias: 'm' }) || 'TUTORIAL.md'
// -y --yaml - default coderoad-config.yml
const yaml = getArg(args, { name: 'yaml', alias: 'y' }) || 'coderoad.yaml'
// -o --output - default coderoad.json
const output =
getArg(args, { name: 'output', alias: 'o' }) || 'tutorial.json'
const validate = getArg(args, { name: 'validate', alias: 'v' }) !== 'false'
console.log(`Building CodeRoad ${output}...`)
options = {
dir,
output,
markdown,
yaml,
validate
}
} catch (e) {
console.error('Error parsing build logs')
console.error(e.message)
return
}
// path to run build from
const localPath = join(process.cwd(), options.dir)
// load markdown and files
let _markdown: string
let _yaml: string
try {
;[_markdown, _yaml] = await Promise.all([
read(join(localPath, options.markdown), 'utf8'),
read(join(localPath, options.yaml), 'utf8')
])
} catch (e) {
console.error('Error reading file:')
console.error(e.message)
return
}
// validate markdown loosely
try {
const isValid = validateMarkdown(_markdown)
if (!isValid) {
console.warn('Invalid markdown')
}
} catch (e) {
console.error('Error validating markdown:')
console.error(e.message)
return
}
// parse yaml skeleton config
let skeleton
try {
skeleton = load(_yaml) as T.TutorialSkeleton
if (!skeleton || !Object.keys(skeleton).length) {
throw new Error(`Skeleton at "${options.yaml}" is invalid`)
}
} catch (e) {
console.error('Error parsing yaml')
console.error(e.message)
return
}
// validate skeleton based on skeleton json schema
try {
const valid = validateSchema(skeletonSchema, skeleton)
if (!valid) {
console.error('Skeleton validation failed. See above to see what to fix')
return
}
} catch (e) {
console.error('Error validating tutorial schema:')
console.error(e.message)
}
// load git commits to use in parse step
let commits: CommitLogObject
try {
commits = await getCommits({
localDir: localPath,
codeBranch: skeleton.config.repo.branch
})
} catch (e) {
console.error('Error loading commits:')
console.error(e.message)
return
}
// parse tutorial from markdown and yaml
let tutorial: T.Tutorial
try {
tutorial = await parse({
text: _markdown,
skeleton,
commits
})
} catch (e) {
console.error('Error parsing tutorial:')
console.error(e.message)
return
}
// validate tutorial based on tutorial json schema
try {
if (options.validate) {
const valid = validateSchema(tutorialSchema, tutorial)
if (!valid) {
console.error(
'Tutorial validation failed. See above to see what to fix'
)
// continue rather than exiting early
}
}
} catch (e) {
console.error('Error validating tutorial schema:')
console.error(e.message)
}
// write tutorial
if (tutorial) {
try {
await write(options.output, JSON.stringify(tutorial, null, 2), 'utf8')
console.info(`Success! See output at ${options.output}`)
} catch (e) {
console.error('Error writing tutorial json file:')
console.error(e.message)
}
}
}
export default build