-
-
Notifications
You must be signed in to change notification settings - Fork 932
/
index.ts
69 lines (61 loc) · 2.27 KB
/
index.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
/// <reference path="../../../__typings__/index.d.ts"/>
import path from 'path'
import { runLifecycleHook, runPostinstallHooks } from '@pnpm/lifecycle'
import loadJsonFile from 'load-json-file'
import rimraf from '@zkochan/rimraf'
import { PnpmError } from '@pnpm/error'
const fixtures = path.join(__dirname, 'fixtures')
const rootModulesDir = path.join(__dirname, '..', 'node_modules')
test('runLifecycleHook()', async () => {
const pkgRoot = path.join(fixtures, 'simple')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('postinstall', pkg, {
depPath: '/simple/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect((await import(path.join(pkgRoot, 'output.json'))).default).toStrictEqual(['install'])
})
test('runLifecycleHook() escapes the args passed to the script', async () => {
const pkgRoot = path.join(fixtures, 'escape-args')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await runLifecycleHook('echo', pkg, {
depPath: '/escape-args/1.0.0',
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
args: ['Revert "feature (#1)"'],
})
expect((await import(path.join(pkgRoot, 'output.json'))).default).toStrictEqual(['Revert "feature (#1)"'])
})
test('runPostinstallHooks()', async () => {
const pkgRoot = path.join(fixtures, 'with-many-scripts')
await rimraf(path.join(pkgRoot, 'output.json'))
await runPostinstallHooks({
depPath: '/with-many-scripts/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect(loadJsonFile.sync(path.join(pkgRoot, 'output.json'))).toStrictEqual(['preinstall', 'install', 'postinstall'])
})
test('runLifecycleHook() should throw an error while missing script start or file server.js', async () => {
const pkgRoot = path.join(fixtures, 'without-scriptstart-serverjs')
const pkg = await import(path.join(pkgRoot, 'package.json'))
await expect(
runLifecycleHook('start', pkg, {
depPath: '/without-scriptstart-serverjs/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
).rejects.toThrow(new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js'))
})