-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
226 lines (194 loc) · 6.42 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const path = require('path')
const getName = require('@npmcli/name-from-folder')
const { minimatch } = require('minimatch')
const pkgJson = require('@npmcli/package-json')
const { glob } = require('glob')
function appendNegatedPatterns (allPatterns) {
const patterns = []
const negatedPatterns = []
for (let pattern of allPatterns) {
const excl = pattern.match(/^!+/)
if (excl) {
pattern = pattern.slice(excl[0].length)
}
// strip off any / or ./ from the start of the pattern. /foo => foo
pattern = pattern.replace(/^\.?\/+/, '')
// an odd number of ! means a negated pattern. !!foo ==> foo
const negate = excl && excl[0].length % 2 === 1
if (negate) {
negatedPatterns.push(pattern)
} else {
// remove negated patterns that appeared before this pattern to avoid
// ignoring paths that were matched afterwards
// e.g: ['packages/**', '!packages/b/**', 'packages/b/a']
// in the above list, the last pattern overrides the negated pattern
// right before it. In effect, the above list would become:
// ['packages/**', 'packages/b/a']
// The order matters here which is why we must do it inside the loop
// as opposed to doing it all together at the end.
for (let i = 0; i < negatedPatterns.length; ++i) {
const negatedPattern = negatedPatterns[i]
if (minimatch(pattern, negatedPattern)) {
negatedPatterns.splice(i, 1)
}
}
patterns.push(pattern)
}
}
// use the negated patterns to eagerly remove all the patterns that
// can be removed to avoid unnecessary crawling
for (const negated of negatedPatterns) {
for (const pattern of minimatch.match(patterns, negated)) {
patterns.splice(patterns.indexOf(pattern), 1)
}
}
return { patterns, negatedPatterns }
}
function getPatterns (workspaces) {
const workspacesDeclaration =
Array.isArray(workspaces.packages)
? workspaces.packages
: workspaces
if (!Array.isArray(workspacesDeclaration)) {
throw getError({
message: 'workspaces config expects an Array',
code: 'EWORKSPACESCONFIG',
})
}
return appendNegatedPatterns(workspacesDeclaration)
}
function getPackageName (pkg, pathname) {
return pkg.name || getName(pathname)
}
// make sure glob pattern only matches folders
function getGlobPattern (pattern) {
pattern = pattern.replace(/\\/g, '/')
return pattern.endsWith('/')
? pattern
: `${pattern}/`
}
function getError ({ Type = TypeError, message, code }) {
return Object.assign(new Type(message), { code })
}
function reverseResultMap (map) {
return new Map(Array.from(map, item => item.reverse()))
}
async function mapWorkspaces (opts = {}) {
if (!opts || !opts.pkg) {
throw getError({
message: 'mapWorkspaces missing pkg info',
code: 'EMAPWORKSPACESPKG',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}
const { workspaces = [] } = opts.pkg
const { patterns, negatedPatterns } = getPatterns(workspaces)
const results = new Map()
if (!patterns.length && !negatedPatterns.length) {
return results
}
const seen = new Map()
const getGlobOpts = () => ({
...opts,
ignore: [
...opts.ignore || [],
'**/node_modules/**',
// just ignore the negated patterns to avoid unnecessary crawling
...negatedPatterns,
],
})
let matches = await glob(patterns.map((p) => getGlobPattern(p)), getGlobOpts())
// preserves glob@8 behavior
matches = matches.sort((a, b) => a.localeCompare(b, 'en'))
// we must preserve the order of results according to the given list of
// workspace patterns
const orderedMatches = []
for (const pattern of patterns) {
orderedMatches.push(...matches.filter((m) => {
return minimatch(m, pattern, { partial: true, windowsPathsNoEscape: true })
}))
}
for (const match of orderedMatches) {
let pkg
try {
pkg = await pkgJson.normalize(path.join(opts.cwd, match))
} catch (err) {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
continue
} else {
throw err
}
}
const name = getPackageName(pkg.content, pkg.path)
let seenPackagePathnames = seen.get(name)
if (!seenPackagePathnames) {
seenPackagePathnames = new Set()
seen.set(name, seenPackagePathnames)
}
seenPackagePathnames.add(pkg.path)
}
const errorMessageArray = ['must not have multiple workspaces with the same name']
for (const [packageName, seenPackagePathnames] of seen) {
if (seenPackagePathnames.size > 1) {
addDuplicateErrorMessages(errorMessageArray, packageName, seenPackagePathnames)
} else {
results.set(packageName, seenPackagePathnames.values().next().value)
}
}
if (errorMessageArray.length > 1) {
throw getError({
Type: Error,
message: errorMessageArray.join('\n'),
code: 'EDUPLICATEWORKSPACE',
})
}
return results
}
function addDuplicateErrorMessages (messageArray, packageName, packagePathnames) {
messageArray.push(
`package '${packageName}' has conflicts in the following paths:`
)
for (const packagePathname of packagePathnames) {
messageArray.push(
' ' + packagePathname
)
}
}
mapWorkspaces.virtual = function (opts = {}) {
if (!opts || !opts.lockfile) {
throw getError({
message: 'mapWorkspaces.virtual missing lockfile info',
code: 'EMAPWORKSPACESLOCKFILE',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}
const { packages = {} } = opts.lockfile
const { workspaces = [] } = packages[''] || {}
// uses a pathname-keyed map in order to negate the exact items
const results = new Map()
const { patterns, negatedPatterns } = getPatterns(workspaces)
if (!patterns.length && !negatedPatterns.length) {
return results
}
negatedPatterns.push('**/node_modules/**')
const packageKeys = Object.keys(packages)
for (const pattern of negatedPatterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
packageKeys.splice(packageKeys.indexOf(packageKey), 1)
}
}
for (const pattern of patterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
const packagePathname = path.join(opts.cwd, packageKey)
const name = getPackageName(packages[packageKey], packagePathname)
results.set(packagePathname, name)
}
}
// Invert pathname-keyed to a proper name-to-pathnames Map
return reverseResultMap(results)
}
module.exports = mapWorkspaces