diff --git a/src/build.ts b/src/build.ts index 86d1165..016b64f 100644 --- a/src/build.ts +++ b/src/build.ts @@ -124,7 +124,7 @@ async function build(args: string[]) { // write tutorial if (tutorial) { try { - await write(options.output, JSON.stringify(tutorial), "utf8"); + await write(options.output, JSON.stringify(tutorial, null, 2), "utf8"); console.info(`Success! See output at ${options.output}`); } catch (e) { console.error("Error writing tutorial json file:"); diff --git a/src/utils/commits.ts b/src/utils/commits.ts index dd8f4bb..ab3ee34 100644 --- a/src/utils/commits.ts +++ b/src/utils/commits.ts @@ -45,41 +45,45 @@ export async function getCommits({ 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); + // track the original branch in case of failure + const originalBranch = branches.current; // Filter relevant logs const commits: CommitLogObject = {}; - for (const commit of logs.all) { - const matches = commit.message.match( - /^(?(?L\d+)(S\d+))(?[QA])?/ - ); - - if (matches && matches.length) { - // Use an object of commit arrays to collect all commits - const position = matches[0]; - if (!commits[position]) { - // does not exist, create the list - commits[position] = [commit.hash]; - } else { - // add to the list - commits[position].push(commit.hash); + try { + // Checkout the code branches + await git.checkout(codeBranch); + + // Load all logs + const logs = await git.log(); + + for (const commit of logs.all) { + const matches = commit.message.match( + /^(?(?L\d+)(S\d+))(?[QA])?/ + ); + + if (matches && matches.length) { + // Use an object of commit arrays to collect all commits + const position = matches[0]; + if (!commits[position]) { + // does not exist, create the list + commits[position] = [commit.hash]; + } else { + // add to the list + commits[position].push(commit.hash); + } } } + } catch (e) { + console.error("Error with checkout or commit matching"); + throw new Error(e.message); + } finally { + // revert back to the original branch on failure + await git.checkout(originalBranch); + // cleanup the tmp directory + await rmdir(tmpDir, { recursive: true }); } - console.log("remove"); - // cleanup the tmp directory - await rmdir(tmpDir, { recursive: true }); return commits; } diff --git a/src/utils/parse.ts b/src/utils/parse.ts index 36fc5d3..787331c 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -98,6 +98,7 @@ export function parse(params: ParseParams): any { const mdContent: TutorialFrame = parseMdContent(params.text); const parsed: Partial = { + version: params.config.version, summary: mdContent.summary, config: params.config.config, levels: [], diff --git a/src/utils/schema/index.ts b/src/utils/schema/index.ts index 29a9c4f..98d3fd6 100644 --- a/src/utils/schema/index.ts +++ b/src/utils/schema/index.ts @@ -193,7 +193,7 @@ export default { "The solution commits that can be loaded if the user gets stuck. It can also run commands and/or open files", }, { - required: ["commits"], + required: [], }, ], },