|
1 |
| -import * as yamlParser from "js-yaml"; |
2 |
| -import * as path from "path"; |
3 |
| -import * as fs from "fs"; |
4 |
| -import * as util from "util"; |
5 |
| -import { parse } from "./utils/parse"; |
6 |
| -import { getArg } from "./utils/args"; |
7 |
| -import { getCommits, CommitLogObject } from "./utils/commits"; |
8 |
| -import skeletonSchema from "./schema/skeleton"; |
9 |
| -import tutorialSchema from "./schema/tutorial"; |
10 |
| -import { validateSchema } from "./utils/validateSchema"; |
11 |
| -import { validateMarkdown } from "./utils/validateMarkdown"; |
12 |
| -import * as T from "../typings/tutorial"; |
13 |
| - |
14 |
| -const write = util.promisify(fs.writeFile); |
15 |
| -const read = util.promisify(fs.readFile); |
| 1 | +import * as yamlParser from 'js-yaml' |
| 2 | +import * as path from 'path' |
| 3 | +import * as fs from 'fs' |
| 4 | +import * as util from 'util' |
| 5 | +import { parse } from './utils/parse' |
| 6 | +import { getArg } from './utils/args' |
| 7 | +import { getCommits, CommitLogObject } from './utils/commits' |
| 8 | +import skeletonSchema from './schema/skeleton' |
| 9 | +import tutorialSchema from './schema/tutorial' |
| 10 | +import { validateSchema } from './utils/validateSchema' |
| 11 | +import { validateMarkdown } from './utils/validateMarkdown' |
| 12 | +import * as T from '../typings/tutorial' |
| 13 | + |
| 14 | +const write = util.promisify(fs.writeFile) |
| 15 | +const read = util.promisify(fs.readFile) |
16 | 16 |
|
17 | 17 | export type BuildConfigOptions = {
|
18 |
| - text: string; // text document from markdown |
19 |
| - config: T.Tutorial; // yaml config file converted to json |
20 |
| - commits: CommitLogObject; // an object of tutorial positions with a list of commit hashes |
21 |
| -}; |
| 18 | + text: string // text document from markdown |
| 19 | + config: T.Tutorial // yaml config file converted to json |
| 20 | + commits: CommitLogObject // an object of tutorial positions with a list of commit hashes |
| 21 | +} |
22 | 22 |
|
23 | 23 | type BuildArgs = {
|
24 |
| - dir: string; |
25 |
| - markdown: string; |
26 |
| - yaml: string; |
27 |
| - output: string; |
28 |
| - validate: boolean; |
29 |
| -}; |
| 24 | + dir: string |
| 25 | + markdown: string |
| 26 | + yaml: string |
| 27 | + output: string |
| 28 | + validate: boolean |
| 29 | +} |
30 | 30 |
|
31 |
| -async function build(args: string[]) { |
32 |
| - let options: BuildArgs; |
| 31 | +async function build (args: string[]) { |
| 32 | + let options: BuildArgs |
33 | 33 |
|
34 | 34 | try {
|
35 | 35 | // dir - default .
|
36 |
| - const dir = !args.length || args[0].match(/^-/) ? "." : args[0]; |
| 36 | + const dir = !args.length || args[0].match(/^-/) ? '.' : args[0] |
37 | 37 | // -m --markdown - default TUTORIAL.md
|
38 | 38 | const markdown =
|
39 |
| - getArg(args, { name: "markdown", alias: "m" }) || "TUTORIAL.md"; |
| 39 | + getArg(args, { name: 'markdown', alias: 'm' }) || 'TUTORIAL.md' |
40 | 40 | // -y --yaml - default coderoad-config.yml
|
41 |
| - const yaml = getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml"; |
| 41 | + const yaml = getArg(args, { name: 'yaml', alias: 'y' }) || 'coderoad.yaml' |
42 | 42 | // -o --output - default coderoad.json
|
43 | 43 | const output =
|
44 |
| - getArg(args, { name: "output", alias: "o" }) || "tutorial.json"; |
45 |
| - const validate = getArg(args, { name: "validate", alias: "v" }) !== "false"; |
| 44 | + getArg(args, { name: 'output', alias: 'o' }) || 'tutorial.json' |
| 45 | + const validate = getArg(args, { name: 'validate', alias: 'v' }) !== 'false' |
46 | 46 |
|
47 |
| - console.log(`Building CodeRoad ${output}...`); |
| 47 | + console.log(`Building CodeRoad ${output}...`) |
48 | 48 |
|
49 | 49 | options = {
|
50 | 50 | dir,
|
51 | 51 | output,
|
52 | 52 | markdown,
|
53 | 53 | yaml,
|
54 |
| - validate, |
55 |
| - }; |
| 54 | + validate |
| 55 | + } |
56 | 56 | } catch (e) {
|
57 |
| - console.error("Error parsing build logs"); |
58 |
| - console.error(e.message); |
59 |
| - return; |
| 57 | + console.error('Error parsing build logs') |
| 58 | + console.error(e.message) |
| 59 | + return |
60 | 60 | }
|
61 | 61 |
|
62 | 62 | // path to run build from
|
63 |
| - const localPath = path.join(process.cwd(), options.dir); |
| 63 | + const localPath = path.join(process.cwd(), options.dir) |
64 | 64 |
|
65 | 65 | // load markdown and files
|
66 |
| - let _markdown: string; |
67 |
| - let _yaml: string; |
| 66 | + let _markdown: string |
| 67 | + let _yaml: string |
68 | 68 | try {
|
69 |
| - [_markdown, _yaml] = await Promise.all([ |
70 |
| - read(path.join(localPath, options.markdown), "utf8"), |
71 |
| - read(path.join(localPath, options.yaml), "utf8"), |
72 |
| - ]); |
| 69 | + ;[_markdown, _yaml] = await Promise.all([ |
| 70 | + read(path.join(localPath, options.markdown), 'utf8'), |
| 71 | + read(path.join(localPath, options.yaml), 'utf8') |
| 72 | + ]) |
73 | 73 | } catch (e) {
|
74 |
| - console.error("Error reading file:"); |
75 |
| - console.error(e.message); |
76 |
| - return; |
| 74 | + console.error('Error reading file:') |
| 75 | + console.error(e.message) |
| 76 | + return |
77 | 77 | }
|
78 | 78 |
|
79 | 79 | // validate markdown loosely
|
80 | 80 | try {
|
81 |
| - const isValid = validateMarkdown(_markdown); |
| 81 | + const isValid = validateMarkdown(_markdown) |
82 | 82 | if (!isValid) {
|
83 |
| - console.warn("Invalid markdown"); |
| 83 | + console.warn('Invalid markdown') |
84 | 84 | }
|
85 | 85 | } catch (e) {
|
86 |
| - console.error("Error validating markdown:"); |
87 |
| - console.error(e.message); |
88 |
| - return; |
| 86 | + console.error('Error validating markdown:') |
| 87 | + console.error(e.message) |
| 88 | + return |
89 | 89 | }
|
90 | 90 |
|
91 | 91 | // parse yaml skeleton config
|
92 |
| - let skeleton; |
| 92 | + let skeleton |
93 | 93 | try {
|
94 |
| - skeleton = yamlParser.load(_yaml); |
| 94 | + skeleton = yamlParser.load(_yaml) |
95 | 95 | if (!skeleton || !Object.keys(skeleton).length) {
|
96 |
| - throw new Error(`Skeleton at "${options.yaml}" is invalid`); |
| 96 | + throw new Error(`Skeleton at "${options.yaml}" is invalid`) |
97 | 97 | }
|
98 | 98 | } catch (e) {
|
99 |
| - console.error("Error parsing yaml"); |
100 |
| - console.error(e.message); |
101 |
| - return; |
| 99 | + console.error('Error parsing yaml') |
| 100 | + console.error(e.message) |
| 101 | + return |
102 | 102 | }
|
103 | 103 |
|
104 | 104 | // validate skeleton based on skeleton json schema
|
105 | 105 | try {
|
106 |
| - const valid = validateSchema(skeletonSchema, skeleton); |
| 106 | + const valid = validateSchema(skeletonSchema, skeleton) |
107 | 107 | if (!valid) {
|
108 |
| - console.error("Skeleton validation failed. See above to see what to fix"); |
109 |
| - return; |
| 108 | + console.error('Skeleton validation failed. See above to see what to fix') |
| 109 | + return |
110 | 110 | }
|
111 | 111 | } catch (e) {
|
112 |
| - console.error("Error validating tutorial schema:"); |
113 |
| - console.error(e.message); |
| 112 | + console.error('Error validating tutorial schema:') |
| 113 | + console.error(e.message) |
114 | 114 | }
|
115 | 115 |
|
116 | 116 | // load git commits to use in parse step
|
117 |
| - let commits: CommitLogObject; |
| 117 | + let commits: CommitLogObject |
118 | 118 | try {
|
119 | 119 | commits = await getCommits({
|
120 | 120 | localDir: localPath,
|
121 |
| - codeBranch: skeleton.config.repo.branch, |
122 |
| - }); |
| 121 | + codeBranch: skeleton.config.repo.branch |
| 122 | + }) |
123 | 123 | } catch (e) {
|
124 |
| - console.error("Error loading commits:"); |
125 |
| - console.error(e.message); |
126 |
| - return; |
| 124 | + console.error('Error loading commits:') |
| 125 | + console.error(e.message) |
| 126 | + return |
127 | 127 | }
|
128 | 128 |
|
129 | 129 | // parse tutorial from markdown and yaml
|
130 |
| - let tutorial: T.Tutorial; |
| 130 | + let tutorial: T.Tutorial |
131 | 131 | try {
|
132 | 132 | tutorial = await parse({
|
133 | 133 | text: _markdown,
|
134 | 134 | skeleton,
|
135 |
| - commits, |
136 |
| - }); |
| 135 | + commits |
| 136 | + }) |
137 | 137 | } catch (e) {
|
138 |
| - console.error("Error parsing tutorial:"); |
139 |
| - console.error(e.message); |
140 |
| - return; |
| 138 | + console.error('Error parsing tutorial:') |
| 139 | + console.error(e.message) |
| 140 | + return |
141 | 141 | }
|
142 | 142 |
|
143 | 143 | // validate tutorial based on tutorial json schema
|
144 | 144 | try {
|
145 | 145 | if (options.validate) {
|
146 |
| - const valid = validateSchema(tutorialSchema, tutorial); |
| 146 | + const valid = validateSchema(tutorialSchema, tutorial) |
147 | 147 | if (!valid) {
|
148 | 148 | console.error(
|
149 |
| - "Tutorial validation failed. See above to see what to fix" |
150 |
| - ); |
| 149 | + 'Tutorial validation failed. See above to see what to fix' |
| 150 | + ) |
151 | 151 | // continue rather than exiting early
|
152 | 152 | }
|
153 | 153 | }
|
154 | 154 | } catch (e) {
|
155 |
| - console.error("Error validating tutorial schema:"); |
156 |
| - console.error(e.message); |
| 155 | + console.error('Error validating tutorial schema:') |
| 156 | + console.error(e.message) |
157 | 157 | }
|
158 | 158 |
|
159 | 159 | // write tutorial
|
160 | 160 | if (tutorial) {
|
161 | 161 | try {
|
162 |
| - await write(options.output, JSON.stringify(tutorial, null, 2), "utf8"); |
163 |
| - console.info(`Success! See output at ${options.output}`); |
| 162 | + await write(options.output, JSON.stringify(tutorial, null, 2), 'utf8') |
| 163 | + console.info(`Success! See output at ${options.output}`) |
164 | 164 | } catch (e) {
|
165 |
| - console.error("Error writing tutorial json file:"); |
166 |
| - console.error(e.message); |
| 165 | + console.error('Error writing tutorial json file:') |
| 166 | + console.error(e.message) |
167 | 167 | }
|
168 | 168 | }
|
169 | 169 | }
|
170 | 170 |
|
171 |
| -export default build; |
| 171 | +export default build |
0 commit comments