Skip to content

Commit 7963ab5

Browse files
committed
fix: cleanup temp script after running
1 parent 24c5165 commit 7963ab5

File tree

3 files changed

+113
-68
lines changed

3 files changed

+113
-68
lines changed

Diff for: lib/make-spawn-args.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint camelcase: "off" */
22
const isWindows = require('./is-windows.js')
33
const setPATH = require('./set-path.js')
4-
const { chmodSync: chmod, writeFileSync: writeFile } = require('fs')
4+
const { chmodSync: chmod, unlinkSync: unlink, writeFileSync: writeFile } = require('fs')
55
const { tmpdir } = require('os')
66
const { resolve } = require('path')
77
const which = require('which')
@@ -55,7 +55,14 @@ const makeSpawnArgs = options => {
5555
...(isCmd ? { windowsVerbatimArguments: true } : {}),
5656
}
5757

58-
return [scriptShell, spawnArgs, spawnOpts]
58+
const cleanup = () => {
59+
// delete the script, this is just a best effort
60+
try {
61+
unlink(scriptFile)
62+
} catch (err) {}
63+
}
64+
65+
return [scriptShell, spawnArgs, spawnOpts, cleanup]
5966
}
6067

6168
module.exports = makeSpawnArgs

Diff for: lib/run-script-pkg.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const runScriptPkg = async options => {
5454
console.log(bruce(pkg._id, event, cmd))
5555
}
5656

57-
const p = promiseSpawn(...makeSpawnArgs({
57+
const [spawnShell, spawnArgs, spawnOpts, cleanup] = makeSpawnArgs({
5858
event,
5959
path,
6060
scriptShell,
@@ -63,7 +63,9 @@ const runScriptPkg = async options => {
6363
cmd,
6464
args,
6565
stdioString,
66-
}), {
66+
})
67+
68+
const p = promiseSpawn(spawnShell, spawnArgs, spawnOpts, {
6769
event,
6870
script: cmd,
6971
pkgid: pkg._id,
@@ -89,7 +91,7 @@ const runScriptPkg = async options => {
8991
} else {
9092
throw er
9193
}
92-
})
94+
}).finally(cleanup)
9395
}
9496

9597
module.exports = runScriptPkg

Diff for: test/make-spawn-args.js

