UNPKG

5.02 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
6
7var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
8
9const config = require('./config');
10
11const _require = require('@parcel/utils'),
12 promisify = _require.promisify;
13
14const resolve = promisify(require('resolve'));
15
16const commandExists = require('command-exists');
17
18const logger = require('@parcel/logger');
19
20const pipeSpawn = require('./pipeSpawn');
21
22const PromiseQueue = require('./PromiseQueue');
23
24const path = require('path');
25
26const fs = require('@parcel/fs');
27
28const WorkerFarm = require('@parcel/workers');
29
30const YARN_LOCK = 'yarn.lock';
31
32function install(_x, _x2) {
33 return _install.apply(this, arguments);
34}
35
36function _install() {
37 _install = (0, _asyncToGenerator2.default)(function* (modules, filepath, options = {}) {
38 let _options$installPeers = options.installPeers,
39 installPeers = _options$installPeers === void 0 ? true : _options$installPeers,
40 _options$saveDev = options.saveDev,
41 saveDev = _options$saveDev === void 0 ? true : _options$saveDev,
42 packageManager = options.packageManager;
43
44 if (typeof modules === 'string') {
45 modules = [modules];
46 }
47
48 logger.progress(`Installing ${modules.join(', ')}...`);
49 let packageLocation = yield config.resolve(filepath, ['package.json']);
50 let cwd = packageLocation ? path.dirname(packageLocation) : process.cwd();
51
52 if (!packageManager) {
53 packageManager = yield determinePackageManager(filepath);
54 }
55
56 let commandToUse = packageManager === 'npm' ? 'install' : 'add';
57 let args = [commandToUse, ...modules];
58
59 if (saveDev) {
60 args.push('-D');
61 } else if (packageManager === 'npm') {
62 args.push('--save');
63 } // npm doesn't auto-create a package.json when installing,
64 // so create an empty one if needed.
65
66
67 if (packageManager === 'npm' && !packageLocation) {
68 yield fs.writeFile(path.join(cwd, 'package.json'), '{}');
69 }
70
71 try {
72 yield pipeSpawn(packageManager, args, {
73 cwd
74 });
75 } catch (err) {
76 throw new Error(`Failed to install ${modules.join(', ')}.`);
77 }
78
79 if (installPeers) {
80 yield Promise.all(modules.map(m => installPeerDependencies(filepath, m, options)));
81 }
82 });
83 return _install.apply(this, arguments);
84}
85
86function installPeerDependencies(_x3, _x4, _x5) {
87 return _installPeerDependencies.apply(this, arguments);
88}
89
90function _installPeerDependencies() {
91 _installPeerDependencies = (0, _asyncToGenerator2.default)(function* (filepath, name, options) {
92 let basedir = path.dirname(filepath);
93
94 const _ref2 = yield resolve(name, {
95 basedir
96 }),
97 _ref3 = (0, _slicedToArray2.default)(_ref2, 1),
98 resolved = _ref3[0];
99
100 const pkg = yield config.load(resolved, ['package.json']);
101 const peers = pkg.peerDependencies || {};
102 const modules = [];
103
104 for (const peer in peers) {
105 modules.push(`${peer}@${peers[peer]}`);
106 }
107
108 if (modules.length) {
109 yield install(modules, filepath, Object.assign({}, options, {
110 installPeers: false
111 }));
112 }
113 });
114 return _installPeerDependencies.apply(this, arguments);
115}
116
117function determinePackageManager(_x6) {
118 return _determinePackageManager.apply(this, arguments);
119}
120
121function _determinePackageManager() {
122 _determinePackageManager = (0, _asyncToGenerator2.default)(function* (filepath) {
123 const yarnLockFile = yield config.resolve(filepath, [YARN_LOCK]);
124 /**
125 * no yarn.lock => use npm
126 * yarn.lock => Use yarn, fallback to npm
127 */
128
129 if (!yarnLockFile) {
130 return 'npm';
131 }
132
133 const hasYarn = yield checkForYarnCommand();
134
135 if (hasYarn) {
136 return 'yarn';
137 }
138
139 return 'npm';
140 });
141 return _determinePackageManager.apply(this, arguments);
142}
143
144let hasYarn = null;
145
146function checkForYarnCommand() {
147 return _checkForYarnCommand.apply(this, arguments);
148}
149
150function _checkForYarnCommand() {
151 _checkForYarnCommand = (0, _asyncToGenerator2.default)(function* () {
152 if (hasYarn != null) {
153 return hasYarn;
154 }
155
156 try {
157 hasYarn = yield commandExists('yarn');
158 } catch (err) {
159 hasYarn = false;
160 }
161
162 return hasYarn;
163 });
164 return _checkForYarnCommand.apply(this, arguments);
165}
166
167let queue = new PromiseQueue(install, {
168 maxConcurrent: 1,
169 retry: false
170});
171
172module.exports =
173/*#__PURE__*/
174function () {
175 var _ref = (0, _asyncToGenerator2.default)(function* (...args) {
176 // Ensure that this function is always called on the master process so we
177 // don't call multiple installs in parallel.
178 if (WorkerFarm.isWorker()) {
179 yield WorkerFarm.callMaster({
180 location: __filename,
181 args
182 });
183 return;
184 }
185
186 queue.add(...args);
187 return queue.run();
188 });
189
190 return function () {
191 return _ref.apply(this, arguments);
192 };
193}();
\No newline at end of file