diff options
author | eric thul <thul.eric@gmail.com> | 2015-04-08 19:49:24 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-04-12 11:19:22 -0400 |
commit | c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68 (patch) | |
tree | 5470b97ffc561915796f5a8a2a9541d9ebef50ae /src/ChildProcess.purs | |
parent | 9d38968bbe4bbf54e2ca836d9a1550d74d4da703 (diff) | |
download | purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.tar.gz purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.tar.zst purs-loader-c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68.zip |
Rewrite using purescript for the implementation
Diffstat (limited to 'src/ChildProcess.purs')
-rw-r--r-- | src/ChildProcess.purs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/ChildProcess.purs b/src/ChildProcess.purs new file mode 100644 index 0000000..c9ff23b --- /dev/null +++ b/src/ChildProcess.purs | |||
@@ -0,0 +1,40 @@ | |||
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 | |||
26 | process.stdout.on('data', function(data){ | ||
27 | stdout = Buffer.concat([stdout, new Buffer(data)]); | ||
28 | }); | ||
29 | |||
30 | process.on('close', function(code){ | ||
31 | if (code !== 0) errback(new Error(stdout.toString()))(); | ||
32 | else callback(stdout.toString())(); | ||
33 | }); | ||
34 | }; | ||
35 | } | ||
36 | """ :: forall eff. Fn4 String | ||
37 | [String] | ||
38 | (Error -> Eff (cp :: ChildProcess | eff) Unit) | ||
39 | (String -> Eff (cp :: ChildProcess | eff) Unit) | ||
40 | (Eff (cp :: ChildProcess | eff) Unit) | ||