add and use constants; DRY glob patterns in main
This commit is contained in:
parent
1a0fe0abf6
commit
a878edfb9b
30
src/docs.mts
30
src/docs.mts
|
@ -38,12 +38,19 @@ import prettier from 'prettier';
|
|||
const SOURCE_DOCS_DIR = 'src/docs/';
|
||||
const FINAL_DOCS_DIR = 'docs/';
|
||||
const AUTOGENERATED_TEXT =
|
||||
'# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit the corresponding file in src/docs.';
|
||||
'# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit the corresponding file in ${SOURCE_DOCS_DIR}.';
|
||||
|
||||
const LOGMSG_TRANSFORMED = 'transformed';
|
||||
const LOGMSG_TO_BE_TRANSFORMED = 'to be transformed';
|
||||
const LOGMSG_COPIED = ' ...and copied to ${FINAL_DOCS_DIR}';
|
||||
|
||||
const WARN_DOCSDIR_DOESNT_MATCH =
|
||||
"Changed files were transformed in `${SOURCE_DOCS_DIR}` but do not match the files in `${FINAL_DOCS_DIR}`. Please run `yarn docs:build` after making changes to '${SOURCE_DOCS_DIR}' to update the `${FINAL_DOCS_DIR}` directory with the transformed files.";
|
||||
|
||||
const verifyOnly = process.argv.includes('--verify');
|
||||
const git = process.argv.includes('--git');
|
||||
|
||||
let filesWereChanged = false;
|
||||
let filesWereTransformed = false;
|
||||
|
||||
/**
|
||||
* Given a source file name and path, return the documentation destination full path and file name
|
||||
|
@ -162,28 +169,33 @@ const transformHtml = (filename: string) => {
|
|||
|
||||
/** Main method (entry point) */
|
||||
(async () => {
|
||||
const sourceDirGlob = join(__dirname, SOURCE_DOCS_DIR, '**');
|
||||
const includeFilesStartingWithDot = true;
|
||||
|
||||
console.log('Transforming markdown files...');
|
||||
const mdFiles = await globby(['./src/docs/**/*.md'], { dot: true });
|
||||
const mdFiles = await globby([join(sourceDirGlob, '*.md')], { dot: includeFilesStartingWithDot });
|
||||
mdFiles.forEach(transformMarkdown);
|
||||
|
||||
console.log('Transforming html files...');
|
||||
const htmlFiles = await globby(['./src/docs/**/*.html'], { dot: true });
|
||||
const htmlFiles = await globby([join(sourceDirGlob, '*.html')], {
|
||||
dot: includeFilesStartingWithDot,
|
||||
});
|
||||
htmlFiles.forEach(transformHtml);
|
||||
|
||||
console.log('Transforming all other files...');
|
||||
const otherFiles = await globby(['src/docs/**', '!**/*.md', '!**/*.html'], { dot: true });
|
||||
const otherFiles = await globby([sourceDirGlob, '!**/*.md', '!**/*.html'], {
|
||||
dot: includeFilesStartingWithDot,
|
||||
});
|
||||
otherFiles.forEach((file) => {
|
||||
verifyAndCopy(file);
|
||||
});
|
||||
if (filesWereChanged) {
|
||||
if (verifyOnly) {
|
||||
console.log(
|
||||
"Changes detected in files in `src/docs`. Please run `yarn docs:build` after making changes to 'src/docs' to update the `docs` folder."
|
||||
);
|
||||
console.log(WARN_DOCSDIR_DOESNT_MATCH);
|
||||
process.exit(1);
|
||||
}
|
||||
if (git) {
|
||||
console.log('Adding changes in docs folder to git');
|
||||
console.log('Adding changes in ${FINAL_DOCS_DIR} folder to git');
|
||||
exec('git add docs');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue