Skip to content

Commit cc36f25

Browse files
authored
Merge pull request #13 from coderoad/fixes
Fixes
2 parents 86c5a2a + 9344d9d commit cc36f25

File tree

9 files changed

+112
-23
lines changed

9 files changed

+112
-23
lines changed

Diff for: package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"esm": "^3.2.25",
2525
"inquirer": "^7.1.0",
2626
"js-yaml": "^3.14.0",
27+
"kleur": "^3.0.3",
2728
"listr": "^0.14.3",
2829
"lodash": "^4.17.15",
2930
"ncp": "^2.0.0",

Diff for: src/build.ts

+54-15
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,72 @@ const parseArgs = (args: string[]): BuildArgs => {
4646
};
4747

4848
async function build(args: string[]) {
49-
const options = parseArgs(args);
49+
let options: BuildArgs;
50+
try {
51+
options = parseArgs(args);
52+
} catch (e) {
53+
console.error("Error parsing build logs");
54+
console.error(e.message);
55+
return;
56+
}
5057

5158
// path to run build from
5259
const localPath = path.join(process.cwd(), options.dir);
5360

5461
// load files
55-
const [_markdown, _yaml] = await Promise.all([
56-
read(path.join(localPath, options.markdown), "utf8"),
57-
read(path.join(localPath, options.yaml), "utf8"),
58-
]);
62+
let _markdown: string;
63+
let _yaml: string;
64+
try {
65+
[_markdown, _yaml] = await Promise.all([
66+
read(path.join(localPath, options.markdown), "utf8"),
67+
read(path.join(localPath, options.yaml), "utf8"),
68+
]);
69+
} catch (e) {
70+
console.error("Error reading file:");
71+
console.error(e.message);
72+
return;
73+
}
5974

60-
const config = yamlParser.load(_yaml);
75+
let config;
76+
try {
77+
config = yamlParser.load(_yaml);
78+
} catch (e) {
79+
console.error("Error parsing yaml");
80+
console.error(e.message);
81+
}
6182

62-
const commits: CommitLogObject = await getCommits(config.config.repo.branch);
83+
let commits: CommitLogObject;
84+
try {
85+
commits = await getCommits({
86+
localDir: localPath,
87+
codeBranch: config.config.repo.branch,
88+
});
89+
} catch (e) {
90+
console.error("Error loading commits:");
91+
console.error(e.message);
92+
return;
93+
}
6394

6495
// Otherwise, continue with the other options
65-
const tutorial: T.Tutorial = await parse({
66-
text: _markdown,
67-
config,
68-
commits,
69-
});
96+
let tutorial: T.Tutorial;
97+
try {
98+
tutorial = await parse({
99+
text: _markdown,
100+
config,
101+
commits,
102+
});
103+
} catch (e) {
104+
console.error("Error parsing tutorial:");
105+
console.error(e.message);
106+
return;
107+
}
70108

71109
if (tutorial) {
72-
if (options.output) {
110+
try {
73111
await write(options.output, JSON.stringify(tutorial), "utf8");
74-
} else {
75-
console.log(JSON.stringify(tutorial, null, 2));
112+
} catch (e) {
113+
console.error("Error writing tutorial json:");
114+
console.error(e.message);
76115
}
77116
}
78117
}

Diff for: src/cli.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import "./utils/logs";
12
import build from "./build";
23
import create from "./create";
4+
import help from "./help";
35

46
export async function cli(rawArgs: string[]): Promise<void> {
57
const command: string = rawArgs[2];
@@ -23,8 +25,6 @@ export async function cli(rawArgs: string[]): Promise<void> {
2325
case "--help":
2426
case "-h":
2527
default:
26-
console.log(
27-
"Docs can be found at github: https://github.com/coderoad/coderoad-cli/"
28-
);
28+
help();
2929
}
3030
}

Diff for: src/help.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function help() {
2+
console.log(`
3+
Usage: coderoad [options]
4+
5+
Options:
6+
help display these help docs
7+
version show coderoad cli version
8+
create start a new tutorial from a template
9+
build generate a coderoad.json file from markdown and yaml
10+
11+
More docs at https://github.com/coderoad/coderoad-cli
12+
`);
13+
}

Diff for: src/utils/commits.ts

+16
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,26 @@ export async function getCommits({
3737
const tempGit = gitP(tmpDir);
3838
await tempGit.clone(localDir, tmpDir);
3939

40+
const branches = await git.branch();
41+
42+
if (!branches.all.length) {
43+
throw new Error("No branches found");
44+
} else if (!branches.all.includes(codeBranch)) {
45+
throw new Error(`Code branch "${codeBranch}" not found`);
46+
}
47+
48+
console.log("branches", branches);
49+
4050
// Checkout the code branches
4151
await git.checkout(codeBranch);
4252

53+
console.log("checked out");
54+
4355
// Load all logs
4456
const logs = await git.log();
4557

58+
console.log("logs", logs);
59+
4660
// Filter relevant logs
4761
const commits: CommitLogObject = {};
4862

@@ -63,6 +77,8 @@ export async function getCommits({
6377
}
6478
}
6579
}
80+
81+
console.log("remove");
6682
// cleanup the tmp directory
6783
await rmdir(tmpDir, { recursive: true });
6884
return commits;

Diff for: src/utils/logs.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { red, yellow, blue } from "kleur";
2+
3+
const _error = console.error;
4+
const _warn = console.warn;
5+
const _info = console.info;
6+
// const log = console.log;
7+
8+
console.error = function () {
9+
// @ts-ignore
10+
_error(red.apply(console, arguments));
11+
};
12+
13+
console.warn = function () {
14+
// @ts-ignore
15+
_warn(yellow.apply(console, arguments));
16+
};
17+
18+
console.info = function () {
19+
// @ts-ignore
20+
_info(blue.apply(console, arguments));
21+
};

Diff for: tests/parse.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ describe("parse", () => {
55
it("should parse summary", () => {
66
const md = `# Insert Tutorial's Title here
77
8-
Short description to be shown as a tutorial's subtitle.
8+
Short description to be shown as a tutorial's subtitle.
99
10-
`;
10+
`;
1111

1212
const config = { version: "0.1.0" };
1313
const result = parse({

Diff for: tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"skipLibCheck": true,
2424
"esModuleInterop": true
2525
},
26-
"exclude": ["node_modules", ".vscode", "bin", "build", "tests"]
26+
"exclude": ["node_modules", ".vscode", "bin", "build", "tests", "coverage"]
2727
}

0 commit comments

Comments
 (0)