@@ -29,19 +29,23 @@ module Node.ChildProcess
2929
3030import Prelude
3131
32+ import Control.Alt ((<|>))
33+ import Control.Bind ((>=>))
3234import Control.Monad.Eff (Eff ())
35+ import Control.Monad.Eff.Exception.Unsafe (unsafeThrow )
3336
3437import Data.StrMap (StrMap ())
3538import Data.Function (Fn2 (), runFn2 )
3639import Data.Nullable (Nullable (), toNullable , toMaybe )
3740import Data.Maybe (Maybe (..), fromMaybe )
3841import Data.Foreign (Foreign ())
42+ import Data.Posix (Pid ())
43+ import Data.Posix.Signal (Signal ())
44+ import Data.Posix.Signal as Signal
3945import Unsafe.Coerce (unsafeCoerce )
4046
4147import Node.FS as FS
42- import Node.Buffer (Buffer ())
4348import Node.Stream (Readable (), Writable (), Stream ())
44- import Node.ChildProcess.Signal (Signal ())
4549
4650-- | A handle for inter-process communication (IPC).
4751foreign import data Handle :: *
@@ -60,9 +64,9 @@ type ChildProcessRec =
6064 { stdin :: forall eff . Nullable (Writable () (cp :: CHILD_PROCESS | eff ))
6165 , stdout :: forall eff . Nullable (Readable () (cp :: CHILD_PROCESS | eff ))
6266 , stderr :: forall eff . Nullable (Readable () (cp :: CHILD_PROCESS | eff ))
63- , pid :: Int
67+ , pid :: Pid
6468 , connected :: Boolean
65- , kill :: Signal -> Boolean
69+ , kill :: String -> Boolean
6670 , send :: forall r . Fn2 { | r } Handle Boolean
6771 , disconnect :: forall eff . Eff eff Unit
6872 }
@@ -92,7 +96,7 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
9296
9397-- | The process ID of a child process. Note that if the process has already
9498-- | exited, another process may have taken the same ID, so be careful!
95- pid :: ChildProcess -> Int
99+ pid :: ChildProcess -> Pid
96100pid = _.pid <<< runChildProcess
97101
98102connected :: forall eff . ChildProcess -> Eff (cp :: CHILD_PROCESS | eff ) Boolean
@@ -108,7 +112,7 @@ disconnect = _.disconnect <<< runChildProcess
108112-- | that this function is called "kill", as sending a signal to a child
109113-- | process won't necessarily kill it.
110114kill :: forall eff . Signal -> ChildProcess -> Eff (cp :: CHILD_PROCESS | eff ) Boolean
111- kill sig (ChildProcess cp) = pure (cp.kill sig)
115+ kill sig (ChildProcess cp) = pure (cp.kill ( Signal .toString sig) )
112116
113117-- | Specifies how a child process exited; normally (with an exit code), or
114118-- | due to a signal.
@@ -129,24 +133,27 @@ type SpawnOptions =
129133 , gid :: Maybe Int
130134 }
131135
132- mkChildProcessExit :: Nullable Int -> Nullable Signal -> ChildProcessExit
136+ mkChildProcessExit :: Nullable Int -> Nullable String -> ChildProcessExit
133137mkChildProcessExit code signal =
134- case toMaybe code of
135- Just code -> Normally code
136- Nothing -> BySignal (unsafeCoerce signal)
138+ case fromCode code <|> fromSignal signal of
139+ Just e -> e
140+ Nothing -> unsafeThrow " Node.ChildProcess.mkChildProcessExit: Invalid arguments"
141+ where
142+ fromCode = toMaybe >>> map Normally
143+ fromSignal = toMaybe >=> Signal .fromString >>> map BySignal
137144
138145onExit :: forall eff . ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
139146onExit = mkOnExit mkChildProcessExit
140147
141148foreign import mkOnExit :: forall eff .
142- (Nullable Int -> Nullable Signal -> ChildProcessExit )
149+ (Nullable Int -> Nullable String -> ChildProcessExit )
143150 -> ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
144151
145152onClose :: forall eff . ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
146153onClose = mkOnClose mkChildProcessExit
147154
148155foreign import mkOnClose :: forall eff .
149- (Nullable Int -> Nullable Signal -> ChildProcessExit )
156+ (Nullable Int -> Nullable String -> ChildProcessExit )
150157 -> ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
151158
152159onMessage :: forall eff . ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit ) -> Eff eff Unit
0 commit comments