UNPKG

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