feat: have if-present apply to recursive runs

PR #2520
This commit is contained in:
Jonathan Morley
2020-05-01 16:39:41 -04:00
committed by Zoltan Kochan
parent f8d6a07fed
commit c80d4ba3c3
4 changed files with 33 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-script-runners": minor
---
Support if-present flag for recursive run

View File

@@ -79,7 +79,6 @@ export function help () {
}
export type RunOpts = Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'> & {
ifPresent?: boolean,
recursive?: boolean,
} & Pick<Config, 'dir' | 'engineStrict'> & (
{ recursive?: false } &

View File

@@ -13,7 +13,10 @@ export type RecursiveRunOpts = Pick<Config,
'unsafePerm' |
'rawConfig'
> & Required<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>> &
Partial<Pick<Config, 'extraBinPaths' | 'bail' | 'sort' | 'workspaceConcurrency'>>
Partial<Pick<Config, 'extraBinPaths' | 'bail' | 'sort' | 'workspaceConcurrency'>> &
{
ifPresent?: boolean,
}
export default async (
params: string[],
@@ -87,7 +90,7 @@ export default async (
)))
}
if (scriptName !== 'test' && !hasCommand) {
if (scriptName !== 'test' && !hasCommand && !opts.ifPresent) {
const allPackagesAreSelected = Object.keys(opts.selectedProjectsGraph).length === opts.allProjects.length
if (allPackagesAreSelected) {
throw new PnpmError('RECURSIVE_RUN_NO_SCRIPT', `None of the packages has a "${scriptName}" script`)

View File

@@ -135,7 +135,7 @@ test('pnpm recursive run concurrently', async (t) => {
t.end()
})
test('`pnpm recursive run` fails when run without filters and no package has the desired command', async (t) => {
test('`pnpm recursive run` fails when run without filters and no package has the desired command, unless if-present is set', async (t) => {
const projects = preparePackages(t, [
{
name: 'project-1',
@@ -173,6 +173,17 @@ test('`pnpm recursive run` fails when run without filters and no package has the
path.resolve(DEFAULT_OPTS.storeDir),
])
t.comment('recursive run does not fail when if-present is true')
await run.handler({
...DEFAULT_OPTS,
allProjects,
dir: process.cwd(),
ifPresent: true,
recursive: true,
selectedProjectsGraph,
workspaceDir: process.cwd(),
}, ['this-command-does-not-exist'])
let err!: PnpmError
try {
await run.handler({
@@ -190,7 +201,7 @@ test('`pnpm recursive run` fails when run without filters and no package has the
t.end()
})
test('`pnpm recursive run` fails when run with a filter that includes all packages and no package has the desired command', async (t) => {
test('`pnpm recursive run` fails when run with a filter that includes all packages and no package has the desired command, unless if-present is set', async (t) => {
const projects = preparePackages(t, [
{
name: 'project-1',
@@ -218,6 +229,16 @@ test('`pnpm recursive run` fails when run with a filter that includes all packag
},
])
t.comment('recursive run does not fail when if-present is true')
await run.handler({
...DEFAULT_OPTS,
...await readProjects(process.cwd(), [{ namePattern: '*' }]),
dir: process.cwd(),
ifPresent: true,
recursive: true,
workspaceDir: process.cwd(),
}, ['this-command-does-not-exist'])
let err!: PnpmError
try {
await run.handler({