From 58688fc9d9a0dd42592503933556d8fcd5d9b69e Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Sat, 10 Mar 2018 15:48:13 -0500 Subject: [PATCH] feat(CLI): don't attempt to commit if user.name/email is not set --- src/cli/tasks.ts | 15 +++++++++++++-- src/cli/tests/cli.unit.spec.ts | 21 ++++++++++++++++++++- src/cli/typescript-starter.ts | 7 +++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/cli/tasks.ts b/src/cli/tasks.ts index 2b8ffed..0cbe37e 100644 --- a/src/cli/tasks.ts +++ b/src/cli/tasks.ts @@ -26,7 +26,12 @@ export interface Tasks { readonly gitEmail: string; readonly gitName: string; }>; - readonly initialCommit: (hash: string, projectDir: string) => Promise; + readonly initialCommit: ( + hash: string, + projectDir: string, + name: string, + email: string + ) => Promise; readonly install: ( shouldInstall: boolean, runner: Runner, @@ -104,7 +109,9 @@ export const getUserInfo = (spawner: ExecaStatic) => async () => { export const initialCommit = (spawner: ExecaStatic) => async ( hash: string, - projectDir: string + projectDir: string, + name: string, + email: string ) => { const opts: Options = { cwd: projectDir, @@ -113,6 +120,9 @@ export const initialCommit = (spawner: ExecaStatic) => async ( }; await spawner('git', ['init'], opts); await spawner('git', ['add', '-A'], opts); + if (name === Placeholders.name || email === Placeholders.email) { + return false; + } await spawner( 'git', [ @@ -122,6 +132,7 @@ export const initialCommit = (spawner: ExecaStatic) => async ( ], opts ); + return true; }; export const install = (spawner: ExecaStatic) => async ( diff --git a/src/cli/tests/cli.unit.spec.ts b/src/cli/tests/cli.unit.spec.ts index 8ac24b5..1f5aa0f 100644 --- a/src/cli/tests/cli.unit.spec.ts +++ b/src/cli/tests/cli.unit.spec.ts @@ -120,10 +120,29 @@ test('getUserInfo: suppresses errors and returns empty strings', async t => { }); test('initialCommit: throws generated errors', async t => { - const error = await t.throws(initialCommit(mockErr(1))('deadbeef', 'fail')); + const error = await t.throws( + initialCommit(mockErr(1))('deadbeef', 'fail', 'name', 'bitjson@github.com') + ); t.is(error.code, 1); }); +test("initialCommit: don't attempt to commit if user.name/email is not set", async t => { + // tslint:disable-next-line:no-let + let calls = 0; + const errorIf3 = ((() => { + calls++; + calls === 1 ? t.pass() : calls === 2 ? t.pass() : t.fail(); + }) as any) as ExecaStatic; + t.false( + await initialCommit(errorIf3)( + 'deadbeef', + 'fail', + Placeholders.name, + Placeholders.email + ) + ); +}); + test('install: uses the correct runner', async t => { const mock = (((runner: Runner) => { runner === Runner.Yarn ? t.pass() : t.fail(); diff --git a/src/cli/typescript-starter.ts b/src/cli/typescript-starter.ts index 2cd2bf6..1994050 100644 --- a/src/cli/typescript-starter.ts +++ b/src/cli/typescript-starter.ts @@ -144,8 +144,11 @@ export async function typescriptStarter( await tasks.install(install, runner, projectPath); const spinner7 = ora(`Initializing git repository`).start(); - await tasks.initialCommit(commitHash, projectPath); - spinner7.succeed(); + (await tasks.initialCommit(commitHash, projectPath, gitName, gitEmail)) + ? spinner7.succeed() + : spinner7.fail( + "Git config user.name and user.email are not configured. You'll need to `git commit` yourself." + ); console.log(`\n${chalk.blue.bold(`Created ${name} 🎉`)}\n`); }