generated from voxpelli/node-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (125 loc) · 3.43 KB
/
index.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import path from 'node:path';
import mapWorkspaces from '@npmcli/map-workspaces';
import { readPackage } from 'read-pkg';
/**
* @param {string[]} filter
* @param {string} name
* @param {string} cwd
* @param {string} baseCwd
* @returns {false|string}
*/
function includeWorkspace (filter, name, cwd, baseCwd) {
const referenceCwd = path.resolve(process.cwd(), baseCwd);
for (const item of filter) {
if (item === name) {
return item;
}
if (cwd.startsWith(path.resolve(referenceCwd, item))) {
return item;
}
}
return false;
}
/** @typedef {import('read-pkg').NormalizedPackageJson} NormalizedPackageJson */
/**
* @typedef Workspace
* @property {string} cwd
* @property {NormalizedPackageJson} pkg
* @property {string} [workspace]
*/
/**
* @typedef Options
* @property {string|undefined} [cwd]
* @property {boolean|undefined} [includeWorkspaceRoot]
* @property {boolean|undefined} [skipWorkspaces]
* @property {string[]|undefined} [workspace]
*/
/**
* @param {Options} [options]
* @returns {AsyncGenerator<Workspace>}
*/
export async function * readWorkspaces (options) {
const {
cwd: baseCwd = '.',
includeWorkspaceRoot = true,
skipWorkspaces = false,
workspace,
} = options || {};
const mainPkg = await readPackage({ cwd: baseCwd }).catch(
/** @param {Error} cause */ cause => {
throw new Error('Failed to read package.json', { cause });
}
);
if (includeWorkspaceRoot) {
yield {
cwd: baseCwd,
pkg: mainPkg,
};
}
if (skipWorkspaces) {
return;
}
const workspaceList =
mainPkg.workspaces
? await mapWorkspaces({ cwd: baseCwd, pkg: mainPkg })
: (
looksLikePnpm(mainPkg)
? await mapPnpmWorkspaces({ cwd: baseCwd, pkg: mainPkg })
: undefined
);
if (!workspaceList) {
return;
}
/** @type {Set<string>} */
const matchingWorkspaces = new Set();
for (const [name, workspaceCwd] of workspaceList) {
const workspaceMatch = workspace?.length
? includeWorkspace(workspace, name, workspaceCwd, baseCwd)
: true;
if (!workspaceMatch) {
continue;
}
if (workspaceMatch !== true) {
matchingWorkspaces.add(workspaceMatch);
}
const workspacePkg = await readPackage({ cwd: workspaceCwd });
yield {
cwd: workspaceCwd,
pkg: workspacePkg,
workspace: name,
};
}
if (workspace?.length && matchingWorkspaces.size !== (new Set(workspace)).size) {
throw new Error(`Couldn't find all workspaces, missing: ${workspace.filter(value => !matchingWorkspaces.has(value)).join(', ')}`);
}
}
/**
* @param {NormalizedPackageJson} pkg
* @returns {boolean}
*/
function looksLikePnpm (pkg) {
return (
pkg.packageManager?.startsWith('pnpm') ||
pkg.engines?.['pnpm'] !== undefined ||
pkg['pnpm'] !== undefined
);
}
/**
* @param {Pick<import('@npmcli/map-workspaces').Options, 'cwd' | 'pkg'>} cwd
* @returns {Promise<Map<string, string> | undefined>}
*/
async function mapPnpmWorkspaces ({ cwd = '.', pkg }) {
const { readWorkspaceManifest } = await import('@pnpm/workspace.read-manifest');
const pnpmWorkspaceManifest = await readWorkspaceManifest(cwd);
if (!pnpmWorkspaceManifest || !pnpmWorkspaceManifest.packages) {
return;
}
const modifiedPkg = /** @type {NormalizedPackageJson} */ ({
...pkg,
workspaces: pnpmWorkspaceManifest.packages,
});
return mapWorkspaces({
cwd,
pkg: modifiedPkg,
});
}