Skip to content

Commit 12fb052

Browse files
committed
convert to ts
Signed-off-by: shmck <[email protected]>
1 parent 7703261 commit 12fb052

File tree

8 files changed

+6485
-619
lines changed

8 files changed

+6485
-619
lines changed

bin/coderoad

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env node
22

3-
require = require('esm')(module /*, options*/);
4-
require('../src/cli').cli(process.argv);
3+
require = require("esm")(module /*, options*/);
4+
require("../build/cli").cli(process.argv);

package-lock.json

+6,324-531
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"access": "public"
1212
},
1313
"scripts": {
14-
"test": "echo \"Error: no test specified\" && exit 1"
14+
"build": "del-cli --force ./build && npm run compile",
15+
"compile": "tsc",
16+
"test": "jest"
1517
},
1618
"keywords": [],
1719
"author": "Argemiro Neto",
@@ -28,10 +30,19 @@
2830
"simple-git": "^2.5.0"
2931
},
3032
"devDependencies": {
33+
"@babel/preset-typescript": "^7.10.1",
3134
"@types/inquirer": "^6.5.0",
35+
"@types/jest": "^25.2.3",
3236
"@types/js-yaml": "^3.12.4",
3337
"@types/lodash": "^4.14.154",
3438
"@types/ncp": "^2.0.4",
39+
"del-cli": "^3.0.1",
40+
"jest": "^26.0.1",
41+
"ts-jest": "^26.0.0",
3542
"typescript": "^3.9.3"
43+
},
44+
"jest": {
45+
"preset": "ts-jest",
46+
"testEnvironment": "node"
3647
}
3748
}

src/parse.js renamed to src/build.ts

+84-54
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import simpleGit from "simple-git/promise";
2-
import yamlParser from "js-yaml";
3-
import path from "path";
4-
import _ from "lodash";
5-
import fs from "fs";
2+
import * as yamlParser from "js-yaml";
3+
import * as path from "path";
4+
import * as _ from "lodash";
5+
import * as fs from "fs";
6+
import * as T from "../typings/tutorial";
67
// import validate from './validator';
78

89
const workingDir = "tmp";
910

