1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs-extra");
|
4 | const path = require("path");
|
5 | const debug = require('debug')('cli:yarn');
|
6 | class Yarn {
|
7 | constructor({ config, cwd }) {
|
8 | this.config = config;
|
9 | this.cwd = cwd;
|
10 | }
|
11 | get bin() {
|
12 | return require.resolve('yarn/bin/yarn.js');
|
13 | }
|
14 | fork(modulePath, args = [], options = {}) {
|
15 | return new Promise((resolve, reject) => {
|
16 | const { fork } = require('child_process');
|
17 | let forked = fork(modulePath, args, options);
|
18 | forked.stdout.on('data', (d) => process.stdout.write(d));
|
19 | forked.stderr.on('data', (d) => process.stderr.write(d));
|
20 | forked.on('error', reject);
|
21 | forked.on('exit', (code) => {
|
22 | if (code === 0) {
|
23 | resolve();
|
24 | }
|
25 | else {
|
26 | reject(new Error(`yarn ${args.join(' ')} exited with code ${code}`));
|
27 | }
|
28 | });
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | });
|
34 | }
|
35 | async exec(args = []) {
|
36 | if (args.length !== 0)
|
37 | await this.checkForYarnLock();
|
38 | if (args[0] !== 'run') {
|
39 | const cacheDir = path.join(this.config.cacheDir, 'yarn');
|
40 | args = [
|
41 | ...args,
|
42 | '--non-interactive',
|
43 | `--mutex=file:${path.join(this.cwd, 'yarn.lock')}`,
|
44 | `--preferred-cache-folder=${cacheDir}`,
|
45 | ...this.proxyArgs(),
|
46 | ];
|
47 | if (this.config.npmRegistry) {
|
48 | args.push(`--registry=${this.config.npmRegistry}`);
|
49 | }
|
50 | }
|
51 | const npmRunPath = require('npm-run-path');
|
52 | let options = {
|
53 | cwd: this.cwd,
|
54 | stdio: [0, null, null, 'ipc'],
|
55 | env: npmRunPath.env({ cwd: this.cwd, env: process.env }),
|
56 | };
|
57 | debug(`${this.cwd}: ${this.bin} ${args.join(' ')}`);
|
58 | try {
|
59 | await this.fork(this.bin, args, options);
|
60 | debug('done');
|
61 | }
|
62 | catch (err) {
|
63 |
|
64 | let networkConcurrency = '--network-concurrency=1';
|
65 | if (err.message.includes('EAI_AGAIN') && !args.includes(networkConcurrency)) {
|
66 | debug('EAI_AGAIN');
|
67 | return this.exec([...args, networkConcurrency]);
|
68 | }
|
69 | throw err;
|
70 | }
|
71 | }
|
72 | async checkForYarnLock() {
|
73 |
|
74 | if (this.cwd && !await fs.pathExists(path.join(this.cwd, 'yarn.lock'))) {
|
75 | await this.exec();
|
76 | }
|
77 | }
|
78 | proxyArgs() {
|
79 | let args = [];
|
80 | let http = process.env.http_proxy || process.env.HTTP_PROXY;
|
81 | let https = process.env.https_proxy || process.env.HTTPS_PROXY;
|
82 | if (http)
|
83 | args.push(`--proxy=${http}`);
|
84 | if (https)
|
85 | args.push(`--https-proxy=${https}`);
|
86 | return args;
|
87 | }
|
88 | }
|
89 | exports.default = Yarn;
|