-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathinit.js
More file actions
583 lines (480 loc) · 14.3 KB
/
init.js
File metadata and controls
583 lines (480 loc) · 14.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
const t = require('tap')
const fs = require('node:fs/promises')
const nodePath = require('node:path')
const { resolve, basename } = require('node:path')
const _mockNpm = require('../../fixtures/mock-npm')
const { cleanTime } = require('../../fixtures/clean-snapshot')
t.cleanSnapshot = cleanTime
const mockNpm = async (t, { noLog, libnpmexec, initPackageJson, ...opts } = {}) => {
const res = await _mockNpm(t, {
...opts,
mocks: {
...(libnpmexec ? { libnpmexec } : {}),
...(initPackageJson ? { 'init-package-json': initPackageJson } : {}),
},
globals: {
// init-package-json prints directly to console.log
// this avoids poluting test output with those logs
...(noLog ? { 'console.log': () => {} } : {}),
},
})
return res
}
t.test('displays output', async t => {
const { npm, joinedOutput } = await mockNpm(t, {
initPackageJson: async () => {},
})
await npm.exec('init', [])
t.matchSnapshot(joinedOutput(), 'displays helper info')
})
t.test('classic npm init -y', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.equal(pkg.version, '1.0.0')
t.equal(pkg.license, 'ISC')
})
t.test('classic interactive npm init', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
initPackageJson: async (path) => {
t.equal(
path,
resolve(npm.localPrefix),
'should start init package.json in expected path'
)
},
})
await npm.exec('init', [])
})
t.test('npm init <arg>', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['create-react-app@*'],
'should npx with listed packages'
)
},
})
await npm.exec('init', ['react-app'])
})
t.test('npm init <arg> -- other-args', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['create-react-app@*', 'my-path', '--some-option', 'some-value'],
'should npm exec with expected args'
)
},
})
await npm.exec('init', ['react-app', 'my-path', '--some-option', 'some-value'])
})
t.test('npm init @scope/name', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create-something@*'],
'should npx with scoped packages'
)
},
})
await npm.exec('init', ['@npmcli/something'])
})
t.test('npm init @scope@spec', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create@foo'],
'should npx with scoped packages'
)
},
})
await npm.exec('init', ['@npmcli@foo'])
})
t.test('npm init @scope/name@spec', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create-something@foo'],
'should npx with scoped packages'
)
},
})
await npm.exec('init', ['@npmcli/something@foo'])
})
t.test('npm init git spec', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['npm/create-something'],
'should npx with git-spec packages'
)
},
})
await npm.exec('init', ['npm/something'])
})
t.test('npm init @scope', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['@npmcli/create'],
'should npx with @scope/create pkgs'
)
},
})
await npm.exec('init', ['@npmcli'])
})
t.test('npm init tgz', async t => {
const { npm } = await mockNpm(t)
await t.rejects(
npm.exec('init', ['something.tgz']),
/Unrecognized initializer: something.tgz/,
'should throw error when using an unsupported spec'
)
})
t.test('npm init <arg>@next', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: ({ args }) => {
t.same(
args,
['create-something@next'],
'should npx with something@next'
)
},
})
await npm.exec('init', ['something@next'])
})
t.test('npm init exec error', async t => {
const { npm } = await mockNpm(t, {
libnpmexec: async () => {
throw new Error('ERROR')
},
})
await t.rejects(
npm.exec('init', ['something@next']),
/ERROR/,
'should exit with exec error'
)
})
t.test('should not rewrite flatOptions', async t => {
t.plan(1)
const { npm } = await mockNpm(t, {
libnpmexec: async ({ args }) => {
t.same(
args,
['create-react-app@*', 'my-app'],
'should npx with extra args'
)
},
})
await npm.exec('init', ['react-app', 'my-app'])
})
t.test('npm init cancel', async t => {
const { npm, logs } = await mockNpm(t, {
initPackageJson: async () => {
throw new Error('canceled')
},
})
await npm.exec('init', [])
t.equal(logs.warn[0], 'init canceled', 'should have init title and canceled')
})
t.test('npm init error', async t => {
const { npm } = await mockNpm(t, {
initPackageJson: async () => {
throw new Error('Unknown Error')
},
})
await t.rejects(
npm.exec('init', []),
/Unknown Error/,
'should throw error'
)
})
t.test('workspaces', async t => {
await t.test('no args -- yes', async t => {
const { npm, prefix, joinedOutput } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'top-level',
}),
},
config: { workspace: 'a', yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'a/package.json'))
t.equal(pkg.name, 'a')
t.equal(pkg.version, '1.0.0')
t.equal(pkg.license, 'ISC')
t.matchSnapshot(joinedOutput(), 'should print helper info')
const lock = require(resolve(prefix, 'package-lock.json'))
t.ok(lock.packages.a)
})
await t.test('no args, existing folder', async t => {
const { npm, prefix } = await mockNpm(t, {
prefixDir: {
packages: {
a: {
'package.json': JSON.stringify({
name: 'a',
version: '2.0.0',
}),
},
},
'package.json': JSON.stringify({
name: 'top-level',
workspaces: ['packages/a'],
}),
},
config: { workspace: 'packages/a', yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'packages/a/package.json'))
t.equal(pkg.name, 'a')
t.equal(pkg.version, '2.0.0')
t.equal(pkg.license, 'ISC')
})
await t.test('fail parsing top-level package.json to set workspace', async t => {
const { npm } = await mockNpm(t, {
prefixDir: {
'package.json': 'not json[',
},
config: { workspace: 'a', yes: true },
noLog: true,
})
await t.rejects(
npm.exec('init', []),
{ code: 'EJSONPARSE' }
)
})
await t.test('missing top-level package.json when settting workspace', async t => {
const { npm, logs } = await mockNpm(t, {
config: { workspace: 'a' },
})
await t.rejects(
npm.exec('init', []),
{ code: 'ENOENT' },
'should exit with missing package.json file error'
)
t.equal(logs.warn[0], 'init Missing package.json. Try with `--include-workspace-root`.')
})
await t.test('bad package.json when settting workspace', async t => {
const { npm, logs } = await mockNpm(t, {
prefixDir: {
'package.json': '{{{{',
},
config: { workspace: 'a' },
})
await t.rejects(
npm.exec('init', []),
{ code: 'EJSONPARSE' },
'should exit with parse file error'
)
t.strictSame(logs.warn, [])
})
await t.test('using args - no package.json', async t => {
const { npm, prefix } = await mockNpm(t, {
prefixDir: {
b: {
'package.json': JSON.stringify({
name: 'b',
}),
},
'package.json': JSON.stringify({
name: 'top-level',
workspaces: ['b'],
}),
},
// Important: exec did not write a package.json here
libnpmexec: async () => {},
config: { workspace: 'a', yes: true },
})
await npm.exec('init', ['react-app'])
const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(pkg.workspaces, ['b'], 'pkg workspaces did not get updated')
})
await t.test('init template - bad package.json', async t => {
const { npm, prefix } = await mockNpm(t, {
prefixDir: {
b: {
'package.json': JSON.stringify({
name: 'b',
}),
},
'package.json': JSON.stringify({
name: 'top-level',
workspaces: ['b'],
}),
},
initPackageJson: async (...args) => {
const [dir] = args
if (dir.endsWith('c')) {
await fs.writeFile(resolve(dir, 'package.json'), JSON.stringify({
name: basename(dir),
}), 'utf-8')
}
},
config: { yes: true, workspace: ['a', 'c'] },
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(pkg.workspaces, ['b', 'c'])
const lock = require(resolve(prefix, 'package-lock.json'))
t.notOk(lock.packages.a)
})
t.test('workspace root', async t => {
const { npm } = await mockNpm(t, {
config: { workspace: 'packages/a', 'include-workspace-root': true, yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(npm.localPrefix, 'package.json'))
t.equal(pkg.version, '1.0.0')
t.equal(pkg.license, 'ISC')
t.strictSame(pkg.workspaces, ['packages/a'])
const ws = require(resolve(npm.localPrefix, 'packages/a/package.json'))
t.equal(ws.version, '1.0.0')
t.equal(ws.license, 'ISC')
})
t.test('init pkg - installed workspace package', async t => {
const { npm } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'init-ws-test',
dependencies: {
'@npmcli/create': '1.0.0',
},
workspaces: ['test/workspace-init-a'],
}),
'test/workspace-init-a': {
'package.json': JSON.stringify({
version: '1.0.0',
name: '@npmcli/create',
bin: { 'init-create': 'index.js' },
}),
'index.js': `#!/usr/bin/env node
require('fs').writeFileSync('npm-init-test-success', '')
console.log('init-create ran')`,
},
},
})
await npm.exec('install', []) // reify
npm.config.set('workspace', ['test/workspace-init-b'])
await npm.exec('init', ['@npmcli'])
const exists = await fs.stat(nodePath.join(
npm.prefix, 'test/workspace-init-b', 'npm-init-test-success'))
t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
})
})
t.test('npm init with init-private config set', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true, 'init-private': true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.equal(pkg.private, true, 'should set private to true when init-private is set')
})
t.test('npm init does not set private by default', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(pkg.private, undefined, 'should not set private by default')
})
t.test('user‑set init-private IS forwarded', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true, 'init-private': true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(pkg.private, true, 'should set private to true when init-private is set')
})
t.test('user‑set init-private IS forwarded when false', async t => {
const { npm, prefix } = await mockNpm(t, {
config: { yes: true, 'init-private': false },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'package.json'))
t.strictSame(pkg.private, false, 'should set private to false when init-private is false')
})
t.test('No init-private is respected in workspaces', async t => {
const { npm, prefix } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'top-level',
}),
},
config: { workspace: 'a', yes: true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'a/package.json'))
t.strictSame(pkg.private, undefined, 'workspace package.json has no private field set')
})
t.test('init-private is respected in workspaces', async t => {
const { npm, prefix } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
name: 'top-level',
}),
},
config: { workspace: 'a', yes: true, 'init-private': true },
noLog: true,
})
await npm.exec('init', [])
const pkg = require(resolve(prefix, 'a/package.json'))
t.equal(pkg.private, true, 'workspace package.json has private field set')
})
t.test('create‑initializer path: init-private flag is forwarded via args', async t => {
const calls = []
const libexecStub = async opts => calls.push(opts)
const { npm } = await mockNpm(t, {
libnpmexec: libexecStub,
// user set the flag in their config
config: { yes: true, 'init-private': true },
noLog: true,
})
await npm.exec('init', ['create-bar'])
t.ok(calls[0].initPrivate, 'init-private included in options')
// Also verify the test for when isDefault returns true
calls.length = 0
npm.config.isDefault = () => true
await npm.exec('init', ['create-bar'])
t.equal(calls[0].initPrivate, undefined, 'init-private not included when using default')
})
t.test('create‑initializer path: false init-private is forwarded', async t => {
const calls = []
const libexecStub = async opts => calls.push(opts)
const { npm } = await mockNpm(t, {
libnpmexec: libexecStub,
// explicitly set to false
config: { yes: true, 'init-private': false },
noLog: true,
})
await npm.exec('init', ['create-baz'])
t.equal(calls[0].initPrivate, false, 'false init-private value is properly forwarded')
})