1 | var $, M, _, kleur, m;
|
2 |
|
3 | $ = {};
|
4 |
|
5 | $.type = require('../dist/type');
|
6 |
|
7 | $.os = require('../dist/os');
|
8 |
|
9 | $.info = require('../dist/info');
|
10 |
|
11 | $.parseString = require('../dist/parseString');
|
12 |
|
13 | _ = {};
|
14 |
|
15 | _.trim = require('lodash/trim');
|
16 |
|
17 | _.trimEnd = require('lodash/trimEnd');
|
18 |
|
19 | kleur = require('kleur');
|
20 |
|
21 | M = (function() {
|
22 | class M {
|
23 | close() {
|
24 | this.process.kill();
|
25 | return this;
|
26 | }
|
27 |
|
28 | async execute_(cmd, option = {}) {
|
29 | var arg, cmder, isIgnoreError, isSilent, result, status, type;
|
30 | type = $.type(cmd);
|
31 | cmd = (function() {
|
32 | switch (type) {
|
33 | case 'array':
|
34 | return cmd.join(' && ');
|
35 | case 'string':
|
36 | return cmd;
|
37 | default:
|
38 | throw new Error(`exec_/error: invalid type '${type}'`);
|
39 | }
|
40 | })();
|
41 | isIgnoreError = !!option.ignoreError;
|
42 | delete option.ignoreError;
|
43 | isSilent = !!option.silent;
|
44 | delete option.silent;
|
45 | [cmder, arg] = $.os() === 'windows' ? ['cmd.exe', ['/s', '/c', cmd]] : ['/bin/sh', ['-c', cmd]];
|
46 | if (!isSilent) {
|
47 | $.info('exec', cmd);
|
48 | }
|
49 | [status, result] = (await new Promise((resolve) => {
|
50 | var res;
|
51 | res = null;
|
52 | this.process = this.spawn(cmder, arg, option);
|
53 |
|
54 | this.process.stderr.on('data', (data) => {
|
55 | res = this.parseMessage(data);
|
56 | if (!isSilent) {
|
57 | return this.info('error', data);
|
58 | }
|
59 | });
|
60 | this.process.stdout.on('data', (data) => {
|
61 | res = this.parseMessage(data);
|
62 | if (!isSilent) {
|
63 | return this.info(data);
|
64 | }
|
65 | });
|
66 | return this.process.on('close', function(code) {
|
67 | if (code === 0 || isIgnoreError) {
|
68 | return resolve([true, res]);
|
69 | }
|
70 | return resolve([false, res]);
|
71 | });
|
72 | }));
|
73 | return [
|
74 | status,
|
75 | result
|
76 | ];
|
77 | }
|
78 |
|
79 | info(...arg) {
|
80 | var string, type;
|
81 | [type, string] = (function() {
|
82 | switch (arg.length) {
|
83 | case 1:
|
84 | return [null, arg[0]];
|
85 | case 2:
|
86 | return arg;
|
87 | default:
|
88 | throw new Error('exec_/error: invalid argument length');
|
89 | }
|
90 | })();
|
91 | string = _.trim(string);
|
92 | if (!string.length) {
|
93 | return;
|
94 | }
|
95 | string = string.replace(/\r/g, '\n').replace(/\n{2,}/g, '');
|
96 | string = (function() {
|
97 | switch (type) {
|
98 | case 'error':
|
99 | return kleur.red(string);
|
100 | default:
|
101 | return kleur.gray(string);
|
102 | }
|
103 | })();
|
104 | return console.log(string);
|
105 | }
|
106 |
|
107 | parseMessage(buffer) {
|
108 | return _.trimEnd($.parseString(buffer), '\n');
|
109 | }
|
110 |
|
111 | };
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | M.prototype.spawn = require('child_process').spawn;
|
122 |
|
123 | return M;
|
124 |
|
125 | }).call(this);
|
126 |
|
127 |
|
128 | m = new M();
|
129 |
|
130 | module.exports = async function(cmd, option) {
|
131 | if (!cmd) {
|
132 | throw new Error('exec_/error: cmd undefined');
|
133 | }
|
134 | return (await m.execute_(cmd, option));
|
135 | };
|