-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.ts
166 lines (140 loc) Β· 6.25 KB
/
main.ts
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
import {Configuration, CommandContext, PluginConfiguration, TelemetryManager, semverUtils, miscUtils} from '@yarnpkg/core';
import {PortablePath, npath, xfs} from '@yarnpkg/fslib';
import {execFileSync} from 'child_process';
import {isCI} from 'ci-info';
import {Cli, UsageError} from 'clipanion';
import {realpathSync} from 'fs';
import {pluginCommands} from './pluginCommands';
function runBinary(path: PortablePath) {
const physicalPath = npath.fromPortablePath(path);
process.on(`SIGINT`, () => {
// We don't want SIGINT to kill our process; we want it to kill the
// innermost process, whose end will cause our own to exit.
});
if (physicalPath) {
execFileSync(process.execPath, [physicalPath, ...process.argv.slice(2)], {
stdio: `inherit`,
env: {
...process.env,
YARN_IGNORE_PATH: `1`,
YARN_IGNORE_CWD: `1`,
},
});
} else {
execFileSync(physicalPath, process.argv.slice(2), {
stdio: `inherit`,
env: {
...process.env,
YARN_IGNORE_PATH: `1`,
YARN_IGNORE_CWD: `1`,
},
});
}
}
export async function main({binaryVersion, pluginConfiguration}: {binaryVersion: string, pluginConfiguration: PluginConfiguration}) {
async function run(): Promise<void> {
const cli = new Cli<CommandContext>({
binaryLabel: `Yarn Package Manager`,
binaryName: `yarn`,
binaryVersion,
});
try {
await exec(cli);
} catch (error) {
process.stdout.write(cli.error(error));
process.exitCode = 1;
}
}
async function exec(cli: Cli<CommandContext>): Promise<void> {
// Non-exhaustive known requirements:
// - 14.0 and 14.1 empty http responses - https://github.com/sindresorhus/got/issues/1496
// - 14.10.0 broken streams - https://github.com/nodejs/node/pull/34035 (fix: https://github.com/nodejs/node/commit/0f94c6b4e4)
const version = process.versions.node;
const range = `>=12 <14 || 14.2 - 14.9 || >14.10.0`;
// YARN_IGNORE_NODE is special because this code needs to execute as early as possible.
// It's not a regular core setting because Configuration.find may use functions not available
// on older Node versions.
const ignoreNode = miscUtils.parseOptionalBoolean(process.env.YARN_IGNORE_NODE);
if (!ignoreNode && !semverUtils.satisfiesWithPrereleases(version, range))
throw new UsageError(`This tool requires a Node version compatible with ${range} (got ${version}). Upgrade Node, or set \`YARN_IGNORE_NODE=1\` in your environment.`);
// Since we only care about a few very specific settings (yarn-path and ignore-path) we tolerate extra configuration key.
// If we didn't, we wouldn't even be able to run `yarn config` (which is recommended in the invalid config error message)
const configuration = await Configuration.find(npath.toPortablePath(process.cwd()), pluginConfiguration, {
usePath: true,
strict: false,
});
const yarnPath: PortablePath = configuration.get(`yarnPath`);
const ignorePath = configuration.get(`ignorePath`);
const ignoreCwd = configuration.get(`ignoreCwd`);
const selfPath = npath.toPortablePath(npath.resolve(process.argv[1]));
const tryRead = (p: PortablePath) => xfs.readFilePromise(p).catch(() => {
return Buffer.of();
});
const isSameBinary = async () =>
yarnPath === selfPath ||
Buffer.compare(...await Promise.all([
tryRead(yarnPath),
tryRead(selfPath),
])) === 0;
// Avoid unnecessary spawn when run directly
if (!ignorePath && !ignoreCwd && await isSameBinary()) {
process.env.YARN_IGNORE_PATH = `1`;
process.env.YARN_IGNORE_CWD = `1`;
await exec(cli);
return;
} else if (yarnPath !== null && !ignorePath) {
if (!xfs.existsSync(yarnPath)) {
process.stdout.write(cli.error(new Error(`The "yarn-path" option has been set (in ${configuration.sources.get(`yarnPath`)}), but the specified location doesn't exist (${yarnPath}).`)));
process.exitCode = 1;
} else {
try {
runBinary(yarnPath);
} catch (error) {
process.exitCode = error.code || 1;
}
}
} else {
if (ignorePath)
delete process.env.YARN_IGNORE_PATH;
const isTelemetryEnabled = configuration.get(`enableTelemetry`);
if (isTelemetryEnabled && !isCI && process.stdout.isTTY)
Configuration.telemetry = new TelemetryManager(configuration, `puba9cdc10ec5790a2cf4969dd413a47270`);
Configuration.telemetry?.reportVersion(binaryVersion);
for (const [name, plugin] of configuration.plugins.entries()) {
if (pluginCommands.has(name.match(/^@yarnpkg\/plugin-(.*)$/)?.[1] ?? ``))
Configuration.telemetry?.reportPluginName(name);
for (const command of plugin.commands || []) {
cli.register(command);
}
}
const command = cli.process(process.argv.slice(2));
if (!command.help)
Configuration.telemetry?.reportCommandName(command.path.join(` `));
// @ts-expect-error: The cwd is a global option defined by BaseCommand
const cwd: string | undefined = command.cwd;
if (typeof cwd !== `undefined` && !ignoreCwd) {
const iAmHere = realpathSync(process.cwd());
const iShouldBeHere = realpathSync(cwd);
if (iAmHere !== iShouldBeHere) {
process.chdir(cwd);
await run();
return;
}
}
await cli.runExit(command, {
cwd: npath.toPortablePath(process.cwd()),
plugins: pluginConfiguration,
quiet: false,
stdin: process.stdin,
stdout: process.stdout,
stderr: process.stderr,
});
}
}
return run()
.catch(error => {
process.stdout.write(error.stack || error.message);
process.exitCode = 1;
})
.finally(() => xfs.rmtempPromise());
}