Skip to content

Commit 0634ba4

Browse files
authored
Merge pull request #14102 from Automattic/IslandRhythms/cleanup
Island rhythms/cleanup
2 parents 280bd4a + 140a118 commit 0634ba4

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
"docs:merge:6x": "git merge 6.x",
9494
"docs:test": "npm run docs:generate && npm run docs:generate:search",
9595
"docs:view": "node ./scripts/static.js",
96-
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
96+
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:generate && npm run docs:generate:search",
9797
"docs:prepare:publish:5x": "npm run docs:checkout:5x && npm run docs:merge:5x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:5x",
9898
"docs:prepare:publish:6x": "npm run docs:checkout:6x && npm run docs:merge:6x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && npm run docs:move:6x:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:6x",
99-
"docs:prepare:publish:7x": "git checkout 7.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/7.x ./tmp && npm run docs:checkout:gh-pages && rimraf ./docs/7.x && ncp ./tmp ./docs/7.x",
99+
"docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && npm run docs:checkout:gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
100100
"docs:check-links": "blc http://127.0.0.1:8089 -ro",
101101
"lint": "eslint .",
102102
"lint-js": "eslint . --ext .js --ext .cjs",

scripts/website.js

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Error.stackTraceLimit = Infinity;
44

55
const acquit = require('acquit');
66
const fs = require('fs');
7+
const fsextra = require('fs-extra');
78
const path = require('path');
89
const pug = require('pug');
910
const pkg = require('../package.json');
@@ -76,6 +77,60 @@ const tests = [
7677
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())
7778
];
7879

80+
function deleteAllHtmlFiles() {
81+
try {
82+
console.log('Delete', path.join(versionObj.versionedPath, 'index.html'));
83+
fs.unlinkSync(path.join(versionObj.versionedPath, 'index.html'));
84+
} catch (err) {
85+
if (err.code !== 'ENOENT') {
86+
throw err;
87+
}
88+
}
89+
const foldersToClean = [
90+
path.join('.', versionObj.versionedPath, 'docs'),
91+
path.join('.', versionObj.versionedPath, 'docs', 'tutorials'),
92+
path.join('.', versionObj.versionedPath, 'docs', 'typescript'),
93+
path.join('.', versionObj.versionedPath, 'docs', 'api'),
94+
path.join('.', versionObj.versionedPath, 'docs', 'source', '_docs'),
95+
'./tmp'
96+
];
97+
for (const folder of foldersToClean) {
98+
let files = [];
99+
100+
try {
101+
files = fs.readdirSync(folder);
102+
} catch (err) {
103+
if (err.code === 'ENOENT') {
104+
continue;
105+
}
106+
}
107+
for (const file of files) {
108+
if (file.endsWith('.html')) {
109+
console.log('Delete', path.join(folder, file));
110+
fs.unlinkSync(path.join(folder, file));
111+
}
112+
}
113+
}
114+
}
115+
116+
function moveDocsToTemp() {
117+
if (!versionObj.versionedPath) {
118+
throw new Error('Cannot move unversioned deploy to /tmp');
119+
}
120+
try {
121+
fs.rmSync('./tmp', { recursive: true });
122+
} catch (err) {
123+
if (err.code !== 'ENOENT') {
124+
throw err;
125+
}
126+
}
127+
const folder = versionObj.versionedPath.replace(/^\//, '');
128+
const directory = fs.readdirSync(folder);
129+
for (const file of directory) {
130+
fsextra.moveSync(`${folder}/${file}`, `./tmp/${file}`);
131+
}
132+
}
133+
79134
/**
80135
* Array of array of semver numbers, sorted with highest number first
81136
* @example
@@ -215,7 +270,7 @@ const versionObj = (() => {
215270
getLatestVersionOf(5),
216271
]
217272
};
218-
const versionedDeploy = process.env.DOCS_DEPLOY === "true" ? !(base.currentVersion.listed === base.latestVersion.listed) : false;
273+
const versionedDeploy = !!process.env.DOCS_DEPLOY ? !(base.currentVersion.listed === base.latestVersion.listed) : false;
219274

220275
const versionedPath = versionedDeploy ? `/docs/${base.currentVersion.path}` : '';
221276

@@ -488,7 +543,6 @@ async function copyAllRequiredFiles() {
488543
return;
489544
}
490545

491-
const fsextra = require('fs-extra');
492546
await Promise.all(pathsToCopy.map(async v => {
493547
const resultPath = path.resolve(cwd, path.join('.', versionObj.versionedPath, v));
494548
await fsextra.copy(v, resultPath);
@@ -505,8 +559,16 @@ exports.cwd = cwd;
505559

506560
// only run the following code if this file is the main module / entry file
507561
if (isMain) {
508-
console.log(`Processing ~${files.length} files`);
509-
Promise.all([pugifyAllFiles(), copyAllRequiredFiles()]).then(() => {
510-
console.log("Done Processing");
511-
})
562+
(async function main() {
563+
console.log(`Processing ~${files.length} files`);
564+
565+
await deleteAllHtmlFiles();
566+
await pugifyAllFiles();
567+
await copyAllRequiredFiles();
568+
if (!!process.env.DOCS_DEPLOY && !!versionObj.versionedPath) {
569+
await moveDocsToTemp();
570+
}
571+
572+
console.log('Done Processing');
573+
})();
512574
}

0 commit comments

Comments
 (0)