|
| 1 | +import {Command, UsageError} from 'clipanion'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import which from 'which'; |
| 5 | + |
| 6 | +import {Context} from '../main'; |
| 7 | +import {isSupportedPackageManager, SupportedPackageManagerSet} from '../types'; |
| 8 | + |
| 9 | +export class EnableCommand extends Command<Context> { |
| 10 | + static usage = Command.Usage({ |
| 11 | + description: `Add the Corepack shims to the install directories`, |
| 12 | + details: ` |
| 13 | + 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. |
| 14 | +
|
| 15 | + 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 \`--bin-folder\` flag. |
| 16 | + `, |
| 17 | + examples: [[ |
| 18 | + `Enable all shims, putting them next to the \`corepath\` binary`, |
| 19 | + `$0 enable`, |
| 20 | + ], [ |
| 21 | + `Enable all shims, putting them in the specified directory`, |
| 22 | + `$0 enable --bin-folder /path/to/folder`, |
| 23 | + ], [ |
| 24 | + `Enable the Yarn shim only`, |
| 25 | + `$0 enable yarn`, |
| 26 | + ]], |
| 27 | + }); |
| 28 | + |
| 29 | + @Command.String(`--install-directory`) |
| 30 | + installDirectory?: string; |
| 31 | + |
| 32 | + @Command.Rest() |
| 33 | + names: Array<string> = []; |
| 34 | + |
| 35 | + @Command.Path(`enable`) |
| 36 | + async execute() { |
| 37 | + let installDirectory = this.installDirectory; |
| 38 | + |
| 39 | + // Node always call realpath on the module it executes, so we already |
| 40 | + // lost track of how the binary got called. To find it back, we need to |
| 41 | + // iterate over the PATH variable. |
| 42 | + if (typeof installDirectory === `undefined`) |
| 43 | + installDirectory = path.dirname(await which(`corepack`)); |
| 44 | + |
| 45 | + if (process.platform === `win32`) { |
| 46 | + return this.executeWin32(installDirectory); |
| 47 | + } else { |
| 48 | + return this.executePosix(installDirectory); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async executePosix(installDirectory: string) { |
| 53 | + // We use `eval` so that Webpack doesn't statically transform it. |
| 54 | + const manifestPath = eval(`require`).resolve(`corepack/package.json`); |
| 55 | + |
| 56 | + const stubFolder = path.join(path.dirname(manifestPath), `shims`); |
| 57 | + if (!fs.existsSync(stubFolder)) |
| 58 | + throw new Error(`Assertion failed: The stub folder doesn't exist`); |
| 59 | + |
| 60 | + const names = this.names.length === 0 |
| 61 | + ? SupportedPackageManagerSet |
| 62 | + : this.names; |
| 63 | + |
| 64 | + for (const name of new Set(names)) { |
| 65 | + if (!isSupportedPackageManager(name)) |
| 66 | + throw new UsageError(`Invalid package manager name '${name}'`); |
| 67 | + |
| 68 | + for (const binName of this.context.engine.getBinariesFor(name)) { |
| 69 | + const file = path.join(installDirectory, binName); |
| 70 | + const symlink = path.relative(installDirectory, path.join(stubFolder, binName)); |
| 71 | + |
| 72 | + if (fs.existsSync(file)) { |
| 73 | + const currentSymlink = await fs.promises.readlink(file); |
| 74 | + if (currentSymlink !== symlink) { |
| 75 | + await fs.promises.unlink(file); |
| 76 | + } else { |
| 77 | + return; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + await fs.promises.symlink(symlink, file); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + async executeWin32(target: string) { |
| 87 | + throw new UsageError(`This command isn't available on Windows at this time`); |
| 88 | + } |
| 89 | +} |
0 commit comments