-
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathUse.ts
More file actions
75 lines (63 loc) · 2.37 KB
/
Use.ts
File metadata and controls
75 lines (63 loc) · 2.37 KB
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
import {Command, Option, UsageError} from 'clipanion';
import {fetchLatestStableVersion} from '../corepackUtils';
import {BaseCommand} from './Base';
export class UseCommand extends BaseCommand {
static paths = [
[`use`],
];
fromNpm = Option.Boolean(`--from-npm`, false, {
description: `If true, the package manager will be installed from the npm registry`,
});
static usage = Command.Usage({
description: `Define the package manager to use for the current project`,
details: `
When run, this command will retrieve the latest release matching the
provided descriptor, assign it to the project's package.json file, and
automatically perform an install.
`,
examples: [[
`Configure the project to use the latest Yarn release`,
`corepack use yarn`,
], [
`Configure the project to use the latest Yarn release available from the "yarn" package on the npm registry`,
`corepack use yarn --from-npm`,
]],
});
pattern = Option.String();
async execute() {
let packageManagerInfo: Parameters<typeof this.setLocalPackageManager>[0];
if (this.fromNpm) {
const registry = {
type: `npm` as const,
package: this.pattern,
};
const versionWithHash = await fetchLatestStableVersion(registry);
const [version, hash] = versionWithHash.split(`+`);
const location = `https://registry.npmjs.com/${this.pattern}/-/${this.pattern}-${version}.tgz`;
packageManagerInfo = {
locator: {
name: this.pattern,
reference: location,
},
spec: {
bin: {},
registry,
url: location,
},
hash,
location,
bin: undefined,
};
} else {
const [descriptor] = await this.resolvePatternsToDescriptors({
patterns: [this.pattern],
});
const resolved = await this.context.engine.resolveDescriptor(descriptor, {allowTags: true, useCache: false});
if (resolved === null)
throw new UsageError(`Failed to successfully resolve '${descriptor.range}' to a valid ${descriptor.name} release`);
this.context.stdout.write(`Installing ${resolved.name}@${resolved.reference} in the project...\n`);
packageManagerInfo = await this.context.engine.ensurePackageManager(resolved);
}
await this.setLocalPackageManager(packageManagerInfo);
}
}