]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.ChildProcess |
2 | ( ChildProcess() | |
3 | , spawn | |
4 | ) where | |
5 | ||
6 | import Control.Monad.Aff (Aff(), makeAff) | |
7 | import Control.Monad.Eff (Eff()) | |
8 | import Control.Monad.Eff.Exception (Error()) | |
9 | ||
10 | import Data.Function | |
11 | ||
12 | foreign import data ChildProcess :: ! | |
13 | ||
14 | spawn :: forall eff. String -> [String] -> Aff (cp :: ChildProcess | eff) String | |
15 | spawn command args = makeAff $ runFn4 spawnFn command args | |
16 | ||
17 | foreign import spawnFn """ | |
18 | function spawnFn(command, args, errback, callback) { | |
19 | return function(){ | |
20 | var child_process = require('child_process'); | |
21 | ||
22 | var process = child_process.spawn(command, args); | |
23 | ||
24 | var stdout = new Buffer(0); | |
25 | ||
e533a315 | 26 | var stderr = new Buffer(0); |
27 | ||
c194f84c | 28 | process.stdout.on('data', function(data){ |
29 | stdout = Buffer.concat([stdout, new Buffer(data)]); | |
30 | }); | |
31 | ||
e533a315 | 32 | process.stderr.on('data', function(data){ |
33 | stderr = Buffer.concat([stderr, new Buffer(data)]); | |
34 | }); | |
35 | ||
c194f84c | 36 | process.on('close', function(code){ |
e533a315 | 37 | var output = stdout.toString(); |
38 | ||
39 | var error = output.length === 0 ? stderr.toString() : output + "\n" + stderr.toString(); | |
40 | ||
41 | if (code !== 0) errback(new Error(error))(); | |
42 | else callback(output)(); | |
c194f84c | 43 | }); |
44 | }; | |
45 | } | |
46 | """ :: forall eff. Fn4 String | |
47 | [String] | |
48 | (Error -> Eff (cp :: ChildProcess | eff) Unit) | |
49 | (String -> Eff (cp :: ChildProcess | eff) Unit) | |
50 | (Eff (cp :: ChildProcess | eff) Unit) |