From 2bb0cf17d1d5f54c0aa006253e5083b9e5fe1c5a Mon Sep 17 00:00:00 2001 From: lemontreejs Date: Sun, 9 Oct 2022 21:03:57 +0530 Subject: [PATCH] refactor: use `posix.join()` instead of replacing `\` --- packages/mermaid/src/docs.mts | 38 ++++++++--------------------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/packages/mermaid/src/docs.mts b/packages/mermaid/src/docs.mts index 74a8658b5..3dc685a2a 100644 --- a/packages/mermaid/src/docs.mts +++ b/packages/mermaid/src/docs.mts @@ -35,33 +35,18 @@ import { exec } from 'child_process'; import { globby } from 'globby'; import { JSDOM } from 'jsdom'; import type { Code, Root } from 'mdast'; -import { join, dirname } from 'path'; +import { posix, dirname } from 'path'; import prettier from 'prettier'; import { remark } from 'remark'; // @ts-ignore No typescript declaration file import flatmap from 'unist-util-flatmap'; -/** - * Windows uses '\\' as path delimitter. - * The package `globby` requires all paths to contain forward-slashes only. - * Also it is better if the `AUTOGENERATED_TEXT` has same paths (type of slashes) in all platforms. - * But while actually using the paths, they should be used with platform-specific delimiters. - * So replace all '\\' with '/' for `globby` and `AUTOGENERATED_TEXT` only to maintain uniformity. - * @param path - * @returns normalized path - */ -const normalizeUniform = (path: string) => { - return path.replace(/\\/g, '/'); -}; - // These paths are from the root of the mono-repo, not from the // mermaid sub-directory -const SOURCE_DOCS_DIR = join(...'packages/mermaid/src/docs'.split('/')); +const SOURCE_DOCS_DIR = 'packages/mermaid/src/docs'; const FINAL_DOCS_DIR = 'docs'; -const AUTOGENERATED_TEXT = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT. Please edit the corresponding file in ${normalizeUniform( - SOURCE_DOCS_DIR -)}.`; +const AUTOGENERATED_TEXT = `# 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'; @@ -93,9 +78,7 @@ let filesWereTransformed = false; * @todo Possible Improvement: combine with lint-staged to only copy files that have changed */ const changeToFinalDocDir = (file: string): string => { - // `SOURCE_DOCS_DIR` will have `\\` delimiter in Windows, but path returned by `globby` will have `/` - // So use `normalizeUniform` - const newDir = file.replace(normalizeUniform(SOURCE_DOCS_DIR), FINAL_DOCS_DIR); + const newDir = file.replace(SOURCE_DOCS_DIR, FINAL_DOCS_DIR); mkdirSync(dirname(newDir), { recursive: true }); return newDir; }; @@ -221,12 +204,7 @@ const transformHtml = (filename: string) => { }; const getFilesFromGlobs = async (globs: string[]): Promise => { - return await globby( - globs.map((glob) => normalizeUniform(glob)), - { - dot: true, - } - ); + return await globby(globs, { dot: true }); }; /** Main method (entry point) */ @@ -234,14 +212,14 @@ const getFilesFromGlobs = async (globs: string[]): Promise => { if (verifyOnly) { console.log('Verifying that all files are in sync with the source files'); } - const sourceDirGlob = join('.', SOURCE_DOCS_DIR, '**'); + const sourceDirGlob = posix.join('.', SOURCE_DOCS_DIR, '**'); const action = verifyOnly ? 'Verifying' : 'Transforming'; - const mdFiles = await getFilesFromGlobs([join(sourceDirGlob, '*.md')]); + const mdFiles = await getFilesFromGlobs([posix.join(sourceDirGlob, '*.md')]); console.log(`${action} ${mdFiles.length} markdown files...`); mdFiles.forEach(transformMarkdown); - const htmlFiles = await getFilesFromGlobs([join(sourceDirGlob, '*.html')]); + const htmlFiles = await getFilesFromGlobs([posix.join(sourceDirGlob, '*.html')]); console.log(`${action} ${htmlFiles.length} html files...`); htmlFiles.forEach(transformHtml);