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..78daa633d --- /dev/null +++ b/lib/utils/find-and-replace-in-directory.js @@ -0,0 +1,53 @@ +/* eslint-disable no-console */ + +'use strict'; + +const fs = require('fs-extra'); +const path = require('path'); + +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, + replaceAddonDocsRootURL(contents, addonDocsRootURL, encodedVersion), + ); +} + +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); + } + }); + }); +} + +module.exports = { + findAndReplaceInDirectory, + replaceAddonDocsRootURL, +}; diff --git a/tests-node/unit/deploy/plugin-test.js b/tests-node/unit/deploy/plugin-test.js index 18387f6c2..b0a6ee35c 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('_macroReplaceIndexContent', function () { + it('replaceAddonDocsRootURL in index.html', function () { const contents = ` @@ -16,6 +14,8 @@ describe('`deploy` | plugin test', function () {
+ + @@ -23,15 +23,15 @@ 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 actual = this.pluginInstance._macroReplaceIndexContent( + const addonDocsRootURL = '/my-addon/versions/main/'; + const actual = replaceAddonDocsRootURL( contents, addonDocsRootURL, encodedVersion, @@ -40,15 +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); + }); });