Skip to content

Commit

Permalink
fix(exec): pnpm exec should work outside of Node.js projects (#3597)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jul 13, 2021
1 parent 0f62aa3 commit 9476d5a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-moose-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-script-runners": patch
---

`pnpm exec` should work outside of Node.js projects.
22 changes: 13 additions & 9 deletions packages/plugin-commands-script-runners/src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { RecursiveSummary, throwOnCommandFail } from '@pnpm/cli-utils'
import { Config, types } from '@pnpm/config'
import { makeNodeRequireOption } from '@pnpm/lifecycle'
import logger from '@pnpm/logger'
import readProjectManifest from '@pnpm/read-project-manifest'
import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
import sortPackages from '@pnpm/sort-packages'
import { Project } from '@pnpm/types'
import execa from 'execa'
import pLimit from 'p-limit'
import PATH from 'path-name'
Expand Down Expand Up @@ -87,14 +88,17 @@ export async function handler (
: [Object.keys(opts.selectedProjectsGraph).sort()]
} else {
chunks = [[opts.dir]]
opts.selectedProjectsGraph = {
[opts.dir]: {
dependencies: [],
package: {
...await readProjectManifest(opts.dir),
dir: opts.dir,
const project = await tryReadProjectManifest(opts.dir)
if (project.manifest != null) {
opts.selectedProjectsGraph = {
[opts.dir]: {
dependencies: [],
package: {
...project,
dir: opts.dir,
} as Project,
},
},
}
}
}
const existsPnp = existsInDir.bind(null, '.pnp.cjs')
Expand All @@ -118,7 +122,7 @@ export async function handler (
path.join(opts.dir, 'node_modules/.bin'),
process.env[PATH],
].join(path.delimiter),
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph[prefix].package.manifest.name,
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph?.[prefix]?.package.manifest.name,
},
stdio: 'inherit',
})
Expand Down
16 changes: 15 additions & 1 deletion packages/plugin-commands-script-runners/test/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import PnpmError from '@pnpm/error'
import { readProjects } from '@pnpm/filter-workspace-packages'
import { exec } from '@pnpm/plugin-commands-script-runners'
import prepare, { preparePackages } from '@pnpm/prepare'
import prepare, { prepareEmpty, preparePackages } from '@pnpm/prepare'
import rimraf from '@zkochan/rimraf'
import execa from 'execa'
import { DEFAULT_OPTS, REGISTRY } from './utils'
Expand Down Expand Up @@ -308,6 +308,20 @@ test('pnpm exec on single project', async () => {
expect(outputs).toStrictEqual([])
})

test('pnpm exec outside of projects', async () => {
prepareEmpty()

await exec.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
recursive: false,
selectedProjectsGraph: {},
}, ['node', '-e', 'require("fs").writeFileSync("output.json", "[]", "utf8")'])

const { default: outputs } = await import(path.resolve('output.json'))
expect(outputs).toStrictEqual([])
})

test('pnpm recursive exec works with PnP', async () => {
preparePackages([
{
Expand Down

0 comments on commit 9476d5a

Please sign in to comment.