From 0cf34a1dd1a8bb5e4ecf1bf5b6e8c573b75ead76 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Fri, 18 Mar 2022 13:32:03 +0100 Subject: [PATCH] fix(cli): Path normalization --- src/cli/commands/generate-component.ts | 6 +- src/cli/commands/migrate.ts | 3 +- src/cli/utils/lookup-closest-module.ts | 84 +++++++++++++------------- src/cli/utils/slash.ts | 10 +-- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/cli/commands/generate-component.ts b/src/cli/commands/generate-component.ts index d69c9e08..d78d28c8 100644 --- a/src/cli/commands/generate-component.ts +++ b/src/cli/commands/generate-component.ts @@ -148,7 +148,8 @@ export function updateModuleImports(fullDestinationPath: string): void { logger.log(`Updating module ${moduleFileName}`); const updateModuleImportsContent = (_fullDestinationPath: string) => { - const components = readdirSync(normalize(_fullDestinationPath), { withFileTypes: true }); + _fullDestinationPath = normalize(_fullDestinationPath); + const components = readdirSync(_fullDestinationPath, { withFileTypes: true }); const files = components.filter((component) => component.isFile()); for (const file of files) { if (file.name.includes('.module.')) continue; @@ -165,7 +166,8 @@ export function updateModuleImports(fullDestinationPath: string): void { if (!shouldUpdateModuleImport) continue; const isComponentInSubDirectory = - _fullDestinationPath.split(slashRegexp).slice(-1).pop() !== resolvedModulePath.split(slashRegexp).slice(-2).shift(); + _fullDestinationPath.split(slashRegexp).slice(-1).pop() !== + resolvedModulePath.split(slashRegexp).slice(-2).shift(); const updatedModuleContent = moduleContent .replace(/imports: \[(.*)\]/, (str: string, match: string) => { diff --git a/src/cli/commands/migrate.ts b/src/cli/commands/migrate.ts index 5058e479..ed6122a7 100644 --- a/src/cli/commands/migrate.ts +++ b/src/cli/commands/migrate.ts @@ -1,5 +1,6 @@ import { createConnection } from 'typeorm'; import { getConfigFile } from 'medusa-core-utils/dist'; +import { normalize, resolve } from 'path'; /** * Run the migrations using the medusa-config.js config. @@ -15,7 +16,7 @@ export async function migrate({ run, show }): Promise { 'dist/**/*.migration.js', 'dist/**/migrations/*.js', ].map((dir) => { - return process.cwd() + '/' + dir; + return normalize(resolve(process.cwd(), dir)); }); const connection = await createConnection({ diff --git a/src/cli/utils/lookup-closest-module.ts b/src/cli/utils/lookup-closest-module.ts index 2b731370..d1f21a56 100644 --- a/src/cli/utils/lookup-closest-module.ts +++ b/src/cli/utils/lookup-closest-module.ts @@ -10,51 +10,51 @@ const slashRegexp = getSlashRegexpFromPlatform(); * @param isMain */ export function lookupClosestModule(fullDestinationPath: string, isMain = true): string | undefined { - let resolvedModulePath = undefined; + let resolvedModulePath = undefined; - const isRootDir = !!readdirSync(fullDestinationPath, { withFileTypes: true }).some( - (component) => component.name === 'package.json' - ); - if (isRootDir) { - return resolvedModulePath; - } + const isRootDir = !!readdirSync(fullDestinationPath, { withFileTypes: true }).some( + (component) => component.name === 'package.json' + ); + if (isRootDir) { + return resolvedModulePath; + } - const components = readdirSync(fullDestinationPath, { withFileTypes: true }); - const files = components.filter((component) => component.isFile()); - for (const file of files) { - const componentFullDestinationPath = resolve(fullDestinationPath, file.name); - const componentContent = readFileSync(componentFullDestinationPath).toString(); - const containsModuleDecorator = !!componentContent.match(/@Module\(/g); - if (containsModuleDecorator) { - resolvedModulePath = componentFullDestinationPath; - break; - } - } + const components = readdirSync(fullDestinationPath, { withFileTypes: true }); + const files = components.filter((component) => component.isFile()); + for (const file of files) { + const componentFullDestinationPath = resolve(fullDestinationPath, file.name); + const componentContent = readFileSync(componentFullDestinationPath).toString(); + const containsModuleDecorator = !!componentContent.match(/@Module\(/g); + if (containsModuleDecorator) { + resolvedModulePath = componentFullDestinationPath; + break; + } + } - const directories = components.filter((component) => component.isDirectory()); - if (!resolvedModulePath) { - for (const directory of directories) { - const childFullDestinationPath = resolve(fullDestinationPath, directory.name); - resolvedModulePath = lookupClosestModule(childFullDestinationPath, false); - if (resolvedModulePath) { - break; - } - } - } + const directories = components.filter((component) => component.isDirectory()); + if (!resolvedModulePath) { + for (const directory of directories) { + const childFullDestinationPath = resolve(fullDestinationPath, directory.name); + resolvedModulePath = lookupClosestModule(childFullDestinationPath, false); + if (resolvedModulePath) { + break; + } + } + } - /* - * At this point, the module was not found in the current and children directories, - * so we will look into the parent directory until the root is reached. - */ - if (isMain && !resolvedModulePath) { - const parentFullDestinationPath = fullDestinationPath.split(slashRegexp).slice(0, -1).join('/'); - if (parentFullDestinationPath) { - const modulePath = lookupClosestModule(parentFullDestinationPath); - if (modulePath) { - resolvedModulePath = modulePath; - } - } - } + /* + * At this point, the module was not found in the current and children directories, + * so we will look into the parent directory until the root is reached. + */ + if (isMain && !resolvedModulePath) { + const parentFullDestinationPath = fullDestinationPath.split(slashRegexp).slice(0, -1).join('/'); + if (parentFullDestinationPath) { + const modulePath = lookupClosestModule(parentFullDestinationPath); + if (modulePath) { + resolvedModulePath = modulePath; + } + } + } - return resolvedModulePath ? normalize(resolvedModulePath) : resolvedModulePath; + return resolvedModulePath ? normalize(resolvedModulePath) : resolvedModulePath; } diff --git a/src/cli/utils/slash.ts b/src/cli/utils/slash.ts index e9f5273d..bf2cd47b 100644 --- a/src/cli/utils/slash.ts +++ b/src/cli/utils/slash.ts @@ -1,7 +1,7 @@ export function getSlashRegexpFromPlatform(): RegExp { - if (process.platform === 'win32') { - return new RegExp(/\\|\\\\/); - } + if (process.platform === 'win32') { + return new RegExp(/\/|\\|\\\\/); + } - return new RegExp(/\//); -} \ No newline at end of file + return new RegExp(/\//); +}