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

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