+99-63
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ if (isWindows) {
6060
t.teardown(() => {
6161
whichPaths.delete('cmd')
6262
})
63-
t.match(makeSpawnArgs({
64-
event: 'event',
65-
path: 'path',
66-
cmd: 'script "quoted parameter"; second command',
67-
}), [
68-
'cmd',
69-
['/d', '/s', '/c', /\.cmd$/],
70-
{
63+
64+
t.test('simple script', (t) => {
65+
const [shell, args, opts, cleanup] = makeSpawnArgs({
66+
event: 'event',
67+
path: 'path',
68+
cmd: 'script "quoted parameter"; second command',
69+
})
70+
t.equal(shell, 'cmd', 'default shell applies')
71+
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
72+
t.match(opts, {
7173
env: {
7274
npm_package_json: /package\.json$/,
7375
npm_lifecycle_event: 'event',
@@ -77,23 +79,29 @@ if (isWindows) {
7779
stdio: undefined,
7880
cwd: 'path',
7981
windowsVerbatimArguments: true,
80-
},
81-
])
82+
}, 'got expected options')
8283

83-
// with a funky ComSpec
84-
process.env.ComSpec = 'blrorp'
85-
whichPaths.set('blrorp', '/bin/blrorp')
86-
t.teardown(() => {
87-
whichPaths.delete('blrorp')
84+
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
85+
cleanup()
86+
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')
87+
88+
t.end()
8889
})
89-
t.match(makeSpawnArgs({
90-
event: 'event',
91-
path: 'path',
92-
cmd: 'script "quoted parameter"; second command',
93-
}), [
94-
'blrorp',
95-
['-c', /\.sh$/],
96-
{
90+
91+
t.test('with a funky ComSpec', (t) => {
92+
process.env.ComSpec = 'blrorp'
93+
whichPaths.set('blrorp', '/bin/blrorp')
94+
t.teardown(() => {
95+
whichPaths.delete('blrorp')
96+
})
97+
const [shell, args, opts, cleanup] = makeSpawnArgs({
98+
event: 'event',
99+
path: 'path',
100+
cmd: 'script "quoted parameter"; second command',
101+
})
102+
t.equal(shell, 'blrorp', 'used ComSpec as default shell')
103+
t.match(args, ['-c', /\.sh$/], 'got expected args')
104+
t.match(opts, {
97105
env: {
98106
npm_package_json: /package\.json$/,
99107
npm_lifecycle_event: 'event',
@@ -102,19 +110,26 @@ if (isWindows) {
102110
stdio: undefined,
103111
cwd: 'path',
104112
windowsVerbatimArguments: undefined,
105-
},
106-
])
107-
108-
t.match(makeSpawnArgs({
109-
event: 'event',
110-
path: 'path',
111-
cmd: 'script',
112-
args: ['"quoted parameter";', 'second command'],
113-
scriptShell: 'cmd.exe',
114-
}), [
115-
'cmd.exe',
116-
['/d', '/s', '/c', /\.cmd$/],
117-
{
113+
}, 'got expected options')
114+
115+
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
116+
cleanup()
117+
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')
118+
119+
t.end()
120+
})
121+
122+
t.test('with cmd.exe as scriptShell', (t) => {
123+
const [shell, args, opts, cleanup] = makeSpawnArgs({
124+
event: 'event',
125+
path: 'path',
126+
cmd: 'script',
127+
args: ['"quoted parameter";', 'second command'],
128+
scriptShell: 'cmd.exe',
129+
})
130+
t.equal(shell, 'cmd.exe', 'kept cmd.exe')
131+
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
132+
t.match(opts, {
118133
env: {
119134
npm_package_json: /package\.json$/,
120135
npm_lifecycle_event: 'event',
@@ -123,8 +138,14 @@ if (isWindows) {
123138
stdio: undefined,
124139
cwd: 'path',
125140
windowsVerbatimArguments: true,
126-
},
127-
])
141+
}, 'got expected options')
142+
143+
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
144+
cleanup()
145+
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')
146+
147+
t.end()
148+
})
128149

129150
t.end()
130151
})
@@ -134,15 +155,17 @@ if (isWindows) {
134155
t.teardown(() => {
135156
whichPaths.delete('sh')
136157
})
137-
t.match(makeSpawnArgs({
138-
event: 'event',
139-
path: 'path',
140-
cmd: 'script',
141-
args: ['"quoted parameter";', 'second command'],
142-
}), [
143-
'sh',
144-
['-c', /\.sh$/],
145-
{
158+
159+
t.test('simple script', (t) => {
160+
const [shell, args, opts, cleanup] = makeSpawnArgs({
161+
event: 'event',
162+
path: 'path',
163+
cmd: 'script',
164+
args: ['"quoted parameter";', 'second command'],
165+
})
166+
t.equal(shell, 'sh', 'defaults to sh')
167+
t.match(args, ['-c', /\.sh$/], 'got expected args')
168+
t.match(opts, {
146169
env: {
147170
npm_package_json: /package\.json$/,
148171
npm_lifecycle_event: 'event',
@@ -151,20 +174,27 @@ if (isWindows) {
151174
stdio: undefined,
152175
cwd: 'path',
153176
windowsVerbatimArguments: undefined,
154-
},
155-
])
156-
157-
// test that we can explicitly run in cmd.exe, even on posix systems
158-
// relevant for running under WSL
159-
t.match(makeSpawnArgs({
160-
event: 'event',
161-
path: 'path',
162-
cmd: 'script "quoted parameter"; second command',
163-
scriptShell: 'cmd.exe',
164-
}), [
165-
'cmd.exe',
166-
['/d', '/s', '/c', /\.cmd$/],
167-
{
177+
}, 'got expected options')
178+
179+
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
180+
cleanup()
181+
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')
182+
183+
t.end()
184+
})
185+
186+
t.test('can use cmd.exe', (t) => {
187+
// test that we can explicitly run in cmd.exe, even on posix systems
188+
// relevant for running under WSL
189+
const [shell, args, opts, cleanup] = makeSpawnArgs({
190+
event: 'event',
191+
path: 'path',
192+
cmd: 'script "quoted parameter"; second command',
193+
scriptShell: 'cmd.exe',
194+
})
195+
t.equal(shell, 'cmd.exe', 'kept cmd.exe')
196+
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
197+
t.match(opts, {
168198
env: {
169199
npm_package_json: /package\.json$/,
170200
npm_lifecycle_event: 'event',
@@ -173,8 +203,14 @@ if (isWindows) {
173203
stdio: undefined,
174204
cwd: 'path',
175205
windowsVerbatimArguments: true,
176-
},
177-
])
206+
}, 'got expected options')
207+
208+
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
209+
cleanup()
210+
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')
211+
212+
t.end()
213+
})
178214

179215
t.end()
180216
})

0 commit comments

Comments
 (0)