From da7309444569f363821f65d9bc4d5ae43beb3b9f Mon Sep 17 00:00:00 2001 From: Samuel Schultze Date: Fri, 5 Aug 2022 16:47:27 -0300 Subject: [PATCH] fix: follow cwd symlink on windows --- src/getAppRootPath.ts | 6 +++--- src/getPackageResolution.ts | 3 ++- src/patch/read.test.ts | 3 +++ src/patch/read.ts | 5 +++-- src/realpathCwd.ts | 6 ++++++ 5 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 src/realpathCwd.ts diff --git a/src/getAppRootPath.ts b/src/getAppRootPath.ts index 61a7f80b..b6614859 100644 --- a/src/getAppRootPath.ts +++ b/src/getAppRootPath.ts @@ -1,11 +1,11 @@ import { join, resolve } from "./path" -import process from "process" import { existsSync } from "fs-extra" +import { realpathCwd } from "./realpathCwd" export const getAppRootPath = (): string => { - let cwd = process.cwd() + let cwd = realpathCwd().replace("\\", "/") while (!existsSync(join(cwd, "package.json"))) { - const up = resolve(cwd, "../") + const up = resolve(cwd, "../").replace("\\", "/") if (up === cwd) { throw new Error("no package.json found for this project") } diff --git a/src/getPackageResolution.ts b/src/getPackageResolution.ts index 5c8c158b..f8822ba7 100644 --- a/src/getPackageResolution.ts +++ b/src/getPackageResolution.ts @@ -5,6 +5,7 @@ import { readFileSync, existsSync } from "fs-extra" import { parse as parseYarnLockFile } from "@yarnpkg/lockfile" import findWorkspaceRoot from "find-yarn-workspace-root" import { getPackageVersion } from "./getPackageVersion" +import { realpathCwd } from "./realpathCwd" export function getPackageResolution({ packageDetails, @@ -104,7 +105,7 @@ if (require.main === module) { } console.log( getPackageResolution({ - appPath: process.cwd(), + appPath: realpathCwd(), packageDetails, packageManager: detectPackageManager(process.cwd(), null), }), diff --git a/src/patch/read.test.ts b/src/patch/read.test.ts index 327bb980..01080c3e 100644 --- a/src/patch/read.test.ts +++ b/src/patch/read.test.ts @@ -7,6 +7,9 @@ const removeAnsiCodes = (s: string) => "", ) +jest.mock("fs", () => ({ + realpathSync: jest.fn((path)=>path), +})) jest.mock("fs-extra", () => ({ readFileSync: jest.fn(), })) diff --git a/src/patch/read.ts b/src/patch/read.ts index 632cdd63..237e37bc 100644 --- a/src/patch/read.ts +++ b/src/patch/read.ts @@ -4,6 +4,7 @@ import { relative, resolve } from "../path" import { normalize } from "path" import { PackageDetails } from "../PackageDetails" import { parsePatchFile, PatchFilePart } from "./parse" +import { realpathCwd } from "../realpathCwd" export function readPatch({ patchFilePath, @@ -19,7 +20,7 @@ export function readPatch({ } catch (e) { const fixupSteps: string[] = [] const relativePatchFilePath = normalize( - relative(process.cwd(), patchFilePath), + relative(realpathCwd(), patchFilePath), ) const patchBaseDir = relativePatchFilePath.slice( 0, @@ -36,7 +37,7 @@ export function readPatch({ fixupSteps.push(`npx patch-package ${packageDetails.pathSpecifier}`) if (patchBaseDir) { fixupSteps.push( - `cd ${relative(resolve(process.cwd(), patchBaseDir), process.cwd())}`, + `cd ${relative(resolve(realpathCwd(), patchBaseDir), realpathCwd())}`, ) } diff --git a/src/realpathCwd.ts b/src/realpathCwd.ts new file mode 100644 index 00000000..92e9cec0 --- /dev/null +++ b/src/realpathCwd.ts @@ -0,0 +1,6 @@ +import fs from "fs" +import process from "process" + +export const realpathCwd = (): string => { + return fs.realpathSync(process.cwd()) +}