From 0337a3cf0e91024d25bbea744dd2e3d7059b79bf Mon Sep 17 00:00:00 2001 From: shmck Date: Sun, 31 May 2020 13:45:15 -0700 Subject: [PATCH 1/4] fix build from coverage Signed-off-by: shmck --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index d3d6cfc..9a67c17 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,5 +23,5 @@ "skipLibCheck": true, "esModuleInterop": true }, - "exclude": ["node_modules", ".vscode", "bin", "build", "tests"] + "exclude": ["node_modules", ".vscode", "bin", "build", "tests", "coverage"] } From 60c48ed739373f5044f41b69226a9f2e7c6b020d Mon Sep 17 00:00:00 2001 From: shmck Date: Sun, 31 May 2020 14:11:59 -0700 Subject: [PATCH 2/4] improve build error handling Signed-off-by: shmck --- src/build.ts | 69 ++++++++++++++++++++++++++++++++++---------- src/utils/commits.ts | 16 ++++++++++ tests/parse.test.ts | 4 +-- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/build.ts b/src/build.ts index 4a861dd..13fb17b 100644 --- a/src/build.ts +++ b/src/build.ts @@ -46,33 +46,72 @@ const parseArgs = (args: string[]): BuildArgs => { }; async function build(args: string[]) { - const options = parseArgs(args); + let options: BuildArgs; + try { + options = parseArgs(args); + } catch (e) { + console.error("Error parsing build logs"); + console.error(e.message); + return; + } // path to run build from const localPath = path.join(process.cwd(), options.dir); // load files - const [_markdown, _yaml] = await Promise.all([ - read(path.join(localPath, options.markdown), "utf8"), - read(path.join(localPath, options.yaml), "utf8"), - ]); + let _markdown: string; + let _yaml: string; + try { + [_markdown, _yaml] = await Promise.all([ + read(path.join(localPath, options.markdown), "utf8"), + read(path.join(localPath, options.yaml), "utf8"), + ]); + } catch (e) { + console.error("Error reading file:"); + console.error(e.message); + return; + } - const config = yamlParser.load(_yaml); + let config; + try { + config = yamlParser.load(_yaml); + } catch (e) { + console.error("Error parsing yaml"); + console.error(e.message); + } - const commits: CommitLogObject = await getCommits(config.config.repo.branch); + let commits: CommitLogObject; + try { + commits = await getCommits({ + localDir: localPath, + codeBranch: config.config.repo.branch, + }); + } catch (e) { + console.error("Error loading commits:"); + console.error(e.message); + return; + } // Otherwise, continue with the other options - const tutorial: T.Tutorial = await parse({ - text: _markdown, - config, - commits, - }); + let tutorial: T.Tutorial; + try { + tutorial = await parse({ + text: _markdown, + config, + commits, + }); + } catch (e) { + console.error("Error parsing tutorial:"); + console.error(e.message); + return; + } if (tutorial) { - if (options.output) { + try { await write(options.output, JSON.stringify(tutorial), "utf8"); - } else { - console.log(JSON.stringify(tutorial, null, 2)); + } catch (e) { + console.error("Error writing tutorial json:"); + console.error(e.message); } } } diff --git a/src/utils/commits.ts b/src/utils/commits.ts index afbc57b..dd8f4bb 100644 --- a/src/utils/commits.ts +++ b/src/utils/commits.ts @@ -37,12 +37,26 @@ export async function getCommits({ const tempGit = gitP(tmpDir); await tempGit.clone(localDir, tmpDir); + const branches = await git.branch(); + + if (!branches.all.length) { + throw new Error("No branches found"); + } else if (!branches.all.includes(codeBranch)) { + throw new Error(`Code branch "${codeBranch}" not found`); + } + + console.log("branches", branches); + // Checkout the code branches await git.checkout(codeBranch); + console.log("checked out"); + // Load all logs const logs = await git.log(); + console.log("logs", logs); + // Filter relevant logs const commits: CommitLogObject = {}; @@ -63,6 +77,8 @@ export async function getCommits({ } } } + + console.log("remove"); // cleanup the tmp directory await rmdir(tmpDir, { recursive: true }); return commits; diff --git a/tests/parse.test.ts b/tests/parse.test.ts index 1c63017..cde17dd 100644 --- a/tests/parse.test.ts +++ b/tests/parse.test.ts @@ -5,9 +5,9 @@ describe("parse", () => { it("should parse summary", () => { const md = `# Insert Tutorial's Title here - Short description to be shown as a tutorial's subtitle. +Short description to be shown as a tutorial's subtitle. - `; +`; const config = { version: "0.1.0" }; const result = parse({ From 56a3dcba3d11d2ea1964869865ff300c94d6d49c Mon Sep 17 00:00:00 2001 From: shmck Date: Sun, 31 May 2020 14:20:59 -0700 Subject: [PATCH 3/4] add colors to logs Signed-off-by: shmck --- package-lock.json | 3 +-- package.json | 1 + src/cli.ts | 1 + src/utils/logs.ts | 21 +++++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/utils/logs.ts diff --git a/package-lock.json b/package-lock.json index 41d8145..e064e04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4318,8 +4318,7 @@ "kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", - "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", - "dev": true + "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==" }, "leven": { "version": "3.1.0", diff --git a/package.json b/package.json index 9e008ab..63cb9ec 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "esm": "^3.2.25", "inquirer": "^7.1.0", "js-yaml": "^3.14.0", + "kleur": "^3.0.3", "listr": "^0.14.3", "lodash": "^4.17.15", "ncp": "^2.0.0", diff --git a/src/cli.ts b/src/cli.ts index e7d0749..d83495e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,3 +1,4 @@ +import "./utils/logs"; import build from "./build"; import create from "./create"; diff --git a/src/utils/logs.ts b/src/utils/logs.ts new file mode 100644 index 0000000..42b44f4 --- /dev/null +++ b/src/utils/logs.ts @@ -0,0 +1,21 @@ +import { red, yellow, blue } from "kleur"; + +const _error = console.error; +const _warn = console.warn; +const _info = console.info; +// const log = console.log; + +console.error = function () { + // @ts-ignore + _error(red.apply(console, arguments)); +}; + +console.warn = function () { + // @ts-ignore + _warn(yellow.apply(console, arguments)); +}; + +console.info = function () { + // @ts-ignore + _info(blue.apply(console, arguments)); +}; From 9344d9d6808f1056312409090cea152aef89f902 Mon Sep 17 00:00:00 2001 From: shmck Date: Sun, 31 May 2020 14:25:22 -0700 Subject: [PATCH 4/4] populate help docs Signed-off-by: shmck --- src/cli.ts | 5 ++--- src/help.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 src/help.ts diff --git a/src/cli.ts b/src/cli.ts index d83495e..3371d0c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,7 @@ import "./utils/logs"; import build from "./build"; import create from "./create"; +import help from "./help"; export async function cli(rawArgs: string[]): Promise { const command: string = rawArgs[2]; @@ -24,8 +25,6 @@ export async function cli(rawArgs: string[]): Promise { case "--help": case "-h": default: - console.log( - "Docs can be found at github: https://github.com/coderoad/coderoad-cli/" - ); + help(); } } diff --git a/src/help.ts b/src/help.ts new file mode 100644 index 0000000..ed20f2c --- /dev/null +++ b/src/help.ts @@ -0,0 +1,13 @@ +export default function help() { + console.log(` +Usage: coderoad [options] + +Options: + help display these help docs + version show coderoad cli version + create start a new tutorial from a template + build generate a coderoad.json file from markdown and yaml + +More docs at https://github.com/coderoad/coderoad-cli + `); +}