-
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.
Merge pull request #1481 from alphagov/ldeb-move-js-to-lib
Move shared JavaScript to lib folder
- Loading branch information
Showing
20 changed files
with
519 additions
and
46 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
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
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 |
---|---|---|
@@ -1,20 +1,3 @@ | ||
window.GOVUKPrototypeKit = { | ||
documentReady: function (fn) { | ||
if (document.readyState !== 'loading') { | ||
// IE9 support | ||
fn() | ||
} else { | ||
// Everything else | ||
document.addEventListener('DOMContentLoaded', fn) | ||
} | ||
} | ||
} | ||
|
||
// Warn about using the kit in production | ||
if (window.console && window.console.info) { | ||
window.console.info('GOV.UK Prototype Kit - do not use for production') | ||
} | ||
|
||
window.GOVUKPrototypeKit.documentReady(function () { | ||
window.GOVUKFrontend.initAll() | ||
window.GOVUKPrototypeKit.documentReady(() => { | ||
// Add JavaScript here | ||
}) |
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
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
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,5 @@ | ||
const { updateJavascripts } = require('./update-javascripts') | ||
const { updateScss } = require('./update-scss') | ||
|
||
updateJavascripts() | ||
updateScss() |
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,29 @@ | ||
const fs = require('fs').promises | ||
const path = require('path') | ||
|
||
const { getProjectVersion, patchUserFile } = require('./util') | ||
const { projectDir } = require('../path-utils') | ||
|
||
async function updateJavascripts () { | ||
// Delete any old shared files | ||
const appJsPath = path.join(projectDir, 'app', 'assets', 'javascripts') | ||
await fs.unlink(path.join(appJsPath, 'auto-store-data.js')).catch(() => {}) | ||
await fs.unlink(path.join(appJsPath, 'jquery-1.11.3.js')).catch(() => {}) | ||
await fs.unlink(path.join(appJsPath, 'step-by-step-nav.js')).catch(() => {}) | ||
await fs.unlink(path.join(appJsPath, 'step-by-step-navigation.js')).catch(() => {}) | ||
|
||
const userVersion = await 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 | ||
} | ||
|
||
await patchUserFile(userVersion, 'app/assets/javascripts/application.js') | ||
} | ||
|
||
module.exports = { | ||
updateJavascripts | ||
} |
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,124 @@ | ||
/* eslint-env jest */ | ||
|
||
const fs = require('fs') | ||
const fsp = require('fs').promises | ||
const path = require('path') | ||
|
||
jest.mock('./util/fetch-original') | ||
jest.mock('./util', () => { | ||
const originalModule = jest.requireActual('./util') | ||
|
||
return { | ||
...originalModule, | ||
getProjectVersion: jest.fn(async () => '') | ||
} | ||
}) | ||
const { fetchOriginal: mockFetchOriginal } = require('./util/fetch-original') | ||
const { getProjectVersion: mockGetProjectVersion } = require('./util') | ||
const { projectDir } = require('../path-utils') | ||
|
||
const { updateJavascripts } = require('./update-javascripts') | ||
|
||
const originalApplicationJs = `/* global $ */ | ||
// Warn about using the kit in production | ||
if (window.console && window.console.info) { | ||
window.console.info('GOV.UK Prototype Kit - do not use for production') | ||
} | ||
$(document).ready(function () { | ||
window.GOVUKFrontend.initAll() | ||
}) | ||
` | ||
const newApplicationJs = fs.readFileSync( | ||
path.join('app', 'assets', 'javascripts', 'application.js'), | ||
'utf8' | ||
) | ||
|
||
describe('updateJavascripts', () => { | ||
let mockCopyFile, mockReadFile, mockUnlink, mockWriteFile | ||
|
||
beforeEach(async () => { | ||
mockGetProjectVersion.mockResolvedValue( | ||
'12.1.1' | ||
) | ||
|
||
mockFetchOriginal.mockResolvedValue( | ||
originalApplicationJs | ||
) | ||
|
||
mockReadFile = jest.spyOn(fsp, 'readFile').mockResolvedValue( | ||
newApplicationJs | ||
) | ||
|
||
mockCopyFile = jest.spyOn(fsp, 'copyFile').mockImplementation(async () => {}) | ||
mockUnlink = jest.spyOn(fsp, 'unlink').mockImplementation(async () => {}) | ||
mockWriteFile = jest.spyOn(fsp, 'writeFile').mockImplementation(async () => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('replaces application.js if the user has not updated it', async () => { | ||
// theirs | ||
mockReadFile.mockResolvedValueOnce( | ||
originalApplicationJs | ||
) | ||
|
||
await updateJavascripts() | ||
|
||
expect(mockCopyFile).toHaveBeenCalledWith( | ||
path.join(projectDir, 'update', 'app', 'assets', 'javascripts', 'application.js'), | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'application.js') | ||
) | ||
}) | ||
|
||
it('rewrites application.js if the user has added lines to the bottom of the file', async () => { | ||
// theirs | ||
mockReadFile.mockResolvedValueOnce( | ||
originalApplicationJs + '\ncallMyCode()\n' | ||
) | ||
// ours | ||
mockReadFile.mockResolvedValue( | ||
newApplicationJs | ||
) | ||
|
||
await updateJavascripts() | ||
|
||
expect(mockWriteFile).toHaveBeenCalledWith( | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'application.js'), | ||
newApplicationJs + '\ncallMyCode()\n', | ||
'utf8' | ||
) | ||
}) | ||
|
||
it('does not touch application.js if the user prototype is already on v13', async () => { | ||
mockGetProjectVersion.mockResolvedValue( | ||
'13.0.0' | ||
) | ||
|
||
await updateJavascripts() | ||
|
||
expect(mockReadFile).not.toHaveBeenCalled() | ||
expect(mockWriteFile).not.toHaveBeenCalled() | ||
expect(mockCopyFile).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('removes files that have been moved from app folder', async () => { | ||
await updateJavascripts() | ||
|
||
expect(mockUnlink).toHaveBeenCalledWith( | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'auto-store-data.js') | ||
) | ||
expect(mockUnlink).toHaveBeenCalledWith( | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'jquery-1.11.3.js') | ||
) | ||
expect(mockUnlink).toHaveBeenCalledWith( | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'step-by-step-nav.js') | ||
) | ||
expect(mockUnlink).toHaveBeenCalledWith( | ||
path.join(projectDir, 'app', 'assets', 'javascripts', 'step-by-step-navigation.js') | ||
) | ||
}) | ||
}) |
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,16 @@ | ||
const { | ||
appSassPatternsPath, | ||
libSassPatternsPath, | ||
appSassPath, | ||
libSassPath, | ||
removeKitSassFromApplicationSass, | ||
removeKitSassFromAppSassPath, | ||
removeLegacyIE8Sass | ||
} = require('./update_scss') | ||
|
||
module.exports.updateScss = function () { | ||
removeKitSassFromApplicationSass() | ||
removeKitSassFromAppSassPath(appSassPatternsPath, libSassPatternsPath) | ||
removeKitSassFromAppSassPath(appSassPath, libSassPath) | ||
removeLegacyIE8Sass() | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/_update_scss/update_scss.js → lib/_update/update-scss/update_scss.js
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
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
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,33 @@ | ||
const https = require('https') | ||
|
||
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) => { | ||
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) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
fetchOriginal | ||
} |
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,29 @@ | ||
/* eslint-env jest */ | ||
|
||
const https = require('https') | ||
const stream = require('stream') | ||
|
||
const { fetchOriginal } = require('./fetch-original') | ||
|
||
describe('fetchOriginal', () => { | ||
it('gets the contents of a file as of version from GitHub', async () => { | ||
const mockHttpsGet = jest.spyOn(https, 'get').mockImplementation((url, callback) => { | ||
const response = new stream.PassThrough() | ||
response.statusCode = 200 | ||
|
||
callback(response) | ||
|
||
response.write('foo\n') | ||
response.write('bar\n') | ||
response.end() | ||
}) | ||
|
||
await expect(fetchOriginal('99.99.99', 'app/views/foo.html')).resolves.toEqual( | ||
'foo\nbar\n' | ||
) | ||
expect(mockHttpsGet).toHaveBeenCalledWith( | ||
'https://raw.githubusercontent.com/alphagov/govuk-prototype-kit/v99.99.99/app/views/foo.html', | ||
expect.anything() | ||
) | ||
}) | ||
}) |
Oops, something went wrong.