|
1 | | -module Node.ChildProcess where |
2 | | - |
3 | | - import Control.Events (Event(..), EventEmitter) |
4 | | - import Control.Monad.Eff (Eff()) |
5 | | - |
6 | | - import Data.Function (Fn0(), Fn1(), Fn2()) |
7 | | - |
8 | | - import Node.ChildProcess.Signal (Signal(..)) |
9 | | - |
10 | | - foreign import data Handle :: * |
11 | | - foreign import data Spawn :: ! |
12 | | - foreign import data Stream :: ! -> * |
13 | | - foreign import data Stderr :: ! |
14 | | - foreign import data Stdin :: ! |
15 | | - foreign import data Stdout :: ! |
16 | | - |
17 | | - newtype ChildProcess = ChildProcess ChildProcessRec |
18 | | - |
19 | | - type ChildProcessRec = |
20 | | - { stderr :: Stream Stderr |
21 | | - , stdin :: Stream Stdin |
22 | | - , stdout :: Stream Stdout |
23 | | - , pid :: Number |
24 | | - , connected :: Boolean |
25 | | - , kill :: forall eff. Fn1 Signal Boolean |
26 | | - , send :: forall eff r. Fn2 { | r} Handle (Eff eff Unit) |
27 | | - , disconnect :: forall eff. Fn0 (Eff eff Unit) |
28 | | - } |
29 | | - |
30 | | - type SpawnOptions = |
31 | | - { cwd :: String |
32 | | - , stdio :: [String] |
33 | | - , env :: forall r. { | r} |
34 | | - , detached :: Boolean |
35 | | - , uid :: Number |
36 | | - , gid :: Number |
37 | | - } |
38 | | - |
39 | | - instance eventEmitterStreamStderr :: EventEmitter (Stream Stderr) |
40 | | - instance eventEmitterStreamStdin :: EventEmitter (Stream Stdin) |
41 | | - instance eventEmitterStreamStdout :: EventEmitter (Stream Stdout) |
42 | | - |
43 | | - instance eventEmitterChildProcess :: EventEmitter ChildProcess |
44 | | - |
45 | | - closeEvent :: Event |
46 | | - closeEvent = Event "close" |
47 | | - |
48 | | - disconnectEvent :: Event |
49 | | - disconnectEvent = Event "disconnect" |
50 | | - |
51 | | - errorEvent :: Event |
52 | | - errorEvent = Event "error" |
53 | | - |
54 | | - exitEvent :: Event |
55 | | - exitEvent = Event "exit" |
56 | | - |
57 | | - messageEvent :: Event |
58 | | - messageEvent = Event "message" |
59 | | - |
60 | | - foreign import spawn |
61 | | - "function spawn(command) {\ |
62 | | - \ return function(args) {\ |
63 | | - \ return function(opts) {\ |
64 | | - \ return function() {\ |
65 | | - \ return require('child_process').spawn(command, args, opts);\ |
66 | | - \ }\ |
67 | | - \ }\ |
68 | | - \ }\ |
69 | | - \}" :: forall eff. String -> [String] -> SpawnOptions -> Eff (spawn :: Spawn | eff) ChildProcess |
70 | | - |
71 | | - defaultSpawnOptions :: SpawnOptions |
72 | | - defaultSpawnOptions = |
73 | | - { cwd: undefined |
74 | | - , stdio: ["pipe", "pipe", "pipe"] |
75 | | - , env: process.env |
76 | | - , detached: false |
77 | | - , uid: undefined |
78 | | - , gid: undefined |
79 | | - } |
80 | | - |
81 | | - -- There's gotta be a better way. |
82 | | - foreign import undefined :: forall a. a |
83 | | - foreign import process :: forall r. {env :: { | r}} |
| 1 | +module Node.ChildProcess |
| 2 | + ( Handle() |
| 3 | + , Spawn() |
| 4 | + , Stdin() |
| 5 | + , Stdout() |
| 6 | + , Stderr() |
| 7 | + , ChildProcess() |
| 8 | + , SpawnOptions() |
| 9 | + , ChildProcessError() |
| 10 | + , onExit |
| 11 | + , onClose |
| 12 | + , onDisconnect |
| 13 | + , onMessage |
| 14 | + , onError |
| 15 | + , spawn |
| 16 | + , defaultSpawnOptions |
| 17 | + ) where |
| 18 | + |
| 19 | +import Prelude |
| 20 | + |
| 21 | +import Control.Monad.Eff (Eff()) |
| 22 | + |
| 23 | +import Data.Function (Fn0(), Fn1(), Fn2()) |
| 24 | +import Data.Maybe (Maybe(..)) |
| 25 | +import Data.Foreign (Foreign()) |
| 26 | + |
| 27 | +import Node.Stream (Readable(), Writable()) |
| 28 | +import Node.ChildProcess.Signal (Signal(..)) |
| 29 | + |
| 30 | +foreign import data Handle :: * |
| 31 | +foreign import data Spawn :: ! |
| 32 | +foreign import data Stdin :: ! |
| 33 | +foreign import data Stdout :: ! |
| 34 | +foreign import data Stderr :: ! |
| 35 | + |
| 36 | +type ChildProcess = |
| 37 | + { stderr :: forall eff. Readable () (stderr :: Stderr | eff) String |
| 38 | + , stdin :: forall eff. Writable () (stdin :: Stdin | eff) String |
| 39 | + , stdout :: forall eff. Readable () (stdout :: Stdout | eff) String |
| 40 | + , pid :: Number |
| 41 | + , connected :: Boolean |
| 42 | + , kill :: forall eff. Fn1 Signal Boolean |
| 43 | + , send :: forall eff r. Fn2 { | r} Handle (Eff eff Unit) |
| 44 | + , disconnect :: forall eff. Fn0 (Eff eff Unit) |
| 45 | + } |
| 46 | + |
| 47 | +type SpawnOptions = |
| 48 | + { cwd :: String |
| 49 | + , stdio :: Array String |
| 50 | + , env :: forall r. { | r} |
| 51 | + , detached :: Boolean |
| 52 | + , uid :: Number |
| 53 | + , gid :: Number |
| 54 | + } |
| 55 | + |
| 56 | +onExit :: forall eff. ChildProcess -> (Maybe Number -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit |
| 57 | +onExit = mkOnExit Nothing Just Signal |
| 58 | + |
| 59 | +onClose :: forall eff. ChildProcess -> (Maybe Number -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit |
| 60 | +onClose = mkOnClose Nothing Just Signal |
| 61 | + |
| 62 | +onMessage :: forall eff. ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit) -> Eff eff Unit |
| 63 | +onMessage = mkOnMessage Nothing Just |
| 64 | + |
| 65 | +foreign import onDisconnect :: forall eff. ChildProcess -> Eff eff Unit -> Eff eff Unit |
| 66 | +foreign import onError :: forall eff. ChildProcess -> (ChildProcessError -> Eff eff Unit) -> Eff eff Unit |
| 67 | + |
| 68 | +foreign import spawn :: forall eff. String -> Array String -> SpawnOptions -> Eff (spawn :: Spawn | eff) ChildProcess |
| 69 | + |
| 70 | +defaultSpawnOptions :: SpawnOptions |
| 71 | +defaultSpawnOptions = |
| 72 | + { cwd: undefined |
| 73 | + , stdio: ["pipe", "pipe", "pipe"] |
| 74 | + , env: process.env |
| 75 | + , detached: false |
| 76 | + , uid: undefined |
| 77 | + , gid: undefined |
| 78 | + } |
| 79 | + |
| 80 | +type ChildProcessError = |
| 81 | + { code :: String |
| 82 | + , errno :: String |
| 83 | + , syscall :: String |
| 84 | + } |
| 85 | + |
| 86 | +foreign import mkOnExit :: forall a eff. |
| 87 | + Maybe a -> (a -> Maybe a) -> (String -> Signal) -> |
| 88 | + ChildProcess -> (Maybe Number -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit |
| 89 | +foreign import mkOnMessage :: forall a eff. |
| 90 | + Maybe a -> (a -> Maybe a) -> |
| 91 | + ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit) -> Eff eff Unit |
| 92 | +foreign import mkOnClose :: forall a eff. |
| 93 | + Maybe a -> (a -> Maybe a) -> (String -> Signal) -> |
| 94 | + ChildProcess -> (Maybe Number -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit |
| 95 | + |
| 96 | +-- There's gotta be a better way. |
| 97 | +foreign import undefined :: forall a. a |
| 98 | +foreign import process :: forall r. {env :: { | r}} |
0 commit comments