Skip to content
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

Recursively replace ADDON_DOCS_ROOT_URL #1615

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions lib/deploy/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };
}

Expand Down
53 changes: 53 additions & 0 deletions lib/utils/find-and-replace-in-directory.js
Original file line number Diff line number Diff line change
@@ -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,
};
52 changes: 38 additions & 14 deletions tests-node/unit/deploy/plugin-test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
'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 = `
<!DOCTYPE html>
<html>
<head>
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2FADDON_DOCS_ROOT_URL%2F%22%7D" />
</head>
<body>
<script src="ADDON_DOCS_ROOT_URL/assets/chunk.805.d0514e7e9edb751c6623.js"></script>
<script src="ADDON_DOCS_ROOT_URL/assets/chunk.524.385868f2db0b958b5ced.js"></script>
<script src="/ADDON_DOCS_ROOT_URL/assets/vendor.js"></script>
<script src="/ADDON_DOCS_ROOT_URL/assets/dummy.js"></script>
</body>
</html>
`;
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,
Expand All @@ -40,15 +40,39 @@ describe('`deploy` | plugin test', function () {
<!DOCTYPE html>
<html>
<head>
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2Fmy-addon%2Fversions%2Fmaster%2F%22%7D" />
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2Fmy-addon%2Fversions%2Fmain%2F%22%7D" />
</head>
<body>
<script src="/my-addon/versions/master/assets/vendor.js"></script>
<script src="/my-addon/versions/master/assets/dummy.js"></script>
<script src="/my-addon/versions/main/assets/chunk.805.d0514e7e9edb751c6623.js"></script>
<script src="/my-addon/versions/main/assets/chunk.524.385868f2db0b958b5ced.js"></script>
<script src="/my-addon/versions/main/assets/vendor.js"></script>
<script src="/my-addon/versions/main/assets/dummy.js"></script>
</body>
</html>
`;

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);
});
});
Loading