Skip to content

Commit

Permalink
feat: allow specify packageManager by name
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 30, 2023
1 parent 0de6bac commit 5376aeb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createRequire } from "node:module";
import { normalize, resolve } from "pathe";
import { withTrailingSlash } from "ufo";
import type { OperationOptions } from "./types";
import type { OperationOptions, PackageManager } from "./types";
import type { DetectPackageManagerOptions } from "./package-manager";
import { detectPackageManager } from "./package-manager";
import { detectPackageManager, packageManagers } from "./package-manager";

export async function findup<T>(
cwd: string,
Expand Down Expand Up @@ -73,15 +73,17 @@ export const NO_PACKAGE_MANAGER_DETECTED_ERROR_MSG =
export async function resolveOperationOptions(
options: OperationOptions = {},
): Promise<
NonPartial<
Pick<OperationOptions, "cwd" | "silent" | "packageManager" | "dev">
> &
Pick<OperationOptions, "workspace">
NonPartial<Pick<OperationOptions, "cwd" | "silent" | "dev">> &
Pick<OperationOptions, "workspace"> & {
packageManager: PackageManager;
}
> {
const cwd = options.cwd || process.cwd();

const packageManager =
options.packageManager ||
(typeof options.packageManager === "string"
? packageManagers.find((pm) => pm.name === options.packageManager)
: options.packageManager) ||
(await detectPackageManager(options.cwd || process.cwd()));

if (!packageManager) {
Expand Down
10 changes: 5 additions & 5 deletions src/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type DetectPackageManagerOptions = {
includeParentDirs?: boolean;
};

const _packageManagers: PackageManager[] = [
export const packageManagers: PackageManager[] = [
{ name: "npm", command: "npm", lockFile: "package-lock.json" },
{
name: "pnpm",
Expand All @@ -53,7 +53,7 @@ const _packageManagers: PackageManager[] = [
lockFile: "yarn.lock",
files: [".yarnrc.yml"],
},
];
] as const;

export async function detectPackageManager(
cwd: string,
Expand All @@ -74,9 +74,9 @@ export async function detectPackageManager(
packageJSON.packageManager.split("@");
const majorVersion = version.split(".")[0];
const packageManager =
_packageManagers.find(
packageManagers.find(
(pm) => pm.name === name && pm.majorVersion === majorVersion,
) || _packageManagers.find((pm) => pm.name === name);
) || packageManagers.find((pm) => pm.name === name);
return {
...packageManager,
name,
Expand All @@ -90,7 +90,7 @@ export async function detectPackageManager(

// 2. Use implicit file detection
if (!options.ignoreLockFile) {
for (const packageManager of _packageManagers) {
for (const packageManager of packageManagers) {
const detectionsFiles = [
packageManager.lockFile,
...(packageManager.files || []),
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type PackageManager = {
export type OperationOptions = {
cwd?: string;
silent?: boolean;
packageManager?: PackageManager;
packageManager?: PackageManager | PackageManagerName;
dev?: boolean;
workspace?: boolean | string;
};
11 changes: 11 additions & 0 deletions test/_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, it, describe } from "vitest";
import { resolveOperationOptions } from "../src/_utils";

describe("internal utils", () => {
describe("resolveOperationOptions", () => {
it("resolved package manager by name", async () => {
const r = await resolveOperationOptions({ packageManager: "yarn" });
expect(r.packageManager.name).toBe("yarn");
});
});
});

0 comments on commit 5376aeb

Please sign in to comment.