From 390ac7291deaa6e726a2dc627557e6fa36cff25b Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 22 Dec 2022 09:59:04 +0000 Subject: [PATCH] Initialising git repo when git is present, fallback provided for those with git but no user configured (as it is on CI). --- __tests__/spec/sanity-checks.js | 14 +++++++++++++- bin/cli | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/__tests__/spec/sanity-checks.js b/__tests__/spec/sanity-checks.js index 4e8ef89574..c138b2daed 100644 --- a/__tests__/spec/sanity-checks.js +++ b/__tests__/spec/sanity-checks.js @@ -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) /** @@ -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('/') diff --git a/bin/cli b/bin/cli index 46215920ad..2a85bd2fb2 100755 --- a/bin/cli +++ b/bin/cli @@ -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') @@ -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(` @@ -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') {