Skip to content

Commit

Permalink
Initialising git repo when git is present, fallback provided for thos…
Browse files Browse the repository at this point in the history
…e with git but no user configured (as it is on CI).
  • Loading branch information
Your Name committed Dec 22, 2022
1 parent e4dac2e commit 467f150
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

- [#1860: Create a git repository for new prototypes (when git is present)](https://github.com/alphagov/govuk-prototype-kit/pull/1860)
- [#1824: Add feature to manage plugins without using the command line](https://github.com/alphagov/govuk-prototype-kit/pull/1824)

### Fixes
Expand Down
14 changes: 13 additions & 1 deletion __tests__/spec/sanity-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let app
process.env.KIT_PROJECT_DIR = tmpDir

const { packageDir, projectDir } = require('../../lib/path-utils')

const { exec } = require('../../lib/exec')
const createKitTimeout = parseInt(process.env.CREATE_KIT_TIMEOUT || '90000', 10)

/**
Expand All @@ -38,6 +38,18 @@ describe('The Prototype Kit', () => {
)
})

it('should initialise git out of the box', async () => {
const outputLines = []
await exec('git log', { cwd: tmpDir }, (data) => outputLines.push(data.toString()))

function getLastMeaningfulLineTrimmed (outputLines) {
const meaningulLines = outputLines.join('').split('\n').filter(line => line !== '')
return meaningulLines.pop().trim()
}

expect(getLastMeaningfulLineTrimmed(outputLines)).toBe('Created prototype kit.')
})

describe('index page', () => {
it('should send a well formed response', async () => {
const response = await request(app).get('/')
Expand Down
19 changes: 17 additions & 2 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs-extra')
const path = require('path')

const { spawn } = require('../lib/exec')
const { spawn, exec } = require('../lib/exec')
const { parse } = require('./utils/argvParser')
const { prepareMigration, preflightChecks } = require('../lib/migrator')
const { npmInstall, packageJsonFormat } = require('./utils')
Expand Down Expand Up @@ -49,6 +49,21 @@ async function updatePackageJson (packageJsonPath) {
await fs.writeJson(packageJsonPath, newPackageJson, packageJsonFormat)
}

async function initialiseGitRepo () {
const presenceCheckOutput = []
const notPresentText = 'not present'
await exec(`which git || echo "${notPresentText}"`, {}, data => presenceCheckOutput.push(data))
if (presenceCheckOutput.join('').trim() === notPresentText) {
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 () {
const prog = 'npx govuk-prototype-kit'
console.log(`
Expand Down Expand Up @@ -175,7 +190,7 @@ function warnIfNpmStart (argv, env) {
fs.writeFile(path.join(installDirectory, '.npmrc'), npmrc, 'utf8'),
copyFile('LICENCE.txt'),
updatePackageJson(path.join(installDirectory, 'package.json'))
])
]).then(initialiseGitRepo)
} else if (argv.command === 'dev') {
require('../start')
} else if (argv.command === 'start' || argv.command === 'serve') {
Expand Down

0 comments on commit 467f150

Please sign in to comment.