Skip to content

Commit aa95081

Browse files
jyh1kritzcreek
authored andcommitted
add execSync and execFileSync
1 parent 5e0e74f commit aa95081

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

src/Node/ChildProcess.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ exports.execImpl = function execImpl (command) {
3131
};
3232
};
3333

34+
3435
exports.execFileImpl = function execImpl (command) {
3536
return function (args) {
3637
return function (opts) {
@@ -45,6 +46,24 @@ exports.execFileImpl = function execImpl (command) {
4546
};
4647
};
4748

49+
exports.execSyncImpl = function execSyncImpl (command) {
50+
return function (opts) {
51+
return function () {
52+
return require('child_process').execSync(command, opts);
53+
};
54+
};
55+
};
56+
57+
exports.execFileSyncImpl = function execFileSyncImpl (command) {
58+
return function (args) {
59+
return function (opts) {
60+
return function () {
61+
return require('child_process').execFileSync(command, args, opts);
62+
};
63+
};
64+
};
65+
};
66+
4867
exports.fork = function fork (cmd) {
4968
return function (args) {
5069
return function () {

src/Node/ChildProcess.purs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ module Node.ChildProcess
3737
, ExecOptions
3838
, ExecResult
3939
, defaultExecOptions
40+
, execSync
41+
, execFileSync
42+
, ExecSyncOptions
43+
, defaultExecSyncOptions
4044
, fork
4145
, StdIOBehaviour(..)
4246
, pipe
@@ -47,24 +51,20 @@ module Node.ChildProcess
4751
import Prelude
4852

4953
import Control.Alt ((<|>))
50-
import Effect (Effect)
51-
import Effect.Exception as Exception
52-
import Effect.Exception.Unsafe (unsafeThrow)
53-
5454
import Data.Function.Uncurried (Fn2, runFn2)
5555
import Data.Maybe (Maybe(..), fromMaybe)
5656
import Data.Nullable (Nullable, toNullable, toMaybe)
5757
import Data.Posix (Pid, Gid, Uid)
5858
import Data.Posix.Signal (Signal)
5959
import Data.Posix.Signal as Signal
60-
60+
import Effect (Effect)
61+
import Effect.Exception as Exception
62+
import Effect.Exception.Unsafe (unsafeThrow)
6163
import Foreign (Foreign)
6264
import Foreign.Object (Object)
63-
6465
import Node.Buffer (Buffer)
6566
import Node.FS as FS
6667
import Node.Stream (Readable, Writable, Stream)
67-
6868
import Unsafe.Coerce (unsafeCoerce)
6969

7070
-- | A handle for inter-process communication (IPC).
@@ -314,6 +314,79 @@ type ExecResult =
314314
, error :: Maybe Exception.Error
315315
}
316316

317+
-- | Generally identical to `exec`, with the exception that
318+
-- | the method will not return until the child process has fully closed.
319+
-- | Returns: The stdout from the command.
320+
execSync
321+
:: String
322+
-> ExecSyncOptions
323+
-> Effect Buffer
324+
execSync cmd opts =
325+
execSyncImpl cmd (convertExecSyncOptions opts)
326+
327+
foreign import execSyncImpl
328+
:: String
329+
-> ActualExecSyncOptions
330+
-> Effect Buffer
331+
332+
-- | Generally identical to `execFile`, with the exception that
333+
-- | the method will not return until the child process has fully closed.
334+
-- | Returns: The stdout from the command.
335+
execFileSync
336+
:: String
337+
-> Array String
338+
-> ExecSyncOptions
339+
-> Effect Buffer
340+
execFileSync cmd args opts =
341+
execFileSyncImpl cmd args (convertExecSyncOptions opts)
342+
343+
foreign import execFileSyncImpl
344+
:: String
345+
-> Array String
346+
-> ActualExecSyncOptions
347+
-> Effect Buffer
348+
349+
foreign import data ActualExecSyncOptions :: Type
350+
351+
convertExecSyncOptions :: ExecSyncOptions -> ActualExecSyncOptions
352+
convertExecSyncOptions opts = unsafeCoerce
353+
{ cwd: fromMaybe undefined opts.cwd
354+
, input: fromMaybe undefined opts.input
355+
, stdio: toActualStdIOOptions opts.stdio
356+
, env: fromMaybe undefined opts.env
357+
, timeout: fromMaybe undefined opts.timeout
358+
, maxBuffer: fromMaybe undefined opts.maxBuffer
359+
, killSignal: fromMaybe undefined opts.killSignal
360+
, uid: fromMaybe undefined opts.uid
361+
, gid: fromMaybe undefined opts.gid
362+
}
363+
364+
type ExecSyncOptions =
365+
{ cwd :: Maybe String
366+
, input :: Maybe String
367+
, stdio :: Array (Maybe StdIOBehaviour)
368+
, env :: Maybe (Object String)
369+
, timeout :: Maybe Number
370+
, maxBuffer :: Maybe Int
371+
, killSignal :: Maybe Signal
372+
, uid :: Maybe Uid
373+
, gid :: Maybe Gid
374+
}
375+
376+
defaultExecSyncOptions :: ExecSyncOptions
377+
defaultExecSyncOptions =
378+
{ cwd: Nothing
379+
, input: Nothing
380+
, stdio: pipe
381+
, env: Nothing
382+
, timeout: Nothing
383+
, maxBuffer: Nothing
384+
, killSignal: Nothing
385+
, uid: Nothing
386+
, gid: Nothing
387+
}
388+
389+
317390
-- | A special case of `spawn` for creating Node.js child processes. The first
318391
-- | argument is the module to be run, and the second is the argv (command line
319392
-- | arguments).

test/Main.purs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ module Test.Main where
22

33
import Prelude
44

5+
import Data.Maybe (Maybe(..))
6+
import Data.Posix.Signal (Signal(..))
57
import Effect (Effect)
68
import Effect.Console (log)
7-
8-
import Data.Posix.Signal (Signal(..))
9-
109
import Node.Buffer as Buffer
11-
import Node.ChildProcess (Exit(..), defaultExecOptions, exec, onError, defaultSpawnOptions, spawn, stdout, onExit, kill)
10+
import Node.ChildProcess (Exit(..), defaultExecOptions, exec, defaultExecSyncOptions, execSync, onError, defaultSpawnOptions, spawn, stdout, onExit, kill)
1211
import Node.Encoding (Encoding(UTF8))
12+
import Node.Encoding as NE
1313
import Node.Stream (onData)
1414

1515
main :: Effect Unit
@@ -60,3 +60,9 @@ execLs :: Effect Unit
6060
execLs = do
6161
exec "ls >&2" defaultExecOptions \r ->
6262
log "redirected to stderr:" *> (Buffer.toString UTF8 r.stderr >>= log)
63+
64+
execSyncEcho :: String -> Effect Unit
65+
execSyncEcho str = do
66+
resBuf <- execSync "cat" (defaultExecSyncOptions {input = Just str})
67+
res <- Buffer.toString NE.UTF8 resBuf
68+
log res

0 commit comments

Comments
 (0)