Skip to content

Commit

Permalink
Supporing users who don't have a git user name set up (as is the case…
Browse files Browse the repository at this point in the history
… on CI).
  • Loading branch information
nataliecarey authored and Your Name committed Dec 20, 2022
1 parent a3f3a80 commit 6a66b92
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
14 changes: 9 additions & 5 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
46 changes: 27 additions & 19 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down

0 comments on commit 6a66b92

Please sign in to comment.