Skip to content

Commit

Permalink
feat: expanded missing command complaints, including 'did you mean' s…
Browse files Browse the repository at this point in the history
…uggestion
  • Loading branch information
JoshuaKGoldberg committed May 2, 2023
1 parent fcfbf96 commit 925cd1b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli/cli-utils/src/readProjectManifest.ts
Expand Up @@ -23,7 +23,7 @@ export async function readProjectManifestOnly (
opts: {
engineStrict?: boolean
nodeVersion?: string
}
} = {}
): Promise<ProjectManifest> {
const manifest = await utils.readProjectManifestOnly(projectDir)
packageIsInstallable(projectDir, manifest as any, opts) // eslint-disable-line @typescript-eslint/no-explicit-any
Expand Down
1 change: 1 addition & 0 deletions exec/plugin-commands-script-runners/package.json
Expand Up @@ -58,6 +58,7 @@
"@pnpm/store-path": "workspace:*",
"@pnpm/types": "workspace:*",
"@zkochan/rimraf": "^2.1.2",
"didyoumean2": "^5.0.0",
"execa": "npm:safe-execa@0.1.2",
"p-limit": "^3.1.0",
"path-exists": "^4.0.0",
Expand Down
7 changes: 6 additions & 1 deletion exec/plugin-commands-script-runners/src/exec.ts
@@ -1,5 +1,5 @@
import path from 'path'
import { docsUrl, type RecursiveSummary, throwOnCommandFail } from '@pnpm/cli-utils'
import { docsUrl, type RecursiveSummary, throwOnCommandFail, readProjectManifestOnly } from '@pnpm/cli-utils'
import { type Config, types } from '@pnpm/config'
import { makeNodeRequireOption } from '@pnpm/lifecycle'
import { logger } from '@pnpm/logger'
Expand All @@ -20,6 +20,7 @@ import {
} from './run'
import { PnpmError } from '@pnpm/error'
import writeJsonFile from 'write-json-file'
import { buildCommandNotFoundHint } from './utils'

export const shorthands = {
parallel: runShorthands.parallel,
Expand Down Expand Up @@ -226,6 +227,10 @@ export async function handler (
return
}

if (err.originalMessage === `spawn ${params[0]} ENOENT`) {
err.hint = buildCommandNotFoundHint(params[0], (await readProjectManifestOnly(opts.dir)).scripts)
}

if (!err['code']?.startsWith('ERR_PNPM_')) {
err['code'] = 'ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL'
}
Expand Down
6 changes: 5 additions & 1 deletion exec/plugin-commands-script-runners/src/run.ts
Expand Up @@ -21,6 +21,7 @@ import renderHelp from 'render-help'
import { runRecursive, type RecursiveRunOpts, getSpecifiedScripts as getSpecifiedScriptWithoutStartCommand } from './runRecursive'
import { existsInDir } from './existsInDir'
import { handler as exec } from './exec'
import { buildCommandNotFoundHint } from './utils'

export const IF_PRESENT_OPTION = {
'if-present': Boolean,
Expand Down Expand Up @@ -197,7 +198,10 @@ so you may run "pnpm -w run ${scriptName}"`,
})
}
}
throw new PnpmError('NO_SCRIPT', `Missing script: ${scriptName}`)

throw new PnpmError('NO_SCRIPT', `Missing script: ${scriptName}`, {
hint: buildCommandNotFoundHint(scriptName, manifest.scripts),
})
}
const lifecycleOpts: RunLifecycleHookOptions = {
depPath: dir,
Expand Down
16 changes: 16 additions & 0 deletions exec/plugin-commands-script-runners/src/utils.ts
@@ -0,0 +1,16 @@
import { type PackageScripts } from '@pnpm/types'
import didYouMean, { ReturnTypeEnums } from 'didyoumean2'

export function buildCommandNotFoundHint (scriptName: string, scripts?: PackageScripts | undefined) {
let hint = `Command "${scriptName}" not found.`

const nearestCommand = scripts && didYouMean(scriptName, Object.keys(scripts), {
returnType: ReturnTypeEnums.FIRST_CLOSEST_MATCH,
})

if (nearestCommand) {
hint += ` Did you mean "pnpm run ${nearestCommand}"`
}

return hint
}
19 changes: 19 additions & 0 deletions exec/plugin-commands-script-runners/test/index.ts
Expand Up @@ -585,3 +585,22 @@ test('pnpm run with RegExp script selector with flag should throw error', async
}
expect(err.message).toBe('RegExp flags are not supported in script command selector')
})

test('pnpm run with slightly incorrect command suggests correct one', async () => {
prepare({
scripts: {
build: 'echo 0',
},
})

await expect(run.handler({
dir: process.cwd(),
extraBinPaths: [],
extraEnv: {},
rawConfig: {},
workspaceConcurrency: 1,
}, ['buil'])).rejects.toEqual(expect.objectContaining({
code: 'ERR_PNPM_NO_SCRIPT',
hint: 'Command "buil" not found. Did you mean "pnpm run build"',
}))
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 925cd1b

Please sign in to comment.