Skip to content

Commit 020a8f3

Browse files
committed
chore: move cleanup logic from npm scripts to website.js script
1 parent 64ef322 commit 020a8f3

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"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",
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 && ncp ./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: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ 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');
11+
const rimraf = require('rimraf');
1012
const transform = require('acquit-require');
1113
const childProcess = require("child_process");
1214

@@ -76,35 +78,45 @@ const tests = [
7678
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())
7779
];
7880

79-
function refreshDocs() {
80-
deleteAllHtmlFiles();
81-
if (process.env.DOCS_DEPLOY) {
82-
moveDocsToTemp();
83-
}
84-
}
85-
8681
function deleteAllHtmlFiles() {
87-
fs.unlinkSync('../index.html');
88-
const locations = ['../docs','../docs/tutorials', '../docs/typescript']
89-
for (let i = 0; i < locations.length; i++) {
90-
const files = fs.readdirSync(locations[i]);
82+
try {
83+
fs.unlinkSync('./index.html');
84+
} catch (err) {
85+
if (err.code !== 'ENOENT') {
86+
throw err;
87+
}
88+
}
89+
const foldersToClean = [
90+
'./docs',
91+
'./docs/tutorials',
92+
'./docs/typescript',
93+
'./docs/api',
94+
'./docs/source/_docs',
95+
'./tmp'
96+
];
97+
for (const folder of foldersToClean) {
98+
let files = [];
99+
try {
100+
files = fs.readdirSync(folder);
101+
} catch (err) {
102+
if (err.code === 'ENOENT') {
103+
continue;
104+
}
105+
}
91106
for (let index = 0; index < files.length; index++) {
92107
if (files[index].endsWith('.html')) {
93-
fs.unlinkSync(files[index]);
108+
fs.unlinkSync(`${folder}/${files[index]}`);
94109
}
95110
}
96111
}
97-
const folders = ['../docs/api', '../docs/source/_docs', '../tmp'];
98-
for (let i = 0; i < folders.length; i++) {
99-
fs.rmdirSync(folders[i])
100-
}
101112
}
102113

103114
function moveDocsToTemp() {
104-
const folder = '../docs/7.x';
115+
rimraf.sync('./tmp');
116+
const folder = `./docs/7.x`;
105117
const directory = fs.readdirSync(folder);
106118
for (let i = 0; i < directory.length; i++) {
107-
fs.renameSync(`${folder}/${directory[i]}`, `./tmp/${directory[i]}`);
119+
fsextra.moveSync(`${folder}/${directory[i]}`, `./tmp/${directory[i]}`);
108120
}
109121
}
110122

@@ -520,7 +532,6 @@ async function copyAllRequiredFiles() {
520532
return;
521533
}
522534

523-
const fsextra = require('fs-extra');
524535
await Promise.all(pathsToCopy.map(async v => {
525536
const resultPath = path.resolve(cwd, path.join('.', versionObj.versionedPath, v));
526537
await fsextra.copy(v, resultPath);
@@ -537,8 +548,16 @@ exports.cwd = cwd;
537548

538549
// only run the following code if this file is the main module / entry file
539550
if (isMain) {
540-
console.log(`Processing ~${files.length} files`);
541-
Promise.all([pugifyAllFiles(), copyAllRequiredFiles()]).then(() => {
542-
console.log("Done Processing");
543-
})
551+
(async function main() {
552+
console.log(`Processing ~${files.length} files`);
553+
554+
await deleteAllHtmlFiles();
555+
await pugifyAllFiles();
556+
await copyAllRequiredFiles();
557+
if (process.env.DOCS_DEPLOY) {
558+
await moveDocsToTemp();
559+
}
560+
561+
console.log('Done Processing');
562+
})();
544563
}

0 commit comments

Comments
 (0)