diff --git a/CHANGELOG.md b/CHANGELOG.md index ca552f8d43..0f5e00516e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### New features + +- [#1906: Improved messaging when creating a prototype](https://github.com/alphagov/govuk-prototype-kit/pull/1906) + ### Fixes - [#1887: You no longer need to press ctrl-c to exit if you say no to a new port](https://github.com/alphagov/govuk-prototype-kit/pull/1887) diff --git a/bin/cli b/bin/cli index 1645bcdb03..eae5e15616 100755 --- a/bin/cli +++ b/bin/cli @@ -16,9 +16,19 @@ const kitRoot = path.join(__dirname, '..') const kitVersion = require('../package.json').version const argv = parse(process.argv, { - booleans: ['no-version-control'] + booleans: ['no-version-control', 'verbose', 'running-within-create-script'] }) +const verboseLogger = !argv.options.verbose + ? () => {} + : function () { + console.log('[verbose]', ...arguments) + } + +const progressLogger = function () { + console.log(' - ', ...arguments) +} + const npmrc = ` audit=false `.trimStart() @@ -52,13 +62,33 @@ async function updatePackageJson (packageJsonPath) { await fs.writeJson(packageJsonPath, newPackageJson, packageJsonFormat) } +function displaySuccessMessage () { + console.log('') + console.log('Prototype created') + if (argv.paths.length > 0) { + console.log('') + console.log('Change to your prototype directory:') + console.log(` cd ${argv.paths[0]}`) + } + console.log('') + console.log('To run your prototype:') + console.log(' npm run dev') + console.log('') +} + async function initialiseGitRepo () { - if (argv.options['no-version-control']) { + const noVersionControlArg = 'no-version-control' + if (argv.options[noVersionControlArg]) { + verboseLogger(`User specified --${noVersionControlArg}, skipping.`) return } + progressLogger('Initialising git') try { await exec('git init --initial-branch=main && git add -A .', {}) } catch (e) { + verboseLogger('Failed to initialise git') + verboseLogger(e.message) + verboseLogger(e.errorOutput) return } @@ -143,6 +173,23 @@ function warnIfNpmStart (argv, env) { } } +function writeEmptyPackageJson (installDirectory) { + return fs.writeJson(path.join(installDirectory, 'package.json'), {}) +} + +function getArgumentsToPassThrough () { + const additionalArgs = Object.keys(argv.options).map(name => `--${name}="${argv.options[name]}"`) + return additionalArgs +} + +function initialiserRequiresOldInitSyntax () { + const dependencies = require(path.join(getInstallLocation(), 'package.json')).dependencies || {} + const kitVersionParts = (dependencies['govuk-prototype-kit'] || '').split('.') + + const requiresOldInitSyntax = kitVersionParts[0].endsWith('13') && Number(kitVersionParts[1]) <= 2 + return requiresOldInitSyntax +} + async function runCreate () { // Install as a two-stage bootstrap process. // @@ -157,6 +204,7 @@ async function runCreate () { // as possible into stage two; stage one should ideally be able to install // any future version of the kit. + console.log('') const installDirectory = getInstallLocation() const kitDependency = getChosenKitDependency() @@ -167,25 +215,34 @@ async function runCreate () { return } - await fs.writeJson(path.join(installDirectory, 'package.json'), {}, packageJsonFormat) - console.log('Creating your prototype') + await writeEmptyPackageJson(installDirectory) + + progressLogger('Installing dependencies') + await npmInstall(installDirectory, [kitDependency, 'govuk-frontend']) - const additionalArgs = Object.keys(argv.options).map(name => `--${name}="${argv.options[name]}"`) + let runningWithinCreateScriptFlag = '--running-within-create-script' + + if (initialiserRequiresOldInitSyntax()) { + runningWithinCreateScriptFlag = '--' + } + + progressLogger('Setting up your prototype') - await spawn('npx', ['govuk-prototype-kit', 'init', '--', installDirectory, ...additionalArgs], { + await spawn('npx', ['govuk-prototype-kit', 'init', runningWithinCreateScriptFlag, installDirectory, `--created-from-version=${kitVersion}`, ...(getArgumentsToPassThrough())], { cwd: installDirectory, stdio: 'inherit' }) + .then(displaySuccessMessage) } async function runInit () { // `init` is stage two of the install process (see above), it should be // called by `create` with the correct arguments. - if (process.argv[3] !== '--') { + if (!argv.options['running-within-create-script'] && process.argv[3] !== '--') { usage() process.exitCode = 2 return @@ -201,7 +258,8 @@ async function runInit () { fs.writeFile(path.join(installDirectory, '.npmrc'), npmrc, 'utf8'), copyFile('LICENCE.txt'), updatePackageJson(path.join(installDirectory, 'package.json')) - ]).then(initialiseGitRepo) + ]) + .then(initialiseGitRepo) } async function runMigrate () { @@ -271,6 +329,8 @@ function runServe () { } ;(async () => { + verboseLogger(`Using kit version [${kitVersion}] for command [${argv.command}]`) + verboseLogger('Argv:', argv) switch (argv.command) { case 'create': return runCreate() diff --git a/bin/utils/argv-parser.js b/bin/utils/argv-parser.js index d434f8a7f7..00f8a04497 100644 --- a/bin/utils/argv-parser.js +++ b/bin/utils/argv-parser.js @@ -23,6 +23,9 @@ function parse (argvInput, config = {}) { } args.forEach(arg => { + if (arg === '--') { + return + } if (arg.startsWith('-')) { const processedArgName = processOptionName(arg) if (booleanOptions.includes(processedArgName)) { diff --git a/bin/utils/index.js b/bin/utils/index.js index 3a5bd1ce90..d2aa3b7e06 100644 --- a/bin/utils/index.js +++ b/bin/utils/index.js @@ -8,13 +8,10 @@ async function npmInstall (cwd, dependencies) { return spawn( 'npm', [ 'install', - '--no-audit', - '--loglevel=error', - '--omit=dev', ...dependencies ], { cwd, - stdio: 'inherit' + stderr: 'inherit' }) } diff --git a/migrator/file-helpers.js b/migrator/file-helpers.js index 9b309bdeb8..082f6a4429 100644 --- a/migrator/file-helpers.js +++ b/migrator/file-helpers.js @@ -9,13 +9,18 @@ const fse = require('fs-extra') // local dependencies const { packageDir, projectDir } = require('../lib/utils/paths') const { log, sanitisePaths } = require('./logger') +const { parse } = require('../bin/utils/argv-parser') + +const argv = parse(process.argv, { + booleans: ['no-version-control', 'verbose'] +}) async function verboseLog () { await log(...arguments) - if (process.env.GPK_UPGRADE_DEBUG !== 'true') { + if (process.env.GPK_UPGRADE_DEBUG !== 'true' || argv.options.verbose) { return } - console.log(...arguments) + console.log('[debug]', ...arguments) } async function verboseLogError (e) {