1
1
module Test.Main where
2
2
3
3
import Prelude
4
- import Control.Apply
5
- import Control.Bind
6
-
7
- import Control.Monad.Eff.Console
8
- import Control.Monad.Eff.Console.Unsafe
9
4
5
+ import Control.Monad.Eff (Eff )
6
+ import Control.Monad.Eff.Console (CONSOLE , log )
7
+ import Control.Monad.Eff.Exception (EXCEPTION )
10
8
import Data.Posix.Signal (Signal (..))
11
- import Node.Encoding (Encoding (UTF8))
12
9
import Node.Buffer as Buffer
13
- import Node.ChildProcess
10
+ import Node.ChildProcess (CHILD_PROCESS , Exit (..), defaultExecOptions , exec , onError ,
11
+ defaultSpawnOptions , spawn , stdout , onExit , kill )
12
+ import Node.Encoding (Encoding (UTF8))
14
13
import Node.Stream (onData )
15
14
15
+ main :: forall eff . Eff
16
+ ( cp :: CHILD_PROCESS
17
+ , console :: CONSOLE
18
+ , err :: EXCEPTION
19
+ , buffer :: Buffer.BUFFER
20
+ | eff
21
+ ) Unit
16
22
main = do
17
23
log " spawns processes ok"
18
24
spawnLs
@@ -22,38 +28,59 @@ main = do
22
28
log " nonexistent executable: all good."
23
29
24
30
log " doesn't perform effects too early"
25
- ls <- spawn " ls" [" -la" ] defaultSpawnOptions
26
- let unused = kill SIGTERM ls
27
- onExit ls \exit ->
28
- case exit of
29
- Normally 0 ->
30
- log " All good!"
31
- _ -> do
32
- log (" Bad exit: expected `Normally 0`, got: " <> show exit)
31
+ spawn " ls" [" -la" ] defaultSpawnOptions >>= \ls -> do
32
+ let unused = kill SIGTERM ls
33
+ onExit ls \exit ->
34
+ case exit of
35
+ Normally 0 ->
36
+ log " All good!"
37
+ _ -> do
38
+ log (" Bad exit: expected `Normally 0`, got: " <> show exit)
33
39
34
40
log " kills processes"
35
- ls <- spawn " ls" [" -la" ] defaultSpawnOptions
36
- kill SIGTERM ls
37
- onExit ls \exit ->
38
- case exit of
39
- BySignal SIGTERM ->
40
- log " All good!"
41
- _ -> do
42
- log (" Bad exit: expected `BySignal SIGTERM`, got: " <> show exit)
41
+ spawn " ls" [" -la" ] defaultSpawnOptions >>= \ls -> do
42
+ kill SIGTERM ls
43
+ onExit ls \exit ->
44
+ case exit of
45
+ BySignal SIGTERM ->
46
+ log " All good!"
47
+ _ -> do
48
+ log (" Bad exit: expected `BySignal SIGTERM`, got: " <> show exit)
43
49
44
50
log " exec"
45
51
execLs
46
52
53
+ spawnLs :: forall eff . Eff ( cp :: CHILD_PROCESS
54
+ , console :: CONSOLE
55
+ , err :: EXCEPTION
56
+ , buffer :: Buffer.BUFFER
57
+ | eff
58
+ ) Unit
47
59
spawnLs = do
48
60
ls <- spawn " ls" [" -la" ] defaultSpawnOptions
49
61
onExit ls \exit ->
50
62
log $ " ls exited: " <> show exit
51
63
onData (stdout ls) (Buffer .toString UTF8 >=> log)
52
64
65
+ nonExistentExecutable
66
+ :: forall eff
67
+ . Eff ( console :: CONSOLE
68
+ , cp :: CHILD_PROCESS
69
+ | eff
70
+ ) Unit
71
+ -> Eff ( cp :: CHILD_PROCESS
72
+ , console :: CONSOLE
73
+ | eff
74
+ ) Unit
53
75
nonExistentExecutable done = do
54
76
ch <- spawn " this-does-not-exist" [] defaultSpawnOptions
55
- onError ch (\err -> logAny err *> done)
77
+ onError ch (\err -> log err.code *> done)
56
78
79
+ execLs :: forall eff . Eff ( cp :: CHILD_PROCESS
80
+ , console :: CONSOLE
81
+ , buffer :: Buffer.BUFFER
82
+ | eff
83
+ ) Unit
57
84
execLs = do
58
85
exec " ls >&2" defaultExecOptions \r ->
59
86
log " redirected to stderr:" *> (Buffer .toString UTF8 r.stderr >>= log)
0 commit comments