Skip to content

Fixes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
69 changes: 54 additions & 15 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "./utils/logs";
import build from "./build";
import create from "./create";
import help from "./help";

export async function cli(rawArgs: string[]): Promise<void> {
const command: string = rawArgs[2];
@@ -23,8 +25,6 @@ export async function cli(rawArgs: string[]): Promise<void> {
case "--help":
case "-h":
default:
console.log(
"Docs can be found at github: https://github.com/coderoad/coderoad-cli/"
);
help();
}
}
13 changes: 13 additions & 0 deletions src/help.ts
Original file line number Diff line number Diff line change
@@ -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
`);
}
16 changes: 16 additions & 0 deletions src/utils/commits.ts
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -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));
};
4 changes: 2 additions & 2 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
@@ -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({
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -23,5 +23,5 @@
"skipLibCheck": true,
"esModuleInterop": true
},
"exclude": ["node_modules", ".vscode", "bin", "build", "tests"]
"exclude": ["node_modules", ".vscode", "bin", "build", "tests", "coverage"]
}