File: Pack.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 (87 lines) | stat: -rw-r--r-- 3,121 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
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
import {Command, Option, UsageError} from 'clipanion';
import {mkdir}                       from 'fs/promises';
import path                          from 'path';

import * as folderUtils              from '../folderUtils';

import {BaseCommand}                 from './Base';

export class PackCommand extends BaseCommand {
  static paths = [
    [`pack`],
  ];

  static usage = Command.Usage({
    description: `Store package managers in a tarball`,
    details: `
      Download the selected package managers and store them inside a tarball suitable for use with \`corepack install -g\`.
    `,
    examples: [[
      `Pack the package manager defined in the package.json file`,
      `corepack pack`,
    ], [
      `Pack the latest version of Yarn 1.x inside a file named corepack.tgz`,
      `corepack pack yarn@^1`,
    ], [
      `Pack the latest versions of all supported package managers inside a file named everything.tgz`,
      `corepack pack --all -o everything.tgz`,
    ]],
  });

  all = Option.Boolean(`--all`, false, {
    description: `If true, all available default package managers will be installed`,
  });

  json = Option.Boolean(`--json`, false, {
    description: `If true, the path to the generated tarball will be printed on stdout`,
  });

  output = Option.String(`-o,--output`, {
    description: `Where the tarball should be generated; by default "corepack.tgz"`,
  });

  patterns = Option.Rest();

  async execute() {
    const descriptors = await this.resolvePatternsToDescriptors({
      all: this.all,
      patterns: this.patterns,
    });

    const installLocations: Array<string> = [];

    for (const descriptor of descriptors) {
      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(`Adding ${resolved.name}@${resolved.reference} to the cache...\n`);
      const packageManagerInfo = await this.context.engine.ensurePackageManager(resolved);

      await this.context.engine.activatePackageManager(packageManagerInfo.locator);
      installLocations.push(packageManagerInfo.location);
    }

    const baseInstallFolder = folderUtils.getInstallFolder();
    const outputPath = path.resolve(this.context.cwd, this.output ?? `corepack.tgz`);

    if (!this.json) {
      this.context.stdout.write(`\n`);
      this.context.stdout.write(`Packing the selected tools in ${path.basename(outputPath)}...\n`);
    }

    const {default: tar} = await import(`tar`);

    // Recreate the folder in case it was deleted somewhere else:
    await mkdir(baseInstallFolder, {recursive: true});
    await tar.c({gzip: true, cwd: baseInstallFolder, file: path.resolve(outputPath)}, installLocations.map(location => {
      return path.relative(baseInstallFolder, location);
    }));

    if (this.json) {
      this.context.stdout.write(`${JSON.stringify(outputPath)}\n`);
    } else {
      this.context.stdout.write(`All done!\n`);
    }
  }
}