forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc-dep-flags.js
More file actions
388 lines (357 loc) · 8.22 KB
/
Copy pathcalc-dep-flags.js
File metadata and controls
388 lines (357 loc) · 8.22 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
const { resolve } = require('node:path')
const t = require('tap')
const calcDepFlags = require('../lib/calc-dep-flags.js')
const Node = require('../lib/node.js')
const Link = require('../lib/link.js')
const {
normalizePath,
printTree,
} = require('./fixtures/utils.js')
const cwd = normalizePath(process.cwd())
t.cleanSnapshot = s => s.split(cwd).join('{CWD}')
t.test('flag stuff', t => {
const root = new Node({
path: '/x',
realpath: '/x',
pkg: {
dependencies: { prod: '' },
devDependencies: { dev: '' },
optionalDependencies: { optional: '' },
peerDependencies: { peer: '', peeroptional: '' },
peerDependenciesMeta: { peeroptional: { optional: true } },
},
})
const optional = new Node({
pkg: {
name: 'optional',
version: '1.2.3',
dependencies: { devoptional: '', missing: '' },
},
parent: root,
})
const devoptional = new Node({
pkg: {
name: 'devoptional',
version: '1.2.3',
},
parent: root,
})
const extraneous = new Node({
pkg: {
name: 'extraneous',
},
parent: root,
})
const peer = new Node({
pkg: {
name: 'peer',
version: '1.2.3',
dependencies: { peerdep: '' },
},
parent: root,
})
const peerdep = new Node({
pkg: {
name: 'peerdep',
version: '1.2.3',
},
parent: root,
})
const prod = new Node({
pkg: {
name: 'prod',
version: '1.2.3',
dependencies: { proddep: '' },
peerDependencies: { metapeer: '' },
},
parent: root,
})
const metapeer = new Node({
pkg: {
name: 'metapeer',
version: '1.2.3',
dependencies: { metapeerdep: '' },
},
parent: root,
})
const metapeerdep = new Node({
pkg: {
name: 'metapeerdep',
version: '1.2.3',
},
parent: root,
})
const proddep = new Node({
pkg: {
name: 'proddep',
version: '1.2.3',
dependencies: { proddep: '' },
},
parent: root,
})
const dev = new Node({
pkg: {
name: 'dev',
version: '1.2.3',
dependencies: { devdep: '' },
},
parent: root,
})
const devdep = new Node({
pkg: {
name: 'devdep',
version: '1.2.3',
dependencies: { proddep: '', linky: '', devoptional: '' },
optionalDependencies: { devandoptional: '' },
},
parent: root,
})
const devandoptional = new Node({
pkg: {
name: 'devandoptional',
version: '1.2.3',
},
parent: root,
})
const linky = new Link({
pkg: {
name: 'linky',
version: '1.2.3',
dependencies: { linklink: '' },
},
realpath: '/x/y/z',
parent: devdep,
})
// a link dep depended upon by the target of a linked dep
const linkylinky = new Link({
pkg: {
name: 'linklink',
version: '1.2.3',
},
realpath: '/l/i/n/k/link',
parent: linky.target,
})
const peeroptional = new Node({
pkg: {
name: 'peeroptional',
version: '1.2.3',
dependencies: { optional: '' },
},
parent: root,
})
calcDepFlags(root)
t.match(optional, {
extraneous: false,
dev: false,
optional: true,
devOptional: false,
peer: false,
})
t.match(devoptional, {
extraneous: false,
dev: false,
optional: false,
devOptional: true,
peer: false,
})
t.match(extraneous, {
extraneous: true,
})
t.match(peer, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: true,
})
t.match(peerdep, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: true,
})
t.match(prod, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: false,
})
t.match(metapeer, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: true,
})
t.match(metapeerdep, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: true,
})
t.match(proddep, {
extraneous: false,
dev: false,
optional: false,
devOptional: false,
peer: false,
})
t.match(dev, {
extraneous: false,
dev: true,
optional: false,
devOptional: false,
peer: false,
})
t.match(devdep, {
extraneous: false,
dev: true,
optional: false,
devOptional: false,
peer: false,
})
t.match(devandoptional, {
extraneous: false,
dev: true,
optional: true,
devOptional: false,
peer: false,
})
t.match(linky, {
extraneous: false,
dev: true,
optional: false,
devOptional: false,
peer: false,
})
t.match(linkylinky, {
extraneous: false,
dev: true,
optional: false,
devOptional: false,
peer: false,
})
t.match(peeroptional, {
extraneous: true,
dev: false,
optional: true,
devOptional: false,
peer: true,
})
t.matchSnapshot(printTree(root), 'after')
t.end()
})
t.test('no reset', async t => {
const root = new Node({
path: '/some/path',
realpath: '/some/path',
pkg: {
dependencies: { foo: '' },
},
})
const foo = new Node({ parent: root, pkg: { name: 'foo', version: '1.2.3' } })
root.optional = false
root.dev = true
root.extraneous = false
calcDepFlags(root, false)
t.matchSnapshot(printTree(root), 'after')
t.equal(root.dev, true, 'root.dev')
t.equal(foo.dev, true, 'foo.dev')
t.equal(root.optional, false, 'root.optional')
t.equal(foo.optional, false, 'foo.optional')
t.equal(root.extraneous, false, 'root.extraneous')
t.equal(foo.extraneous, false, 'foo.extraneous')
})
t.test('set parents to not extraneous when visiting', t => {
const root = new Node({
path: '/some/path',
realpath: '/some/path',
pkg: {
dependencies: {
baz: 'file:node_modules/asdf/node_modules/baz',
foo: 'file:bar/foo',
},
},
})
const bar = new Node({
root,
path: resolve(root.path, 'bar'),
})
const foo = new Node({
root,
path: resolve(bar.path, 'foo'),
pkg: { name: 'foo', version: '1.2.3' },
})
const asdf = new Node({
parent: root,
pkg: { name: 'asdf', version: '1.2.3' },
})
const baz = new Node({
parent: asdf,
pkg: { name: 'baz', version: '1.2.3' },
})
const fooLink = new Link({
name: 'foo',
target: foo,
parent: root,
realpath: foo.path,
})
const bazLink = new Link({
name: 'baz',
target: baz,
parent: root,
realpath: baz.path,
})
calcDepFlags(root, true)
t.equal(root.extraneous, false, 'root is not extraneous')
t.equal(asdf.extraneous, false, 'asdf is not extraneous')
t.equal(bar.extraneous, false, 'bar is not extraneous')
t.equal(baz.extraneous, false, 'baz is not extraneous')
t.equal(foo.extraneous, false, 'foo is not extraneous')
t.equal(fooLink.extraneous, false, 'fooLink is not extraneous')
t.equal(bazLink.extraneous, false, 'bazLink is not extraneous')
t.end()
})
t.test('multiple links to one target keep the most permissive flags', async t => {
// `tools` is reached by a prod root link and a dev `app` link; the dev link must not overwrite the prod dev flag.
const root = new Node({
path: '/r',
realpath: '/r',
pkg: { name: 'root', workspaces: ['app', 'tools'] },
})
const app = new Node({
path: '/r/app',
realpath: '/r/app',
root,
pkg: { name: 'app', version: '1.0.0', devDependencies: { tools: '*' } },
})
const tools = new Node({
path: '/r/tools',
realpath: '/r/tools',
root,
pkg: { name: 'tools', version: '1.0.0' },
})
new Link({ name: 'app', parent: root, realpath: '/r/app', target: app })
new Link({ name: 'tools', parent: root, realpath: '/r/tools', target: tools })
new Link({ name: 'tools', parent: app, realpath: '/r/tools', target: tools })
root.workspaces = new Map([['app', '/r/app'], ['tools', '/r/tools']])
calcDepFlags(root)
t.equal(tools.dev, false, 'tools is prod via the root workspace link')
t.equal(app.dev, false, 'app is prod via the root workspace link')
t.end()
})
t.test('check null target in link', async t => {
const root = new Link({
path: '/some/path',
realpath: '/some/path',
pkg: {
dependencies: { foo: '' },
},
})
t.doesNotThrow(() => calcDepFlags(root))
t.doesNotThrow(() => calcDepFlags(root, false))
t.end()
})