Skip to content

Commit

Permalink
Replace --location=global with --global.
Browse files Browse the repository at this point in the history
--global was erroneously deprecated in node v16.15.1 and npm v8.12.0 on Windows.

npm/cli#4980
npm/cli#4982
  • Loading branch information
raineorshine committed Sep 13, 2023
1 parent ef7308e commit 6e65de6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 36 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/package-managers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function spawnBun(
const fullArgs = [
...args,
...(npmOptions.prefix ? `--prefix=${npmOptions.prefix}` : []),
...(npmOptions.location === 'global' ? ['--global'] : []),
...(npmOptions.global ? ['--global'] : []),
]

return spawn('bun', fullArgs, spawnOptions)
Expand Down
39 changes: 8 additions & 31 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -476,7 +461,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={}]
Expand All @@ -491,17 +476,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)
}

Expand Down Expand Up @@ -616,8 +596,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
const result = await spawnNpm(
['ls', '--depth=0'],
{
// spawnNpm takes the modern --location option and converts it to --global on older versions of npm
...(options.global ? { location: 'global' } : null),
...(options.global ? { global: true } : null),
...(options.prefix ? { prefix: options.prefix } : null),
},
{
Expand All @@ -628,9 +607,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
const dependencies = parseJson<{
dependencies: Index<{ version?: Version; required?: { version: Version } }>
}>(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) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/package-managers/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default async function spawnPnpm(
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}` : []),
]
Expand Down
2 changes: 1 addition & 1 deletion src/package-managers/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}` : []),
Expand Down
2 changes: 1 addition & 1 deletion src/types/NpmOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Options that can be provided to npm. */
export interface NpmOptions {
location?: string
global?: boolean
prefix?: string
registry?: string
}

0 comments on commit 6e65de6

Please sign in to comment.