diff --git a/bin/cli b/bin/cli index d14d12fcbf..caff9af846 100755 --- a/bin/cli +++ b/bin/cli @@ -50,12 +50,16 @@ async function updatePackageJson (packageJsonPath) { } async function initialiseGitRepo () { - try { - return exec('git init && git add -A . && git -c "user.email=gov.uk-prototype@digital.cabinet-office.gov.uk" -c "user.name=GOV.UK Prototype Kit" commit -am "Created prototype kit."') - } catch (e) { - console.log('error initialising git repo') - console.log(e.message) + const whichGitResult = [] + await exec('which git', {}, data => whichGitResult.push(data.toString())) + if (('' + whichGitResult[0]).trim() === '') { + console.log('git not present.') + return } + await exec('git init && git add -A .') + const overrideParams = '-c "user.email=gov.uk-prototype@digital.cabinet-office.gov.uk" -c "user.name=GOV.UK Prototype Kit"' + const commitMessage = 'Created prototype kit.' + await exec(`git commit -am "${commitMessage}" || git ${overrideParams} commit -am "${commitMessage}"`) } function usage () { diff --git a/lib/exec.js b/lib/exec.js index b4971069b4..c560b55647 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -2,36 +2,44 @@ const { spawn } = require('cross-spawn') const colors = require('ansi-colors') module.exports = { - exec: (command, options = {}, stdout) => { + exec: (command, options = {}, stdout) => new Promise(function (resolve, reject) { const errorOutput = [] - const child = spawn(command, [], { shell: true, ...options }) + try { + const child = spawn(command, [], { shell: true, ...options }) - if (child.stdout) { - child.stdout.on('data', function (data) { - if (stdout) { - stdout(data) - } - }) - } - if (child.stderr) { - child.stderr.on('data', function (data) { - errorOutput.push(data) - }) - } + if (child.stdout) { + child.stdout.on('data', function (data) { + if (stdout) { + stdout(data) + } + }) + } + if (child.stderr) { + child.stderr.on('data', function (data) { + errorOutput.push(data) + }) + } - return new Promise(function (resolve, reject) { child.on('close', function (code) { if (code === 0) { resolve(true) } else { if (errorOutput.length > 0) { - console.error(colors.red(errorOutput.join('\n'))) + const formattedErrorOutput = errorOutput.join('\n') + console.error(colors.red(formattedErrorOutput)) + reject(new Error(formattedErrorOutput)) + } else { + reject(new Error(`Exit code was ${code}`)) } - reject(new Error(`Exit code was ${code}`)) } }) - }) - }, + } catch (e) { + console.log('caught error - ignoring for now (while experimenting)') + console.log(e.message) + console.log(e) + reject(new Error('caught in try catch')) + } + }), spawn: (command, args, options = {}) => { const child = spawn(command, args, { ...options })