1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
6 |
|
7 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
8 |
|
9 | const config = require('./config');
|
10 |
|
11 | const _require = require('@parcel/utils'),
|
12 | promisify = _require.promisify;
|
13 |
|
14 | const resolve = promisify(require('resolve'));
|
15 |
|
16 | const commandExists = require('command-exists');
|
17 |
|
18 | const logger = require('@parcel/logger');
|
19 |
|
20 | const pipeSpawn = require('./pipeSpawn');
|
21 |
|
22 | const PromiseQueue = require('./PromiseQueue');
|
23 |
|
24 | const path = require('path');
|
25 |
|
26 | const fs = require('@parcel/fs');
|
27 |
|
28 | const WorkerFarm = require('@parcel/workers');
|
29 |
|
30 | const YARN_LOCK = 'yarn.lock';
|
31 |
|
32 | function install(_x, _x2) {
|
33 | return _install.apply(this, arguments);
|
34 | }
|
35 |
|
36 | function _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 | }
|
64 |
|
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 |
|
86 | function installPeerDependencies(_x3, _x4, _x5) {
|
87 | return _installPeerDependencies.apply(this, arguments);
|
88 | }
|
89 |
|
90 | function _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 |
|
117 | function determinePackageManager(_x6) {
|
118 | return _determinePackageManager.apply(this, arguments);
|
119 | }
|
120 |
|
121 | function _determinePackageManager() {
|
122 | _determinePackageManager = (0, _asyncToGenerator2.default)(function* (filepath) {
|
123 | const yarnLockFile = yield config.resolve(filepath, [YARN_LOCK]);
|
124 | |
125 |
|
126 |
|
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 |
|
144 | let hasYarn = null;
|
145 |
|
146 | function checkForYarnCommand() {
|
147 | return _checkForYarnCommand.apply(this, arguments);
|
148 | }
|
149 |
|
150 | function _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 |
|
167 | let queue = new PromiseQueue(install, {
|
168 | maxConcurrent: 1,
|
169 | retry: false
|
170 | });
|
171 |
|
172 | module.exports =
|
173 |
|
174 | function () {
|
175 | var _ref = (0, _asyncToGenerator2.default)(function* (...args) {
|
176 |
|
177 |
|
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 |