1 |
|
2 | var path = require('path');
|
3 | var fs = require('fs');
|
4 | var child = require('child_process');
|
5 |
|
6 | var DEFAULT_MAXBUFFER_SIZE = 20 * 1024 * 1024;
|
7 |
|
8 | function _exec(command, options, callback) {
|
9 | options = options || {};
|
10 |
|
11 | if (typeof options === 'function') {
|
12 | callback = options;
|
13 | }
|
14 |
|
15 | if (typeof options === 'object' && typeof callback === 'function') {
|
16 | options.async = true;
|
17 | }
|
18 |
|
19 | if (!command) {
|
20 | try {
|
21 | console.error('[sexec] must specify command');
|
22 | } catch (e) {
|
23 | return;
|
24 | }
|
25 | }
|
26 |
|
27 | options = Object.assign({
|
28 | silent: false,
|
29 | cwd: path.resolve(process.cwd()).toString(),
|
30 | env: process.env,
|
31 | maxBuffer: DEFAULT_MAXBUFFER_SIZE,
|
32 | encoding: 'utf8',
|
33 | }, options);
|
34 |
|
35 | var c = child.exec(command, options, function (err, stdout, stderr) {
|
36 | if (callback) {
|
37 | if (!err) {
|
38 | callback(0, stdout, stderr);
|
39 | } else if (err.code === undefined) {
|
40 |
|
41 |
|
42 | callback(1, stdout, stderr);
|
43 | } else {
|
44 | callback(err.code, stdout, stderr);
|
45 | }
|
46 | }
|
47 | });
|
48 |
|
49 | if (!options.silent) {
|
50 | c.stdout.pipe(process.stdout);
|
51 | c.stderr.pipe(process.stderr);
|
52 | }
|
53 | }
|
54 |
|
55 | module.exports = _exec;
|