执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.run = run;
exports.option = option;
exports.options = options;
exports.help = help;
 
var _child_process = require('child_process');
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _common = require('./common');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
const loggerAlias = _common.logger;
 
 
function runSync(command, options) {
  try {
    const nextOptions = {
      cwd: options.cwd,
      env: options.env,
      stdio: options.stdio,
      timeout: options.timeout
    };
    const buffer = (0, _child_process.execSync)(command, nextOptions);
    if (buffer) {
      return buffer.toString();
    }
    return null;
  } catch (error) {
    throw new _common.RunJSError(error.message);
  }
}
 
function runAsync(command, options) {
  return new Promise((resolve, reject) => {
    const nextOptions = {
      cwd: options.cwd,
      env: options.env,
      stdio: options.stdio,
      shell: true
    };
    const asyncProcess = (0, _child_process.spawn)(command, nextOptions);
    let output = null;
 
    asyncProcess.on('error', error => {
      reject(new Error(`Failed to start command: ${command}; ${error.toString()}`));
    });
 
    asyncProcess.on('close', exitCode => {
      if (exitCode === 0) {
        resolve(output);
      } else {
        reject(new Error(`Command failed: ${command} with exit code ${exitCode}`));
      }
    });
 
    if (options.stdio === 'pipe') {
      asyncProcess.stdout.on('data', buffer => {
        output = buffer.toString();
      });
    }
 
    if (options.timeout) {
      setTimeout(() => {
        asyncProcess.kill();
        reject(new Error(`Command timeout: ${command}`));
      }, options.timeout);
    }
  });
}
 
function run(command, options = {}, logger = loggerAlias) {
  const binPath = _path2.default.resolve('./node_modules/.bin');
 
  // Pick relevant option keys and set default values
  const nextOptions = {
    env: options.env || process.env,
    cwd: options.cwd,
    async: !!options.async,
    stdio: options.stdio || 'inherit',
    timeout: options.timeout
  };
 
  const env = nextOptions.env;
 
  // Include in PATH node_modules bin path
  if (env) {
    env.PATH = [binPath, env.PATH || process.env.PATH].join(_path2.default.delimiter);
  }
 
  logger.title(command);
 
  // Handle async call
  if (options.async) {
    return runAsync(command, nextOptions);
  }
 
  // Handle sync call by default
  return runSync(command, nextOptions);
}
 
/**
 * @deprecated
 */
function option(thisObj, name) {
  return thisObj && thisObj.options && thisObj.options[name] || null;
}
 
function options(thisObj) {
  return thisObj && thisObj.options || {};
}
 
function help(func, annotation) {
  func.help = annotation;
}