fix: recursive cmd add reverse option (#3984)

close #3972
This commit is contained in:
zoomdong
2021-11-15 01:05:25 +08:00
committed by GitHub
parent 828e3b9e48
commit 435626ad39
8 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
---
"@pnpm/plugin-commands-script-runners": minor
"pnpm": minor
---
Added `--reverse` option support to `pnpm exec` [#3984](https://github.com/pnpm/pnpm/issues/3972).
Usage example:
```
pnpm --reverse -r exec pwd
```

View File

@@ -14,6 +14,7 @@
"test-with-preview": "ts-node test",
"_test": "jest",
"test": "pnpm run compile && pnpm run _test",
"start": "pnpm tsc --watch",
"compile": "rimraf lib tsconfig.tsbuildinfo && tsc --build && pnpm run lint -- --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/config",

View File

@@ -16,6 +16,7 @@
"_test": "jest",
"test": "pnpm run compile && pnpm run _test",
"prepublishOnly": "pnpm run compile",
"start": "pnpm tsc --watch",
"compile": "rimraf lib tsconfig.tsbuildinfo && tsc --build && pnpm run lint -- --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/parse-cli-args",

View File

@@ -19,6 +19,7 @@
"_test": "cross-env PNPM_REGISTRY_MOCK_PORT=7778 pnpm run test:e2e",
"test": "pnpm run compile && pnpm run _test",
"prepublishOnly": "pnpm run compile",
"start": "pnpm tsc --watch",
"compile": "rimraf lib tsconfig.tsbuildinfo && tsc --build && pnpm run lint -- --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/master/packages/plugin-commands-script-runners",

View File

@@ -35,6 +35,7 @@ export function rcOptionsTypes () {
export const cliOptionsTypes = () => ({
...rcOptionsTypes(),
recursive: Boolean,
reverse: Boolean,
})
export function help () {
@@ -66,6 +67,7 @@ export async function handler (
bail?: boolean
unsafePerm?: boolean
rawConfig: object
reverse?: boolean
sort?: boolean
workspaceConcurrency?: number
} & Pick<Config, 'extraBinPaths' | 'lockfileDir' | 'dir' | 'recursive' | 'workspaceDir'>,
@@ -87,6 +89,9 @@ export async function handler (
chunks = opts.sort
? sortPackages(opts.selectedProjectsGraph)
: [Object.keys(opts.selectedProjectsGraph).sort()]
if (opts.reverse) {
chunks = chunks.reverse()
}
} else {
chunks = [[opts.dir]]
const project = await tryReadProjectManifest(opts.dir)

View File

@@ -64,6 +64,7 @@ export function cliOptionsTypes () {
], allTypes),
...IF_PRESENT_OPTION,
recursive: Boolean,
reverse: Boolean,
}
}

View File

@@ -294,6 +294,68 @@ test('pnpm recursive exec --no-sort', async () => {
expect(outputs).toStrictEqual(['a-dependent', 'b-dependency'])
})
test('pnpm recursive exec --reverse', 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',
},
},
{
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',
},
},
{
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 ../output1.json',
},
},
])
const { selectedProjectsGraph } = await readProjects(process.cwd(), [])
await execa(pnpmBin, [
'install',
'-r',
'--registry',
REGISTRY,
'--store-dir',
path.resolve(DEFAULT_OPTS.storeDir),
])
await exec.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
selectedProjectsGraph,
recursive: true,
sort: true,
reverse: true,
}, ['npm', 'run', 'build'])
const { default: outputs1 } = await import(path.resolve('output1.json'))
expect(outputs1).toStrictEqual(['project-2', 'project-3', 'project-1'])
})
test('pnpm exec on single project', async () => {
prepare({})

View File

@@ -15,3 +15,6 @@ ${COMPATIBILITY_PAGE}`)
}
require('../dist/pnpm.cjs')
// if you want to debug at your local env, you can use this
// require('../lib/pnpm')