feat: add --reverse flag (#3119)

close #2985
This commit is contained in:
Jonathan Morley
2021-02-04 12:31:30 -05:00
committed by GitHub
parent 66f96b457e
commit c4cc625063
8 changed files with 92 additions and 4 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/config": minor
"@pnpm/plugin-commands-script-runners": minor
"pnpm": minor
---
Add '--reverse' flag for reversing the order of package executions during 'recursive run'

View File

@@ -104,6 +104,7 @@ export interface Config {
reporter?: string
linkWorkspacePackages: boolean | 'deep'
preferWorkspacePackages: boolean
reverse: boolean
sort: boolean
strictPeerDependencies: boolean
lockfileDir?: string

View File

@@ -177,6 +177,7 @@ export default async (
'shared-workspace-shrinkwrap': true,
'shell-emulator': false,
shrinkwrap: npmDefaults.shrinkwrap,
reverse: false,
sort: true,
'strict-peer-dependencies': false,
'unsafe-perm': npmDefaults['unsafe-perm'],

View File

@@ -4,11 +4,11 @@ import PnpmError from '@pnpm/error'
import { makeNodeRequireOption } from '@pnpm/lifecycle'
import logger from '@pnpm/logger'
import sortPackages from '@pnpm/sort-packages'
import existsInDir from './existsInDir'
import {
PARALLEL_OPTION_HELP,
shorthands as runShorthands,
} from './run'
import existsInDir from './existsInDir'
import execa = require('execa')
import pLimit = require('p-limit')
import R = require('ramda')

View File

@@ -18,7 +18,7 @@ export type RecursiveRunOpts = Pick<Config,
| 'scriptShell'
| 'shellEmulator'
> & Required<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>> &
Partial<Pick<Config, 'extraBinPaths' | 'bail' | 'sort' | 'workspaceConcurrency'>> &
Partial<Pick<Config, 'extraBinPaths' | 'bail' | 'reverse' | 'sort' | 'workspaceConcurrency'>> &
{
ifPresent?: boolean
}
@@ -32,9 +32,11 @@ export default async (
throw new PnpmError('SCRIPT_NAME_IS_REQUIRED', 'You must specify the script you want to run')
}
let hasCommand = 0
const packageChunks = opts.sort
const sortedPackageChunks = opts.sort
? sortPackages(opts.selectedProjectsGraph)
: [Object.keys(opts.selectedProjectsGraph).sort()]
const packageChunks = opts.reverse ? sortedPackageChunks.reverse() : sortedPackageChunks
const result = {
fails: [],

View File

@@ -9,10 +9,10 @@ import {
import prepare, { preparePackages } from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { DEFAULT_OPTS, REGISTRY } from './utils'
import path = require('path')
import execa = require('execa')
import isWindows = require('is-windows')
import fs = require('mz/fs')
import path = require('path')
import writeYamlFile = require('write-yaml-file')
const pnpmBin = path.join(__dirname, '../../pnpm/bin/pnpm.js')

View File

@@ -82,6 +82,79 @@ test('pnpm recursive run', async () => {
expect(outputs2).toStrictEqual(['project-1', 'project-3'])
})
test('pnpm recursive run reversed', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
'json-append': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-1\')" | json-append ../output1.json && node -e "process.stdout.write(\'project-1\')" | json-append ../output2.json',
},
},
{
name: 'project-2',
version: '1.0.0',
dependencies: {
'json-append': '1',
'project-1': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-2\')" | json-append ../output1.json',
postbuild: 'node -e "process.stdout.write(\'project-2-postbuild\')" | json-append ../output1.json',
prebuild: 'node -e "process.stdout.write(\'project-2-prebuild\')" | json-append ../output1.json',
},
},
{
name: 'project-3',
version: '1.0.0',
dependencies: {
'json-append': '1',
'project-1': '1',
},
scripts: {
build: 'node -e "process.stdout.write(\'project-3\')" | json-append ../output2.json',
},
},
{
name: 'project-0',
version: '1.0.0',
dependencies: {},
},
])
const { allProjects, selectedProjectsGraph } = await readProjects(process.cwd(), [])
await execa('pnpm', [
'install',
'-r',
'--registry',
REGISTRY,
'--store-dir',
path.resolve(DEFAULT_OPTS.storeDir),
])
await run.handler({
...DEFAULT_OPTS,
allProjects,
dir: process.cwd(),
recursive: true,
reverse: true,
selectedProjectsGraph,
workspaceDir: process.cwd(),
}, ['build'])
const { default: outputs1 } = await import(path.resolve('output1.json'))
const { default: outputs2 } = await import(path.resolve('output2.json'))
expect(outputs1).toStrictEqual(['project-2-prebuild', 'project-2', 'project-2-postbuild', 'project-1'])
expect(outputs2).toStrictEqual(['project-3', 'project-1'])
})
test('pnpm recursive run concurrently', async () => {
preparePackages([
{

View File

@@ -89,6 +89,10 @@ and must recompile all your C++ addons with the new binary.',
Convenient to use in a multi-package repository.',
name: '--link-workspace-packages',
},
{
description: 'Reverse the order that packages get ordered in. Disabled by default.',
name: '--reverse',
},
{
description: 'Sort packages topologically (dependencies before dependents). Pass --no-sort to disable.',
name: '--sort',