Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: normalize packageManager field when parsing #184

Merged
merged 22 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 13 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,16 @@ export function doesDependencyExist(
return false;
}
}

export function sanitizePackageManagerName(
name: string,
): [any, string | undefined] {
const sanitized = name.replace(/^\W+/, "");
if (name !== sanitized) {
return [
sanitized,
`Abnormal characters found in \`packageManager\` field, sanitizing from \`'${name}'\` to \`'${sanitized}'\``,
];
}
return [name, undefined];
}
13 changes: 13 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,27 @@ const detect = defineCommand({
type: "string",
description: "Current working directory",
},
warn: {
type: "boolean",
description: "Log warnings",
default: true,
},
},
run: async ({ args }) => {
const cwd = resolve(args.cwd || ".");
const packageManager = await detectPackageManager(cwd);

if (args.warn && packageManager?.warnings) {
for (const warning of packageManager.warnings) {
consola.warn(warning);
}
}

if (!packageManager) {
consola.error(`Cannot detect package manager in \`${cwd}\``);
return process.exit(1);
}

consola.log(
`Detected package manager in \`${cwd}\`: \`${packageManager.name}@${packageManager.version}\``,
);
Expand Down
7 changes: 5 additions & 2 deletions src/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { join, resolve } from "pathe";
import { findup } from "./_utils";
import { findup, sanitizePackageManagerName } from "./_utils";
import type { PackageManager } from "./types";

export type DetectPackageManagerOptions = {
Expand Down Expand Up @@ -92,8 +92,10 @@ export async function detectPackageManager(
await readFile(packageJSONPath, "utf8"),
);
if (packageJSON?.packageManager) {
const [name, version = "0.0.0"] =
const [rawName, version = "0.0.0"] =
packageJSON.packageManager.split("@");
const [name, sanitizationWarning] =
sanitizePackageManagerName(rawName);
const majorVersion = version.split(".")[0];
const packageManager =
packageManagers.find(
Expand All @@ -105,6 +107,7 @@ export async function detectPackageManager(
command: name,
version,
majorVersion,
warnings: [sanitizationWarning].filter(Boolean) as string[],
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type PackageManager = {
majorVersion?: string;
lockFile?: string | string[];
files?: string[];
warnings?: string[];
};

export type OperationOptions = {
Expand Down
21 changes: 20 additions & 1 deletion test/_utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect, it, describe } from "vitest";
import { resolveOperationOptions } from "../src/_utils";
import {
sanitizePackageManagerName,
resolveOperationOptions,
} from "../src/_utils";

describe("internal utils", () => {
describe("resolveOperationOptions", () => {
Expand All @@ -8,4 +11,20 @@ describe("internal utils", () => {
expect(r.packageManager.name).toBe("yarn");
});
});

describe("parsePackageManagerName", () => {
it("preserves valid (unknown) name", () => {
const [name, warning] = sanitizePackageManagerName("unknownName@0.0.0");
expect(name).toBe("unknownName@0.0.0");
expect(warning).toMatchInlineSnapshot(`undefined`);
});

it("sanitize abnormal characters with warning", () => {
const [name, warning] = sanitizePackageManagerName("~^&#-!yarn@0.0.0");
expect(name).toBe("yarn@0.0.0");
expect(warning).toMatchInlineSnapshot(
`"Abnormal characters found in \`packageManager\` field, sanitizing from \`'~^&#-!yarn@0.0.0'\` to \`'yarn@0.0.0'\`"`,
);
});
});
});
Loading