Skip to content

Commit 53e14a4

Browse files
committed
Add execFile
1 parent 692de47 commit 53e14a4

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/Node/ChildProcess.js

+13
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ exports.execImpl = function execImpl(command) {
3232
};
3333
};
3434
};
35+
exports.execFileImpl = function execImpl(command) {
36+
return function(args) {
37+
return function(opts) {
38+
return function(callback) {
39+
return function() {
40+
return require("child_process").execFile(command, args, opts, function(err, stdout, stderr) {
41+
callback(err)(stdout)(stderr)();
42+
});
43+
};
44+
};
45+
};
46+
};
47+
};
3548
exports.fork = function fork(cmd) {
3649
return function(args) {
3750
return function() {

src/Node/ChildProcess.purs

+44-16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Node.ChildProcess
3434
, SpawnOptions()
3535
, defaultSpawnOptions
3636
, exec
37+
, execFile
3738
, ExecOptions()
3839
, ExecResult()
3940
, defaultExecOptions
@@ -220,8 +221,10 @@ defaultSpawnOptions =
220221
, gid: Nothing
221222
}
222223

223-
-- | Similar to `spawn`, except that this variant will buffer output, and wait
224-
-- | until the process has exited before calling the callback.
224+
-- | Similar to `spawn`, except that this variant will:
225+
-- | * run the given command with the shell,
226+
-- | * buffer output, and wait until the process has exited before calling the
227+
-- | callback.
225228
-- |
226229
-- | Note that the child process will be killed if the amount of output exceeds
227230
-- | a certain threshold (the default is defined by Node.js).
@@ -231,28 +234,53 @@ exec :: forall eff.
231234
(ExecResult -> Eff (cp :: CHILD_PROCESS | eff) Unit) ->
232235
Eff (cp :: CHILD_PROCESS | eff) Unit
233236
exec cmd opts callback =
234-
execImpl cmd (convert opts) \err stdout' stderr' ->
235-
callback { error: (toMaybe err)
237+
execImpl cmd (convertExecOptions opts) \err stdout' stderr' ->
238+
callback { error: toMaybe err
236239
, stdout: stdout'
237240
, stderr: stderr'
238241
}
239-
where
240-
convert opts =
241-
{ cwd: fromMaybe undefined opts.cwd
242-
, env: fromMaybe undefined opts.env
243-
, timeout: fromMaybe undefined opts.timeout
244-
, maxBuffer: fromMaybe undefined opts.maxBuffer
245-
, killSignal: fromMaybe undefined opts.killSignal
246-
, uid: fromMaybe undefined opts.uid
247-
, gid: fromMaybe undefined opts.gid
248-
}
249242

250-
foreign import execImpl :: forall eff opts.
243+
foreign import execImpl :: forall eff.
251244
String ->
252-
{ | opts } ->
245+
ActualExecOptions ->
253246
(Nullable Exception.Error -> Buffer -> Buffer -> Eff (cp :: CHILD_PROCESS | eff) Unit) ->
254247
Eff (cp :: CHILD_PROCESS | eff) Unit
255248

249+
-- | Like `exec`, except instead of using a shell, it passes the arguments
250+
-- | directly to the specified command.
251+
execFile :: forall eff.
252+
String ->
253+
Array String ->
254+
ExecOptions ->
255+
(ExecResult -> Eff (cp :: CHILD_PROCESS | eff) Unit) ->
256+
Eff (cp :: CHILD_PROCESS | eff) Unit
257+
execFile cmd args opts callback =
258+
execFileImpl cmd args (convertExecOptions opts) \err stdout' stderr' ->
259+
callback { error: toMaybe err
260+
, stdout: stdout'
261+
, stderr: stderr'
262+
}
263+
264+
foreign import execFileImpl :: forall eff.
265+
String ->
266+
Array String ->
267+
ActualExecOptions ->
268+
(Nullable Exception.Error -> Buffer -> Buffer -> Eff (cp :: CHILD_PROCESS | eff) Unit) ->
269+
Eff (cp :: CHILD_PROCESS | eff) Unit
270+
271+
foreign import data ActualExecOptions :: *
272+
273+
convertExecOptions :: ExecOptions -> ActualExecOptions
274+
convertExecOptions opts = unsafeCoerce
275+
{ cwd: fromMaybe undefined opts.cwd
276+
, env: fromMaybe undefined opts.env
277+
, timeout: fromMaybe undefined opts.timeout
278+
, maxBuffer: fromMaybe undefined opts.maxBuffer
279+
, killSignal: fromMaybe undefined opts.killSignal
280+
, uid: fromMaybe undefined opts.uid
281+
, gid: fromMaybe undefined opts.gid
282+
}
283+
256284
type ExecOptions =
257285
{ cwd :: Maybe String
258286
, env :: Maybe (StrMap String)

0 commit comments

Comments
 (0)