Skip to content

Commit

Permalink
fix(dlx): set saveProd to true for getting pkgName from dependencies (#…
Browse files Browse the repository at this point in the history
…7540)

close #7424

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
await-ovo and zkochan committed Jan 19, 2024
1 parent 6964ead commit be27890
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-apricots-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---

Set saveProd to true for getting pkgName from dependencies [7424](https://github.com/pnpm/pnpm/issues/7424).
10 changes: 9 additions & 1 deletion exec/plugin-commands-script-runners/src/dlx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export async function handler (
dir: prefix,
lockfileDir: prefix,
rootProjectManifestDir: prefix, // This property won't be used as rootProjectManifest will be undefined
saveProd: true, // dlx will be looking for the package in the "dependencies" field!
saveDev: false,
saveOptional: false,
savePeer: false,
}, pkgs)
const binName = opts.package
? command
Expand All @@ -119,7 +123,11 @@ export async function handler (

async function getPkgName (pkgDir: string) {
const manifest = await readPackageJsonFromDir(pkgDir)
return Object.keys(manifest.dependencies ?? {})[0]
const dependencyNames = Object.keys(manifest.dependencies ?? {})
if (dependencyNames.length === 0) {
throw new PnpmError('DLX_NO_DEP', 'dlx was unable to find the installed dependency in "dependencies"')
}
return dependencyNames[0]
}

async function getBinName (modulesDir: string, pkgName: string): Promise<string> {
Expand Down
13 changes: 12 additions & 1 deletion pnpm/test/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs, mkdirSync } from 'fs'
import path from 'path'
import PATH_NAME from 'path-name'
import { prepare, preparePackages } from '@pnpm/prepare'
import { prepare, prepareEmpty, preparePackages } from '@pnpm/prepare'
import isWindows from 'is-windows'
import { execPnpm, execPnpmSync } from './utils'

Expand Down Expand Up @@ -259,3 +259,14 @@ test('--reporter-hide-prefix should hide workspace prefix', async () => {
expect(output).toContain('2')
expect(output).not.toContain('script2: 2')
})

test('dlx should work with npm_config_save_dev env variable', async () => {
prepareEmpty()
const result = execPnpmSync(['dlx', '@foo/touch-file-one-bin@latest'], {
env: {
npm_config_save_dev: 'true',
},
stdio: 'inherit',
})
expect(result.status).toBe(0)
})

0 comments on commit be27890

Please sign in to comment.