-
Notifications
You must be signed in to change notification settings - Fork 2
[DEVOPS-3113] Add ability to version static assets #2
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
Merged
pa-eps
merged 49 commits into
elasticpath:master
from
ep-linden:automate-asset-versioning
Dec 2, 2021
Merged
Changes from 21 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
cab63cd
Add the asset versioning logic from doc-extension-framework repo
ep-linden d235d89
Fix typo
ep-linden c5679e7
Refactor move asset files function to be modular
ep-linden 93311dc
Remove incomplete function
ep-linden 6185a62
Refactor commander to specify paramters in CLI when assigning values …
ep-linden 6400f03
Add assetType parameter to createVersion module
ep-linden c4b66aa
Fix bug because create function for creatVersion was missing.
ep-linden ebc7d27
Set store options as properties to store it as the commander object's…
ep-linden a9930eb
Change parameter name to staticDir and change assetVersioning to stat…
ep-linden 122c00f
Refactor logic for versioning static asset files to be backwards comp…
ep-linden 17a806b
Rename static types to static assets to improve clarity. Change funct…
ep-linden feb7378
Reverse changes to static versioning function to use newer core funct…
ep-linden 65d2c85
Seperate function into smaller functions for replacing relative paths…
ep-linden 0038548
Place replacing relative function into versioning functionality. Add …
ep-linden 02b4153
Remove old commented out funciton
ep-linden f2aeef5
Rename function for clarity
ep-linden 5e0ac95
Remove unused console log in versioning function
ep-linden d570e38
Add comment for the new logic
ep-linden 439caec
Improve the comment for update relative path function
ep-linden 4f8451c
Rename static assets variable and pass that variable to the createVer…
ep-linden ee6cc1a
Add blankline in snapshot version file. Use node fs.access function t…
ep-linden f785227
Add unit test for the static asset function. Add rewire to dev depend…
ep-linden 7054fab
Add ./ and remove blank line
ep-linden d0b0a07
Remove unused function and add blank line at end of file.
ep-linden 7f24e01
Handle the version error in the CLI and refactor accordingly. Change …
ep-linden 5c55029
Handle error when static asset type directory does not exist. Change …
ep-linden a319847
Use fs.access instead of opendir to check if static asset folder exis…
ep-linden d453efc
Refactor static asset versioning logic to handle the potential versio…
ep-linden a36d930
Fix typo in paths
ep-linden c9fd896
Add a then block to prevent unnecessary file search if the static ass…
ep-linden 1a75a09
Update staticVersionTest to match with new changes in staticVersioner…
ep-linden a704fad
Remove variable checks in snapshotVersion and move them into createVe…
ep-linden 678bd5b
Remove console.log in createVersion
ep-linden 4663f5e
Add semicolon in createVersion. Add tests for createVersion because c…
ep-linden 851a952
Refacator code based on feedback. Use fs.access instead of fs.existsS…
ep-linden 945a9ba
Add additional describes to explain the scenarios in the staticVersio…
ep-linden af345bd
Change if check in removeFilesInDirectory function check an array of …
ep-linden 1507079
Add checks for staticDir in the siteUtilsTest.js.
ep-linden 157dcd3
Add s to error message. Add logic to check if the docs repo as a whol…
ep-linden 4f8aa9d
Set replacement text for versioned_docs based on if condition.
ep-linden 2b3a68c
Change search pattern to replace in versioned_docs dir.
ep-linden c5e9a6a
Move options variables inside the create function since the standalon…
ep-linden df87a13
Declare the replacementText variable for each updateRelativePath to i…
ep-linden 6c6eb11
Organize test suite. Add more tests to cover the different use-cases …
ep-linden d1067f8
Fix and refactor tests in createVersionTest.js.
ep-linden e3e6d48
Rename test file name. Remove unit test for function call ordering as…
ep-linden b2bc6d8
Change console.info to avoid confusion with static asset versioning.
ep-linden dd688b7
Deleted CLI test because no extra logic is done outside of Commander'…
ep-linden ca16279
Reorganize test cases in staticVersionerTest.js. Update comments for …
ep-linden 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 hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
|
||
async function versionStaticAssets(sitePaths, staticAssets, version) { | ||
let numberOfVersions = await getNumberOfVersions(); | ||
let staticDir = sitePaths.staticFolder; | ||
let versionDocsDir = sitePaths.versionedDocs; | ||
let excludeFromRemoval = ["next"]; | ||
|
||
for (const staticType of staticAssets) { | ||
console.info("Versioning static asset files..."); | ||
let staticTypePath = path.join(staticDir, staticType); | ||
let staticTypeNextPath = path.join(staticTypePath, "next"); | ||
// if a static asset is versioned for the first time, create the next directory | ||
await fs.access(staticTypeNextPath) | ||
.catch(async () => { | ||
await copyDirectory(staticTypePath, staticTypeNextPath); | ||
await removeFilesInDirectory(staticTypePath, excludeFromRemoval); | ||
}) | ||
let staticTypeVersionPath = path.join(staticTypePath, version); | ||
await copyDirectory(staticTypeNextPath, staticTypeVersionPath) | ||
} | ||
|
||
let baseVersionedDocsPath = path.join(versionDocsDir, `version-${version}`); | ||
if (numberOfVersions === 1) { | ||
await updateRelativePaths(sitePaths.docs, staticAssets); | ||
} | ||
await updateRelativePaths(baseVersionedDocsPath, staticAssets, version); | ||
} | ||
|
||
|
||
async function getNumberOfVersions() { | ||
let fileContent = await fs.readFile("versions.json", "utf8"); | ||
let jsonContent = JSON.parse(fileContent); | ||
return jsonContent.length; | ||
} | ||
|
||
async function copyDirectory(from, to) { | ||
await fs.mkdir(to); | ||
|
||
const files = await fs.readdir(from, {withFileTypes: true}); | ||
for (const file of files) { | ||
let relativePath = path.join(from, file.name); | ||
let targetPath = path.join(to, file.name); | ||
if (file.isDirectory() && relativePath !== to) { | ||
await copyDirectory(relativePath, targetPath); | ||
} else if(!file.isDirectory()) { | ||
await fs.copyFile(relativePath, targetPath) | ||
.catch((err) => console.log(err + '\n' + `${relativePath} could not be copied`)); | ||
} | ||
} | ||
} | ||
|
||
async function removeFilesInDirectory(from, exclude) { | ||
const files = await fs.readdir(from, {withFileTypes: true}); | ||
for (const file of files) { | ||
if (!exclude.includes(file.name)) { | ||
await fs.rm(path.join(from, file.name), {recursive: true}); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
Recursive function that loops through all files in the current directory and | ||
subdirectories, and replaces the links in each file. | ||
*/ | ||
async function updateRelativePaths(basePath, staticAssets, version="") { | ||
// read all files from directory | ||
const files = await fs.readdir(basePath, {withFileTypes: true}); | ||
for (const file of files) { | ||
// if file type is a directory, a recursive call is made | ||
if (file.isDirectory()) { | ||
let subDirName = file.name; | ||
await updateRelativePaths(path.join(basePath, subDirName), staticAssets, version); | ||
} else { | ||
let filePath = path.join(basePath, file.name); | ||
await replaceRelativePaths(filePath, staticAssets, version); | ||
} | ||
} | ||
} | ||
|
||
async function replaceRelativePaths(filePath, staticAssets, version="") { | ||
let fileContent = await fs.readFile(filePath, {encoding: "utf8", flag: "r+"}); | ||
// flag to set when files are modified | ||
let isFileContentModified = false; | ||
let numberOfVersions = await getNumberOfVersions(); | ||
|
||
// loop through all the passed in static assets and replace the text | ||
for (const staticType of staticAssets) { | ||
|
||
let relativeLinkPattern = new RegExp(`../${staticType}/next`, 'gm'); | ||
let replacementText = `${staticType}/${version}`; | ||
|
||
// if versioning for the first time, replace the links in docs path and versioned docs path differently | ||
if (numberOfVersions === 1 && filePath.includes('/docs/')) { | ||
relativeLinkPattern = new RegExp(`../${staticType}/`, 'gm') | ||
replacementText = `../../${staticType}/next/`; | ||
} else if (numberOfVersions === 1 && filePath.includes('/versioned_docs/')) { | ||
relativeLinkPattern = new RegExp(`../${staticType}/`, 'gm'); | ||
replacementText = `../${staticType}/${version}/`; | ||
} | ||
|
||
fileContent = fileContent.replace(relativeLinkPattern, function() { | ||
isFileContentModified = true; | ||
return replacementText; | ||
}); | ||
} | ||
|
||
// if contents have been modified then write to file | ||
if (isFileContentModified) { | ||
await fs.writeFile(filePath, fileContent); | ||
} | ||
} | ||
|
||
module.exports.versionStaticAssets = versionStaticAssets; |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.