1 module PursLoader.ChildProcess
6 import Control.Monad.Aff (Aff(), makeAff)
7 import Control.Monad.Eff (Eff())
8 import Control.Monad.Eff.Exception (Error())
12 foreign import data ChildProcess :: !
14 spawn :: forall eff. String -> [String] -> Aff (cp :: ChildProcess | eff) String
15 spawn command args = makeAff $ runFn4 spawnFn command args
17 foreign import spawnFn """
18 function spawnFn(command, args, errback, callback) {
20 var child_process = require('child_process');
22 var process = child_process.spawn(command, args);
24 var stdout = new Buffer(0);
26 var stderr = new Buffer(0);
28 process.stdout.on('data', function(data){
29 stdout = Buffer.concat([stdout, new Buffer(data)]);
32 process.stderr.on('data', function(data){
33 stderr = Buffer.concat([stderr, new Buffer(data)]);
36 process.on('close', function(code){
37 var chalk = require('chalk');
39 var output = stdout.toString('utf-8');
41 var error = stderr.toString('utf-8');
43 if (error.length > 0) {
44 console.error('\n' + chalk.red('*') + ' ' + error);
47 if (code !== 0) errback(new Error('Process terminated with code ' + code))();
48 else callback(output)();
52 """ :: forall eff. Fn4 String
54 (Error -> Eff (cp :: ChildProcess | eff) Unit)
55 (String -> Eff (cp :: ChildProcess | eff) Unit)
56 (Eff (cp :: ChildProcess | eff) Unit)