Skip to content

Commit

Permalink
Merge pull request #14102 from Automattic/IslandRhythms/cleanup
Browse files Browse the repository at this point in the history
Island rhythms/cleanup
  • Loading branch information
vkarpov15 authored Nov 27, 2023
2 parents 280bd4a + 140a118 commit 0634ba4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
"docs:merge:6x": "git merge 6.x",
"docs:test": "npm run docs:generate && npm run docs:generate:search",
"docs:view": "node ./scripts/static.js",
"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",
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:generate && npm run docs:generate:search",
"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",
"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",
"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",
"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",
"docs:check-links": "blc http://127.0.0.1:8089 -ro",
"lint": "eslint .",
"lint-js": "eslint . --ext .js --ext .cjs",
Expand Down
74 changes: 68 additions & 6 deletions scripts/website.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Error.stackTraceLimit = Infinity;

const acquit = require('acquit');
const fs = require('fs');
const fsextra = require('fs-extra');
const path = require('path');
const pug = require('pug');
const pkg = require('../package.json');
Expand Down Expand Up @@ -76,6 +77,60 @@ const tests = [
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())
];

function deleteAllHtmlFiles() {
try {
console.log('Delete', path.join(versionObj.versionedPath, 'index.html'));
fs.unlinkSync(path.join(versionObj.versionedPath, 'index.html'));
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
const foldersToClean = [
path.join('.', versionObj.versionedPath, 'docs'),
path.join('.', versionObj.versionedPath, 'docs', 'tutorials'),
path.join('.', versionObj.versionedPath, 'docs', 'typescript'),
path.join('.', versionObj.versionedPath, 'docs', 'api'),
path.join('.', versionObj.versionedPath, 'docs', 'source', '_docs'),
'./tmp'
];
for (const folder of foldersToClean) {
let files = [];

try {
files = fs.readdirSync(folder);
} catch (err) {
if (err.code === 'ENOENT') {
continue;
}
}
for (const file of files) {
if (file.endsWith('.html')) {
console.log('Delete', path.join(folder, file));
fs.unlinkSync(path.join(folder, file));
}
}
}
}

function moveDocsToTemp() {
if (!versionObj.versionedPath) {
throw new Error('Cannot move unversioned deploy to /tmp');
}
try {
fs.rmSync('./tmp', { recursive: true });
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
const folder = versionObj.versionedPath.replace(/^\//, '');
const directory = fs.readdirSync(folder);
for (const file of directory) {
fsextra.moveSync(`${folder}/${file}`, `./tmp/${file}`);
}
}

/**
* Array of array of semver numbers, sorted with highest number first
* @example
Expand Down Expand Up @@ -215,7 +270,7 @@ const versionObj = (() => {
getLatestVersionOf(5),
]
};
const versionedDeploy = process.env.DOCS_DEPLOY === "true" ? !(base.currentVersion.listed === base.latestVersion.listed) : false;
const versionedDeploy = !!process.env.DOCS_DEPLOY ? !(base.currentVersion.listed === base.latestVersion.listed) : false;

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

Expand Down Expand Up @@ -488,7 +543,6 @@ async function copyAllRequiredFiles() {
return;
}

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

// only run the following code if this file is the main module / entry file
if (isMain) {
console.log(`Processing ~${files.length} files`);
Promise.all([pugifyAllFiles(), copyAllRequiredFiles()]).then(() => {
console.log("Done Processing");
})
(async function main() {
console.log(`Processing ~${files.length} files`);

await deleteAllHtmlFiles();
await pugifyAllFiles();
await copyAllRequiredFiles();
if (!!process.env.DOCS_DEPLOY && !!versionObj.versionedPath) {
await moveDocsToTemp();
}

console.log('Done Processing');
})();
}

0 comments on commit 0634ba4

Please sign in to comment.