-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
lifecycleScripts.ts
719 lines (597 loc) · 26.6 KB
/
lifecycleScripts.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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import * as path from 'path'
import { promises as fs } from 'fs'
import { assertProject } from '@pnpm/assert-project'
import { LifecycleLog } from '@pnpm/core-loggers'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import {
addDependenciesToPackage,
install,
mutateModulesInSingleProject,
MutatedProject,
mutateModules,
} from '@pnpm/core'
import rimraf from '@zkochan/rimraf'
import isWindows from 'is-windows'
import loadJsonFile from 'load-json-file'
import exists from 'path-exists'
import PATH from 'path-name'
import sinon from 'sinon'
import { testDefaults } from '../utils'
const testOnNonWindows = isWindows() ? test.skip : test
test('run pre/postinstall scripts', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'],
await testDefaults({ fastUnpack: false, targetDependenciesField: 'devDependencies' })
)
{
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-prepare.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeTruthy()
const generatedByPreinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall')
expect(typeof generatedByPreinstall).toBe('function')
const generatedByPostinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall')
expect(typeof generatedByPostinstall).toBe('function')
}
await rimraf('node_modules')
// testing that the packages are not installed even though they are in lockfile
// and that their scripts are not tried to be executed
await install(manifest, await testDefaults({ fastUnpack: false, production: true }))
{
const generatedByPreinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall')
expect(typeof generatedByPreinstall).toBe('function')
const generatedByPostinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall')
expect(typeof generatedByPostinstall).toBe('function')
}
const lockfile = await project.readLockfile()
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild)
})
test('run pre/postinstall scripts, when PnP is used and no symlinks', async () => {
prepareEmpty()
await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'],
await testDefaults({
fastUnpack: false,
enablePnp: true,
symlink: false,
targetDependenciesField: 'devDependencies',
})
)
const pkgDir = 'node_modules/.pnpm/@pnpm.e2e+pre-and-postinstall-scripts-example@1.0.0/node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example'
expect(await exists(path.resolve(pkgDir, 'generated-by-prepare.js'))).toBeFalsy()
expect(await exists(path.resolve(pkgDir, 'generated-by-preinstall.js'))).toBeTruthy()
expect(await exists(path.resolve(pkgDir, 'generated-by-postinstall.js'))).toBeTruthy()
})
test('testing that the bins are linked when the package with the bins was already in node_modules', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/hello-world-js-bin'], await testDefaults({ fastUnpack: false }))
await addDependenciesToPackage(manifest, ['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'], await testDefaults({ fastUnpack: false, targetDependenciesField: 'devDependencies' }))
const generatedByPreinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall')
expect(typeof generatedByPreinstall).toBe('function')
const generatedByPostinstall = project.requireModule('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall')
expect(typeof generatedByPostinstall).toBe('function')
})
test('run install scripts', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, ['@pnpm.e2e/install-script-example'], await testDefaults({ fastUnpack: false }))
const generatedByInstall = project.requireModule('@pnpm.e2e/install-script-example/generated-by-install')
expect(typeof generatedByInstall).toBe('function')
})
test('run install scripts in the current project', async () => {
prepareEmpty()
const manifest = await addDependenciesToPackage({
scripts: {
'pnpm:devPreinstall': 'node -e "require(\'fs\').writeFileSync(\'test.txt\', \'\', \'utf-8\')"',
install: 'node -e "process.stdout.write(\'install\')" | json-append output.json',
postinstall: 'node -e "process.stdout.write(\'postinstall\')" | json-append output.json',
preinstall: 'node -e "process.stdout.write(\'preinstall\')" | json-append output.json',
},
}, ['json-append@1.1.1'], await testDefaults({ fastUnpack: false }))
await install(manifest, await testDefaults({ fastUnpack: false }))
const output = await loadJsonFile<string[]>('output.json')
expect(output).toStrictEqual(['preinstall', 'install', 'postinstall'])
expect(await exists('test.txt')).toBeTruthy()
})
test('run install scripts in the current project when its name is different than its directory', async () => {
prepareEmpty()
const manifest = await addDependenciesToPackage({
name: 'different-name',
scripts: {
install: 'node -e "process.stdout.write(\'install\')" | json-append output.json',
postinstall: 'node -e "process.stdout.write(\'postinstall\')" | json-append output.json',
preinstall: 'node -e "process.stdout.write(\'preinstall\')" | json-append output.json',
},
}, ['json-append@1.1.1'], await testDefaults({ fastUnpack: false }))
await install(manifest, await testDefaults({ fastUnpack: false }))
const output = await loadJsonFile('output.json')
expect(output).toStrictEqual(['preinstall', 'install', 'postinstall'])
})
test('installation fails if lifecycle script fails', async () => {
prepareEmpty()
await expect(
install({
scripts: {
preinstall: 'exit 1',
},
}, await testDefaults({ fastUnpack: false }))
).rejects.toThrow(/@ preinstall: `exit 1`/)
})
test('INIT_CWD is always set to lockfile directory', async () => {
prepareEmpty()
const rootDir = process.cwd()
await fs.mkdir('subd')
process.chdir('subd')
await mutateModulesInSingleProject({
mutation: 'install',
manifest: {
dependencies: {
'json-append': '1.1.1',
'@pnpm.e2e/write-lifecycle-env': '1.0.0',
},
scripts: {
install: 'node -e "process.stdout.write(process.env.INIT_CWD)" | json-append output.json',
},
},
rootDir,
}, await testDefaults({
fastUnpack: false,
lockfileDir: rootDir,
}))
const childEnv = await loadJsonFile<{ INIT_CWD: string }>(path.join(rootDir, 'node_modules/@pnpm.e2e/write-lifecycle-env/env.json'))
expect(childEnv.INIT_CWD).toBe(rootDir)
const output = await loadJsonFile(path.join(rootDir, 'output.json'))
expect(output).toStrictEqual([process.cwd()])
})
// TODO: duplicate this test to @pnpm/lifecycle
test("reports child's output", async () => {
prepareEmpty()
const reporter = sinon.spy()
await addDependenciesToPackage({}, ['@pnpm.e2e/count-to-10'], await testDefaults({ fastUnpack: false, reporter }))
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/count-to-10/1.0.0',
level: 'debug',
name: 'pnpm:lifecycle',
script: 'node postinstall',
stage: 'postinstall',
} as LifecycleLog)).toBeTruthy()
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/count-to-10/1.0.0',
level: 'debug',
line: '1',
name: 'pnpm:lifecycle',
stage: 'postinstall',
stdio: 'stdout',
} as LifecycleLog)).toBeTruthy()
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/count-to-10/1.0.0',
level: 'debug',
line: '2',
name: 'pnpm:lifecycle',
stage: 'postinstall',
stdio: 'stdout',
} as LifecycleLog)).toBeTruthy()
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/count-to-10/1.0.0',
level: 'debug',
line: '6',
name: 'pnpm:lifecycle',
stage: 'postinstall',
stdio: 'stderr',
} as LifecycleLog)).toBeTruthy()
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/count-to-10/1.0.0',
exitCode: 0,
level: 'debug',
name: 'pnpm:lifecycle',
stage: 'postinstall',
} as LifecycleLog)).toBeTruthy()
})
test("reports child's close event", async () => {
prepareEmpty()
const reporter = sinon.spy()
await expect(
addDependenciesToPackage({}, ['@pnpm.e2e/failing-postinstall'], await testDefaults({ reporter }))
).rejects.toThrow()
expect(reporter.calledWithMatch({
depPath: '/@pnpm.e2e/failing-postinstall/1.0.0',
exitCode: 1,
level: 'debug',
name: 'pnpm:lifecycle',
stage: 'postinstall',
} as LifecycleLog)).toBeTruthy()
})
testOnNonWindows('lifecycle scripts have access to node-gyp', async () => {
prepareEmpty()
// `npm test` adds node-gyp to the PATH
// it is removed here to test that pnpm adds it
const initialPath = process.env[PATH]
if (typeof initialPath !== 'string') throw new Error('PATH is not defined')
process.env[PATH] = initialPath
.split(path.delimiter)
.filter((p: string) => !p.includes('node-gyp-bin') &&
!p.includes(`${path.sep}npm${path.sep}`) &&
!p.includes(`${path.sep}.npm${path.sep}`))
.join(path.delimiter)
await addDependenciesToPackage({}, ['drivelist@5.1.8'], await testDefaults({ fastUnpack: false }))
process.env[PATH] = initialPath
})
test('run lifecycle scripts of dependent packages after running scripts of their deps', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, ['@pnpm.e2e/with-postinstall-a'], await testDefaults({ fastUnpack: false }))
expect(+project.requireModule('.pnpm/@pnpm.e2e+with-postinstall-b@1.0.0/node_modules/@pnpm.e2e/with-postinstall-b/output.json')[0] < +project.requireModule('@pnpm.e2e/with-postinstall-a/output.json')[0]).toBeTruthy()
})
test('run prepare script for git-hosted dependencies', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, ['pnpm/test-git-fetch#299c6d89507571462b992b92407a8a07663e32ee'], await testDefaults({ fastUnpack: false }))
const scripts = project.requireModule('test-git-fetch/output.json')
expect(scripts).toStrictEqual([
'preinstall',
'install',
'postinstall',
'prepare',
'prepublishOnly',
'preinstall',
'install',
'postinstall',
])
const lockfile = await project.readLockfile()
expect(lockfile.packages['github.com/pnpm/test-git-fetch/299c6d89507571462b992b92407a8a07663e32ee'].prepare === true).toBeTruthy()
})
test('lifecycle scripts run before linking bins', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/generated-bins'], await testDefaults({ fastUnpack: false }))
await project.isExecutable('.bin/cmd1')
await project.isExecutable('.bin/cmd2')
await rimraf('node_modules')
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ frozenLockfile: true }))
await project.isExecutable('.bin/cmd1')
await project.isExecutable('.bin/cmd2')
})
test('hoisting does not fail on commands that will be created by lifecycle scripts on a later stage', async () => {
prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/has-generated-bins-as-dep'], await testDefaults({ fastUnpack: false, hoistPattern: '*' }))
// await project.isExecutable('.pnpm/node_modules/.bin/cmd1')
// await project.isExecutable('.pnpm/node_modules/.bin/cmd2')
// Testing the same with headless installation
await rimraf('node_modules')
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ frozenLockfile: true, hoistPattern: '*' }))
// await project.isExecutable('.pnpm/node_modules/.bin/cmd1')
// await project.isExecutable('.pnpm/node_modules/.bin/cmd2')
})
test('bins are linked even if lifecycle scripts are ignored', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage(
{},
[
'@pnpm.e2e/pkg-with-peer-having-bin',
'@pnpm.e2e/peer-with-bin',
'@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0',
],
await testDefaults({ fastUnpack: false, ignoreScripts: true })
)
await project.isExecutable('.bin/peer-with-bin')
await project.isExecutable('@pnpm.e2e/pkg-with-peer-having-bin/node_modules/.bin/hello-world-js-bin')
// Verifying that the scripts were ignored
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/package.json')).toBeTruthy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
await rimraf('node_modules')
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ frozenLockfile: true, ignoreScripts: true }))
await project.isExecutable('.bin/peer-with-bin')
await project.isExecutable('@pnpm.e2e/pkg-with-peer-having-bin/node_modules/.bin/hello-world-js-bin')
// Verifying that the scripts were ignored
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/package.json')).toBeTruthy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
})
test('dependency should not be added to current lockfile if it was not built successfully during headless install', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage(
{},
[
'package-that-cannot-be-installed@0.0.0', // TODO: this package should be replaced
],
await testDefaults({
ignoreScripts: true,
lockfileOnly: true,
})
)
await expect(
mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ frozenLockfile: true }))
).rejects.toThrow()
expect(await project.readCurrentLockfile()).toBeFalsy()
})
test('scripts have access to unlisted bins when hoisting is used', async () => {
const project = prepareEmpty()
await addDependenciesToPackage(
{},
['@pnpm.e2e/pkg-that-calls-unlisted-dep-in-hooks'],
await testDefaults({ fastUnpack: false, hoistPattern: '*' })
)
expect(project.requireModule('@pnpm.e2e/pkg-that-calls-unlisted-dep-in-hooks/output.json')).toStrictEqual(['Hello world!'])
})
test('selectively ignore scripts in some dependencies by neverBuiltDependencies', async () => {
const project = prepareEmpty()
const neverBuiltDependencies = ['@pnpm.e2e/pre-and-postinstall-scripts-example']
const manifest = await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0', '@pnpm.e2e/install-script-example'],
await testDefaults({ fastUnpack: false, neverBuiltDependencies })
)
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/install-script-example/generated-by-install.js')).toBeTruthy()
const lockfile = await project.readLockfile()
expect(lockfile.neverBuiltDependencies).toStrictEqual(neverBuiltDependencies)
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBe(undefined)
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBeTruthy()
await rimraf('node_modules')
await install(manifest, await testDefaults({ fastUnpack: false, frozenLockfile: true, neverBuiltDependencies }))
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/install-script-example/generated-by-install.js')).toBeTruthy()
})
test('throw an exception when both neverBuiltDependencies and onlyBuiltDependencies are used', async () => {
prepareEmpty()
await expect(
addDependenciesToPackage(
{},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0'],
await testDefaults({ onlyBuiltDependencies: ['@pnpm.e2e/foo'], neverBuiltDependencies: ['@pnpm.e2e/bar'] })
)
).rejects.toThrow(/Cannot have both/)
})
test('selectively allow scripts in some dependencies by onlyBuiltDependencies', async () => {
const project = prepareEmpty()
const onlyBuiltDependencies = ['@pnpm.e2e/install-script-example']
const manifest = await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0', '@pnpm.e2e/install-script-example'],
await testDefaults({ fastUnpack: false, onlyBuiltDependencies })
)
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/install-script-example/generated-by-install.js')).toBeTruthy()
const lockfile = await project.readLockfile()
expect(lockfile.onlyBuiltDependencies).toStrictEqual(onlyBuiltDependencies)
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBe(undefined)
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBe(true)
await rimraf('node_modules')
await install(manifest, await testDefaults({ fastUnpack: false, frozenLockfile: true, onlyBuiltDependencies }))
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')).toBeFalsy()
expect(await exists('node_modules/@pnpm.e2e/install-script-example/generated-by-install.js')).toBeTruthy()
})
test('lockfile is updated if neverBuiltDependencies is changed', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0', '@pnpm.e2e/install-script-example'],
await testDefaults({ fastUnpack: false })
)
{
const lockfile = await project.readLockfile()
expect(lockfile.neverBuiltDependencies).toBeFalsy()
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBeTruthy()
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBeTruthy()
}
const neverBuiltDependencies = ['@pnpm.e2e/pre-and-postinstall-scripts-example']
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ neverBuiltDependencies }))
{
const lockfile = await project.readLockfile()
expect(lockfile.neverBuiltDependencies).toStrictEqual(neverBuiltDependencies)
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBe(undefined)
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBeTruthy()
}
})
test('lockfile is updated if onlyBuiltDependencies is changed', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({},
['@pnpm.e2e/pre-and-postinstall-scripts-example@1.0.0', '@pnpm.e2e/install-script-example'],
await testDefaults({ fastUnpack: false })
)
{
const lockfile = await project.readLockfile()
expect(lockfile.onlyBuiltDependencies).toBeFalsy()
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBeTruthy()
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBeTruthy()
}
const onlyBuiltDependencies: string[] = []
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ onlyBuiltDependencies }))
{
const lockfile = await project.readLockfile()
expect(lockfile.onlyBuiltDependencies).toStrictEqual(onlyBuiltDependencies)
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBe(undefined)
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBe(undefined)
}
onlyBuiltDependencies.push('@pnpm.e2e/pre-and-postinstall-scripts-example')
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ onlyBuiltDependencies }))
{
const lockfile = await project.readLockfile()
expect(lockfile.onlyBuiltDependencies).toStrictEqual(onlyBuiltDependencies)
expect(lockfile.packages['/@pnpm.e2e/pre-and-postinstall-scripts-example/1.0.0'].requiresBuild).toBe(true)
expect(lockfile.packages['/@pnpm.e2e/install-script-example/1.0.0'].requiresBuild).toBe(undefined)
}
})
test('lifecycle scripts have access to package\'s own binary by binary name', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({},
['@pnpm.e2e/runs-own-bin'],
await testDefaults({ fastUnpack: false })
)
await project.isExecutable('.pnpm/@pnpm.e2e+runs-own-bin@1.0.0/node_modules/@pnpm.e2e/runs-own-bin/node_modules/.bin/runs-own-bin')
})
test('lifecycle scripts run after linking root dependencies', async () => {
prepareEmpty()
const manifest = {
dependencies: {
'is-positive': '1.0.0',
'@pnpm.e2e/postinstall-requires-is-positive': '1.0.0',
},
}
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ fastUnpack: false }))
await rimraf('node_modules')
await mutateModulesInSingleProject({
manifest,
mutation: 'install',
rootDir: process.cwd(),
}, await testDefaults({ fastUnpack: false, frozenLockfile: true }))
// if there was no exception, the test passed
})
test('ignore-dep-scripts', async () => {
prepareEmpty()
const manifest = {
scripts: {
'pnpm:devPreinstall': 'node -e "require(\'fs\').writeFileSync(\'test.txt\', \'\', \'utf-8\')"',
install: 'node -e "process.stdout.write(\'install\')" | json-append output.json',
postinstall: 'node -e "process.stdout.write(\'postinstall\')" | json-append output.json',
preinstall: 'node -e "process.stdout.write(\'preinstall\')" | json-append output.json',
},
dependencies: {
'json-append': '1.1.1',
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1.0.0',
},
}
await install(manifest, await testDefaults({ fastUnpack: false, ignoreDepScripts: true }))
{
const output = await loadJsonFile<string[]>('output.json')
expect(output).toStrictEqual(['preinstall', 'install', 'postinstall'])
expect(await exists('test.txt')).toBeTruthy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
}
await rimraf('node_modules')
await rimraf('output.json')
await install(manifest, await testDefaults({ fastUnpack: false, ignoreDepScripts: true }))
{
const output = await loadJsonFile<string[]>('output.json')
expect(output).toStrictEqual(['preinstall', 'install', 'postinstall'])
expect(await exists('test.txt')).toBeTruthy()
expect(await exists('node_modules/@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')).toBeFalsy()
}
})
test('run pre/postinstall scripts in a workspace that uses node-linker=hoisted', async () => {
const projects = preparePackages([
{
location: 'project-1',
package: { name: 'project-1' },
},
{
location: 'project-2',
package: { name: 'project-2' },
},
{
location: 'project-3',
package: { name: 'project-3' },
},
{
location: 'project-4',
package: { name: 'project-4' },
},
])
const importers: MutatedProject[] = [
{
mutation: 'install',
rootDir: path.resolve('project-1'),
},
{
mutation: 'install',
rootDir: path.resolve('project-2'),
},
{
mutation: 'install',
rootDir: path.resolve('project-3'),
},
{
mutation: 'install',
rootDir: path.resolve('project-4'),
},
]
const allProjects = [
{
buildIndex: 0,
manifest: {
name: 'project-1',
version: '1.0.0',
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1',
},
},
rootDir: path.resolve('project-1'),
},
{
buildIndex: 0,
manifest: {
name: 'project-2',
version: '1.0.0',
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1',
},
},
rootDir: path.resolve('project-2'),
},
{
buildIndex: 0,
manifest: {
name: 'project-3',
version: '1.0.0',
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '2',
},
},
rootDir: path.resolve('project-3'),
},
{
buildIndex: 0,
manifest: {
name: 'project-4',
version: '1.0.0',
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '2',
},
},
rootDir: path.resolve('project-4'),
},
]
await mutateModules(importers, await testDefaults({
allProjects,
fastUnpack: false,
nodeLinker: 'hoisted',
}))
const rootProject = assertProject(process.cwd())
await rootProject.has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')
await rootProject.has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')
await projects['project-1'].hasNot('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')
await projects['project-1'].hasNot('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')
await projects['project-2'].hasNot('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')
await projects['project-2'].hasNot('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')
await projects['project-3'].has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')
await projects['project-3'].has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')
await projects['project-4'].has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-preinstall.js')
await projects['project-4'].has('@pnpm.e2e/pre-and-postinstall-scripts-example/generated-by-postinstall.js')
})