Skip to content

fix: make npm run autocomplete work with workspaces #8135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class RunScript extends BaseCommand {
static async completion (opts, npm) {
const argv = opts.conf.argv.remain
if (argv.length === 2) {
const { content: { scripts = {} } } = await pkgJson.normalize(npm.localPrefix)
const workspacePrefixes = npm.config.get('workspace', 'default')
const localPrefix = workspacePrefixes.length
? workspacePrefixes[0]
: npm.localPrefix
const { content: { scripts = {} } } = await pkgJson.normalize(localPrefix)
.catch(() => ({ content: {} }))
if (opts.isFish) {
return Object.keys(scripts).map(s => `${s}\t${scripts[s].slice(0, 30)}`)
Expand Down
19 changes: 19 additions & 0 deletions test/lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { resolve } = require('node:path')
const realRunScript = require('@npmcli/run-script')
const mockNpm = require('../../fixtures/mock-npm')
const { cleanCwd } = require('../../fixtures/clean-snapshot')
const path = require('node:path')

const mockRs = async (t, { windows = false, runScript, ...opts } = {}) => {
let RUN_SCRIPTS = []
Expand Down Expand Up @@ -491,6 +492,7 @@ t.test('workspaces', async t => {
prefixDir,
workspaces = true,
exec = [],
chdir = ({ prefix }) => prefix,
...config
} = {}) => {
const mock = await mockRs(t, {
Expand Down Expand Up @@ -554,6 +556,7 @@ t.test('workspaces', async t => {
...Array.isArray(workspaces) ? { workspace: workspaces } : { workspaces },
...config,
},
chdir,
runScript,
})
if (exec) {
Expand All @@ -562,6 +565,22 @@ t.test('workspaces', async t => {
return mock
}

t.test('completion', async t => {
t.test('in root dir', async t => {
const { runScript } = await mockWorkspaces(t)
const res = await runScript.completion({ conf: { argv: { remain: ['npm', 'run'] } } })
t.strictSame(res, [])
})

t.test('in workspace dir', async t => {
const { runScript } = await mockWorkspaces(t, {
chdir: ({ prefix }) => path.join(prefix, 'packages/c'),
})
const res = await runScript.completion({ conf: { argv: { remain: ['npm', 'run'] } } })
t.strictSame(res, ['test', 'posttest', 'lorem'])
})
})

t.test('list all scripts', async t => {
const { joinedOutput } = await mockWorkspaces(t)
t.matchSnapshot(joinedOutput())
Expand Down
Loading