-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathindex.js
442 lines (384 loc) · 13.2 KB
/
index.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* @flow */
import {ConsoleReporter, JSONReporter} from '../reporters/index.js';
import {sortAlpha} from '../util/misc.js';
import {registries, registryNames} from '../registries/index.js';
import * as commands from './commands/index.js';
import * as constants from '../constants.js';
import * as network from '../util/network.js';
import {MessageError} from '../errors.js';
import aliases from './aliases.js';
import Config from '../config.js';
import {hyphenate, camelCase} from '../util/misc.js';
const chalk = require('chalk');
const commander = require('commander');
const fs = require('fs');
const invariant = require('invariant');
const lockfile = require('proper-lockfile');
const loudRejection = require('loud-rejection');
const net = require('net');
const onDeath = require('death');
const path = require('path');
const pkg = require('../../package.json');
loudRejection();
//
const startArgs = process.argv.slice(0, 2);
let args = process.argv.slice(2);
// ignore all arguments after a --
let endArgs = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--') {
endArgs = args.slice(i + 1);
args = args.slice(0, i);
}
}
// NOTE: Pending resolution of https://github.com/tj/commander.js/issues/346
// Remove this (and subsequent use in the logic below) after bug is resolved and issue is closed
const ARGS_THAT_SHARE_NAMES_WITH_OPTIONS = [
'version',
];
// set global options
commander.version(pkg.version);
commander.usage('[command] [flags]');
commander.option('--verbose', 'output verbose messages on internal operations');
commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache');
commander.option('--prefer-offline', 'use network only if dependencies are not available in local cache');
commander.option('--strict-semver');
commander.option('--json', '');
commander.option('--ignore-scripts', "don't run lifecycle scripts");
commander.option('--har', 'save HAR output of network traffic');
commander.option('--ignore-platform', 'ignore platform checks');
commander.option('--ignore-engines', 'ignore engines check');
commander.option('--ignore-optional', 'ignore optional dependencies');
commander.option('--force', 'ignore all caches');
commander.option('--no-bin-links', "don't generate bin links when setting up packages");
commander.option('--flat', 'only allow one version of a package');
commander.option('--prod, --production [prod]', '');
commander.option('--no-lockfile', "don't read or generate a lockfile");
commander.option('--pure-lockfile', "don't generate a lockfile");
commander.option('--frozen-lockfile', "don't generate a lockfile and fail if an update is needed");
commander.option('--global-folder <path>', '');
commander.option(
'--modules-folder <path>',
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
);
commander.option(
'--cache-folder <path>',
'specify a custom folder to store the yarn cache',
);
commander.option(
'--mutex <type>[:specifier]',
'use a mutex to ensure only one yarn instance is executing',
);
commander.option(
'--no-emoji',
'disable emoji in output',
);
commander.option('--proxy <host>', '');
commander.option('--https-proxy <host>', '');
commander.option(
'--no-progress',
'disable progress bar',
);
commander.option('--network-concurrency <number>', 'maximum number of concurrent network requests', parseInt);
// get command name
let commandName: ?string = args.shift() || '';
let command;
//
const getDocsLink = (name) => `${constants.YARN_DOCS}${name || ''}`;
const getDocsInfo = (name) => 'Visit ' + chalk.bold(getDocsLink(name)) + ' for documentation about this command.';
//
if (commandName === 'help' || commandName === '--help' || commandName === '-h') {
commandName = 'help';
if (args.length) {
const helpCommand = hyphenate(args[0]);
if (commands[helpCommand]) {
commander.on('--help', () => console.log(' ' + getDocsInfo(helpCommand) + '\n'));
}
} else {
commander.on('--help', () => {
console.log(' Commands:\n');
for (const name of Object.keys(commands).sort(sortAlpha)) {
if (commands[name].useless) {
continue;
}
console.log(` - ${hyphenate(name)}`);
}
console.log('\n Run `' + chalk.bold('yarn help COMMAND') + '` for more information on specific commands.');
console.log(' Visit ' + chalk.bold(getDocsLink()) + ' to learn more about Yarn.\n');
});
}
}
// if no args or command name looks like a flag then default to `install`
if (!commandName || commandName[0] === '-') {
if (commandName) {
args.unshift(commandName);
}
commandName = 'install';
}
// aliases: i -> install
if (commandName && typeof aliases[commandName] === 'string') {
const alias = aliases[commandName];
command = {
run(config: Config, reporter: ConsoleReporter | JSONReporter): Promise<void> {
throw new MessageError(`Did you mean \`yarn ${alias}\`?`);
},
};
}
//
if (commandName === 'help' && args.length) {
commandName = camelCase(args.shift());
args.push('--help');
}
//
invariant(commandName, 'Missing command name');
if (!command) {
const camelised = camelCase(commandName);
if (camelised) {
command = commands[camelised];
}
}
//
if (command && typeof command.setFlags === 'function') {
command.setFlags(commander);
}
if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h') >= 0) {
const examples: Array<string> = (command && command.examples) || [];
if (examples.length) {
commander.on('--help', () => {
console.log(' Examples:\n');
for (const example of examples) {
console.log(` $ yarn ${example}`);
}
console.log();
});
}
commander.parse(startArgs.concat(args));
commander.help();
process.exit(1);
}
// parse flags
args.unshift(commandName);
if (ARGS_THAT_SHARE_NAMES_WITH_OPTIONS.indexOf(commandName) >= 0 && args[0] === commandName) {
args.shift();
}
commander.parse(startArgs.concat(args));
commander.args = commander.args.concat(endArgs);
if (command) {
commander.args.shift();
} else {
command = commands.run;
}
invariant(command, 'missing command');
//
let Reporter = ConsoleReporter;
if (commander.json) {
Reporter = JSONReporter;
}
const reporter = new Reporter({
emoji: commander.emoji && process.stdout.isTTY && process.platform === 'darwin',
verbose: commander.verbose,
noProgress: !commander.progress,
});
reporter.initPeakMemoryCounter();
//
const config = new Config(reporter);
// print header
let outputWrapper = true;
if (typeof command.hasWrapper === 'function') {
outputWrapper = command.hasWrapper(commander, commander.args);
}
if (commander.json) {
outputWrapper = false;
}
if (outputWrapper) {
reporter.header(commandName, pkg);
}
if (command.noArguments && commander.args.length) {
reporter.error(reporter.lang('noArguments'));
reporter.info(getDocsInfo(commandName));
process.exit(1);
}
//
if (commander.yes) {
reporter.warn(reporter.lang('yesWarning'));
}
//
if (!commander.offline && network.isOffline()) {
reporter.warn(reporter.lang('networkWarning'));
}
//
if (command.requireLockfile && !fs.existsSync(path.join(config.cwd, constants.LOCKFILE_FILENAME))) {
reporter.error(reporter.lang('noRequiredLockfile'));
process.exit(1);
}
//
const run = (): Promise<void> => {
invariant(command, 'missing command');
return command.run(config, reporter, commander, commander.args).then(() => {
reporter.close();
if (outputWrapper) {
reporter.footer(false);
}
});
};
//
const runEventuallyWithFile = (mutexFilename: ?string, isFirstTime?: boolean): Promise<void> => {
return new Promise((ok) => {
const lockFilename = mutexFilename || path.join(config.cwd, constants.SINGLE_INSTANCE_FILENAME);
lockfile.lock(lockFilename, {realpath: false}, (err: mixed, release: () => void) => {
if (err) {
if (isFirstTime) {
reporter.warn(reporter.lang('waitingInstance'));
}
setTimeout(() => {
ok(runEventuallyWithFile(mutexFilename, isFirstTime));
}, 200); // do not starve the CPU
} else {
onDeath(() => {
process.exit(1);
});
ok(run().then(release));
}
});
});
};
//
const runEventuallyWithNetwork = (mutexPort: ?string): Promise<void> => {
return new Promise((ok) => {
const connectionOptions = {
port: +mutexPort || constants.SINGLE_INSTANCE_PORT,
};
const server = net.createServer();
server.on('error', () => {
// another Yarn instance exists, let's connect to it to know when it dies.
reporter.warn(reporter.lang('waitingInstance'));
const socket = net.createConnection(connectionOptions);
socket
.on('connect', () => {
// Allow the program to exit if this is the only active server in the event system.
socket.unref();
})
.on('close', (hadError?: boolean) => {
// the `close` event gets always called after the `error` event
if (!hadError) {
process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
}
})
.on('error', () => {
// No server to listen to ? Let's retry to become the next server then.
process.nextTick(() => {
ok(runEventuallyWithNetwork(mutexPort));
});
});
});
const onServerEnd = (): Promise<void> => {
server.close();
return Promise.resolve();
};
// open the server and continue only if succeed.
server.listen(connectionOptions, () => {
// ensure the server gets closed properly on SIGNALS.
onDeath(onServerEnd);
ok(run().then(onServerEnd));
});
});
};
function onUnexpectedError(err: Error) {
function indent(str: string): string {
return '\n ' + str.trim().split('\n').join('\n ');
}
const log = [];
log.push(`Arguments: ${indent(process.argv.join(' '))}`);
log.push(`PATH: ${indent(process.env.PATH || 'undefined')}`);
log.push(`Yarn version: ${indent(pkg.version)}`);
log.push(`Node version: ${indent(process.versions.node)}`);
log.push(`Platform: ${indent(process.platform + ' ' + process.arch)}`);
// add manifests
for (const registryName of registryNames) {
const possibleLoc = path.join(config.cwd, registries[registryName].filename);
const manifest = fs.existsSync(possibleLoc) ? fs.readFileSync(possibleLoc, 'utf8') : 'No manifest';
log.push(`${registryName} manifest: ${indent(manifest)}`);
}
// lockfile
const lockLoc = path.join(config.cwd, constants.LOCKFILE_FILENAME);
const lockfile = fs.existsSync(lockLoc) ? fs.readFileSync(lockLoc, 'utf8') : 'No lockfile';
log.push(`Lockfile: ${indent(lockfile)}`);
log.push(`Trace: ${indent(err.stack)}`);
const errorReportLoc = writeErrorReport(log);
reporter.error(reporter.lang('unexpectedError', err.message));
if (errorReportLoc) {
reporter.info(reporter.lang('bugReport', errorReportLoc));
}
}
function writeErrorReport(log) : ?string {
const errorReportLoc = path.join(config.cwd, 'yarn-error.log');
try {
fs.writeFileSync(errorReportLoc, log.join('\n\n') + '\n');
} catch (err) {
reporter.error(reporter.lang('fileWriteError', errorReportLoc, err.message));
return undefined;
}
return errorReportLoc;
}
//
config.init({
binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
globalFolder: commander.globalFolder,
cacheFolder: commander.cacheFolder,
preferOffline: commander.preferOffline,
captureHar: commander.har,
ignorePlatform: commander.ignorePlatform,
ignoreEngines: commander.ignoreEngines,
ignoreScripts: commander.ignoreScripts,
offline: commander.preferOffline || commander.offline,
looseSemver: !commander.strictSemver,
production: commander.production,
httpProxy: commander.proxy,
httpsProxy: commander.httpsProxy,
networkConcurrency: commander.networkConcurrency,
commandName,
}).then(() => {
// option "no-progress" stored in yarn config
const noProgressConfig = config.registries.yarn.getOption('no-progress');
if (noProgressConfig) {
reporter.disableProgress();
}
const exit = () => {
process.exit(0);
};
// verbose logs outputs process.uptime() with this line we can sync uptime to absolute time on the computer
reporter.verbose(`current time: ${new Date().toISOString()}`);
const mutex: mixed = commander.mutex;
if (mutex && typeof mutex === 'string') {
const parts = mutex.split(':');
const mutexType = parts.shift();
const mutexSpecifier = parts.join(':');
if (mutexType === 'file') {
return runEventuallyWithFile(mutexSpecifier, true).then(exit);
} else if (mutexType === 'network') {
return runEventuallyWithNetwork(mutexSpecifier).then(exit);
} else {
throw new MessageError(`Unknown single instance type ${mutexType}`);
}
} else {
return run().then(exit);
}
}).catch((err: Error) => {
reporter.verbose(err.stack);
if (err instanceof MessageError) {
reporter.error(err.message);
} else {
onUnexpectedError(err);
}
if (commandName) {
const actualCommandForHelp = commands[commandName] ? commandName : aliases[commandName];
if (command && actualCommandForHelp) {
reporter.info(getDocsInfo(actualCommandForHelp));
}
}
process.exit(1);
});