执行过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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
 
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
var fs = require('fs');
var path = require('path');
var which = require('which');
 
var findPrefix = require('./find-prefix');
 
var PATH = getPATHKey();
var SEPARATOR = getPATHSeparator();
 
/**
 * Get new $PATH setting with additional paths supplied by the npm.
 *
 * @param Object options Config options Object.
 * @param Object options.env Environment to use. Default: process.env
 * @param String options.wd Working directory. Default: process.cwd()
 * @param Function fn callback function.
 */
 
function getPath(options, fn) {
  options.cwd = options.cwd || process.cwd();
  var env = options.env = options.env || process.env;
  var pathArr = getPathArr(options);
 
  findPrefix(options, function (err, prefixPath) {
    if (!err && prefixPath) {
      // ignore err if cannot find prefix
      pathArr.unshift(path.join(prefixPath, 'node_modules', '.bin'));
    }
 
    whichNpm(options, function (err, npmPath) {
      // we also unshift the bundled node-gyp-bin folder so that
      // the bundled one will be used for installing things.
 
      // simply ignore this step if there was no npm found
      if (err || !npmPath) {
        // ...unless npm path was explicitly passed in
        if (options.npm) {
          return fn(err || new Error('Cannot find ' + options.npm));
        }
      } else {
        pathArr.unshift(path.join(path.dirname(npmPath), 'node-gyp-bin'));
      }
 
      if (env[PATH]) pathArr = pathArr.concat(env[PATH].split(SEPARATOR));
 
      // Remove duplicated entries
      pathArr = [].concat(_toConsumableArray(new Set(pathArr)));
 
      fn(null, pathArr.join(SEPARATOR));
    });
  });
}
 
/**
 * Async wrapper around `getPath`.
 */
 
function getPathAsync(options, fn) {
  // options is optional
  if (typeof options === 'function') {
    fn = options;
    options = {};
  }
  // if no fn, execute as sync
  if (typeof fn !== 'function') return getPathSync(options);
  options = options || {};
  options.isSync = false;
  return getPath(options, fn);
}
 
/**
 * Sync wrapper around `getPath`.
 */
 
function getPathSync(options) {
  options = options || {};
  options.isSync = true;
  var thePath = null;
  // sync magic: if sync true, callback is executed sync
  // therefore we can set thePath from inside it before returning
  getPath(options, function (err, foundPath) {
    if (err) throw err;
    thePath = foundPath;
  });
  return thePath;
}
 
/**
 * Change environment to include npm path adjustments.
 *
 * @param Object options Config options Object.
 * @param Object options.env Environment to use. Default: process.env
 * @param String options.wd Working directory. Default: process.cwd()
 * @param Function fn callback function.
 */
 
function setPathAsync(options, fn) {
  // options is optional
  if (typeof options === 'function') {
    fn = options;
    options = {};
  }
 
  // if no fn, execute as sync
  if (typeof fn !== 'function') return setPathSync(options);
 
  getPathAsync(options, function (err, newPath) {
    if (err) return fn(err);
    fn(null, options.env[PATH] = newPath);
  });
}
 
/**
 * Sync version of `setPathAsync`
 */
function setPathSync(options) {
  options = options || {};
  var newPath = getPathSync(options);
  options.env[PATH] = newPath;
  return newPath;
}
 
/**
 * Generate simple parts of the npm path. Basically everything that doesn't
 * depend on potentially async operations.
 *
 * @return Array
 */
function getPathArr(options) {
  var wd = options.cwd;
  var pathArr = [];
  var p = wd.split(path.sep + 'node_modules' + path.sep);
  var acc = path.resolve(p.shift());
 
  // first add the directory containing the `node` executable currently
  // running, so that any lifecycle script that invoke 'node' will execute
  // this same one.
  pathArr.unshift(path.dirname(process.execPath));
 
  p.forEach(function (pp) {
    pathArr.unshift(path.join(acc, 'node_modules', '.bin'));
    acc = path.join(acc, 'node_modules', pp);
  });
  pathArr.unshift(path.join(acc, 'node_modules', '.bin'));
  return pathArr;
}
 
/**
 * Use callback-style signature but toggle sync execution if `isSync` is true.
 * If options.npm is supplied, this will simply provide npm/bin/npm-cli.
 */
function whichNpm(options, fn) {
  var npmCli = options.npm && path.join(options.npm, 'bin', 'npm-cli.js');
 
  if (options.isSync) {
    var npmPath = null;
 
    try {
      npmPath = fs.realpathSync(npmCli || which.sync('npm'));
    } catch (err) {
      return fn(err);
    }
 
    fn(null, npmPath);
    return;
  }
 
  if (options.npm) {
    fs.realpath(npmCli, fn);
    return;
  }
 
  which('npm', function (err, npmPath) {
    if (err) return fn(err);
    fs.realpath(npmPath, fn);
  });
}
 
/**
 * Get key to use as $PATH in environment
 */
 
function getPATHKey() {
  var PATH = 'PATH';
 
  // windows calls it's path 'Path' usually, but this is not guaranteed.
  if (process.platform === 'win32') {
    PATH = 'Path';
    Object.keys(process.env).forEach(function (e) {
      if (e.match(/^PATH$/i)) {
        PATH = e;
      }
    });
  }
  return PATH;
}
 
/**
 * Get $PATH separator based on environment
 */
function getPATHSeparator() {
  return process.platform === 'win32' ? ';' : ':';
}
 
module.exports = setPathAsync;
module.exports.get = getPathAsync;
module.exports.get.sync = getPathSync;
module.exports.getSync = getPathSync;
 
module.exports.set = setPathAsync;
module.exports.set.sync = setPathSync;
module.exports.setSync = setPathSync;
 
module.exports.PATH = PATH;
module.exports.SEPARATOR = SEPARATOR;