Skip to content

Commit 323e749

Browse files
outsleptdreyfus92
andauthored
fix: normalize Windows path separators across operations (#51)
* fix: normalize path separators in tarball file system * chore: change variable name to `normalizePath` Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com> * chore: change variable name to `normalizedPath` * fix: remove unnecessary path normalization * fix: windows path separators * fix: windows path separators --------- Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>
1 parent ab0ddd7 commit 323e749

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

src/analyze/dependencies.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Stats
88
} from '../types.js';
99
import {FileSystem} from '../file-system.js';
10+
import {normalizePath} from '../utils/path.js';
1011

1112
interface DependencyNode {
1213
name: string;
@@ -214,7 +215,7 @@ export async function runDependencyAnalysis(
214215
const allDeps = {...depPkg.dependencies, ...depPkg.devDependencies};
215216
for (const depName of Object.keys(allDeps)) {
216217
let packageMatch = packageFiles.find((packageFile) =>
217-
packageFile.endsWith(`/node_modules/${depName}/package.json`)
218+
normalizePath(packageFile).endsWith(`/node_modules/${depName}/package.json`)
218219
);
219220

220221
if (!packageMatch) {
@@ -247,7 +248,8 @@ export async function runDependencyAnalysis(
247248
// find the ones that don't exist in the parent package's dependency list.
248249
// there must be a better way
249250
for (const file of packageFiles) {
250-
if (file === rootDir + '/package.json') {
251+
const rootPackageJsonPath = normalizePath(rootDir) + '/package.json';
252+
if (file === rootPackageJsonPath) {
251253
continue;
252254
}
253255

@@ -264,7 +266,8 @@ export async function runDependencyAnalysis(
264266

265267
if (!alreadyExists) {
266268
// Extract path information from the file path
267-
const pathParts = file.split('/node_modules/');
269+
const normalizedFile = normalizePath(file);
270+
const pathParts = normalizedFile.split('/node_modules/');
268271
if (pathParts.length > 1) {
269272
const packageDirName = pathParts[pathParts.length - 1].replace(
270273
'/package.json',

src/local-file-system.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'node:fs/promises';
44
import {type Logger, pino} from 'pino';
55
import {fdir} from 'fdir';
66
import {readFile, stat} from 'node:fs/promises';
7+
import {normalizePath} from './utils/path.js';
78

89
export class LocalFileSystem implements FileSystem {
910
#root: string;
@@ -35,10 +36,13 @@ export class LocalFileSystem implements FileSystem {
3536
const crawler = new fdir()
3637
.withFullPaths()
3738
.withSymlinks()
38-
.filter((filePath) => filePath.endsWith('/package.json'))
39+
.filter((filePath) => normalizePath(filePath).endsWith('/package.json'))
3940
.crawl(nodeModulesPath);
4041
const files = await crawler.withPromise();
41-
return files.map((file) => `/${path.relative(this.#root, file)}`);
42+
return files.map((file) => {
43+
const relativePath = path.relative(this.#root, file);
44+
return '/' + normalizePath(relativePath);
45+
});
4246
} catch {
4347
return [];
4448
}

src/tarball-file-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class TarballFileSystem implements FileSystem {
3636

3737
async readFile(filePath: string): Promise<string> {
3838
const {files} = await this.#getUnpackResult();
39-
const fullPath = path.join(await this.getRootDir(), filePath);
39+
const fullPath = path.posix.join(await this.getRootDir(), filePath);
4040
const file = files.find((f) => f.name === fullPath);
4141
if (!file) {
4242
throw new Error(`File not found: ${filePath}`);

src/utils/path.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function normalizePath(path: string): string {
2+
return path.replaceAll('\\', '/');
3+
}

0 commit comments

Comments
 (0)