feat(pnpm): make pnpm add require the <pkg> parameter

PR #1976
close #1970
This commit is contained in:
ExE Boss
2019-08-26 14:31:21 +02:00
committed by Zoltan Kochan
parent ec05e534c9
commit e29c040d38
7 changed files with 70 additions and 19 deletions

View File

@@ -11,18 +11,9 @@ function getHelpText (command: string) {
switch (getCommandFullName(command)) {
case 'install':
return stripIndent`
pnpm install (with no args, in package dir)
pnpm install [<@scope>/]<name>
pnpm install [<@scope>/]<name>@<tag>
pnpm install [<@scope>/]<name>@<version>
pnpm install [<@scope>/]<name>@<version range>
pnpm install <git-host>:<git-user>/<repo-name>
pnpm install <git repo url>
pnpm install <tarball file>
pnpm install <tarball url>
pnpm install <folder>
pnpm install
Aliases: i, install, add
Aliases: i
Installs all dependencies of the project in the current working directory.
To install dependencies in every project of a monorepo, run \`pnpm recursive install\`
@@ -30,12 +21,6 @@ function getHelpText (command: string) {
Options:
-P, --save-prod save package to your \`dependencies\`. The default behavior
-D, --save-dev save package to your \`devDependencies\`
-O, --save-optional save package to your \`optionalDependencies\`
--save-peer save package to your \`peerDependencies\` and \`devDependencies\`
-E, --save-exact install exact version
-g, --global install as a global package
-r run installation recursively in every package found in subdirectories
or in every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
@@ -112,6 +97,33 @@ function getHelpText (command: string) {
Includes all packages that are under the current working directory.
`
case 'add':
return stripIndent`
pnpm add [<@scope>/]<name>
pnpm add [<@scope>/]<name>@<tag>
pnpm add [<@scope>/]<name>@<version>
pnpm add [<@scope>/]<name>@<version range>
pnpm add <git-host>:<git-user>/<repo-name>
pnpm add <git repo url>
pnpm add <tarball file>
pnpm add <tarball url>
pnpm add <folder>
Installs a package and any packages that it depends on.
Options:
-P, --save-prod save package to your \`dependencies\`. The default behavior
-D, --save-dev save package to your \`devDependencies\`
-O, --save-optional save package to your \`optionalDependencies\`
--save-peer save package to your \`peerDependencies\` and \`devDependencies\`
-E, --save-exact install exact version
-g, --global install as a global package
-r run installation recursively in every package found in subdirectories
or in every workspace package, when executed inside a workspace.
For options that may be used with \`-r\`, see "pnpm help recursive"
`
case 'import':
return stripIndent`
pnpm import
@@ -412,6 +424,8 @@ function getHelpText (command: string) {
install
add
update
uninstall [<@scope>/]<pkg>...
@@ -489,6 +503,7 @@ function getHelpText (command: string) {
manage your dependencies:
- install
- add
- update
- uninstall
- link
@@ -517,6 +532,7 @@ function getHelpText (command: string) {
manage you monorepo:
- recursive exec
- recursive install
- recursive add
- recursive list
- recursive outdated
- recursive rebuild

View File

@@ -18,6 +18,7 @@ import unlink from './unlink'
import update from './update'
export default {
add: install,
help,
import: importCmd,
install,

View File

@@ -28,7 +28,9 @@ export default async function installCmd (
input: string[],
opts: PnpmOptions & {
allowNew?: boolean,
useBetaCli?: boolean,
},
invocation?: string,
) {
// `pnpm install ""` is going to be just `pnpm install`
input = input.filter(Boolean)
@@ -73,6 +75,9 @@ export default async function installCmd (
delete installOpts.include
}
if (!input || !input.length) {
if (invocation === 'add' && opts.useBetaCli) {
throw new PnpmError('MISSING_PACKAGE_NAME', '`pnpm add` requires the package name')
}
await install(manifest, installOpts)
} else {
const [updatedImporter] = await mutateModules([

View File

@@ -102,6 +102,7 @@ export async function recursive (
allowNew?: boolean,
packageSelectors?: PackageSelector[],
ignoredPackages?: Set<string>,
useBetaCli?: boolean,
},
cmdFullName: string,
cmd: string,
@@ -143,6 +144,11 @@ export async function recursive (
case 'outdated':
await outdated(pkgs, input, cmd, opts as any) // tslint:disable-line:no-any
return true
case 'install':
if (cmd === 'add' && opts.useBetaCli && (!input || !input.length)) {
throw new PnpmError('MISSING_PACKAGE_NAME', '`pnpm recursive add` requires the package name')
}
break
}
const chunks = opts.sort

View File

@@ -1,6 +1,5 @@
export default function getCommandFullName (cmd: string) {
switch (cmd) {
case 'add':
case 'install':
case 'i':
return 'install'
@@ -41,7 +40,7 @@ export default function getCommandFullName (cmd: string) {
case 'multi':
case 'm':
return 'recursive'
// some commands have no aliases: publish, prune
// some commands have no aliases: publish, prune, add
default:
return cmd
}

View File

@@ -32,6 +32,7 @@ import initReporter, { ReporterType } from './reporter'
pnpmCmds['install-test'] = pnpmCmds.installTest
type CANONICAL_COMMAND_NAMES = 'help'
| 'add'
| 'import'
| 'install-test'
| 'install'
@@ -58,6 +59,7 @@ type CANONICAL_COMMAND_NAMES = 'help'
const COMMANDS_WITH_NO_DASHDASH_FILTER = new Set(['run', 'exec', 'restart', 'start', 'stop', 'test'])
const supportedCmds = new Set<CANONICAL_COMMAND_NAMES>([
'add',
'install',
'uninstall',
'update',

View File

@@ -302,6 +302,28 @@ test('pnpm install --save-peer', async (t) => {
}
})
test('`pnpm add` should fail if no package name was provided (beta)', (t: tape.Test) => {
prepare(t)
const { status, stdout } = execPnpmSync('add', '--use-beta-cli')
t.equal(status, 1)
t.ok(stdout.toString().includes('`pnpm add` requires the package name'))
t.end()
})
test('`pnpm recursive add` should fail if no package name was provided (beta)', (t: tape.Test) => {
prepare(t)
const { status, stdout } = execPnpmSync('recursive', 'add', '--use-beta-cli')
t.equal(status, 1)
t.ok(stdout.toString().includes('`pnpm recursive add` requires the package name'))
t.end()
})
test('install should fail if the used pnpm version does not satisfy the pnpm version specified in engines', async (t: tape.Test) => {
prepare(t, {
name: 'project',