Skip to content

Commit

Permalink
fix(cli): Path normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Mar 18, 2022
1 parent c254dc8 commit 0cf34a1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
6 changes: 4 additions & 2 deletions src/cli/commands/generate-component.ts
Expand Up @@ -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;
Expand All @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion 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.
Expand All @@ -15,7 +16,7 @@ export async function migrate({ run, show }): Promise<void> {
'dist/**/*.migration.js',
'dist/**/migrations/*.js',
].map((dir) => {
return process.cwd() + '/' + dir;
return normalize(resolve(process.cwd(), dir));
});

const connection = await createConnection({
Expand Down
84 changes: 42 additions & 42 deletions src/cli/utils/lookup-closest-module.ts
Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions 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(/\//);
}
return new RegExp(/\//);
}

0 comments on commit 0cf34a1

Please sign in to comment.