99-- | ```
1010-- |
1111-- | The [Node.js documentation](https://nodejs.org/api/child_process.html)
12- -- | will probably also be useful to read if you want to use this module.
12+ -- | forms the basis for this module and has in-depth documentation about
13+ -- | runtime behaviour.
1314module Node.ChildProcess
1415 ( Handle
1516 , ChildProcess
16- , stderr
17- , stdout
1817 , stdin
18+ , stdout
19+ , stderr
1920 , pid
2021 , connected
2122 , kill
@@ -70,6 +71,8 @@ import Unsafe.Coerce (unsafeCoerce)
7071-- | A handle for inter-process communication (IPC).
7172foreign import data Handle :: Type
7273
74+ -- | Opaque type returned by `spawn`, `fork` and `exec`.
75+ -- | Needed as input for most methods in this module.
7376newtype ChildProcess = ChildProcess ChildProcessRec
7477
7578runChildProcess :: ChildProcess -> ChildProcessRec
@@ -116,12 +119,24 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
116119pid :: ChildProcess -> Pid
117120pid = _.pid <<< runChildProcess
118121
122+ -- | Indicates whether it is still possible to send and receive
123+ -- | messages from the child process.
119124connected :: ChildProcess -> Effect Boolean
120125connected (ChildProcess cp) = mkEffect \_ -> cp.connected
121126
122- send :: forall props . { | props } -> Handle -> ChildProcess -> Effect Boolean
127+ -- | Send messages to the (`nodejs`) child process.
128+ -- |
129+ -- | See the [node documentation](https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback)
130+ -- | for in-depth documentation.
131+ send
132+ :: forall props
133+ . { | props }
134+ -> Handle
135+ -> ChildProcess
136+ -> Effect Boolean
123137send msg handle (ChildProcess cp) = mkEffect \_ -> runFn2 cp.send msg handle
124138
139+ -- | Closes the IPC channel between parent and child.
125140disconnect :: ChildProcess -> Effect Unit
126141disconnect = _.disconnect <<< runChildProcess
127142
@@ -130,8 +145,8 @@ disconnect = _.disconnect <<< runChildProcess
130145-- | sending a signal to a child process won't necessarily kill it.
131146-- |
132147-- | The resulting effects of this function depend on the process
133- -- | and the signal and can vary from system to system.
134- -- | The child process might emit an "error" event if the signal
148+ -- | and the signal. They can vary from system to system.
149+ -- | The child process might emit an ` "error"` event if the signal
135150-- | could not be delivered.
136151kill :: Signal -> ChildProcess -> Effect Unit
137152kill sig (ChildProcess cp) = mkEffect \_ -> cp.kill (Signal .toString sig)
@@ -158,7 +173,11 @@ mkExit code signal =
158173 fromCode = toMaybe >>> map Normally
159174 fromSignal = toMaybe >=> Signal .fromString >>> map BySignal
160175
161- onExit :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
176+ -- | Handle the `"exit"` signal.
177+ onExit
178+ :: ChildProcess
179+ -> (Exit -> Effect Unit )
180+ -> Effect Unit
162181onExit = mkOnExit mkExit
163182
164183foreign import mkOnExit
@@ -167,7 +186,11 @@ foreign import mkOnExit
167186 -> (Exit -> Effect Unit )
168187 -> Effect Unit
169188
170- onClose :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
189+ -- | Handle the `"close"` signal.
190+ onClose
191+ :: ChildProcess
192+ -> (Exit -> Effect Unit )
193+ -> Effect Unit
171194onClose = mkOnClose mkExit
172195
173196foreign import mkOnClose
@@ -176,7 +199,11 @@ foreign import mkOnClose
176199 -> (Exit -> Effect Unit )
177200 -> Effect Unit
178201
179- onMessage :: ChildProcess -> (Foreign -> Maybe Handle -> Effect Unit ) -> Effect Unit
202+ -- | Handle the `"message"` signal.
203+ onMessage
204+ :: ChildProcess
205+ -> (Foreign -> Maybe Handle -> Effect Unit )
206+ -> Effect Unit
180207onMessage = mkOnMessage Nothing Just
181208
182209foreign import mkOnMessage
@@ -187,14 +214,27 @@ foreign import mkOnMessage
187214 -> (Foreign -> Maybe Handle -> Effect Unit )
188215 -> Effect Unit
189216
190- foreign import onDisconnect :: ChildProcess -> Effect Unit -> Effect Unit
191- foreign import onError :: ChildProcess -> (Error -> Effect Unit ) -> Effect Unit
217+ -- | Handle the `"disconnect"` signal.
218+ foreign import onDisconnect
219+ :: ChildProcess
220+ -> Effect Unit
221+ -> Effect Unit
222+
223+ -- | Handle the `"error"` signal.
224+ foreign import onError
225+ :: ChildProcess
226+ -> (Error -> Effect Unit )
227+ -> Effect Unit
192228
193229-- | Spawn a child process. Note that, in the event that a child process could
194230-- | not be spawned (for example, if the executable was not found) this will
195231-- | not throw an error. Instead, the `ChildProcess` will be created anyway,
196232-- | but it will immediately emit an 'error' event.
197- spawn :: String -> Array String -> SpawnOptions -> Effect ChildProcess
233+ spawn
234+ :: String
235+ -> Array String
236+ -> SpawnOptions
237+ -> Effect ChildProcess
198238spawn cmd args = spawnImpl cmd args <<< convertOpts
199239 where
200240 convertOpts opts =
@@ -206,11 +246,18 @@ spawn cmd args = spawnImpl cmd args <<< convertOpts
206246 , gid: fromMaybe undefined opts.gid
207247 }
208248
209- foreign import spawnImpl :: forall opts . String -> Array String -> { | opts } -> Effect ChildProcess
249+ foreign import spawnImpl
250+ :: forall opts
251+ . String
252+ -> Array String
253+ -> { | opts }
254+ -> Effect ChildProcess
210255
211256-- There's gotta be a better way.
212257foreign import undefined :: forall a . a
213258
259+ -- | Configuration of `spawn`. Fields set to `Nothing` will use
260+ -- | the node defaults.
214261type SpawnOptions =
215262 { cwd :: Maybe String
216263 , stdio :: Array (Maybe StdIOBehaviour )
@@ -220,6 +267,8 @@ type SpawnOptions =
220267 , gid :: Maybe Gid
221268 }
222269
270+ -- | A default set of `SpawnOptions`. Everything is set to `Nothing`,
271+ -- | `detached` is `false` and `stdio` is `ChildProcess.pipe`.
223272defaultSpawnOptions :: SpawnOptions
224273defaultSpawnOptions =
225274 { cwd: Nothing
@@ -292,6 +341,8 @@ convertExecOptions opts = unsafeCoerce
292341 , gid: fromMaybe undefined opts.gid
293342 }
294343
344+ -- | Configuration of `exec`. Fields set to `Nothing`
345+ -- | will use the node defaults.
295346type ExecOptions =
296347 { cwd :: Maybe String
297348 , env :: Maybe (Object String )
@@ -302,6 +353,7 @@ type ExecOptions =
302353 , gid :: Maybe Gid
303354 }
304355
356+ -- | A default set of `ExecOptions`. Everything is set to `Nothing`.
305357defaultExecOptions :: ExecOptions
306358defaultExecOptions =
307359 { cwd: Nothing
@@ -313,6 +365,7 @@ defaultExecOptions =
313365 , gid: Nothing
314366 }
315367
368+ -- | The combined output of a process calld with `exec`.
316369type ExecResult =
317370 { stderr :: Buffer
318371 , stdout :: Buffer
@@ -391,11 +444,13 @@ defaultExecSyncOptions =
391444 , gid: Nothing
392445 }
393446
394-
395447-- | A special case of `spawn` for creating Node.js child processes. The first
396448-- | argument is the module to be run, and the second is the argv (command line
397449-- | arguments).
398- foreign import fork :: String -> Array String -> Effect ChildProcess
450+ foreign import fork
451+ :: String
452+ -> Array String
453+ -> Effect ChildProcess
399454
400455-- | An error which occurred inside a child process.
401456type Error =
@@ -431,7 +486,8 @@ data StdIOBehaviour
431486pipe :: Array (Maybe StdIOBehaviour )
432487pipe = map Just [Pipe , Pipe , Pipe ]
433488
434- -- | Share stdin with stdin, stdout with stdout, and stderr with stderr.
489+ -- | Share `stdin` with `stdin`, `stdout` with `stdout`,
490+ -- | and `stderr` with `stderr`.
435491inherit :: Array (Maybe StdIOBehaviour )
436492inherit = map Just
437493 [ ShareStream process.stdin
@@ -445,6 +501,9 @@ foreign import process :: forall props. { | props }
445501ignore :: Array (Maybe StdIOBehaviour )
446502ignore = map Just [Ignore , Ignore , Ignore ]
447503
504+
505+ -- Helpers
506+
448507foreign import data ActualStdIOBehaviour :: Type
449508
450509toActualStdIOBehaviour :: StdIOBehaviour -> ActualStdIOBehaviour
0 commit comments