1 | define(["../fileHandleThrottle", "../messages"], function(fht, messages){ |
---|
2 | |
---|
3 | var match = process.version.match(/(\d+)\.(\d+)\.(\d+)/), |
---|
4 | versionMajor = Number(match[1]), |
---|
5 | versionMinor = Number(match[2]), |
---|
6 | versionPatch = Number(match[3]), |
---|
7 | spawn = require.nodeRequire("child_process").spawn; |
---|
8 | return { |
---|
9 | cwd:process.cwd, |
---|
10 | exit:function(code){ |
---|
11 | // no more messages |
---|
12 | messages.stop(); |
---|
13 | |
---|
14 | process.exit(code); |
---|
15 | }, |
---|
16 | |
---|
17 | exec:function() { |
---|
18 | // signature is (command, arg1, ..., argn, errorMessage, bc, callback) |
---|
19 | for(var command = arguments[0], args = [], i = 1; i<arguments.length-3; i++){ |
---|
20 | args.push(arguments[i]); |
---|
21 | } |
---|
22 | var |
---|
23 | errorMessage = arguments[i++], |
---|
24 | bc = arguments[i++], |
---|
25 | callback = arguments[i]; |
---|
26 | fht.enqueue(function(){ |
---|
27 | var |
---|
28 | text = "", |
---|
29 | process = spawn(command, args), |
---|
30 | status = 0, |
---|
31 | finish = function(code){ |
---|
32 | // release when both exit and close events occur; see below |
---|
33 | if(++status===2){ |
---|
34 | fht.release(); |
---|
35 | if(code){ |
---|
36 | bc.log("execFailed", ["message", errorMessage, "output", text]); |
---|
37 | } |
---|
38 | callback && callback(code, text); |
---|
39 | } |
---|
40 | }; |
---|
41 | |
---|
42 | process.on("exit", finish); |
---|
43 | // for node>=0.8, close is called; for node <0.8 close appears to not be called (verified for 0.6) |
---|
44 | // in 0.8+ releasing the file handle before close is called can use up file handles too fast (see #15620) |
---|
45 | if(versionMajor==0 && versionMinor<=7){ |
---|
46 | ++status; |
---|
47 | }else{ |
---|
48 | process.on("close", finish); |
---|
49 | } |
---|
50 | process.stdout.on("data", function(data){ |
---|
51 | text+= data; |
---|
52 | }); |
---|
53 | process.stderr.on("data", function(data){ |
---|
54 | text+= data; |
---|
55 | }); |
---|
56 | }); |
---|
57 | } |
---|
58 | }; |
---|
59 | }); |
---|
60 | |
---|