Skip to content

Commit 8cdb655

Browse files
Recursively replace ADDON_DOCS_ROOT_URL (#1615)
* Recursively replace ADDON_DOCS_ROOT_URL * Update test * Update plugin-test.js
1 parent 397e23e commit 8cdb655

File tree

3 files changed

+101
-39
lines changed

3 files changed

+101
-39
lines changed

lib/deploy/plugin.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const execa = require('execa');
66
const quickTemp = require('quick-temp');
77
const hostedGitInfo = require('hosted-git-info');
88
const maybeMigrateSiteFormat = require('./migration');
9+
const findAndReplaceInDirectory = require('../utils/find-and-replace-in-directory');
910
const { LATEST_VERSION_NAME } = require('../..');
1011

1112
module.exports = class AddonDocsDeployPlugin {
@@ -201,38 +202,22 @@ module.exports = class AddonDocsDeployPlugin {
201202
}
202203

203204
_updateIndexContents(context, stagingDirectory, appRoot, deployVersion) {
204-
let indexPath = `${stagingDirectory}/${appRoot}/index.html`;
205-
let rootURL = [this._getRootURL(), appRoot]
205+
const directory = `${stagingDirectory}/${appRoot}`;
206+
const rootURL = [this._getRootURL(), appRoot]
206207
.filter(Boolean)
207208
.join('/')
208209
.replace(/\\/g, '/');
209-
let addonDocsRootURL = rootURL === '' ? '/' : `/${rootURL}/`;
210-
let contents = fs.readFileSync(indexPath, 'utf-8');
211-
let encodedVersion = encodeURIComponent(JSON.stringify(deployVersion));
212-
let updated = this._macroReplaceIndexContent(
213-
contents,
214-
addonDocsRootURL,
215-
encodedVersion,
216-
);
217-
218-
fs.writeFileSync(indexPath, updated);
219-
}
210+
const addonDocsRootURL = rootURL === '' ? '/' : `/${rootURL}/`;
211+
const encodedVersion = encodeURIComponent(JSON.stringify(deployVersion));
220212

221-
_macroReplaceIndexContent(contents, addonDocsRootURL, encodedVersion) {
222-
return contents
223-
.replace(
224-
'%2FADDON_DOCS_ROOT_URL%2F',
225-
encodeURIComponent(addonDocsRootURL),
226-
)
227-
.replace(/\/?ADDON_DOCS_ROOT_URL\/?/g, addonDocsRootURL)
228-
.replace(/%22ADDON_DOCS_DEPLOY_VERSION%22/g, encodedVersion);
213+
findAndReplaceInDirectory(directory, addonDocsRootURL, encodedVersion);
229214
}
230215

231216
_currentDeployVersion() {
232-
let curpath = path.join('versions', this._getVersionPath());
233-
let name = this.userConfig.getVersionName();
234-
let sha = this.userConfig.repoInfo.sha;
235-
let tag = this.userConfig.repoInfo.tag;
217+
const curpath = path.join('versions', this._getVersionPath());
218+
const name = this.userConfig.getVersionName();
219+
const sha = this.userConfig.repoInfo.sha;
220+
const tag = this.userConfig.repoInfo.tag;
236221
return { path: curpath, name, sha, tag, key: name };
237222
}
238223

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-disable no-console */
2+
3+
'use strict';
4+
5+
const fs = require('fs-extra');
6+
const path = require('path');
7+
8+
function replaceAddonDocsRootURL(contents, addonDocsRootURL, encodedVersion) {
9+
return contents
10+
.replace('%2FADDON_DOCS_ROOT_URL%2F', encodeURIComponent(addonDocsRootURL))
11+
.replace(/\/?ADDON_DOCS_ROOT_URL\/?/g, addonDocsRootURL)
12+
.replace(/%22ADDON_DOCS_DEPLOY_VERSION%22/g, encodedVersion);
13+
}
14+
15+
function processFile(filePath, addonDocsRootURL, encodedVersion) {
16+
const contents = fs.readFileSync(filePath, 'utf-8');
17+
18+
// Write the updated content to the file
19+
fs.writeFileSync(
20+
filePath,
21+
replaceAddonDocsRootURL(contents, addonDocsRootURL, encodedVersion),
22+
);
23+
}
24+
25+
function findAndReplaceInDirectory(
26+
directory,
27+
addonDocsRootURL,
28+
encodedVersion,
29+
) {
30+
fs.readdir(directory, { withFileTypes: true }, (err, entries) => {
31+
if (err) {
32+
console.error(`Error reading directory ${directory}:`, err);
33+
return;
34+
}
35+
36+
entries.forEach((entry) => {
37+
const fullPath = path.join(directory, entry.name);
38+
39+
if (entry.isDirectory()) {
40+
// Recursively process subdirectories
41+
findAndReplaceInDirectory(fullPath, addonDocsRootURL, encodedVersion);
42+
} else if (entry.isFile()) {
43+
// Process files
44+
processFile(fullPath, addonDocsRootURL, encodedVersion);
45+
}
46+
});
47+
});
48+
}
49+
50+
module.exports = {
51+
findAndReplaceInDirectory,
52+
replaceAddonDocsRootURL,
53+
};

tests-node/unit/deploy/plugin-test.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
'use strict';
22

33
const assert = require('chai').assert;
4-
const AddonDocsDeployPlugin = require('../../../lib/deploy/plugin');
4+
const {
5+
replaceAddonDocsRootURL,
6+
} = require('../../../lib/utils/find-and-replace-in-directory');
57

68
describe('`deploy` | plugin test', function () {
7-
beforeEach(function () {
8-
this.pluginInstance = new AddonDocsDeployPlugin();
9-
});
10-
11-
it('_macroReplaceIndexContent', function () {
9+
it('replaceAddonDocsRootURL in index.html', function () {
1210
const contents = `
1311
<!DOCTYPE html>
1412
<html>
1513
<head>
1614
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2FADDON_DOCS_ROOT_URL%2F%22%7D" />
1715
</head>
1816
<body>
17+
<script src="ADDON_DOCS_ROOT_URL/assets/chunk.805.d0514e7e9edb751c6623.js"></script>
18+
<script src="ADDON_DOCS_ROOT_URL/assets/chunk.524.385868f2db0b958b5ced.js"></script>
1919
<script src="/ADDON_DOCS_ROOT_URL/assets/vendor.js"></script>
2020
<script src="/ADDON_DOCS_ROOT_URL/assets/dummy.js"></script>
2121
</body>
2222
</html>
2323
`;
2424
const encodedVersion = encodeURIComponent(
2525
JSON.stringify({
26-
path: 'versions/master',
27-
name: 'master',
26+
path: 'versions/main',
27+
name: 'main',
2828
sha: 'eef3',
2929
tag: null,
30-
key: 'master',
30+
key: 'main',
3131
}),
3232
);
33-
const addonDocsRootURL = '/my-addon/versions/master/';
34-
const actual = this.pluginInstance._macroReplaceIndexContent(
33+
const addonDocsRootURL = '/my-addon/versions/main/';
34+
const actual = replaceAddonDocsRootURL(
3535
contents,
3636
addonDocsRootURL,
3737
encodedVersion,
@@ -40,15 +40,39 @@ describe('`deploy` | plugin test', function () {
4040
<!DOCTYPE html>
4141
<html>
4242
<head>
43-
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2Fmy-addon%2Fversions%2Fmaster%2F%22%7D" />
43+
<meta name="dummy/config/environment" content="%7B%22rootURL%22%3A%22%2Fmy-addon%2Fversions%2Fmain%2F%22%7D" />
4444
</head>
4545
<body>
46-
<script src="/my-addon/versions/master/assets/vendor.js"></script>
47-
<script src="/my-addon/versions/master/assets/dummy.js"></script>
46+
<script src="/my-addon/versions/main/assets/chunk.805.d0514e7e9edb751c6623.js"></script>
47+
<script src="/my-addon/versions/main/assets/chunk.524.385868f2db0b958b5ced.js"></script>
48+
<script src="/my-addon/versions/main/assets/vendor.js"></script>
49+
<script src="/my-addon/versions/main/assets/dummy.js"></script>
4850
</body>
4951
</html>
5052
`;
5153

5254
assert.equal(actual, expected);
5355
});
56+
it('replaceAddonDocsRootURL in chunks', function () {
57+
const chunk =
58+
'(e.children=[]),e),o.p="ADDON_DOCS_ROOT_URL/assets/",(()=>{var e={143:0}';
59+
const encodedVersion = encodeURIComponent(
60+
JSON.stringify({
61+
path: 'versions/main',
62+
name: 'main',
63+
sha: 'eef3',
64+
tag: null,
65+
key: 'main',
66+
}),
67+
);
68+
const addonDocsRootURL = '/my-addon/versions/main/';
69+
const actual = replaceAddonDocsRootURL(
70+
chunk,
71+
addonDocsRootURL,
72+
encodedVersion,
73+
);
74+
const expected =
75+
'(e.children=[]),e),o.p="/my-addon/versions/main/assets/",(()=>{var e={143:0}';
76+
assert.equal(actual, expected);
77+
});
5478
});

0 commit comments

Comments
 (0)