diff --git a/bin/cli b/bin/cli index eae5e15616..f1e82d0784 100755 --- a/bin/cli +++ b/bin/cli @@ -6,7 +6,7 @@ const path = require('path') const { spawn, exec } = require('../lib/exec') const { parse } = require('./utils/argv-parser') const { prepareMigration, preflightChecks } = require('../migrator') -const { npmInstall, packageJsonFormat } = require('./utils') +const { npmInstall, packageJsonFormat, getPackageVersionFromPackageJson, splitSemverVersion } = require('./utils') // Avoid requiring any kit server code at the top-level as we might want to // change environment variables below. @@ -26,6 +26,13 @@ const verboseLogger = !argv.options.verbose } const progressLogger = function () { + if (argv.options['created-from-version']) { + const version = splitSemverVersion(argv.options['created-from-version']) + if (version.major === 13 && version.minor < 2) { + return + } + } + console.log(' - ', ...arguments) } @@ -182,11 +189,10 @@ function getArgumentsToPassThrough () { return additionalArgs } -function initialiserRequiresOldInitSyntax () { - const dependencies = require(path.join(getInstallLocation(), 'package.json')).dependencies || {} - const kitVersionParts = (dependencies['govuk-prototype-kit'] || '').split('.') +async function initialiserRequiresOldInitSyntax () { + const version = await getPackageVersionFromPackageJson(path.join(getInstallLocation(), 'node_modules', 'govuk-prototype-kit', 'package.json')) - const requiresOldInitSyntax = kitVersionParts[0].endsWith('13') && Number(kitVersionParts[1]) <= 2 + const requiresOldInitSyntax = version.major === 13 && version.minor < 2 return requiresOldInitSyntax } @@ -204,7 +210,10 @@ async function runCreate () { // as possible into stage two; stage one should ideally be able to install // any future version of the kit. + verboseLogger('Cli running from', __filename) + console.log('') + const installDirectory = getInstallLocation() const kitDependency = getChosenKitDependency() @@ -225,7 +234,7 @@ async function runCreate () { let runningWithinCreateScriptFlag = '--running-within-create-script' - if (initialiserRequiresOldInitSyntax()) { + if (await initialiserRequiresOldInitSyntax()) { runningWithinCreateScriptFlag = '--' } diff --git a/bin/utils/argv-parser.spec.js b/bin/utils/argv-parser.spec.js index f5ab4376cc..95b5bf430c 100644 --- a/bin/utils/argv-parser.spec.js +++ b/bin/utils/argv-parser.spec.js @@ -137,7 +137,7 @@ describe('argv parser', () => { paths: ['/tmp/hello-world'] }) }) - it('should support semvar numbers as values', () => { + it('should support semver numbers as values', () => { const result = parser.parse(addStandardArgs([ '--version', '13.0.1', diff --git a/bin/utils/index.js b/bin/utils/index.js index d2aa3b7e06..826e6d89ca 100644 --- a/bin/utils/index.js +++ b/bin/utils/index.js @@ -1,6 +1,7 @@ // local dependencies const { spawn } = require('../../lib/exec') +const fse = require('fs-extra') const packageJsonFormat = { encoding: 'utf8', spaces: 2 } @@ -15,7 +16,24 @@ async function npmInstall (cwd, dependencies) { }) } +function splitSemverVersion (version) { + const versionParts = version.split('.').map(Number) + + return { + major: versionParts[0], + minor: versionParts[1], + patch: versionParts[2] + } +} + +async function getPackageVersionFromPackageJson (packageJsonPath) { + const version = (await fse.readJson(packageJsonPath)).version + return splitSemverVersion(version) +} + module.exports = { npmInstall, - packageJsonFormat + packageJsonFormat, + getPackageVersionFromPackageJson, + splitSemverVersion }