Skip to content

Commit

Permalink
Add update post script to update user's app javascripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lfdebrux committed Jul 22, 2022
1 parent 61f24a4 commit 3cd2582
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
85 changes: 85 additions & 0 deletions lib/_update_javascripts.js
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()
}
10 changes: 6 additions & 4 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ copy () {
}

post () {
# execute _update_scss if it exists in the update folder
if [ -d "update/lib/_update_scss" ]; then
node "update/lib/_update_scss"
fi
if [ -d "update/lib/_update_scss" ]; then
node "update/lib/_update_scss"
fi
if [ -f "update/lib/_update_javascripts.js" ]; then
node "update/lib/_update_javascripts"
fi
}

if [ "$0" == "${BASH_SOURCE:-$0}" ]
Expand Down

0 comments on commit 3cd2582

Please sign in to comment.