10-
function parseContent(md) {
11-
let start = -1;
12-
const parts = [];
11+
type TutorialContent = {};
12+
13+
function parseContent(md: string): TutorialContent {
14+
let start: number = -1;
15+
const parts: any[] = [];
1316

1417
const lines = md.split("\n");
1518

@@ -76,7 +79,7 @@ function parseContent(md) {
7679
return sections;
7780
}
7881

79-
function rmDir(dir, rmSelf) {
82+
function rmDir(dir: string, rmSelf = false) {
8083
try {
8184
let files;
8285
rmSelf = rmSelf === undefined ? true : rmSelf;
@@ -89,11 +92,11 @@ function rmDir(dir, rmSelf) {
8992
}
9093

9194
if (files.length > 0) {
92-
files.forEach(function (x, i) {
93-
if (fs.statSync(path.join(dir, x)).isDirectory()) {
94-
rmDir(path.join(dir, x));
95+
files.forEach(function (filePath: string) {
96+
if (fs.statSync(path.join(dir, filePath)).isDirectory()) {
97+
rmDir(path.join(dir, filePath));
9598
} else {
96-
fs.unlinkSync(path.join(dir, x));
99+
fs.unlinkSync(path.join(dir, filePath));
97100
}
98101
});
99102
}
@@ -107,7 +110,7 @@ function rmDir(dir, rmSelf) {
107110
}
108111
}
109112

110-
async function cleanupFiles(workingDir) {
113+
async function cleanupFiles(workingDir: string) {
111114
try {
112115
const gitModule = simpleGit(process.cwd());
113116

@@ -121,17 +124,18 @@ async function cleanupFiles(workingDir) {
121124
}
122125
}
123126

124-
/**
125-
*
126-
* @param {string} repo Git url to the repo. It should finish with .git
127-
* @param {string} codeBranch The branch containing the tutorial code
128-
* @param {string} setupBranch The branch containing the configuration files
129-
* @param {string} isLocal define if the repo is local or remote
130-
*/
131-
async function build({ repo, codeBranch, setupBranch, isLocal }) {
132-
let git;
127+
export type BuildOptions = {
128+
repo: string; // Git url to the repo. It should finish with .git
129+
codeBranch: string; // The branch containing the tutorial code
130+
setupBranch: string; // The branch containing the tutorialuration files
131+
isLocal: boolean; // define if the repo is local or remote
132+
output: string;
133+
};
134+
135+
async function build({ repo, codeBranch, setupBranch, isLocal }: BuildOptions) {
136+
let git: any;
133137
let isSubModule = false;
134-
let localPath;
138+
let localPath: string;
135139

136140
if (isLocal) {
137141
git = simpleGit(repo);
@@ -154,30 +158,35 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
154158

155159
await git.fetch();
156160

157-
// checkout the branch to load configuration and content branch
161+
// checkout the branch to load tutorialuration and content branch
158162
await git.checkout(setupBranch);
159163

160164
// Load files
161165
const _mdContent = fs.readFileSync(
162166
path.join(localPath, "TUTORIAL.md"),
163167
"utf8"
164168
);
165-
let _config = fs.readFileSync(path.join(localPath, "coderoad.yaml"), "utf8");
169+
let _tutorial = fs.readFileSync(
170+
path.join(localPath, "coderoad.yaml"),
171+
"utf8"
172+
);
166173

167174
// Add one more line to the content as per Shawn's request
168-
const mdContent = parseContent(_mdContent);
175+
const mdContent: any = parseContent(_mdContent);
169176

170-
// Parse config to JSON
171-
const config = yamlParser.load(_config);
177+
console.log(mdContent);
172178

173-
// Add the summary to the config file
174-
config["summary"] = mdContent.summary;
179+
// Parse tutorial to JSON
180+
const tutorial: T.Tutorial = yamlParser.load(_tutorial);
175181

176-
// merge content and config
177-
config.levels.forEach((level) => {
182+
// Add the summary to the tutorial file
183+
tutorial.summary = mdContent.summary;
184+
185+
// merge content and tutorial
186+
tutorial.levels.forEach((level: T.Level) => {
178187
const { steps, ...content } = mdContent[level.id];
179188

180-
level.steps.forEach((step) => _.merge(step, steps[step.id]));
189+
level.steps.forEach((step: T.Step) => _.merge(step, steps[step.id]));
181190

182191
_.merge(level, content);
183192
});
@@ -200,29 +209,50 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
200209
// Uses a set to make sure only the latest commit is proccessed
201210
parts.add(matches[0]);
202211

203-
// Add the content and git hash to the config
212+
// Add the content and git hash to the tutorial
204213
if (matches.groups.stepId) {
205214
// If it's a step: add the content and the setup/solution hashes depending on the type
206-
const theStep = config.levels
207-
.find((level) => level.id === matches.groups.levelId)
208-
.steps.find((step) => step.id === matches.groups.stepId);
209-
210-
if (matches.groups.stepType === "Q") {
211-
theStep.setup.commits.push(commit.hash.substr(0, 7));
212-
} else if (
213-
matches.groups.stepType === "A" &&
214-
theStep.solution.commits
215-
) {
216-
theStep.solution.commits.push(commit.hash.substr(0, 7));
215+
const level: T.Level | null =
216+
tutorial.levels.find(
217+
(level: T.Level) => level.id === matches.groups.levelId
218+
) || null;
219+
if (!level) {
220+
console.log(`Level ${matches.groups.levelId} not found`);
221+
} else {
222+
const theStep: T.Step | null =
223+
level.steps.find(
224+
(step: T.Step) => step.id === matches.groups.stepId
225+
) || null;
226+
227+
if (!theStep) {
228+
console.log(`Step ${matches.groups.stepId} not found`);
229+
} else {
230+
if (matches.groups.stepType === "Q") {
231+
theStep.setup.commits.push(commit.hash.substr(0, 7));
232+
} else if (
233+
matches.groups.stepType === "A" &&
234+
theStep.solution &&
235+
theStep.solution.commits
236+
) {
237+
theStep.solution.commits.push(commit.hash.substr(0, 7));
238+
}
239+
}
217240
}
218241
} else {
219-
// If it's level: add the commit hash (if the level has the commit key) and the content to the config
220-
const theLevel = config.levels.find(
221-
(level) => level.id === matches.groups.levelId
222-
);
223-
224-
if (_.has(theLevel, "config.commits")) {
225-
theLevel.setup.commits.push(commit.hash.substr(0, 7));
242+
// If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
243+
const theLevel: T.Level | null =
244+
tutorial.levels.find(
245+
(level: T.Level) => level.id === matches.groups.levelId
246+
) || null;
247+
248+
if (!theLevel) {
249+
console.log(`Level ${matches.groups.levelId} not found`);
250+
} else {
251+
if (_.has(theLevel, "tutorial.commits")) {
252+
if (theLevel.setup) {
253+
theLevel.setup.commits.push(commit.hash.substr(0, 7));
254+
}
255+
}
226256
}
227257
}
228258
}
@@ -247,14 +277,14 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
247277
}
248278
}
249279

250-
// const isValid = validate(config);
280+
// const isValid = validate(tutorial);
251281

252282
// if (!isValid) {
253283
// console.log(JSON.stringify(validate.errors, null, 2));
254284
// return;
255285
// }
256286

257-
return config;
287+
return tutorial;
258288
}
259289

260290
export default build;

0 commit comments

Comments
 (0)