-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate changed files if possible #2049
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f7d82c2
Migrate changed files if possible
BenSurgisonGDS 66c836d
adding test for upgradeIfPossible method
HannahJMWood 58e6336
refactoring application update into its own method
HannahJMWood b783ffb
started fixing issues with failing test
HannahJMWood c68628a
Replace any of the sections if possible
BenSurgisonGDS d4816f9
add globals line if there's jquery in application
HannahJMWood fe19a65
adding another new line and comment
HannahJMWood 7c41308
Make sure all original lines exist in sequence within the file before…
BenSurgisonGDS 2cb2652
addressing some review comments
HannahJMWood 6a0618e
Just insert user's code into starter application code
BenSurgisonGDS abfa486
Fix verbose log
BenSurgisonGDS 67fafe5
Simplify
BenSurgisonGDS c442ed0
Add change log entry
BenSurgisonGDS 9db7e7a
Migrate changed filters if possible
BenSurgisonGDS 3c9b1f9
Fixed tests
BenSurgisonGDS e70e3cb
Add change log entry
BenSurgisonGDS 59764d4
Fix application.js conversion
BenSurgisonGDS c6a064c
Merge pull request #2079 from alphagov/migrate-changed-filters-if-pos…
BenSurgisonGDS 40f2d4e
Add more complex application.js to test
BenSurgisonGDS e0518bc
Add line containing the frontend initAll function to make sure it is …
BenSurgisonGDS 6a2f813
Remove big comment if unchanged from filters file and move upgrade fu…
BenSurgisonGDS 46e3a5f
Fix test
BenSurgisonGDS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,190 @@ | ||
// core dependencies | ||
const path = require('path') | ||
const fsp = require('fs').promises | ||
|
||
// local dependencies | ||
const { starterDir, projectDir } = require('../lib/utils/paths') | ||
const { addReporter } = require('./reporter') | ||
|
||
function removeMatchedText (originalContent, matchText) { | ||
const originalContentLines = originalContent.split('\n') | ||
const newContentLines = [] | ||
|
||
// Remove the matchText from the original code | ||
while (originalContentLines.length) { | ||
const match = matchText.findIndex((text) => text.every((line, index) => { | ||
const originalContentLine = originalContentLines[index] | ||
return line === originalContentLine?.trim() | ||
})) | ||
if (match === -1) { | ||
newContentLines.push(originalContentLines.shift()) | ||
} else { | ||
matchText[match].forEach(() => originalContentLines.shift()) | ||
} | ||
} | ||
return newContentLines.join('\n') | ||
} | ||
|
||
function occurencesOf (searchText, text) { | ||
return text.split(searchText).length - 1 | ||
} | ||
|
||
async function upgradeApplicationJs (fullPath, reporter) { | ||
const commentText = `// | ||
// For guidance on how to add JavaScript see: | ||
// https://prototype-kit.service.gov.uk/docs/adding-css-javascript-and-images | ||
//` | ||
const matchText = [ | ||
[ | ||
'// 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.GOVUKFrontend.initAll()'] | ||
] | ||
const jqueryReadyText = '$(document).ready(' | ||
const kitReadyText = 'window.GOVUKPrototypeKit.documentReady(' | ||
const originalContent = await fsp.readFile(fullPath, 'utf8') | ||
|
||
// Remove the matchText from the original code | ||
let modifiedContent = removeMatchedText(originalContent, matchText) | ||
if (occurencesOf(jqueryReadyText, modifiedContent) === occurencesOf('$.', modifiedContent) + occurencesOf('$(', modifiedContent)) { | ||
modifiedContent = modifiedContent.replaceAll(jqueryReadyText, kitReadyText) | ||
// Remove if jQuery is not necessary | ||
if (!modifiedContent.includes('$(') && !modifiedContent.includes('$.')) { | ||
modifiedContent = modifiedContent.replaceAll(/\/\*\s*global\s+\$\s?\*\/[\s\r\n]*/g, '') | ||
} | ||
} | ||
|
||
const newContentLines = [] | ||
let commentInserted = false | ||
|
||
const modifiedContentLines = modifiedContent.split('\n') | ||
|
||
modifiedContentLines.forEach((line, index) => { | ||
if (!commentInserted) { | ||
if (![ | ||
/\/\*\s*global\s+/, | ||
/"use strict"/ | ||
].some((regex) => line.search(regex) > -1)) { | ||
if (index > 0) { | ||
newContentLines.push('') | ||
} | ||
commentText.split('\n').forEach((commentLine) => newContentLines.push(commentLine)) | ||
// insert a blank line after the comments if it's not the first line | ||
commentInserted = true | ||
if (line.trim().length > 0) { | ||
newContentLines.push('') | ||
} | ||
} | ||
} | ||
newContentLines.push(line) | ||
}) | ||
|
||
await fsp.writeFile(fullPath, newContentLines.join('\n')) | ||
await reporter(true) | ||
return true | ||
} | ||
|
||
const oldFilterFirstCommentLines = `/** | ||
* Instantiate object used to store the methods registered as a | ||
* 'filter' (of the same name) within nunjucks. You can override | ||
* gov.uk core filters by creating filter methods of the same name. | ||
* @type {Object} | ||
*/`.split('\n').map(line => line.trim()) | ||
|
||
const oldFilterSecondCommentLines = `/* ------------------------------------------------------------------ | ||
add your methods to the filters obj below this comment block: | ||
@example: | ||
|
||
filters.sayHi = function(name) { | ||
return 'Hi ' + name + '!' | ||
} | ||
|
||
Which in your templates would be used as: | ||
|
||
{{ 'Paul' | sayHi }} => 'Hi Paul' | ||
|
||
Notice the first argument of your filters method is whatever | ||
gets 'piped' via '|' to the filter. | ||
|
||
Filters can take additional arguments, for example: | ||
|
||
filters.sayHi = function(name,tone) { | ||
return (tone == 'formal' ? 'Greetings' : 'Hi') + ' ' + name + '!' | ||
} | ||
|
||
Which would be used like this: | ||
|
||
{{ 'Joel' | sayHi('formal') }} => 'Greetings Joel!' | ||
{{ 'Gemma' | sayHi }} => 'Hi Gemma!' | ||
|
||
For more on filters and how to write them see the Nunjucks | ||
documentation. | ||
|
||
------------------------------------------------------------------ */ | ||
`.split('\n').map(line => line.trim()) | ||
|
||
const oldFilterThirdCommentLines = ` | ||
/* ------------------------------------------------------------------ | ||
keep the following line to return your filters to the app | ||
------------------------------------------------------------------ */`.split('\n').map(line => line.trim()) | ||
|
||
async function upgradeFiltersJs (fullPath, reporter) { | ||
const firstLine = 'module.exports = function (env) {' | ||
const lastLine = 'return filters' | ||
const originalContent = await fsp.readFile(fullPath, 'utf8') | ||
|
||
// Only convert the filters if the first and last lines above are found in the file | ||
if (![firstLine, lastLine].every(line => originalContent.includes(line))) { | ||
await reporter(false) | ||
return false | ||
} | ||
|
||
const matchText = [ | ||
[firstLine], | ||
oldFilterFirstCommentLines, | ||
oldFilterSecondCommentLines, | ||
oldFilterThirdCommentLines, | ||
[lastLine] | ||
] | ||
const starterFile = path.join(starterDir, 'app', 'filters.js') | ||
const starterContent = await fsp.readFile(starterFile, 'utf8') | ||
|
||
// Remove the matchText from the original code | ||
const modifiedContent = removeMatchedText(originalContent, matchText) | ||
|
||
// Remove the final bracket and add the addFilter conversion code | ||
const newContent = starterContent + '\n' + | ||
modifiedContent.substring(0, modifiedContent.lastIndexOf('}')).trimEnd() + '\n' + ` | ||
// Add the filters using the addFilter function | ||
Object.entries(filters).forEach(([name, fn]) => addFilter(name, fn)) | ||
` | ||
await fsp.writeFile(fullPath, newContent) | ||
await reporter(true) | ||
return true | ||
} | ||
|
||
async function upgradeIfPossible (filePath, matchFound) { | ||
if (matchFound) { | ||
return true | ||
} else { | ||
const reporter = await addReporter(`Update ${filePath}`) | ||
const fullPath = path.join(projectDir, filePath) | ||
const filename = fullPath.split(path.sep).pop() | ||
switch (filename) { | ||
case 'application.js': | ||
return await upgradeApplicationJs(fullPath, reporter) | ||
case 'filters.js': | ||
return await upgradeFiltersJs(fullPath, reporter) | ||
default: | ||
await reporter(false) | ||
return false | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
upgradeIfPossible | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've removed
initAll()
from this test, I think it's important that it's still in.