-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcmd-info.ts
88 lines (74 loc) · 2.48 KB
/
cmd-info.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import colors from 'yoctocolors-cjs'
import { logger } from '@socketsecurity/registry/lib/logger'
import { handlePackageInfo } from './handle-package-info'
import constants from '../../constants'
import { commonFlags, outputFlags, validationFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
const { DRY_RUN_BAIL_TEXT } = constants
const config: CliCommandConfig = {
commandName: 'info',
description: 'Look up info regarding a package',
hidden: false,
flags: {
...commonFlags,
...outputFlags,
...validationFlags
},
help: (command, config) => `
Usage
$ ${command} <name>
Options
${getFlagListOutput(config.flags, 6)}
Examples
$ ${command} webtorrent
$ ${command} [email protected]
`
}
export const cmdInfo = {
description: config.description,
hidden: config.hidden,
run
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})
const { all, json, markdown, strict } = cli.flags
const [rawPkgName = ''] = cli.input
if (!rawPkgName || cli.input.length > 1) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
process.exitCode = 2
logger.fail(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Expecting a package name ${!rawPkgName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- Can only accept one package at a time ${cli.input.length > 1 ? colors.red('(got ' + cli.input.length + '!)') : colors.green('(ok)')}\n`)
return
}
const versionSeparator = rawPkgName.lastIndexOf('@')
const pkgName =
versionSeparator < 1 ? rawPkgName : rawPkgName.slice(0, versionSeparator)
const pkgVersion =
versionSeparator < 1 ? 'latest' : rawPkgName.slice(versionSeparator + 1)
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}
await handlePackageInfo({
commandName: `${parentName} ${config.commandName}`,
includeAllIssues: Boolean(all),
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
pkgName,
pkgVersion,
strict: Boolean(strict)
})
}