Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
pnpm doctor
command to do checks for know common issues
- Loading branch information
Showing
7 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@pnpm/plugin-commands-audit": minor | ||
"pnpm": minor | ||
--- | ||
|
||
Add `pnpm doctor` command to do checks for know common issues |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import renderHelp from 'render-help' | ||
import { docsUrl } from '@pnpm/cli-utils' | ||
import { logger } from '@pnpm/logger' | ||
|
||
export const rcOptionsTypes = cliOptionsTypes | ||
|
||
export function cliOptionsTypes () { | ||
return { | ||
config: Boolean, | ||
} | ||
} | ||
|
||
export const shorthands = { | ||
C: '--config', | ||
} | ||
|
||
export const commandNames = ['doctor'] | ||
|
||
export function help () { | ||
return renderHelp({ | ||
description: 'Checks for known common issues.', | ||
descriptionLists: [ | ||
{ | ||
title: 'Options', | ||
list: [ | ||
{ | ||
description: 'Check the global config', | ||
name: '--config', | ||
shortAlias: '-C', | ||
}, | ||
], | ||
}, | ||
], | ||
url: docsUrl('doctor'), | ||
usages: ['pnpm doctor [options]'], | ||
}) | ||
} | ||
|
||
export async function handler ( | ||
opts: { | ||
config: boolean | ||
}) { | ||
if (opts.config) { | ||
const paths = require.resolve.paths('npm') | ||
try { | ||
require.resolve('npm', { paths: paths?.slice(-1) }) | ||
} catch (e) { | ||
// If error, means loading npm builtin config failed | ||
if ( | ||
process.platform === 'darwin' && | ||
process.env.HOMEBREW_PREFIX && | ||
process.execPath.startsWith(process.env.HOMEBREW_PREFIX) | ||
) { | ||
// Npm installed via brew may have prefix error, related: https://github.com/pnpm/pnpm/issues/5404 | ||
logger.warn({ | ||
message: 'Load npm builtin configs failed. If the prefix builtin config does not work, you can use "pnpm config ls" to show builtin configs. And then use "pnpm config --global set <key> <value>" to migrate configs from builtin to global.', | ||
prefix: process.cwd(), | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import * as audit from './audit' | ||
import * as doctor from './doctor' | ||
|
||
export { audit } | ||
export { | ||
audit, | ||
doctor, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { doctor } from '@pnpm/plugin-commands-audit' | ||
import sinon from 'sinon' | ||
import { logger } from '@pnpm/logger' | ||
|
||
test('doctor --config', async () => { | ||
const oldExecPath = process.execPath | ||
const oldPlatform = process.platform | ||
const oldEnv = process.env | ||
const HOMEBREW_PREFIX = '.' | ||
|
||
process.env = { ...oldEnv, HOMEBREW_PREFIX } | ||
process.execPath = HOMEBREW_PREFIX | ||
// platform is read only | ||
Object.defineProperty(process, 'platform', { | ||
value: 'darwin', | ||
}) | ||
|
||
const reporter = sinon.spy(logger, 'warn') | ||
|
||
// In the scope of jest, require.resolve.paths('npm') cannot reach global npm path by default | ||
await doctor.handler({ | ||
config: true, | ||
}) | ||
|
||
expect(reporter.calledWithMatch({ | ||
message: 'Load npm builtin configs failed. If the prefix builtin config does not work, you can use "pnpm config ls" to show builtin configs. And then use "pnpm config --global set <key> <value>" to migrate configs from builtin to global.', | ||
prefix: process.cwd(), | ||
})).toBeTruthy() | ||
|
||
process.env = oldEnv | ||
process.execPath = oldExecPath | ||
Object.defineProperty(process, 'platform', { | ||
value: oldPlatform, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.