forked from npm/run-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-script.js
132 lines (123 loc) · 3.34 KB
/
run-script.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
const t = require('tap')
const spawk = require('spawk')
const runScript = require('..')
t.test('run-script', async t => {
const emptyDir = t.testdir({})
await t.test('no package provided, local package read', async t => {
spawk.spawn(/.*/, a => a.includes('echo test'))
const testdir = t.testdir({
'package.json': JSON.stringify({
name: '@npmcli/run-script-test-package',
scripts: {
test: 'echo test',
},
}),
})
await t.resolves(() => runScript({
path: testdir,
event: 'test',
}))
t.ok(spawk.done())
})
await t.test('package provided, skip look up', async t => {
spawk.spawn(/.*/, a => a.includes('echo test'))
await t.resolves(() => runScript({
pkg: {
name: '@npmcli/run-script-test-package',
scripts: {
test: 'echo test',
},
},
path: emptyDir,
event: 'test',
}))
t.ok(spawk.done())
})
await t.test('non-install event, pkg has no scripts, early exit', async t => {
const res = await runScript({
event: 'foo',
path: emptyDir,
pkg: {},
})
t.strictSame(res, { code: 0, signal: null })
})
await t.test('non-install event, pkg does not have requested script', async t => {
const res = await runScript({
event: 'foo',
path: emptyDir,
pkg: {
scripts: {},
},
})
t.strictSame(res, { code: 0, signal: null })
})
await t.test('install event, pkg has no scripts, early exit', async t => {
const res = await runScript({
event: 'install',
path: emptyDir,
pkg: {},
})
t.strictSame(res, { code: 0, signal: null })
})
await t.test('start event, pkg has no scripts, no server.js', async t => {
const res = await runScript({
event: 'start',
path: emptyDir,
pkg: {},
})
t.strictSame(res, { code: 0, signal: null })
})
await t.test('start event, pkg has server.js but no start script', async t => {
const path = t.testdir({ 'server.js': '' })
spawk.spawn(/.*/, a => a.includes('node server.js'))
const res = await runScript({
event: 'start',
path,
pkg: {
_id: '@npmcli/run-script-test@1.2.3',
scripts: {},
},
})
t.match(res, {
event: 'start',
script: 'node server.js',
pkgid: '@npmcli/run-script-test@1.2.3',
})
})
await t.test('pkg does not have requested script, with custom cmd', async t => {
spawk.spawn(/.*/, a => a.includes('testcmd'))
const res = await runScript({
event: 'foo',
cmd: 'testcmd',
path: emptyDir,
pkg: {
scripts: {},
},
})
t.match(res, {
event: 'foo',
script: 'testcmd',
code: 0,
signal: null,
})
t.ok(spawk.done())
})
})
t.test('isServerPackage', async t => {
await t.test('is server package', async t => {
const testdir = t.testdir({
'server.js': '',
})
await t.resolves(runScript.isServerPackage(testdir), true)
})
await t.test('is not server package - no server.js', async t => {
const testdir = t.testdir({})
await t.resolves(runScript.isServerPackage(testdir), false)
})
await t.test('is not server package - invalid server.js', async t => {
const testdir = t.testdir({
'server.js': {},
})
await t.resolves(runScript.isServerPackage(testdir), false)
})
})