Fail commit if docs changed
This commit is contained in:
parent
0aabae40ad
commit
f45c0e3617
31
src/docs.mts
31
src/docs.mts
|
@ -1,11 +1,12 @@
|
||||||
import { remark } from 'remark';
|
import { remark } from 'remark';
|
||||||
import type { Code, Root } from 'mdast';
|
import type { Code, Root } from 'mdast';
|
||||||
import { readFileSync, writeFileSync, mkdirSync, copyFileSync } from 'fs';
|
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, existsSync } from 'fs';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import flatmap from 'unist-util-flatmap';
|
import flatmap from 'unist-util-flatmap';
|
||||||
import { globby } from 'globby';
|
import { globby } from 'globby';
|
||||||
import { join, dirname } from 'path';
|
import { join, dirname } from 'path';
|
||||||
|
|
||||||
|
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 => {
|
||||||
const outFile = join('docs', file.replace('src/docs/', ''));
|
const outFile = join('docs', file.replace('src/docs/', ''));
|
||||||
|
@ -13,6 +14,25 @@ const prepareOutFile = (file: string): string => {
|
||||||
return outFile;
|
return outFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 transform = (file: string) => {
|
const transform = (file: string) => {
|
||||||
const doc = readFileSync(file, 'utf8');
|
const doc = readFileSync(file, 'utf8');
|
||||||
const ast: Root = remark.parse(doc);
|
const ast: Root = remark.parse(doc);
|
||||||
|
@ -29,8 +49,7 @@ const transform = (file: string) => {
|
||||||
const transformed = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit corresponding file in src/docs.\n${remark.stringify(
|
const transformed = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit corresponding file in src/docs.\n${remark.stringify(
|
||||||
out
|
out
|
||||||
)}`;
|
)}`;
|
||||||
const outFile = prepareOutFile(file);
|
verifyAndCopy(file, transformed);
|
||||||
writeFileSync(outFile, transformed);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
@ -38,6 +57,10 @@ const transform = (file: string) => {
|
||||||
mdFiles.forEach(transform);
|
mdFiles.forEach(transform);
|
||||||
const nonMDFiles = await globby(['src/docs/**', '!**/*.md']);
|
const nonMDFiles = await globby(['src/docs/**', '!**/*.md']);
|
||||||
nonMDFiles.forEach((file) => {
|
nonMDFiles.forEach((file) => {
|
||||||
copyFileSync(file, prepareOutFile(file));
|
verifyAndCopy(file);
|
||||||
});
|
});
|
||||||
|
if (fileChanged) {
|
||||||
|
console.log('Please commit the changes to the docs folder');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue