1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | module.exports = class CommonHelper {
|
7 |
|
8 | static isWinPlatform () {
|
9 | return /^win/.test(process.platform);
|
10 | }
|
11 |
|
12 | static parseArguments (args, keyPrefix = '--') {
|
13 | let result = {}, key;
|
14 | args = Array.isArray(args) ? args : [];
|
15 | for (const item of args) {
|
16 | if (typeof item === 'string' && item.indexOf(keyPrefix) === 0) {
|
17 | key = item.substring(keyPrefix.length);
|
18 | } else if (key !== undefined) {
|
19 | const value = item === 'true' ? true : item === 'false' ? false : item;
|
20 | if (Array.isArray(result[key])) {
|
21 | result[key].push(value);
|
22 | } else if (result.hasOwnProperty(key)) {
|
23 | result[key] = [result[key], value];
|
24 | } else {
|
25 | result[key] = value;
|
26 | }
|
27 | }
|
28 | }
|
29 | return result;
|
30 | }
|
31 |
|
32 | static spawnProcess (path, command, args) {
|
33 | if (this.isWinPlatform()) {
|
34 | command += '.cmd';
|
35 | }
|
36 | const childProcess = require('child_process');
|
37 | const sub = childProcess.spawn(command, args, {
|
38 | cwd: path,
|
39 | env: process.env
|
40 | });
|
41 | sub.stdout.on('data', data => console.log(`${data}`));
|
42 | sub.stderr.on('data', data => console.error(`${data}`));
|
43 | return new Promise((resolve, reject) => {
|
44 | sub.on('error', err => reject(`Spawn process failed: ${err}`));
|
45 | sub.on('close', err => {
|
46 | err ? reject(`Spawn process: ${command}: failed: ${err}`)
|
47 | : resolve();
|
48 | });
|
49 | });
|
50 | }
|
51 | }; |
\ | No newline at end of file |