diff --git a/lib/_update_javascripts.js b/lib/_update_javascripts.js index d5242e0f26..9b27e89864 100644 --- a/lib/_update_javascripts.js +++ b/lib/_update_javascripts.js @@ -7,9 +7,12 @@ const path = require('path') const { projectDir, packageDir } = require('./path-utils') const updateDir = path.join(projectDir, 'update') -async function fetchOriginal(filePath) { - const oldVersion = (await fs.readFile(path.join(projectDir, 'VERSION.txt'), 'utf8')).trim() - const remoteUrl = `https://raw.githubusercontent.com/alphagov/govuk-prototype-kit/v${oldVersion}/${filePath}` +async function getProjectVersion () { + return (await fs.readFile(path.join(projectDir, 'VERSION.txt'), 'utf8')).trim() +} + +async function fetchOriginal(version, filePath) { + const remoteUrl = `https://raw.githubusercontent.com/alphagov/govuk-prototype-kit/v${version}/${filePath}` let data = '' return new Promise((resolve, reject) => { @@ -37,10 +40,20 @@ async function fetchOriginal(filePath) { } async function removeKitJsFromApplicationJs () { + const userVersion = getProjectVersion() + + // If the user already has version 13 or greater of the kit installed then + // their application.js file is all their code and we don't don't want to + // change it + if (userVersion >= '13.0.0') { + return + } + const assetPath = 'assets/javascripts/application.js' - const original = await fetchOriginal(path.posix.join('app', assetPath)) + const original = await fetchOriginal(userVersion, path.posix.join('app', assetPath)) const theirs = await fs.readFile(path.resolve(projectDir, 'app', assetPath), 'utf8') + // If the user hasn't changed their application.js file we can just replace it completely if (original === theirs) { return fs.copyFile(path.join(packageDir, 'lib', assetPath), path.join(projectDir, 'app', assetPath))