File: Use.ts

package info (click to toggle)
node-corepack 0.24.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 262,916 kB
  • sloc: javascript: 94; makefile: 18; sh: 12
file content (40 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download
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
import {Command, Option, UsageError} from 'clipanion';

import {BaseCommand}                 from './Base';

export class UseCommand extends BaseCommand {
  static paths = [
    [`use`],
  ];

  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@*'`,
    ]],
  });

  pattern = Option.String();

  async execute() {
    const [descriptor] = await this.resolvePatternsToDescriptors({
      all: false,
      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`);

    const packageManagerInfo = await this.context.engine.ensurePackageManager(resolved);
    await this.setLocalPackageManager(packageManagerInfo);
  }
}