fix(dlx): set saveProd to true for getting pkgName from dependencies (#7540)

close #7424

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
await-ovo
2024-01-19 23:27:29 +08:00
committed by Zoltan Kochan
parent 6964eade56
commit be27890eea
3 changed files with 27 additions and 2 deletions

View File

@@ -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).

View File

@@ -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
@@ -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> {

View File

@@ -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'
@@ -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)
})