From 7a33ad2c685602755b6231f1938df76d51f11433 Mon Sep 17 00:00:00 2001 From: Robbie Wagner Date: Wed, 20 Nov 2024 07:38:50 -0500 Subject: [PATCH 1/3] Recursively replace ADDON_DOCS_ROOT_URL --- lib/deploy/plugin.js | 35 +++++------------- lib/utils/find-and-replace-in-directory.js | 43 ++++++++++++++++++++++ tests-node/unit/deploy/plugin-test.js | 2 +- 3 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 lib/utils/find-and-replace-in-directory.js diff --git a/lib/deploy/plugin.js b/lib/deploy/plugin.js index 4497340c5..80e58fada 100644 --- a/lib/deploy/plugin.js +++ b/lib/deploy/plugin.js @@ -6,6 +6,7 @@ const execa = require('execa'); const quickTemp = require('quick-temp'); const hostedGitInfo = require('hosted-git-info'); const maybeMigrateSiteFormat = require('./migration'); +const findAndReplaceInDirectory = require('../utils/find-and-replace-in-directory'); const { LATEST_VERSION_NAME } = require('../..'); module.exports = class AddonDocsDeployPlugin { @@ -201,38 +202,22 @@ module.exports = class AddonDocsDeployPlugin { } _updateIndexContents(context, stagingDirectory, appRoot, deployVersion) { - let indexPath = `${stagingDirectory}/${appRoot}/index.html`; - let rootURL = [this._getRootURL(), appRoot] + const directory = `${stagingDirectory}/${appRoot}`; + const rootURL = [this._getRootURL(), appRoot] .filter(Boolean) .join('/') .replace(/\\/g, '/'); - let addonDocsRootURL = rootURL === '' ? '/' : `/${rootURL}/`; - let contents = fs.readFileSync(indexPath, 'utf-8'); - let encodedVersion = encodeURIComponent(JSON.stringify(deployVersion)); - let updated = this._macroReplaceIndexContent( - contents, - addonDocsRootURL, - encodedVersion, - ); - - fs.writeFileSync(indexPath, updated); - } + const addonDocsRootURL = rootURL === '' ? '/' : `/${rootURL}/`; + const encodedVersion = encodeURIComponent(JSON.stringify(deployVersion)); - _macroReplaceIndexContent(contents, addonDocsRootURL, encodedVersion) { - return contents - .replace( - '%2FADDON_DOCS_ROOT_URL%2F', - encodeURIComponent(addonDocsRootURL), - ) - .replace(/\/?ADDON_DOCS_ROOT_URL\/?/g, addonDocsRootURL) - .replace(/%22ADDON_DOCS_DEPLOY_VERSION%22/g, encodedVersion); + findAndReplaceInDirectory(directory, addonDocsRootURL, encodedVersion); } _currentDeployVersion() { - let curpath = path.join('versions', this._getVersionPath()); - let name = this.userConfig.getVersionName(); - let sha = this.userConfig.repoInfo.sha; - let tag = this.userConfig.repoInfo.tag; + const curpath = path.join('versions', this._getVersionPath()); + const name = this.userConfig.getVersionName(); + const sha = this.userConfig.repoInfo.sha; + const tag = this.userConfig.repoInfo.tag; return { path: curpath, name, sha, tag, key: name }; } diff --git a/lib/utils/find-and-replace-in-directory.js b/lib/utils/find-and-replace-in-directory.js new file mode 100644 index 000000000..f8de0f6e2 --- /dev/null +++ b/lib/utils/find-and-replace-in-directory.js @@ -0,0 +1,43 @@ +/* eslint-disable no-console */ + +'use strict'; + +const fs = require('fs-extra'); +const path = require('path'); + +function processFile(filePath, addonDocsRootURL, encodedVersion) { + const contents = fs.readFileSync(filePath, 'utf-8'); + + contents + .replace('%2FADDON_DOCS_ROOT_URL%2F', encodeURIComponent(addonDocsRootURL)) + .replace(/\/?ADDON_DOCS_ROOT_URL\/?/g, addonDocsRootURL) + .replace(/%22ADDON_DOCS_DEPLOY_VERSION%22/g, encodedVersion); + + // Write the updated content to the file + fs.writeFileSync(filePath, contents); +} + +module.exports = function findAndReplaceInDirectory( + directory, + addonDocsRootURL, + encodedVersion, +) { + fs.readdir(directory, { withFileTypes: true }, (err, entries) => { + if (err) { + console.error(`Error reading directory ${directory}:`, err); + return; + } + + entries.forEach((entry) => { + const fullPath = path.join(directory, entry.name); + + if (entry.isDirectory()) { + // Recursively process subdirectories + findAndReplaceInDirectory(fullPath, addonDocsRootURL, encodedVersion); + } else if (entry.isFile()) { + // Process files + processFile(fullPath, addonDocsRootURL, encodedVersion); + } + }); + }); +}; diff --git a/tests-node/unit/deploy/plugin-test.js b/tests-node/unit/deploy/plugin-test.js index 18387f6c2..ceb42e03c 100644 --- a/tests-node/unit/deploy/plugin-test.js +++ b/tests-node/unit/deploy/plugin-test.js @@ -8,7 +8,7 @@ describe('`deploy` | plugin test', function () { this.pluginInstance = new AddonDocsDeployPlugin(); }); - it('_macroReplaceIndexContent', function () { + it.skip('_macroReplaceIndexContent', function () { const contents = ` From 2f81d9b23a3adfc56abb14377e101ce9c41189a3 Mon Sep 17 00:00:00 2001 From: Robbie Wagner Date: Wed, 20 Nov 2024 08:00:49 -0500 Subject: [PATCH 2/3] Update test --- lib/utils/find-and-replace-in-directory.js | 22 ++++++++++++++++------ tests-node/unit/deploy/plugin-test.js | 16 +++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/utils/find-and-replace-in-directory.js b/lib/utils/find-and-replace-in-directory.js index f8de0f6e2..78daa633d 100644 --- a/lib/utils/find-and-replace-in-directory.js +++ b/lib/utils/find-and-replace-in-directory.js @@ -5,19 +5,24 @@ const fs = require('fs-extra'); const path = require('path'); -function processFile(filePath, addonDocsRootURL, encodedVersion) { - const contents = fs.readFileSync(filePath, 'utf-8'); - - contents +function replaceAddonDocsRootURL(contents, addonDocsRootURL, encodedVersion) { + return contents .replace('%2FADDON_DOCS_ROOT_URL%2F', encodeURIComponent(addonDocsRootURL)) .replace(/\/?ADDON_DOCS_ROOT_URL\/?/g, addonDocsRootURL) .replace(/%22ADDON_DOCS_DEPLOY_VERSION%22/g, encodedVersion); +} + +function processFile(filePath, addonDocsRootURL, encodedVersion) { + const contents = fs.readFileSync(filePath, 'utf-8'); // Write the updated content to the file - fs.writeFileSync(filePath, contents); + fs.writeFileSync( + filePath, + replaceAddonDocsRootURL(contents, addonDocsRootURL, encodedVersion), + ); } -module.exports = function findAndReplaceInDirectory( +function findAndReplaceInDirectory( directory, addonDocsRootURL, encodedVersion, @@ -40,4 +45,9 @@ module.exports = function findAndReplaceInDirectory( } }); }); +} + +module.exports = { + findAndReplaceInDirectory, + replaceAddonDocsRootURL, }; diff --git a/tests-node/unit/deploy/plugin-test.js b/tests-node/unit/deploy/plugin-test.js index ceb42e03c..3d951670c 100644 --- a/tests-node/unit/deploy/plugin-test.js +++ b/tests-node/unit/deploy/plugin-test.js @@ -1,14 +1,12 @@ 'use strict'; const assert = require('chai').assert; -const AddonDocsDeployPlugin = require('../../../lib/deploy/plugin'); +const { + replaceAddonDocsRootURL, +} = require('../../../lib/utils/find-and-replace-in-directory'); describe('`deploy` | plugin test', function () { - beforeEach(function () { - this.pluginInstance = new AddonDocsDeployPlugin(); - }); - - it.skip('_macroReplaceIndexContent', function () { + it('replaceAddonDocsRootURL', function () { const contents = ` @@ -16,6 +14,8 @@ describe('`deploy` | plugin test', function () { + + @@ -31,7 +31,7 @@ describe('`deploy` | plugin test', function () { }), ); const addonDocsRootURL = '/my-addon/versions/master/'; - const actual = this.pluginInstance._macroReplaceIndexContent( + const actual = replaceAddonDocsRootURL( contents, addonDocsRootURL, encodedVersion, @@ -43,6 +43,8 @@ describe('`deploy` | plugin test', function () { + + From 6a9eeff7e35327a8d259a472ccf396fa07c4e1af Mon Sep 17 00:00:00 2001 From: Robbie Wagner Date: Wed, 20 Nov 2024 08:10:01 -0500 Subject: [PATCH 3/3] Update plugin-test.js --- tests-node/unit/deploy/plugin-test.js | 42 ++++++++++++++++++++------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/tests-node/unit/deploy/plugin-test.js b/tests-node/unit/deploy/plugin-test.js index 3d951670c..b0a6ee35c 100644 --- a/tests-node/unit/deploy/plugin-test.js +++ b/tests-node/unit/deploy/plugin-test.js @@ -6,7 +6,7 @@ const { } = require('../../../lib/utils/find-and-replace-in-directory'); describe('`deploy` | plugin test', function () { - it('replaceAddonDocsRootURL', function () { + it('replaceAddonDocsRootURL in index.html', function () { const contents = ` @@ -23,14 +23,14 @@ describe('`deploy` | plugin test', function () { `; const encodedVersion = encodeURIComponent( JSON.stringify({ - path: 'versions/master', - name: 'master', + path: 'versions/main', + name: 'main', sha: 'eef3', tag: null, - key: 'master', + key: 'main', }), ); - const addonDocsRootURL = '/my-addon/versions/master/'; + const addonDocsRootURL = '/my-addon/versions/main/'; const actual = replaceAddonDocsRootURL( contents, addonDocsRootURL, @@ -40,17 +40,39 @@ describe('`deploy` | plugin test', function () { - + - - - - + + + + `; assert.equal(actual, expected); }); + it('replaceAddonDocsRootURL in chunks', function () { + const chunk = + '(e.children=[]),e),o.p="ADDON_DOCS_ROOT_URL/assets/",(()=>{var e={143:0}'; + const encodedVersion = encodeURIComponent( + JSON.stringify({ + path: 'versions/main', + name: 'main', + sha: 'eef3', + tag: null, + key: 'main', + }), + ); + const addonDocsRootURL = '/my-addon/versions/main/'; + const actual = replaceAddonDocsRootURL( + chunk, + addonDocsRootURL, + encodedVersion, + ); + const expected = + '(e.children=[]),e),o.p="/my-addon/versions/main/assets/",(()=>{var e={143:0}'; + assert.equal(actual, expected); + }); });