-
-
Notifications
You must be signed in to change notification settings - Fork 938
/
run.ts
148 lines (119 loc) · 4.21 KB
/
run.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
import { promises as fs } from 'fs'
import path from 'path'
import prepare, { preparePackages } from '@pnpm/prepare'
import { execPnpm, execPnpmSync } from './utils'
const RECORD_ARGS_FILE = 'require(\'fs\').writeFileSync(\'args.json\', JSON.stringify(require(\'./args.json\').concat([process.argv.slice(2)])), \'utf8\')'
test('run -r: pass the args to the command that is specfied in the build script', async () => {
preparePackages([{
name: 'project',
scripts: {
foo: 'node recordArgs',
postfoo: 'node recordArgs',
prefoo: 'node recordArgs',
},
}])
await fs.writeFile('project/args.json', '[]', 'utf8')
await fs.writeFile('project/recordArgs.js', RECORD_ARGS_FILE, 'utf8')
await execPnpm(['run', '-r', '--config.enable-pre-post-scripts', 'foo', 'arg', '--flag=true'])
const { default: args } = await import(path.resolve('project/args.json'))
expect(args).toStrictEqual([
[],
['arg', '--flag=true'],
[],
])
})
test('run: pass the args to the command that is specfied in the build script', async () => {
prepare({
name: 'project',
scripts: {
foo: 'node recordArgs',
postfoo: 'node recordArgs',
prefoo: 'node recordArgs',
},
})
await fs.writeFile('args.json', '[]', 'utf8')
await fs.writeFile('recordArgs.js', RECORD_ARGS_FILE, 'utf8')
await execPnpm(['run', 'foo', 'arg', '--flag=true'])
const { default: args } = await import(path.resolve('args.json'))
expect(args).toStrictEqual([
['arg', '--flag=true'],
])
})
// Before pnpm v7, `--` was required to pass flags to a build script.
test('run: handle -- in a backwards compatible manner', async () => {
prepare({
name: 'project',
scripts: {
foo: 'node recordArgs',
postfoo: 'node recordArgs',
prefoo: 'node recordArgs',
},
})
await fs.writeFile('args.json', '[]', 'utf8')
await fs.writeFile('recordArgs.js', RECORD_ARGS_FILE, 'utf8')
await execPnpm(['run', 'foo', 'arg', '--', '--flag=true'])
const { default: args } = await import(path.resolve('args.json'))
expect(args).toStrictEqual([
['arg', '--flag=true'],
])
})
test('run: pass -- to the build script if specified twice', async () => {
prepare({
name: 'project',
scripts: {
foo: 'node recordArgs',
postfoo: 'node recordArgs',
prefoo: 'node recordArgs',
},
})
await fs.writeFile('args.json', '[]', 'utf8')
await fs.writeFile('recordArgs.js', RECORD_ARGS_FILE, 'utf8')
await execPnpm(['run', 'foo', '--', 'arg', '--', '--flag=true'])
const { default: args } = await import(path.resolve('args.json'))
expect(args).toStrictEqual([
['arg', '--', '--flag=true'],
])
})
test('test -r: pass the args to the command that is specfied in the build script of a package.json manifest', async () => {
preparePackages([{
name: 'project',
scripts: {
test: 'ts-node test',
},
}])
const result = execPnpmSync(['test', '-r', 'arg', '--', '--flag=true'])
expect((result.stdout as Buffer).toString('utf8')).toMatch(/ts-node test "arg" "--flag=true"/)
})
test('start: run "node server.js" by default', async () => {
prepare({}, { manifestFormat: 'YAML' })
await fs.writeFile('server.js', 'console.log("Hello world!")', 'utf8')
const result = execPnpmSync(['start'])
expect((result.stdout as Buffer).toString('utf8')).toMatch(/Hello world!/)
})
test('install-test: install dependencies and runs tests', async () => {
prepare({
dependencies: {
'json-append': '1',
},
scripts: {
test: 'node -e "process.stdout.write(\'test\')" | json-append ./output.json',
},
}, { manifestFormat: 'JSON5' })
await execPnpm(['install-test'])
const { default: scriptsRan } = await import(path.resolve('output.json'))
expect(scriptsRan).toStrictEqual(['test'])
})
test('silent run only prints the output of the child process', async () => {
prepare({
scripts: {
hi: 'echo hi && exit 1',
},
})
const result = execPnpmSync(['run', '--silent', 'hi'])
expect(result.stdout.toString().trim()).toBe('hi')
})
test('silent dlx prints the output of the child process only', async () => {
prepare({})
const result = execPnpmSync(['--silent', 'dlx', 'shx', 'echo', 'hi'])
expect(result.stdout.toString().trim()).toBe('hi')
})