diff --git a/src/docs.mts b/src/docs.mts index d211e0fbf..ad9ab1bf0 100644 --- a/src/docs.mts +++ b/src/docs.mts @@ -14,30 +14,29 @@ * */ -import { remark } from "remark"; -import type { Code, Root } from "mdast"; -import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs"; -import { JSDOM } from "jsdom"; +import { remark } from 'remark'; +import type { Code, Root } from 'mdast'; +import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'; +import { JSDOM } from 'jsdom'; // @ts-ignore -import flatmap from "unist-util-flatmap"; -import { globby } from "globby"; -import { join, dirname } from "path"; -import { exec } from "child_process"; +import flatmap from 'unist-util-flatmap'; +import { globby } from 'globby'; +import { join, dirname } from 'path'; +import { exec } from 'child_process'; // @ts-ignore -import prettier from "prettier"; +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 src/docs.'; -const verifyOnly = process.argv.includes("--verify"); -const git = process.argv.includes("--git"); +const verifyOnly = process.argv.includes('--verify'); +const git = process.argv.includes('--git'); let filesWereChanged = false; - /** * Given a source file name and path, return the documentation destination full path and file name * Create the destination path if it does not already exist. @@ -47,7 +46,7 @@ let filesWereChanged = false; * @returns {string} name of the file with the path changed from src/docs to docs */ const prepareOutFile = (file: string): string => { - const outFile = join(FINAL_DOCS_DIR, file.replace(SOURCE_DOCS_DIR, "")); + const outFile = join(FINAL_DOCS_DIR, file.replace(SOURCE_DOCS_DIR, '')); mkdirSync(dirname(outFile), { recursive: true }); return outFile; }; @@ -61,7 +60,7 @@ const prepareOutFile = (file: string): string => { */ const verifyAndCopy = (file: string, content?: string) => { const outFile = prepareOutFile(file); - const existingBuffer = existsSync(outFile) ? readFileSync(outFile) : Buffer.from("#NEW FILE#"); + 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. @@ -71,19 +70,19 @@ const verifyAndCopy = (file: string, content?: string) => { if (verifyOnly) { changeMsg = 'to be changed'; } - let logMsg = ` File ${changeMsg}: ${outFile}` + let logMsg = ` File ${changeMsg}: ${outFile}`; filesWereChanged = true; if (!verifyOnly) { writeFileSync(outFile, newBuffer); - logMsg += ' ...and copied to /docs' + logMsg += ' ...and copied to /docs'; } console.log(logMsg); }; const readSyncedUTF8file = (file: string): string => { - return readFileSync(file, "utf8"); -} + return readFileSync(file, 'utf8'); +}; /** * Transform a markdown file and write the transformed file to the directory for published documentation @@ -99,13 +98,13 @@ const transformMarkdown = (file: string) => { const doc = readSyncedUTF8file(file); const ast: Root = remark.parse(doc); const out = flatmap(ast, (c: Code) => { - if (c.type !== "code" || !c.lang?.startsWith("mermaid")) { + if (c.type !== 'code' || !c.lang?.startsWith('mermaid')) { return [c]; } - if (c.lang === "mermaid" || c.lang === "mmd") { - c.lang = "mermaid-example"; + if (c.lang === 'mermaid' || c.lang === 'mmd') { + c.lang = 'mermaid-example'; } - return [c, Object.assign({}, c, { lang: "mermaid" })]; + return [c, Object.assign({}, c, { lang: 'mermaid' })]; }); // Add the AUTOGENERATED_TEXT to the start of the file @@ -114,12 +113,12 @@ const transformMarkdown = (file: string) => { verifyAndCopy( file, prettier.format(transformed, { - parser: "markdown", + parser: 'markdown', useTabs: false, tabWidth: 2, - endOfLine: "auto", + endOfLine: 'auto', printWidth: 100, - singleQuote: true + singleQuote: true, }) ); }; @@ -132,39 +131,38 @@ const transformMarkdown = (file: string) => { * @param filename {string} name of the HTML file to transform */ const transformHtml = (filename: string) => { - /** * Insert the '...auto generated...' comment into an HTML file after the element * - * @param fname {string} file name that should have the comment inserted + * @param fileName {string} file name that should have the comment inserted * @returns {string} the contents of the file with the comment inserted */ - function insertAutoGeneratedComment(fname: string): string { - const fileContents = readSyncedUTF8file(fname); + const insertAutoGeneratedComment = (fileName: string): string => { + const fileContents = readSyncedUTF8file(fileName); const jsdom = new JSDOM(fileContents); const htmlDoc = jsdom.window.document; const autoGeneratedComment = jsdom.window.document.createComment(AUTOGENERATED_TEXT); - let rootElement = htmlDoc.documentElement; + const rootElement = htmlDoc.documentElement; rootElement.prepend(autoGeneratedComment); return jsdom.serialize(); } - let transformedHTML = insertAutoGeneratedComment(filename); + const transformedHTML = insertAutoGeneratedComment(filename); verifyAndCopy(filename, transformedHTML); }; (async () => { - console.log("Transforming markdown files..."); - const mdFiles = await globby(["./src/docs/**/*.md"], { dot: true }); + console.log('Transforming markdown files...'); + const mdFiles = await globby(['./src/docs/**/*.md'], { dot: true }); mdFiles.forEach(transformMarkdown); - console.log("Transforming html files..."); - const htmlFiles = await globby(["./src/docs/**/*.html"], { dot: true }); + console.log('Transforming html files...'); + const htmlFiles = await globby(['./src/docs/**/*.html'], { dot: true }); htmlFiles.forEach(transformHtml); - console.log("Transforming all other files..."); - const otherFiles = await globby(["src/docs/**", "!**/*.md", "!**/*.html"], { dot: true }); + console.log('Transforming all other files...'); + const otherFiles = await globby(['src/docs/**', '!**/*.md', '!**/*.html'], { dot: true }); otherFiles.forEach((file) => { verifyAndCopy(file); }); @@ -176,8 +174,8 @@ const transformHtml = (filename: string) => { process.exit(1); } if (git) { - console.log("Adding changes in docs folder to git"); - exec("git add docs"); + console.log('Adding changes in docs folder to git'); + exec('git add docs'); } } })();