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 Nov 5, 2023
1 parent 070564b commit 50ec0b2
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 39 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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/lib/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const npm = (
}

const npmOptions = {
...(options.global ? { location: 'global' } : null),
...(options.global ? { global: true } : null),
...(options.prefix ? { prefix: options.prefix } : null),
}

Expand Down
4 changes: 2 additions & 2 deletions src/package-managers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,7 +46,7 @@ export const list = async (options: Options = {}): Promise<Index<string | undefi
const stdout = await spawnBun(
['pm', 'ls'],
{
...(options.global ? { location: 'global' } : null),
...(options.global ? { global: true } : null),
...(options.prefix ? { prefix: options.prefix } : null),
},
{
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 @@ -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={}]
Expand All @@ -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)
}

Expand Down Expand Up @@ -645,8 +625,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 @@ -657,9 +636,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 @@ -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}` : []),
]
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 50ec0b2

Please sign in to comment.