Skip to content

Commit 366639b

Browse files
feat: detect package manager
1 parent c8933b1 commit 366639b

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,3 @@ Moreover, deployment statuses are reported as successful even when builds are sk
6060
Then, it proceeds to check whether the given package or any of its dependencies were modified since the last commit with the use of `git diff "HEAD^" "HEAD" --quiet`.
6161

6262
Currently, only `pnpm` and `yarn` workspaces are supported but more is on the roadmap.
63-
64-
### Yarn workspaces
65-
If you are using `yarn` workspaces pass `--yarn` flag

src/detectPackageManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Path from "node:path";
2+
import { PackageManager } from "./types.js";
3+
import { isRootDir as isPnpmRootDir } from "./pnpmWorkspace.js";
4+
import { isRootDir as isYarnRootDir } from "./yarnWorkspace.js";
5+
6+
export function detectPackageManager(cwd: string): PackageManager {
7+
const paths = cwd
8+
.split(Path.sep)
9+
.map((_, idx) => Path.join(cwd, "../".repeat(idx)));
10+
11+
for (const path of paths) {
12+
if (isPnpmRootDir(path)) return "pnpm";
13+
if (isYarnRootDir(path)) return "yarn";
14+
}
15+
throw new Error("Package manager could not be detected");
16+
}

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as yarn from "./yarnWorkspace.js";
88
import { compare } from "./git.js";
99
import { debug } from "./debug.js";
1010
import { parseArgs } from "./parseArgs.js";
11+
import { PackageManager } from "./types.js";
12+
import { detectPackageManager } from "./detectPackageManager.js";
1113

1214
const cwd = process.cwd();
1315

@@ -21,21 +23,21 @@ const configuration = parseArgs({
2123
type: "boolean",
2224
default: false,
2325
},
24-
yarn: {
25-
type: "boolean",
26-
default: false,
27-
},
2826
},
2927
});
3028

29+
const log = debug(configuration.values.verbose);
30+
31+
const packageManager: PackageManager = detectPackageManager(cwd);
32+
33+
log({ packageManager });
34+
3135
const {
3236
readWorkspaceDirs,
3337
readWorkspaceSettings,
3438
resolveWorkspaceDeps,
3539
isRootDir,
36-
} = configuration.values.yarn ? yarn : pnpm;
37-
38-
const log = debug(configuration.values.verbose);
40+
} = packageManager === "pnpm" ? pnpm : yarn;
3941

4042
const [gitFromPointer = "HEAD^", gitToPointer = "HEAD"] =
4143
configuration.positionals;

src/pnpmWorkspace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { readFile, readdir } from "node:fs/promises";
22
import Path from "node:path";
33
import { existsSync } from "node:fs";
44
import { fileExist, readJson } from "./utils.js";
5-
import { Ctx, Workspace, PackageJson } from "./types.js";
5+
import { Ctx, Workspace, PackageJson, WorkspaceSettings } from "./types.js";
66

7-
export async function readWorkspaceSettings({ rootDir, cwd }: Ctx): Promise<{
8-
workspaces: Record<string, Workspace>;
9-
currentWorkspace: Workspace;
10-
}> {
7+
export async function readWorkspaceSettings({
8+
rootDir,
9+
cwd,
10+
}: Ctx): Promise<WorkspaceSettings> {
1111
const workspaceDirs = await readWorkspaceDirs({ rootDir, cwd });
1212

1313
const workspaces = (await Promise.all(workspaceDirs.map(findPackagesInDir)))

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export type WorkspaceSettings = {
1919
workspaces: Record<string, Workspace>;
2020
currentWorkspace: Workspace;
2121
};
22+
23+
export type PackageManager = "yarn" | "npm" | "pnpm";

0 commit comments

Comments
 (0)