Add verification for doc change

This commit is contained in:
Sidharth Vinod
2022-09-03 10:06:21 +05:30
parent 829e1c2390
commit 3833dcd0d8
3 changed files with 21 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
{ {
"src/docs/**": [ "src/docs/**": [
"yarn build:docs" "yarn docs:build"
], ],
"*.{js,json,html,md}": [ "*.{js,json,html,md}": [
"yarn lint:fix" "yarn lint:fix"

View File

@@ -26,8 +26,9 @@
"build:dev": "webpack --mode development --progress --color", "build:dev": "webpack --mode development --progress --color",
"build:prod": "webpack --mode production --progress --color", "build:prod": "webpack --mode production --progress --color",
"build": "concurrently \"yarn build:dev\" \"yarn build:prod\"", "build": "concurrently \"yarn build:dev\" \"yarn build:prod\"",
"build:docs": "ts-node-esm src/docs.mts", "docs:build": "ts-node-esm src/docs.mts",
"postbuild": "documentation build src/mermaidAPI.ts src/config.ts src/defaultConfig.ts --shallow -f md --markdown-toc false > src/docs/Setup.md; yarn build:docs", "docs:verify": "ts-node-esm src/docs.mts --verify",
"postbuild": "documentation build src/mermaidAPI.ts src/config.ts src/defaultConfig.ts --shallow -f md --markdown-toc false > src/docs/Setup.md; yarn docs:build",
"build:watch": "yarn build:dev --watch", "build:watch": "yarn build:dev --watch",
"release": "yarn build", "release": "yarn build",
"lint": "eslint --cache ./ --ext .js,.json,.html,.md", "lint": "eslint --cache ./ --ext .js,.json,.html,.md",

View File

@@ -7,6 +7,7 @@ import { globby } from 'globby';
import { join, dirname } from 'path'; import { join, dirname } from 'path';
import { exec } from 'child_process'; import { exec } from 'child_process';
const verify = process.argv.includes('--verify');
let fileChanged = false; let fileChanged = false;
// Possible Improvement: combine with lint-staged to only copy files that have changed // Possible Improvement: combine with lint-staged to only copy files that have changed
const prepareOutFile = (file: string): string => { const prepareOutFile = (file: string): string => {
@@ -17,20 +18,16 @@ const prepareOutFile = (file: string): string => {
const verifyAndCopy = (file: string, content?: string) => { const verifyAndCopy = (file: string, content?: string) => {
const outFile = prepareOutFile(file); const outFile = prepareOutFile(file);
const existing = existsSync(outFile) ? readFileSync(outFile) : Buffer.from('#NEW FILE#'); const existingBuffer = existsSync(outFile) ? readFileSync(outFile) : Buffer.from('#NEW FILE#');
if (content !== undefined) { const newBuffer = content ? Buffer.from(content) : readFileSync(file);
if (!existing.equals(Buffer.from(content))) { if (existingBuffer.equals(newBuffer)) {
console.log(`Updating ${outFile}`); // Files are same, skip.
writeFileSync(outFile, content); return;
fileChanged = true; }
} console.log(`File changed: ${outFile}`);
} else { fileChanged = true;
const newFile = readFileSync(file); if (!verify) {
if (!existing.equals(newFile)) { writeFileSync(outFile, newBuffer);
console.log(`Copying ${file} to ${outFile}`);
writeFileSync(outFile, newFile);
fileChanged = true;
}
} }
}; };
@@ -61,6 +58,12 @@ const transform = (file: string) => {
verifyAndCopy(file); verifyAndCopy(file);
}); });
if (fileChanged) { if (fileChanged) {
if (verify) {
console.log(
"Changes detected in files in `docs`. Please run `yarn docs:build` after making changes to 'src/docs' to update the `docs` folder."
);
process.exit(1);
}
console.log('Committing changes to the docs folder'); console.log('Committing changes to the docs folder');
exec('git add docs'); exec('git add docs');
} }