Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/diffci-cli/src/client/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,31 @@ export function detectPackageManager(repoPath: string): SupportedPackageManager
return detected.values().next().value ?? "npm";
}

/**
* Whether the repository uses Yarn 2 or later (Berry). Berry's `yarn add` rejects `--ignore-scripts`
* ("Unsupported option name") and skips build scripts with `--mode=skip-build` instead. The declared
* packageManager version decides; without one, a .yarnrc.yml or a Berry lockfile (`__metadata:`) does.
*/
export function usesYarnBerry(repoPath: string): boolean {
const packagePath = join(repoPath, "package.json");
let declared: unknown;
try { declared = (JSON.parse(readFileSync(packagePath, "utf8")) as { packageManager?: unknown }).packageManager; } catch { declared = undefined; }
const version = typeof declared === "string" ? /^yarn@(\d+)/.exec(declared.trim()) : null;
if (version) return Number(version[1]) >= 2;
if (existsSync(join(repoPath, ".yarnrc.yml"))) return true;
try { return /^__metadata:/m.test(readFileSync(join(repoPath, "yarn.lock"), "utf8")); } catch { return false; }
}

export function installPlan(repoPath: string, version: string, platform = process.platform): InstallPlan {
const manager = detectPackageManager(repoPath);
const packageSpec = `@diffci.com/diffci@${version}`;
const managerCommand = platform === "win32" ? `${manager}.cmd` : manager;
const argsByManager: Record<SupportedPackageManager, string[]> = {
npm: ["install", "--save-dev", "--save-exact", "--ignore-scripts", packageSpec],
pnpm: ["add", "--save-dev", "--save-exact", "--ignore-scripts", packageSpec],
yarn: ["add", "--dev", "--exact", "--ignore-scripts", packageSpec],
yarn: manager === "yarn" && usesYarnBerry(repoPath)
? ["add", "--dev", "--exact", "--mode=skip-build", packageSpec]
: ["add", "--dev", "--exact", "--ignore-scripts", packageSpec],
bun: ["add", "--dev", "--exact", "--ignore-scripts", packageSpec],
};
const managerArgs = argsByManager[manager];
Expand Down
23 changes: 23 additions & 0 deletions packages/diffci-cli/tests/client/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ describe("DiffCI dependency installation", () => {
}
});

it("skips build scripts the way each Yarn line accepts: --ignore-scripts for Classic, --mode=skip-build for Berry", () => {
// Yarn 2+ rejects --ignore-scripts: `Unknown Syntax Error: Unsupported option name ("--ignore-scripts")`.
const berryArgs = ["add", "--dev", "--exact", "--mode=skip-build", "@diffci.com/diffci@1.2.3"];
const classicArgs = ["add", "--dev", "--exact", "--ignore-scripts", "@diffci.com/diffci@1.2.3"];
const cases: Array<{ pkg: object; files: Record<string, string>; args: string[] }> = [
{ pkg: { packageManager: "yarn@4.9.2" }, files: {}, args: berryArgs },
{ pkg: { packageManager: "yarn@2.4.3" }, files: {}, args: berryArgs },
{ pkg: {}, files: { "yarn.lock": "__metadata:\n version: 8\n cacheKey: 10c0\n" }, args: berryArgs },
{ pkg: {}, files: { "yarn.lock": "", ".yarnrc.yml": "nodeLinker: node-modules\n" }, args: berryArgs },
{ pkg: { packageManager: "yarn@1.22.22" }, files: {}, args: classicArgs },
{ pkg: {}, files: { "yarn.lock": "# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n# yarn lockfile v1\n" }, args: classicArgs },
];
for (const { pkg, files, args } of cases) {
const dir = fixture(pkg);
try {
for (const [name, content] of Object.entries(files)) writeFileSync(join(dir, name), content, "utf8");
assert.deepEqual(installPlan(dir, "1.2.3", "linux"), { manager: "yarn", command: "yarn", args, packageSpec: "@diffci.com/diffci@1.2.3" }, JSON.stringify({ pkg, files }));
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
});

it("detects lockfiles, defaults to npm, and invokes Windows package-manager shims through cmd.exe", () => {
const yarnDir = fixture();
const npmDir = fixture();
Expand Down
Loading