Skip to content

Commit d4442ce

Browse files
authored
Merge pull request #13 from justinwoo/compiler/0.12
changes for 0.12
2 parents 5c09f73 + 3e4fd53 commit d4442ce

File tree

3 files changed

+72
-93
lines changed

3 files changed

+72
-93
lines changed

bower.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
"package.json"
1717
],
1818
"dependencies": {
19-
"purescript-exceptions": "^3.0.0",
20-
"purescript-foreign": "^4.0.0",
21-
"purescript-functions": "^3.0.0",
22-
"purescript-maps": "^3.0.0",
23-
"purescript-node-fs": "^4.0.0",
24-
"purescript-node-streams": "^3.0.0",
25-
"purescript-nullable": "^3.0.0",
26-
"purescript-posix-types": "^3.0.0",
27-
"purescript-unsafe-coerce": "^3.0.0"
19+
"purescript-exceptions": "#compiler/0.12",
20+
"purescript-foreign": "#compiler/0.12",
21+
"purescript-functions": "#compiler/0.12",
22+
"purescript-node-fs": "#compiler/0.12",
23+
"purescript-node-streams": "#compiler/0.12",
24+
"purescript-nullable": "#compiler/0.12",
25+
"purescript-posix-types": "#compiler/0.12",
26+
"purescript-unsafe-coerce": "#compiler/0.12",
27+
"purescript-foreign-object": "#compiler/0.12"
2828
},
2929
"devDependencies": {
30-
"purescript-console": "^3.0.0"
30+
"purescript-console": "#compiler/0.12"
3131
}
3232
}

src/Node/ChildProcess.purs

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
module Node.ChildProcess
1414
( Handle
1515
, ChildProcess
16-
, CHILD_PROCESS
1716
, stderr
1817
, stdout
1918
, stdin
@@ -48,18 +47,19 @@ module Node.ChildProcess
4847
import Prelude
4948

5049
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)
5453

55-
import Data.Foreign (Foreign)
5654
import Data.Function.Uncurried (Fn2, runFn2)
5755
import Data.Maybe (Maybe(..), fromMaybe)
5856
import Data.Nullable (Nullable, toNullable, toMaybe)
5957
import Data.Posix (Pid, Gid, Uid)
6058
import Data.Posix.Signal (Signal)
6159
import Data.Posix.Signal as Signal
62-
import Data.StrMap (StrMap)
60+
61+
import Foreign (Foreign)
62+
import Foreign.Object (Object)
6363

6464
import Node.Buffer (Buffer)
6565
import Node.FS as FS
@@ -70,9 +70,6 @@ import Unsafe.Coerce (unsafeCoerce)
7070
-- | A handle for inter-process communication (IPC).
7171
foreign import data Handle :: Type
7272

73-
-- | The effect for creating and interacting with child processes.
74-
foreign import data CHILD_PROCESS :: Effect
75-
7673
newtype ChildProcess = ChildProcess ChildProcessRec
7774

7875
runChildProcess :: ChildProcess -> ChildProcessRec
@@ -81,29 +78,29 @@ runChildProcess (ChildProcess r) = r
8178
-- | Note: some of these types are lies, and so it is unsafe to access some of
8279
-- | these record fields directly.
8380
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 ())
8784
, pid :: Pid
8885
, connected :: Boolean
8986
, kill :: String -> Boolean
9087
, send :: forall r. Fn2 { | r} Handle Boolean
91-
, disconnect :: forall eff. Eff eff Unit
88+
, disconnect :: Effect Unit
9289
}
9390

9491
-- | The standard input stream of a child process. Note that this is only
9592
-- | 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 ()
9794
stdin = unsafeFromNullable (missingStream "stdin") <<< _.stdin <<< runChildProcess
9895

9996
-- | The standard output stream of a child process. Note that this is only
10097
-- | 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 ()
10299
stdout = unsafeFromNullable (missingStream "stdout") <<< _.stdout <<< runChildProcess
103100

104101
-- | The standard error stream of a child process. Note that this is only
105102
-- | 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 ()
107104
stderr = unsafeFromNullable (missingStream "stderr") <<< _.stderr <<< runChildProcess
108105

109106
missingStream :: String -> String
@@ -119,23 +116,23 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
119116
pid :: ChildProcess -> Pid
120117
pid = _.pid <<< runChildProcess
121118

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
124121

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
127124

128-
disconnect :: forall eff. ChildProcess -> Eff (cp :: CHILD_PROCESS | eff) Unit
125+
disconnect :: ChildProcess -> Effect Unit
129126
disconnect = _.disconnect <<< runChildProcess
130127

