UNPKG

2.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cli_ux_1 = require("cli-ux");
4const path = require("path");
5const debug = require('debug')('cli:yarn');
6class Yarn {
7 constructor({ config }) {
8 this.config = config;
9 }
10 get bin() {
11 return require.resolve('yarn/bin/yarn.js');
12 }
13 fork(modulePath, args = [], options = {}) {
14 return new Promise((resolve, reject) => {
15 const { fork } = require('child_process');
16 const forked = fork(modulePath, args, options);
17 forked.stderr.on('data', (d) => process.stderr.write(d));
18 forked.stdout.setEncoding('utf8');
19 forked.stdout.on('data', (d) => {
20 if (options.verbose)
21 process.stdout.write(d);
22 else
23 cli_ux_1.default.action.status = d.replace(/\n$/, '').split('\n').pop();
24 });
25 forked.on('error', reject);
26 forked.on('exit', (code) => {
27 if (code === 0) {
28 resolve();
29 }
30 else {
31 reject(new Error(`yarn ${args.join(' ')} exited with code ${code}`));
32 }
33 });
34 // Fix windows bug with node-gyp hanging for input forever
35 // if (this.config.windows) {
36 // forked.stdin.write('\n')
37 // }
38 });
39 }
40 async exec(args = [], opts) {
41 const cwd = opts.cwd;
42 if (args[0] !== 'run') {
43 const cacheDir = path.join(this.config.cacheDir, 'yarn');
44 args = [
45 ...args,
46 '--non-interactive',
47 `--mutex=file:${path.join(cwd, 'yarn.lock')}`,
48 `--preferred-cache-folder=${cacheDir}`,
49 '--check-files',
50 ];
51 if (opts.verbose) {
52 args.push('--verbose');
53 }
54 if (this.config.npmRegistry) {
55 args.push(`--registry=${this.config.npmRegistry}`);
56 }
57 }
58 const npmRunPath = require('npm-run-path');
59 const options = Object.assign(Object.assign({}, opts), { cwd, stdio: [0, null, null, 'ipc'], env: npmRunPath.env({ cwd, env: process.env }) });
60 if (opts.verbose) {
61 process.stderr.write(`${cwd}: ${this.bin} ${args.join(' ')}`);
62 }
63 debug(`${cwd}: ${this.bin} ${args.join(' ')}`);
64 try {
65 await this.fork(this.bin, args, options);
66 debug('done');
67 }
68 catch (error) {
69 // TODO: https://github.com/yarnpkg/yarn/issues/2191
70 const networkConcurrency = '--network-concurrency=1';
71 if (error.message.includes('EAI_AGAIN') && !args.includes(networkConcurrency)) {
72 debug('EAI_AGAIN');
73 return this.exec([...args, networkConcurrency], opts);
74 }
75 throw error;
76 }
77 }
78}
79exports.default = Yarn;