File: Enable.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 (99 lines) | stat: -rw-r--r-- 3,899 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
88
89
90
91
92
93
94
95
96
97
98
99
import cmdShim                                                           from '@zkochan/cmd-shim';
import {Command, Option, UsageError}                                     from 'clipanion';
import fs                                                                from 'fs';
import path                                                              from 'path';
import which                                                             from 'which';

import {Context}                                                         from '../main';
import {isSupportedPackageManager, SupportedPackageManagerSetWithoutNpm} from '../types';

export class EnableCommand extends Command<Context> {
  static paths = [
    [`enable`],
  ];

  static usage = Command.Usage({
    description: `Add the Corepack shims to the install directories`,
    details: `
      When run, this commmand will check whether the shims for the specified package managers can be found with the correct values inside the install directory. If not, or if they don't exist, they will be created.

      By default it will locate the install directory by running the equivalent of \`which corepack\`, but this can be tweaked by explicitly passing the install directory via the \`--install-directory\` flag.
    `,
    examples: [[
      `Enable all shims, putting them next to the \`corepack\` binary`,
      `$0 enable`,
    ], [
      `Enable all shims, putting them in the specified directory`,
      `$0 enable --install-directory /path/to/folder`,
    ], [
      `Enable the Yarn shim only`,
      `$0 enable yarn`,
    ]],
  });

  installDirectory = Option.String(`--install-directory`, {
    description: `Where the shims are to be installed`,
  });

  names = Option.Rest();

  async execute() {
    let installDirectory = this.installDirectory;

    // Node always call realpath on the module it executes, so we already
    // lost track of how the binary got called. To find it back, we need to
    // iterate over the PATH variable.
    if (typeof installDirectory === `undefined`)
      installDirectory = path.dirname(await which(`corepack`));

    // Otherwise the relative symlink we'll compute will be incorrect, if the
    // install directory is within a symlink
    installDirectory = fs.realpathSync(installDirectory);

    const manifestPath = require.resolve(`corepack/package.json`);

    const distFolder = path.join(path.dirname(manifestPath), `dist`);
    if (!fs.existsSync(distFolder))
      throw new Error(`Assertion failed: The stub folder doesn't exist`);

    const names = this.names.length === 0
      ? SupportedPackageManagerSetWithoutNpm
      : this.names;

    for (const name of new Set(names)) {
      if (!isSupportedPackageManager(name))
        throw new UsageError(`Invalid package manager name '${name}'`);

      for (const binName of this.context.engine.getBinariesFor(name)) {
        if (process.platform === `win32`) {
          await this.generateWin32Link(installDirectory, distFolder, binName);
        } else {
          await this.generatePosixLink(installDirectory, distFolder, binName);
        }
      }
    }
  }

  async generatePosixLink(installDirectory: string, distFolder: string, binName: string) {
    const file = path.join(installDirectory, binName);
    const symlink = path.relative(installDirectory, path.join(distFolder, `${binName}.js`));

    if (fs.existsSync(file)) {
      const currentSymlink = await fs.promises.readlink(file);
      if (currentSymlink !== symlink) {
        await fs.promises.unlink(file);
      } else {
        return;
      }
    }

    await fs.promises.symlink(symlink, file);
  }

  async generateWin32Link(installDirectory: string, distFolder: string, binName: string) {
    const file = path.join(installDirectory, binName);
    await cmdShim(path.join(distFolder, `${binName}.js`), file, {
      createCmdFile: true,
    });
  }
}