From a6b1c5ccc3913dda9ae0cc1afd4d00991ee88919 Mon Sep 17 00:00:00 2001 From: Raine Revere Date: Wed, 13 Sep 2023 12:41:22 +0000 Subject: [PATCH] Replace --location=global with --global. --global was erroneously deprecated in node v16.15.1 and npm v8.12.0 on Windows. https://github.com/npm/cli/issues/4980 https://github.com/npm/cli/pull/4982 --- package-lock.json | 3 ++- package.json | 3 ++- src/package-managers/bun.ts | 2 +- src/package-managers/npm.ts | 39 ++++++++---------------------------- src/package-managers/pnpm.ts | 2 +- src/package-managers/yarn.ts | 2 +- src/types/NpmOptions.ts | 2 +- 7 files changed, 16 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index fff9fe573..d258d253d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -103,7 +103,8 @@ "yarn": "^1.22.19" }, "engines": { - "node": ">=16" + "node": "^16 < 16.15.1 || >16.15.1", + "npm": ">=8.12.1" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/package.json b/package.json index 1ba25dd72..847a9d957 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "pnpm" ], "engines": { - "node": ">=16" + "node": "^16 < 16.15.1 || >16.15.1", + "npm": ">=8.12.1" }, "main": "build/src/index.js", "scripts": { diff --git a/src/package-managers/bun.ts b/src/package-managers/bun.ts index 022ab4149..9c03c9124 100644 --- a/src/package-managers/bun.ts +++ b/src/package-managers/bun.ts @@ -24,7 +24,7 @@ async function spawnBun( const fullArgs = [ ...args, ...(npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : []), - ...(npmOptions.location === 'global' ? ['--global'] : []), + ...(npmOptions.global ? ['--global'] : []), ] return spawn('bun', fullArgs, spawnOptions) diff --git a/src/package-managers/npm.ts b/src/package-managers/npm.ts index 0e66b5086..fe6d41487 100644 --- a/src/package-managers/npm.ts +++ b/src/package-managers/npm.ts @@ -209,21 +209,6 @@ const findNpmConfig = memoize((configPath?: string): NpmConfig | null => { // this may be partially overwritten by .npmrc config files when using --deep const npmConfig = findNpmConfig() -/** A promise that returns true if --global is deprecated on the system npm. Spawns "npm --version". */ -const isGlobalDeprecated = memoize(async () => { - const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm' - const output = await spawn(cmd, ['--version']) - const npmVersion = output.trim() - // --global was deprecated in npm v8.11.0. - return nodeSemver.valid(npmVersion) && nodeSemver.gte(npmVersion, '8.11.0') -}) - -/** - * @typedef {object} CommandAndPackageName - * @property {string} command - * @property {string} packageName - */ - /** * Parse JSON and throw an informative error on failure. * @@ -505,7 +490,7 @@ export async function viewOne( } /** - * Spawns npm with --json. Handles different commands for Window and Linux/OSX, and automatically converts --location=global to --global on npm < 8.11.0. + * Spawns npm with --json. Handles different commands for Window and Linux/OSX. * * @param args * @param [npmOptions={}] @@ -520,17 +505,12 @@ async function spawnNpm( const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm' args = Array.isArray(args) ? args : [args] - const fullArgs = args.concat( - npmOptions.location - ? (await isGlobalDeprecated()) - ? `--location=${npmOptions.location}` - : npmOptions.location === 'global' - ? '--global' - : '' - : [], - npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : [], + const fullArgs = [ + ...args, + ...(npmOptions.global ? [`--global`] : []), + ...(npmOptions.prefix ? [`--prefix=${npmOptions.prefix}`] : []), '--json', - ) + ] return spawn(cmd, fullArgs, spawnOptions) } @@ -645,8 +625,7 @@ export const list = async (options: Options = {}): Promise }>(result, { - command: `npm${process.platform === 'win32' ? '.cmd' : ''} ls --json${options.global ? ' --location=global' : ''}${ - options.prefix ? ' --prefix ' + options.prefix : '' - }`, + command: `npm${process.platform === 'win32' ? '.cmd' : ''} ls --json${options.global ? ' --global' : ''}`, }).dependencies return keyValueBy(dependencies, (name, info) => ({ diff --git a/src/package-managers/pnpm.ts b/src/package-managers/pnpm.ts index c76d83397..f6e1d87ff 100644 --- a/src/package-managers/pnpm.ts +++ b/src/package-managers/pnpm.ts @@ -75,7 +75,7 @@ const spawnPnpm = async ( const cmd = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm' const fullArgs = [ - ...(npmOptions.location === 'global' ? 'global' : []), + ...(npmOptions.global ? [`--global`] : []), ...(Array.isArray(args) ? args : [args]), ...(npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : []), ] diff --git a/src/package-managers/yarn.ts b/src/package-managers/yarn.ts index 6c943a871..443b26b19 100644 --- a/src/package-managers/yarn.ts +++ b/src/package-managers/yarn.ts @@ -202,7 +202,7 @@ async function spawnYarn( const cmd = process.platform === 'win32' ? 'yarn.cmd' : 'yarn' const fullArgs = [ - ...(yarnOptions.location === 'global' ? 'global' : []), + ...(yarnOptions.global ? 'global' : []), ...(Array.isArray(args) ? args : [args]), '--depth=0', ...(yarnOptions.prefix ? `--prefix=${yarnOptions.prefix}` : []), diff --git a/src/types/NpmOptions.ts b/src/types/NpmOptions.ts index 360ffb8f2..25842ec4f 100644 --- a/src/types/NpmOptions.ts +++ b/src/types/NpmOptions.ts @@ -1,6 +1,6 @@ /** Options that can be provided to npm. */ export interface NpmOptions { - location?: string + global?: boolean prefix?: string registry?: string }