131128
-- | Send a signal to a child process. It's an unfortunate historical decision
132129
-- | that this function is called "kill", as sending a signal to a child
133130
-- | 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)
136133

137-
mkEff :: forall eff a. (Unit -> a) -> Eff eff a
138-
mkEff = unsafeCoerce
134+
mkEffect :: forall a. (Unit -> a) -> Effect a
135+
mkEffect = unsafeCoerce
139136

140137
-- | Specifies how a child process exited; normally (with an exit code), or
141138
-- | due to a signal.
@@ -156,45 +153,43 @@ mkExit code signal =
156153
fromCode = toMaybe >>> map Normally
157154
fromSignal = toMaybe >=> Signal.fromString >>> map BySignal
158155

159-
onExit :: forall eff. ChildProcess -> (Exit -> Eff eff Unit) -> Eff eff Unit
156+
onExit :: ChildProcess -> (Exit -> Effect Unit) -> Effect Unit
160157
onExit = mkOnExit mkExit
161158

162159
foreign import mkOnExit
163-
:: forall eff
164-
. (Nullable Int -> Nullable String -> Exit)
160+
:: (Nullable Int -> Nullable String -> Exit)
165161
-> ChildProcess
166-
-> (Exit -> Eff eff Unit)
167-
-> Eff eff Unit
162+
-> (Exit -> Effect Unit)
163+
-> Effect Unit
168164

169-
onClose :: forall eff. ChildProcess -> (Exit -> Eff eff Unit) -> Eff eff Unit
165+
onClose :: ChildProcess -> (Exit -> Effect Unit) -> Effect Unit
170166
onClose = mkOnClose mkExit
171167

172168
foreign import mkOnClose
173-
:: forall eff
174-
. (Nullable Int -> Nullable String -> Exit)
169+
:: (Nullable Int -> Nullable String -> Exit)
175170
-> ChildProcess
176-
-> (Exit -> Eff eff Unit)
177-
-> Eff eff Unit
171+
-> (Exit -> Effect Unit)
172+
-> Effect Unit
178173

179-
onMessage :: forall eff. ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit) -> Eff eff Unit
174+
onMessage :: ChildProcess -> (Foreign -> Maybe Handle -> Effect Unit) -> Effect Unit
180175
onMessage = mkOnMessage Nothing Just
181176

182177
foreign import mkOnMessage
183-
:: forall a eff
178+
:: forall a
184179
. Maybe a
185180
-> (a -> Maybe a)
186181
-> ChildProcess
187-
-> (Foreign -> Maybe Handle -> Eff eff Unit)
188-
-> Eff eff Unit
182+
-> (Foreign -> Maybe Handle -> Effect Unit)
183+
-> Effect Unit
189184

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
192187

193188
-- | Spawn a child process. Note that, in the event that a child process could
194189
-- | not be spawned (for example, if the executable was not found) this will
195190
-- | not throw an error. Instead, the `ChildProcess` will be created anyway,
196191
-- | 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
198193
spawn cmd args = spawnImpl cmd args <<< convertOpts
199194
where
200195
convertOpts opts =
@@ -206,15 +201,15 @@ spawn cmd args = spawnImpl cmd args <<< convertOpts
206201
, gid: fromMaybe undefined opts.gid
207202
}
208203

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
210205

211206
-- There's gotta be a better way.
212207
foreign import undefined :: forall a. a
213208

214209
type SpawnOptions =
215210
{ cwd :: Maybe String
216211
, stdio :: Array (Maybe StdIOBehaviour)
217-
, env :: Maybe (StrMap String)
212+
, env :: Maybe (Object String)
218213
, detached :: Boolean
219214
, uid :: Maybe Uid
220215
, gid :: Maybe Gid
@@ -238,11 +233,10 @@ defaultSpawnOptions =
238233
-- | Note that the child process will be killed if the amount of output exceeds
239234
-- | a certain threshold (the default is defined by Node.js).
240235
exec
241-
:: forall eff
242-
. String
236+
:: String
243237
-> ExecOptions
244-
-> (ExecResult -> Eff (cp :: CHILD_PROCESS | eff) Unit)
245-
-> Eff (cp :: CHILD_PROCESS | eff) Unit
238+
-> (ExecResult -> Effect Unit)
239+
-> Effect Unit
246240
exec cmd opts callback =
247241
execImpl cmd (convertExecOptions opts) \err stdout' stderr' ->
248242
callback
@@ -252,21 +246,19 @@ exec cmd opts callback =
252246
}
253247

254248
foreign import execImpl
255-
:: forall eff
256-
. String
249+
:: String
257250
-> 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
260253

