|
1 |
| -import*asyamlParserfrom"js-yaml"; |
2 |
| -import*aspathfrom"path"; |
3 |
| -import*asfsfrom"fs"; |
4 |
| -import*asutilfrom"util"; |
5 |
| -import{parse}from"./utils/parse"; |
6 |
| -import{getArg}from"./utils/args"; |
7 |
| -import{getCommits,CommitLogObject}from"./utils/commits"; |
8 |
| -importskeletonSchemafrom"./schema/skeleton"; |
9 |
| -importtutorialSchemafrom"./schema/tutorial"; |
10 |
| -import{validateSchema}from"./utils/validateSchema"; |
11 |
| -import{validateMarkdown}from"./utils/validateMarkdown"; |
12 |
| -import*asTfrom"../typings/tutorial"; |
13 |
| - |
14 |
| -constwrite=util.promisify(fs.writeFile); |
15 |
| -constread=util.promisify(fs.readFile); |
| 1 | +import*asyamlParserfrom'js-yaml' |
| 2 | +import*aspathfrom'path' |
| 3 | +import*asfsfrom'fs' |
| 4 | +import*asutilfrom'util' |
| 5 | +import{parse}from'./utils/parse' |
| 6 | +import{getArg}from'./utils/args' |
| 7 | +import{getCommits,CommitLogObject}from'./utils/commits' |
| 8 | +importskeletonSchemafrom'./schema/skeleton' |
| 9 | +importtutorialSchemafrom'./schema/tutorial' |
| 10 | +import{validateSchema}from'./utils/validateSchema' |
| 11 | +import{validateMarkdown}from'./utils/validateMarkdown' |
| 12 | +import*asTfrom'../typings/tutorial' |
| 13 | + |
| 14 | +constwrite=util.promisify(fs.writeFile) |
| 15 | +constread=util.promisify(fs.readFile) |
16 | 16 |
|
17 | 17 | exporttypeBuildConfigOptions={
|
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 | typeBuildArgs={
|
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 |
| -asyncfunctionbuild(args:string[]){ |
32 |
| -letoptions:BuildArgs; |
| 31 | +asyncfunctionbuild(args:string[]){ |
| 32 | +letoptions:BuildArgs |
33 | 33 |
|
34 | 34 | try{
|
35 | 35 | // dir - default .
|
36 |
| -constdir=!args.length||args[0].match(/^-/) ?"." :args[0]; |
| 36 | +constdir=!args.length||args[0].match(/^-/) ?'.' :args[0] |
37 | 37 | // -m --markdown - default TUTORIAL.md
|
38 | 38 | constmarkdown=
|
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 |
| -constyaml=getArg(args,{name:"yaml",alias:"y"})||"coderoad.yaml"; |
| 41 | +constyaml=getArg(args,{name:'yaml',alias:'y'})||'coderoad.yaml' |
42 | 42 | // -o --output - default coderoad.json
|
43 | 43 | constoutput=
|
44 |
| -getArg(args,{name:"output",alias:"o"})||"tutorial.json"; |
45 |
| -constvalidate=getArg(args,{name:"validate",alias:"v"})!=="false"; |
| 44 | +getArg(args,{name:'output',alias:'o'})||'tutorial.json' |
| 45 | +constvalidate=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 |
| -constlocalPath=path.join(process.cwd(),options.dir); |
| 63 | +constlocalPath=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]=awaitPromise.all([ |
70 |
| -read(path.join(localPath,options.markdown),"utf8"), |
71 |
| -read(path.join(localPath,options.yaml),"utf8"), |
72 |
| -]); |
| 69 | +;[_markdown,_yaml]=awaitPromise.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 |
| -constisValid=validateMarkdown(_markdown); |
| 81 | +constisValid=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 |
| -letskeleton; |
| 92 | +letskeleton |
93 | 93 | try{
|
94 |
| -skeleton=yamlParser.load(_yaml); |
| 94 | +skeleton=yamlParser.load(_yaml) |
95 | 95 | if(!skeleton||!Object.keys(skeleton).length){
|
96 |
| -thrownewError(`Skeleton at "${options.yaml}" is invalid`); |
| 96 | +thrownewError(`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 |
| -constvalid=validateSchema(skeletonSchema,skeleton); |
| 106 | +constvalid=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 |
| -letcommits:CommitLogObject; |
| 117 | +letcommits:CommitLogObject |
118 | 118 | try{
|
119 | 119 | commits=awaitgetCommits({
|
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 |
| -lettutorial:T.Tutorial; |
| 130 | +lettutorial:T.Tutorial |
131 | 131 | try{
|
132 | 132 | tutorial=awaitparse({
|
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 |
| -constvalid=validateSchema(tutorialSchema,tutorial); |
| 146 | +constvalid=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 |
| -awaitwrite(options.output,JSON.stringify(tutorial,null,2),"utf8"); |
163 |
| -console.info(`Success! See output at${options.output}`); |
| 162 | +awaitwrite(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 |
| -exportdefaultbuild; |
| 171 | +exportdefaultbuild |