@@ -11,15 +11,19 @@ module Node.ChildProcess
1111 , send
1212 , disconnect
1313 , ChildProcessError ()
14+ , ChildProcessExit ()
1415 , onExit
1516 , onClose
1617 , onDisconnect
1718 , onMessage
1819 , onError
1920 , spawn
2021 , SpawnOptions ()
21- , StdIOBehaviour ()
2222 , defaultSpawnOptions
23+ , StdIOBehaviour (..)
24+ , pipe
25+ , inherit
26+ , ignore
2327 ) where
2428
2529import Prelude
@@ -28,7 +32,7 @@ import Control.Monad.Eff (Eff())
2832
2933import Data.StrMap (StrMap ())
3034import Data.Function (Fn2 (), runFn2 )
31- import Data.Nullable (Nullable (), toNullable )
35+ import Data.Nullable (Nullable (), toNullable , toMaybe )
3236import Data.Maybe (Maybe (..), fromMaybe )
3337import Data.Foreign (Foreign ())
3438import Unsafe.Coerce (unsafeCoerce )
@@ -97,6 +101,16 @@ disconnect = _.disconnect <<< runChildProcess
97101kill :: forall eff . Signal -> ChildProcess -> Eff (cp :: CHILD_PROCESS | eff ) Boolean
98102kill sig (ChildProcess cp) = pure (cp.kill sig)
99103
104+ -- | Specifies how a child process exited; normally (with an exit code), or
105+ -- | due to a signal.
106+ data ChildProcessExit
107+ = Normally Int
108+ | BySignal Signal
109+
110+ instance showChildProcessExit :: Show ChildProcessExit where
111+ show (Normally x) = " Normally " <> show x
112+ show (BySignal sig) = " BySignal " <> show sig
113+
100114type SpawnOptions =
101115 { cwd :: Maybe String
102116 , stdio :: Array (Maybe StdIOBehaviour )
@@ -106,19 +120,25 @@ type SpawnOptions =
106120 , gid :: Maybe Int
107121 }
108122
109- onExit :: forall eff . ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit ) -> Eff eff Unit
110- onExit = mkOnExit Nothing Just Signal
123+ mkChildProcessExit :: Nullable Int -> Nullable Signal -> ChildProcessExit
124+ mkChildProcessExit code signal =
125+ case toMaybe code of
126+ Just code -> Normally code
127+ Nothing -> BySignal (unsafeCoerce signal)
128+
129+ onExit :: forall eff . ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
130+ onExit = mkOnExit mkChildProcessExit
111131
112- foreign import mkOnExit :: forall a eff .
113- Maybe a -> ( a -> Maybe a ) -> ( String -> Signal ) ->
114- ChildProcess -> ( Maybe Int -> Maybe Signal -> Eff eff Unit ) -> Eff eff Unit
132+ foreign import mkOnExit :: forall eff .
133+ ( Nullable Int -> Nullable Signal -> ChildProcessExit )
134+ -> ChildProcess -> ( ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
115135
116- onClose :: forall eff . ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit ) -> Eff eff Unit
117- onClose = mkOnClose Nothing Just Signal
136+ onClose :: forall eff . ChildProcess -> (ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
137+ onClose = mkOnClose mkChildProcessExit
118138
119- foreign import mkOnClose :: forall a eff .
120- Maybe a -> ( a -> Maybe a ) -> ( String -> Signal ) ->
121- ChildProcess -> ( Maybe Int -> Maybe Signal -> Eff eff Unit ) -> Eff eff Unit
139+ foreign import mkOnClose :: forall eff .
140+ ( Nullable Int -> Nullable Signal -> ChildProcessExit )
141+ -> ChildProcess -> ( ChildProcessExit -> Eff eff Unit ) -> Eff eff Unit
122142
123143onMessage :: forall eff . ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit ) -> Eff eff Unit
124144onMessage = mkOnMessage Nothing Just
@@ -130,6 +150,10 @@ foreign import mkOnMessage :: forall a eff.
130150foreign import onDisconnect :: forall eff . ChildProcess -> Eff eff Unit -> Eff eff Unit
131151foreign import onError :: forall eff . ChildProcess -> (ChildProcessError -> Eff eff Unit ) -> Eff eff Unit
132152
153+ -- | Spawn a child process. Note that, in the event that a child process could
154+ -- | not be spawned (for example, if the executable was not found) this will
155+ -- | not throw an error. Instead, the `ChildProcess` will be created anyway,
156+ -- | but it will immediately emit an 'error' event.
133157spawn :: forall eff . String -> Array String -> SpawnOptions -> Eff (cp :: CHILD_PROCESS | eff ) ChildProcess
134158spawn cmd args opts = spawnImpl cmd args (convertOpts opts)
135159 where
0 commit comments