-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathget-workspaces.ts
46 lines (40 loc) · 1.14 KB
/
get-workspaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import path from 'node:path';
import fse from 'fs-extra';
import jsYaml from 'js-yaml';
import zod from 'zod';
const WorkspaceModel = zod.optional(
zod.union([
zod.array(zod.string()),
zod.object({
packages: zod.optional(zod.array(zod.string())),
nohoist: zod.optional(zod.array(zod.string())),
}),
]),
);
export async function getWorkspaces(
packageJSON: Record<string, unknown>,
): Promise<string[] | null> {
let result = WorkspaceModel.parse(packageJSON.workspaces);
const pnpmWorkspacePath = path.join(process.cwd(), 'pnpm-workspace.yaml');
const isPnpmWorkspace = await fse.pathExists(pnpmWorkspacePath);
if (isPnpmWorkspace) {
if (result) {
throw new Error(
'Both `pnpm-workspace.yaml` and `package.json#workspaces` are not supported. Remove `package.json#workspaces` field.',
);
}
result = jsYaml.load(await fse.readFile(pnpmWorkspacePath, 'utf8')) as {
packages?: string[];
};
}
if (result == null) {
return null;
}
if (Array.isArray(result)) {
return result;
}
if (Array.isArray(result?.packages)) {
return result.packages;
}
return null;
}