-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathmain.ts
89 lines (72 loc) · 2.78 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
import {BaseContext, Builtins, Cli} from 'clipanion';
import {version as corepackVersion} from '../package.json';
import {Engine, PackageManagerRequest} from './Engine';
import {CacheCommand} from './commands/Cache';
import {DisableCommand} from './commands/Disable';
import {EnableCommand} from './commands/Enable';
import {InstallGlobalCommand} from './commands/InstallGlobal';
import {InstallLocalCommand} from './commands/InstallLocal';
import {PackCommand} from './commands/Pack';
import {ProjectInstallCommand} from './commands/Project';
import {UpCommand} from './commands/Up';
import {UseCommand} from './commands/Use';
import {HydrateCommand} from './commands/deprecated/Hydrate';
import {PrepareCommand} from './commands/deprecated/Prepare';
export type CustomContext = {cwd: string, engine: Engine};
export type Context = BaseContext & CustomContext;
function getPackageManagerRequestFromCli(parameter: string | undefined, engine: Engine): PackageManagerRequest | null {
if (!parameter)
return null;
const match = parameter.match(/^([^@]*)(?:@(.*))?$/);
if (!match)
return null;
const [, binaryName, binaryVersion] = match;
const packageManager = engine.getPackageManagerFor(binaryName)!;
if (packageManager == null && binaryVersion == null) return null;
return {
packageManager,
binaryName,
binaryVersion: binaryVersion || null,
};
}
export async function runMain(argv: Array<string>) {
const engine = new Engine();
const [firstArg, ...restArgs] = argv;
const request = getPackageManagerRequestFromCli(firstArg, engine);
if (!request) {
// If the first argument doesn't match any supported package manager, we fallback to the standard Corepack CLI
const cli = new Cli({
binaryLabel: `Corepack`,
binaryName: `corepack`,
binaryVersion: corepackVersion,
});
cli.register(Builtins.HelpCommand);
cli.register(Builtins.VersionCommand);
cli.register(CacheCommand);
cli.register(DisableCommand);
cli.register(EnableCommand);
cli.register(InstallGlobalCommand);
cli.register(InstallLocalCommand);
cli.register(PackCommand);
cli.register(UpCommand);
cli.register(UseCommand);
cli.register(ProjectInstallCommand);
// Deprecated commands
cli.register(HydrateCommand);
cli.register(PrepareCommand);
const context = {
...Cli.defaultContext,
cwd: process.cwd(),
engine,
};
const code = await cli.run(argv, context);
if (code !== 0) {
process.exitCode ??= code;
}
} else {
await engine.executePackageManagerRequest(request, {
cwd: process.cwd(),
args: restArgs,
});
}
}