Skip to content

Commit 60c48ed

Browse files
committed
improve build error handling
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 0337a3c commit 60c48ed

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

src/build.ts

Lines changed: 54 additions & 15 deletions
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
}

src/utils/commits.ts

Lines changed: 16 additions & 0 deletions
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;

tests/parse.test.ts

Lines changed: 2 additions & 2 deletions
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({

0 commit comments

Comments
 (0)