refactor: use `posix.join()` instead of replacing `\`

This commit is contained in:
lemontreejs 2022-10-09 21:03:57 +05:30
parent 622b441eb0
commit 2bb0cf17d1
1 changed files with 8 additions and 30 deletions

View File

@ -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<string[]> => {
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<string[]> => {
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);