261254
-- | Like `exec`, except instead of using a shell, it passes the arguments
262255
-- | directly to the specified command.
263256
execFile
264-
:: forall eff
265-
. String
257+
:: String
266258
-> Array String
267259
-> ExecOptions
268-
-> (ExecResult -> Eff (cp :: CHILD_PROCESS | eff) Unit)
269-
-> Eff (cp :: CHILD_PROCESS | eff) Unit
260+
-> (ExecResult -> Effect Unit)
261+
-> Effect Unit
270262
execFile cmd args opts callback =
271263
execFileImpl cmd args (convertExecOptions opts) \err stdout' stderr' ->
272264
callback
@@ -276,12 +268,11 @@ execFile cmd args opts callback =
276268
}
277269

278270
foreign import execFileImpl
279-
:: forall eff
280-
. String
271+
:: String
281272
-> Array String
282273
-> 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
285276

286277
foreign import data ActualExecOptions :: Type
287278

@@ -298,7 +289,7 @@ convertExecOptions opts = unsafeCoerce
298289

299290
type ExecOptions =
300291
{ cwd :: Maybe String
301-
, env :: Maybe (StrMap String)
292+
, env :: Maybe (Object String)
302293
, timeout :: Maybe Number
303294
, maxBuffer :: Maybe Int
304295
, killSignal :: Maybe Signal
@@ -326,7 +317,7 @@ type ExecResult =
326317
-- | A special case of `spawn` for creating Node.js child processes. The first
327318
-- | argument is the module to be run, and the second is the argv (command line
328319
-- | 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
330321

331322
-- | An error which occurred inside a child process.
332323
type Error =
@@ -336,7 +327,7 @@ type Error =
336327
}
337328

338329
-- | 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).
340331
toStandardError :: Error -> Exception.Error
341332
toStandardError = unsafeCoerce
342333

@@ -355,7 +346,7 @@ toStandardError = unsafeCoerce
355346
data StdIOBehaviour
356347
= Pipe
357348
| Ignore
358-
| ShareStream (forall r eff. Stream r eff)
349+
| ShareStream (forall r. Stream r)
359350
| ShareFD FS.FileDescriptor
360351

361352
-- | Create pipes for each of the three standard IO streams.

test/Main.purs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ module Test.Main where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff)
6-
import Control.Monad.Eff.Console (CONSOLE, log)
7-
import Control.Monad.Eff.Exception (EXCEPTION)
5+
import Effect (Effect)
6+
import Effect.Console (log)
87

98
import Data.Posix.Signal (Signal(..))
109

1110
import Node.Buffer as Buffer
12-
import Node.ChildProcess (CHILD_PROCESS, Exit(..), defaultExecOptions, exec, onError, defaultSpawnOptions, spawn, stdout, onExit, kill)
11+
import Node.ChildProcess (Exit(..), defaultExecOptions, exec, onError, defaultSpawnOptions, spawn, stdout, onExit, kill)
1312
import Node.Encoding (Encoding(UTF8))
1413
import Node.Stream (onData)
1514

16-
type TestEff = Eff (cp :: CHILD_PROCESS, console :: CONSOLE, exception :: EXCEPTION, buffer :: Buffer.BUFFER) Unit
17-
18-
main :: TestEff
15+
main :: Effect Unit
1916
main = do
2017
log "spawns processes ok"
2118
spawnLs
@@ -47,28 +44,19 @@ main = do
4744
log "exec"
4845
execLs
4946

50-
spawnLs :: TestEff
47+
spawnLs :: Effect Unit
5148
spawnLs = do
5249
ls <- spawn "ls" ["-la"] defaultSpawnOptions
5350
onExit ls \exit ->
5451
log $ "ls exited: " <> show exit
5552
onData (stdout ls) (Buffer.toString UTF8 >=> log)
5653

57-
nonExistentExecutable
58-
:: forall eff
59-
. Eff ( console :: CONSOLE
60-
, cp :: CHILD_PROCESS
61-
| eff
62-
) Unit
63-
-> Eff ( cp :: CHILD_PROCESS
64-
, console :: CONSOLE
65-
| eff
66-
) Unit
54+
nonExistentExecutable :: Effect Unit -> Effect Unit
6755
nonExistentExecutable done = do
6856
ch <- spawn "this-does-not-exist" [] defaultSpawnOptions
6957
onError ch (\err -> log err.code *> done)
7058

71-
execLs :: TestEff
59+
execLs :: Effect Unit
7260
execLs = do
7361
exec "ls >&2" defaultExecOptions \r ->
7462
log "redirected to stderr:" *> (Buffer.toString UTF8 r.stderr >>= log)

0 commit comments

Comments
 (0)