13
13
module Node.ChildProcess
14
14
( Handle
15
15
, ChildProcess
16
- , CHILD_PROCESS
17
16
, stderr
18
17
, stdout
19
18
, stdin
@@ -48,18 +47,19 @@ module Node.ChildProcess
48
47
import Prelude
49
48
50
49
import Control.Alt ((<|>))
51
- import Control.Monad.Eff ( kind Effect , Eff )
52
- import Control.Monad.Eff .Exception as Exception
53
- import Control.Monad.Eff .Exception.Unsafe (unsafeThrow )
50
+ import Effect ( Effect )
51
+ import Effect .Exception as Exception
52
+ import Effect .Exception.Unsafe (unsafeThrow )
54
53
55
- import Data.Foreign (Foreign )
56
54
import Data.Function.Uncurried (Fn2 , runFn2 )
57
55
import Data.Maybe (Maybe (..), fromMaybe )
58
56
import Data.Nullable (Nullable , toNullable , toMaybe )
59
57
import Data.Posix (Pid , Gid , Uid )
60
58
import Data.Posix.Signal (Signal )
61
59
import Data.Posix.Signal as Signal
62
- import Data.StrMap (StrMap )
60
+
61
+ import Foreign (Foreign )
62
+ import Foreign.Object (Object )
63
63
64
64
import Node.Buffer (Buffer )
65
65
import Node.FS as FS
@@ -70,9 +70,6 @@ import Unsafe.Coerce (unsafeCoerce)
70
70
-- | A handle for inter-process communication (IPC).
71
71
foreign import data Handle :: Type
72
72
73
- -- | The effect for creating and interacting with child processes.
74
- foreign import data CHILD_PROCESS :: Effect
75
-
76
73
newtype ChildProcess = ChildProcess ChildProcessRec
77
74
78
75
runChildProcess :: ChildProcess -> ChildProcessRec
@@ -81,29 +78,29 @@ runChildProcess (ChildProcess r) = r
81
78
-- | Note: some of these types are lies, and so it is unsafe to access some of
82
79
-- | these record fields directly.
83
80
type ChildProcessRec =
84
- { stdin :: forall eff . Nullable (Writable () ( cp :: CHILD_PROCESS | eff ))
85
- , stdout :: forall eff . Nullable (Readable () ( cp :: CHILD_PROCESS | eff ))
86
- , stderr :: forall eff . Nullable (Readable () ( cp :: CHILD_PROCESS | eff ))
81
+ { stdin :: Nullable (Writable ())
82
+ , stdout :: Nullable (Readable ())
83
+ , stderr :: Nullable (Readable ())
87
84
, pid :: Pid
88
85
, connected :: Boolean
89
86
, kill :: String -> Boolean
90
87
, send :: forall r . Fn2 { | r } Handle Boolean
91
- , disconnect :: forall eff . Eff eff Unit
88
+ , disconnect :: Effect Unit
92
89
}
93
90
94
91
-- | The standard input stream of a child process. Note that this is only
95
92
-- | available if the process was spawned with the stdin option set to "pipe".
96
- stdin :: forall eff . ChildProcess -> Writable () ( cp :: CHILD_PROCESS | eff )
93
+ stdin :: ChildProcess -> Writable ()
97
94
stdin = unsafeFromNullable (missingStream " stdin" ) <<< _.stdin <<< runChildProcess
98
95
99
96
-- | The standard output stream of a child process. Note that this is only
100
97
-- | available if the process was spawned with the stdout option set to "pipe".
101
- stdout :: forall eff . ChildProcess -> Readable () ( cp :: CHILD_PROCESS | eff )
98
+ stdout :: ChildProcess -> Readable ()
102
99
stdout = unsafeFromNullable (missingStream " stdout" ) <<< _.stdout <<< runChildProcess
103
100
104
101
-- | The standard error stream of a child process. Note that this is only
105
102
-- | available if the process was spawned with the stderr option set to "pipe".
106
- stderr :: forall eff . ChildProcess -> Readable () ( cp :: CHILD_PROCESS | eff )
103
+ stderr :: ChildProcess -> Readable ()
107
104
stderr = unsafeFromNullable (missingStream " stderr" ) <<< _.stderr <<< runChildProcess
108
105
109
106
missingStream :: String -> String
@@ -119,23 +116,23 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
119
116
pid :: ChildProcess -> Pid
120
117
pid = _.pid <<< runChildProcess
121
118
122
- connected :: forall eff . ChildProcess -> Eff ( cp :: CHILD_PROCESS | eff ) Boolean
123
- connected (ChildProcess cp) = mkEff \_ -> cp.connected
119
+ connected :: ChildProcess -> Effect Boolean
120
+ connected (ChildProcess cp) = mkEffect \_ -> cp.connected
124
121
125
- send :: forall eff props . { | props } -> Handle -> ChildProcess -> Eff ( cp :: CHILD_PROCESS | eff ) Boolean
126
- send msg handle (ChildProcess cp) = mkEff \_ -> runFn2 cp.send msg handle
122
+ send :: forall props . { | props } -> Handle -> ChildProcess -> Effect Boolean
123
+ send msg handle (ChildProcess cp) = mkEffect \_ -> runFn2 cp.send msg handle
127
124
128
- disconnect :: forall eff . ChildProcess -> Eff ( cp :: CHILD_PROCESS | eff ) Unit
125
+ disconnect :: ChildProcess -> Effect Unit
129
126
disconnect = _.disconnect <<< runChildProcess
130
127
131
128
-- | Send a signal to a child process. It's an unfortunate historical decision
132
129
-- | that this function is called "kill", as sending a signal to a child
133
130
-- | process won't necessarily kill it.
134
- kill :: forall eff . Signal -> ChildProcess -> Eff ( cp :: CHILD_PROCESS | eff ) Boolean
135
- kill sig (ChildProcess cp) = mkEff \_ -> cp.kill (Signal .toString sig)
131
+ kill :: Signal -> ChildProcess -> Effect Boolean
132
+ kill sig (ChildProcess cp) = mkEffect \_ -> cp.kill (Signal .toString sig)
136
133
137
- mkEff :: forall eff a . (Unit -> a ) -> Eff eff a
138
- mkEff = unsafeCoerce
134
+ mkEffect :: forall a . (Unit -> a ) -> Effect a
135
+ mkEffect = unsafeCoerce
139
136
140
137
-- | Specifies how a child process exited; normally (with an exit code), or
141
138
-- | due to a signal.
@@ -156,45 +153,43 @@ mkExit code signal =
156
153
fromCode = toMaybe >>> map Normally
157
154
fromSignal = toMaybe >=> Signal .fromString >>> map BySignal
158
155
159
- onExit :: forall eff . ChildProcess -> (Exit -> Eff eff Unit ) -> Eff eff Unit
156
+ onExit :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
160
157
onExit = mkOnExit mkExit
161
158
162
159
foreign import mkOnExit
163
- :: forall eff
164
- . (Nullable Int -> Nullable String -> Exit )
160
+ :: (Nullable Int -> Nullable String -> Exit )
165
161
-> ChildProcess
166
- -> (Exit -> Eff eff Unit )
167
- -> Eff eff Unit
162
+ -> (Exit -> Effect Unit )
163
+ -> Effect Unit
168
164
169
- onClose :: forall eff . ChildProcess -> (Exit -> Eff eff Unit ) -> Eff eff Unit
165
+ onClose :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
170
166
onClose = mkOnClose mkExit
171
167
172
168
foreign import mkOnClose
173
- :: forall eff
174
- . (Nullable Int -> Nullable String -> Exit )
169
+ :: (Nullable Int -> Nullable String -> Exit )
175
170
-> ChildProcess
176
- -> (Exit -> Eff eff Unit )
177
- -> Eff eff Unit
171
+ -> (Exit -> Effect Unit )
172
+ -> Effect Unit
178
173
179
- onMessage :: forall eff . ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit ) -> Eff eff Unit
174
+ onMessage :: ChildProcess -> (Foreign -> Maybe Handle -> Effect Unit ) -> Effect Unit
180
175
onMessage = mkOnMessage Nothing Just
181
176
182
177
foreign import mkOnMessage
183
- :: forall a eff
178
+ :: forall a
184
179
. Maybe a
185
180
-> (a -> Maybe a )
186
181
-> ChildProcess
187
- -> (Foreign -> Maybe Handle -> Eff eff Unit )
188
- -> Eff eff Unit
182
+ -> (Foreign -> Maybe Handle -> Effect Unit )
183
+ -> Effect Unit
189
184
190
- foreign import onDisconnect :: forall eff . ChildProcess -> Eff eff Unit -> Eff eff Unit
191
- foreign import onError :: forall eff . ChildProcess -> (Error -> Eff eff Unit ) -> Eff eff Unit
185
+ foreign import onDisconnect :: ChildProcess -> Effect Unit -> Effect Unit
186
+ foreign import onError :: ChildProcess -> (Error -> Effect Unit ) -> Effect Unit
192
187
193
188
-- | Spawn a child process. Note that, in the event that a child process could
194
189
-- | not be spawned (for example, if the executable was not found) this will
195
190
-- | not throw an error. Instead, the `ChildProcess` will be created anyway,
196
191
-- | but it will immediately emit an 'error' event.
197
- spawn :: forall eff . String -> Array String -> SpawnOptions -> Eff ( cp :: CHILD_PROCESS | eff ) ChildProcess
192
+ spawn :: String -> Array String -> SpawnOptions -> Effect ChildProcess
198
193
spawn cmd args = spawnImpl cmd args <<< convertOpts
199
194
where
200
195
convertOpts opts =
@@ -206,15 +201,15 @@ spawn cmd args = spawnImpl cmd args <<< convertOpts
206
201
, gid: fromMaybe undefined opts.gid
207
202
}
208
203
209
- foreign import spawnImpl :: forall opts eff . String -> Array String -> { | opts } -> Eff ( cp :: CHILD_PROCESS | eff ) ChildProcess
204
+ foreign import spawnImpl :: forall opts . String -> Array String -> { | opts } -> Effect ChildProcess
210
205
211
206
-- There's gotta be a better way.
212
207
foreign import undefined :: forall a . a
213
208
214
209
type SpawnOptions =
215
210
{ cwd :: Maybe String
216
211
, stdio :: Array (Maybe StdIOBehaviour )
217
- , env :: Maybe (StrMap String )
212
+ , env :: Maybe (Object String )
218
213
, detached :: Boolean
219
214
, uid :: Maybe Uid
220
215
, gid :: Maybe Gid
@@ -238,11 +233,10 @@ defaultSpawnOptions =
238
233
-- | Note that the child process will be killed if the amount of output exceeds
239
234
-- | a certain threshold (the default is defined by Node.js).
240
235
exec
241
- :: forall eff
242
- . String
236
+ :: String
243
237
-> ExecOptions
244
- -> (ExecResult -> Eff ( cp :: CHILD_PROCESS | eff ) Unit )
245
- -> Eff ( cp :: CHILD_PROCESS | eff ) Unit
238
+ -> (ExecResult -> Effect Unit )
239
+ -> Effect Unit
246
240
exec cmd opts callback =
247
241
execImpl cmd (convertExecOptions opts) \err stdout' stderr' ->
248
242
callback
@@ -252,21 +246,19 @@ exec cmd opts callback =
252
246
}
253
247
254
248
foreign import execImpl
255
- :: forall eff
256
- . String
249
+ :: String
257
250
-> ActualExecOptions
258
- -> (Nullable Exception.Error -> Buffer -> Buffer -> Eff ( cp :: CHILD_PROCESS | eff ) Unit )
259
- -> Eff ( cp :: CHILD_PROCESS | eff ) Unit
251
+ -> (Nullable Exception.Error -> Buffer -> Buffer -> Effect Unit )
252
+ -> Effect Unit
260
253
261
254
-- | Like `exec`, except instead of using a shell, it passes the arguments
262
255
-- | directly to the specified command.
263
256
execFile
264
- :: forall eff
265
- . String
257
+ :: String
266
258
-> Array String
267
259
-> ExecOptions
268
- -> (ExecResult -> Eff ( cp :: CHILD_PROCESS | eff ) Unit )
269
- -> Eff ( cp :: CHILD_PROCESS | eff ) Unit
260
+ -> (ExecResult -> Effect Unit )
261
+ -> Effect Unit
270
262
execFile cmd args opts callback =
271
263
execFileImpl cmd args (convertExecOptions opts) \err stdout' stderr' ->
272
264
callback
@@ -276,12 +268,11 @@ execFile cmd args opts callback =
276
268
}
277
269
278
270
foreign import execFileImpl
279
- :: forall eff
280
- . String
271
+ :: String
281
272
-> Array String
282
273
-> ActualExecOptions
283
- -> (Nullable Exception.Error -> Buffer -> Buffer -> Eff ( cp :: CHILD_PROCESS | eff ) Unit )
284
- -> Eff ( cp :: CHILD_PROCESS | eff ) Unit
274
+ -> (Nullable Exception.Error -> Buffer -> Buffer -> Effect Unit )
275
+ -> Effect Unit
285
276
286
277
foreign import data ActualExecOptions :: Type
287
278
@@ -298,7 +289,7 @@ convertExecOptions opts = unsafeCoerce
298
289
299
290
type ExecOptions =
300
291
{ cwd :: Maybe String
301
- , env :: Maybe (StrMap String )
292
+ , env :: Maybe (Object String )
302
293
, timeout :: Maybe Number
303
294
, maxBuffer :: Maybe Int
304
295
, killSignal :: Maybe Signal
@@ -326,7 +317,7 @@ type ExecResult =
326
317
-- | A special case of `spawn` for creating Node.js child processes. The first
327
318
-- | argument is the module to be run, and the second is the argv (command line
328
319
-- | arguments).
329
- foreign import fork :: forall eff . String -> Array String -> Eff ( cp :: CHILD_PROCESS | eff ) ChildProcess
320
+ foreign import fork :: String -> Array String -> Effect ChildProcess
330
321
331
322
-- | An error which occurred inside a child process.
332
323
type Error =
@@ -336,7 +327,7 @@ type Error =
336
327
}
337
328
338
329
-- | Convert a ChildProcess.Error to a standard Error, which can then be thrown
339
- -- | inside an Eff or Aff computation (for example).
330
+ -- | inside an Effect or Aff computation (for example).
340
331
toStandardError :: Error -> Exception.Error
341
332
toStandardError = unsafeCoerce
342
333
@@ -355,7 +346,7 @@ toStandardError = unsafeCoerce
355
346
data StdIOBehaviour
356
347
= Pipe
357
348
| Ignore
358
- | ShareStream (forall r eff . Stream r eff )
349
+ | ShareStream (forall r . Stream r )
359
350
| ShareFD FS.FileDescriptor
360
351
361
352
-- | Create pipes for each of the three standard IO streams.
0 commit comments