-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update post script to update user's app javascripts
- Loading branch information
Showing
2 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { Buffer } = require('buffer') | ||
const fs = require('fs').promises | ||
const https = require('https') | ||
const os = require('os') | ||
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}` | ||
|
||
let data = '' | ||
return new Promise((resolve, reject) => { | ||
https.get(remoteUrl, (response) => { | ||
let error | ||
|
||
if (response.statusCode !== 200) { | ||
error = new Error(`could not fetch ${remoteUrl}: status code ${response.statusCode}`) | ||
Object.assign(error, response) | ||
response.resume() | ||
reject(error) | ||
} | ||
|
||
response.setEncoding('utf8') | ||
|
||
response.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
|
||
response.on('end', () => { | ||
resolve(data) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
async function removeKitJsFromApplicationJs () { | ||
const assetPath = 'assets/javascripts/application.js' | ||
const original = await fetchOriginal(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)) | ||
} | ||
|
||
// Otherwise, if the original code is contained as-is in their file, we can | ||
// remove the shared lines, and add our hints | ||
if (theirs.includes(original)) { | ||
const ourLines = await fs.readFile(path.resolve(packageDir, 'app', assetPath), 'utf8').split('\n') | ||
|
||
let merged | ||
merged = theirs.replace(original, '\n') | ||
merged = ourLines[0] + '\n' + merged | ||
merged = merged + '\n' + ourLines[1:] | ||
return fs.writeFile(path.resolve(projectDir, 'app', assetPath), merged, 'utf8') | ||
} | ||
|
||
// If the original code is not recognisable, we should give up, but not | ||
// without giving a warning to the user | ||
console.warn( | ||
`WARNING: update.sh was not able to automatically update your ${assetPath} file.\n` + | ||
'If you have issues when running your prototype please contact the GOV.UK Prototype Kit team for support,\n' | ||
'using one of the methods listed at https://design-system.service.gov.uk/get-in-touch/' | ||
) | ||
} | ||
|
||
async function removeKitJSFromAppJsPath () { | ||
const appJsPath = path.join(projectDir, 'app', 'assets', 'javascripts') | ||
await fs.unlink('auto-store-data.js').catch(() => {}) | ||
await fs.unlink('jquery-1.11.3.js').catch(() => {}) | ||
await fs.unlink('step-by-step-nav.js').catch(() => {}) | ||
await fs.unlink('step-by-step-navigation.js').catch(() => {}) | ||
} | ||
|
||
async function main() { | ||
await removeKitJsFromAppJsPath() | ||
await removeKitJsFromApplicationJs() | ||
} | ||
|
||
if (require.main === module) { | ||
main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters