aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/ChildProcess.purs
diff options
context:
space:
mode:
Diffstat (limited to 'src/PursLoader/ChildProcess.purs')
-rw-r--r--src/PursLoader/ChildProcess.purs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/PursLoader/ChildProcess.purs b/src/PursLoader/ChildProcess.purs
new file mode 100644
index 0000000..34558fa
--- /dev/null
+++ b/src/PursLoader/ChildProcess.purs
@@ -0,0 +1,56 @@
1module PursLoader.ChildProcess
2 ( ChildProcess()
3 , spawn
4 ) where
5
6import Control.Monad.Aff (Aff(), makeAff)
7import Control.Monad.Eff (Eff())
8import Control.Monad.Eff.Exception (Error())
9
10import Data.Function
11
12foreign import data ChildProcess :: !
13
14spawn :: forall eff. String -> [String] -> Aff (cp :: ChildProcess | eff) String
15spawn command args = makeAff $ runFn4 spawnFn command args
16
17foreign import spawnFn """
18function 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 var stderr = new Buffer(0);
27
28 process.stdout.on('data', function(data){
29 stdout = Buffer.concat([stdout, new Buffer(data)]);
30 });
31
32 process.stderr.on('data', function(data){
33 stderr = Buffer.concat([stderr, new Buffer(data)]);
34 });
35
36 process.on('close', function(code){
37 var chalk = require('chalk');
38
39 var output = stdout.toString('utf-8');
40
41 var error = stderr.toString('utf-8');
42
43 if (error.length > 0) {
44 console.error('\n' + chalk.red('*') + ' ' + error);
45 }
46
47 if (code !== 0) errback(new Error('Process terminated with code ' + code))();
48 else callback(output)();
49 });
50 };
51}
52""" :: forall eff. Fn4 String
53 [String]
54 (Error -> Eff (cp :: ChildProcess | eff) Unit)
55 (String -> Eff (cp :: ChildProcess | eff) Unit)
56 (Eff (cp :: ChildProcess | eff) Unit)