Skip to content

Commit 416f44c

Browse files
committed
fix(core): simplify file path handling in module and barrel file functions
1 parent 403d431 commit 416f44c

3 files changed

Lines changed: 7 additions & 14 deletions

File tree

packages/core/src/lib/modules/internal/find-module-paths-with-barrel.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ export function findModulePathsWithBarrel(
1111
for (const projectDir of projectDirs) {
1212
const found = getFs().findFiles(projectDir, barrelFileName);
1313
for (const filePath of found) {
14-
const lastSep = Math.max(
15-
filePath.lastIndexOf('/'),
16-
filePath.lastIndexOf('\\'),
17-
);
18-
modulePaths.add(filePath.substring(0, lastSep));
14+
modulePaths.add(getFs().getParent(filePath));
1915
}
2016
}
2117
}

packages/core/src/lib/modules/internal/find-module-paths-without-barrel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createModulePathPatternsTree,
55
ModulePathPatternsTree,
66
} from './create-module-path-patterns-tree';
7+
import * as path from 'path';
78
import getFs from '../../fs/getFs';
89
import { FOLDER_CHARACTERS_REGEX_STRING } from '../../tags/calc-tags-for-module';
910
import { flattenModules } from './flatten-modules';
@@ -104,8 +105,7 @@ function hasBarrelFile(directory: FsPath, barrelFileNames: string[]): boolean {
104105
const children = fs.readDirectory(directory);
105106
return children.some((child) => {
106107
if (fs.isFile(child)) {
107-
const lastSep = Math.max(child.lastIndexOf('/'), child.lastIndexOf('\\'));
108-
const fileName = lastSep === -1 ? child : child.substring(lastSep + 1);
108+
const fileName = path.basename(child);
109109
return barrelFileNames.some((pattern) => matchGlob(pattern, fileName));
110110
}
111111
return false;

packages/core/src/lib/modules/module.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { UnassignedFileInfo } from '../file-info/unassigned-file-info';
22
import { FileInfo } from './file.info';
3+
import * as path from 'path';
34
import { FsPath } from '../file-info/fs-path';
5+
import getFs from '../fs/getFs';
46
import { matchGlob } from '../util/match-glob';
57

68
/**
@@ -37,13 +39,8 @@ export class Module {
3739
* are not treated as barrel entries.
3840
*/
3941
isBarrelFile(filePath: FsPath): boolean {
40-
const lastSep = Math.max(
41-
filePath.lastIndexOf('/'),
42-
filePath.lastIndexOf('\\'),
43-
);
44-
const fileDir = lastSep === -1 ? '' : filePath.substring(0, lastSep);
45-
const fileName =
46-
lastSep === -1 ? filePath : filePath.substring(lastSep + 1);
42+
const fileDir = getFs().getParent(filePath);
43+
const fileName = path.basename(filePath);
4744

4845
if (fileDir !== this.path) {
4946
return false;

0 commit comments

Comments
 (0)