Skip to content

Commit b7ff353

Browse files
committed
Integrate --help into more things
1 parent 747d86e commit b7ff353

27 files changed

+276
-332
lines changed

src/commands/analytics.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import chalk from 'chalk'
77
import meow from 'meow'
88
import ora from 'ora'
99

10-
import { outputFlags } from '../flags'
10+
import { commonFlags, outputFlags } from '../flags'
1111
import {
1212
handleApiCall,
1313
handleUnsuccessfulApiResponse
@@ -103,10 +103,10 @@ function setupCommand(
103103
): void | CommandContext {
104104
const flags: { [key: string]: any } = {
105105
__proto__: null,
106+
...commonFlags,
106107
...outputFlags,
107108
...analyticsFlags
108109
}
109-
110110
const cli = meow(
111111
`
112112
Usage
@@ -127,31 +127,30 @@ function setupCommand(
127127
flags
128128
}
129129
)
130-
131-
const { json: outputJson, scope, time, repo, file } = cli.flags
132-
130+
const { repo, scope, time } = cli.flags
133131
if (scope !== 'org' && scope !== 'repo') {
134132
throw new InputError("The scope must either be 'org' or 'repo'")
135133
}
136-
137134
if (time !== 7 && time !== 30 && time !== 90) {
138135
throw new InputError('The time filter must either be 7, 30 or 90')
139136
}
140-
137+
let showHelp = cli.flags['help']
141138
if (scope === 'repo' && !repo) {
139+
showHelp = true
142140
console.error(
143141
`${chalk.bgRed.white('Input error')}: Please provide a repository name when using the repository scope. \n`
144142
)
143+
}
144+
if (showHelp) {
145145
cli.showHelp()
146146
return
147147
}
148-
149148
return <CommandContext>{
150149
scope,
151150
time,
152151
repo,
153-
outputJson,
154-
file
152+
outputJson: cli.flags['json'],
153+
file: cli.flags['file']
155154
}
156155
}
157156

src/commands/audit-log.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from 'chalk'
33
import meow from 'meow'
44
import ora from 'ora'
55

6-
import { outputFlags } from '../flags'
6+
import { commonFlags, outputFlags } from '../flags'
77
import {
88
handleApiCall,
99
handleUnsuccessfulApiResponse
@@ -75,9 +75,9 @@ function setupCommand(
7575
const flags: { [key: string]: any } = {
7676
__proto__: null,
7777
...auditLogFlags,
78+
...commonFlags,
7879
...outputFlags
7980
}
80-
8181
const cli = meow(
8282
`
8383
Usage
@@ -96,25 +96,25 @@ function setupCommand(
9696
flags
9797
}
9898
)
99-
100-
const {
101-
json: outputJson,
102-
markdown: outputMarkdown,
103-
page,
104-
perPage
105-
} = cli.flags
106-
107-
const type = <string>cli.flags['type']
108-
99+
let showHelp = cli.flags['help']
109100
if (cli.input.length < 1) {
101+
showHelp = true
110102
console.error(
111103
`${chalk.white.bgRed('Input error')}: Please provide an organization slug\n`
112104
)
105+
}
106+
if (showHelp) {
113107
cli.showHelp()
114108
return
115109
}
110+
const {
111+
json: outputJson,
112+
markdown: outputMarkdown,
113+
page,
114+
perPage
115+
} = cli.flags
116+
const type = <string>cli.flags['type']
116117
const { 0: orgSlug = '' } = cli.input
117-
118118
return <CommandContext>{
119119
outputJson,
120120
outputMarkdown,

src/commands/dependencies.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import chalkTable from 'chalk-table'
44
import meow from 'meow'
55
import ora from 'ora'
66

7-
import { outputFlags } from '../flags'
7+
import { commonFlags, outputFlags } from '../flags'
88
import {
99
handleApiCall,
1010
handleUnsuccessfulApiResponse
@@ -60,8 +60,9 @@ function setupCommand(
6060
): CommandContext | undefined {
6161
const flags: { [key: string]: any } = {
6262
__proto__: null,
63-
...outputFlags,
64-
...dependenciesFlags
63+
...commonFlags,
64+
...dependenciesFlags,
65+
...outputFlags
6566
}
6667

6768
const cli = meow(

src/commands/diff-scan/get.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk from 'chalk'
55
import meow from 'meow'
66
import ora from 'ora'
77

8-
import { outputFlags } from '../../flags'
8+
import { commonFlags, outputFlags } from '../../flags'
99
import { handleAPIError, queryAPI } from '../../utils/api-helpers'
1010
import { AuthError } from '../../utils/errors'
1111
import { printFlagList } from '../../utils/formatting'
@@ -80,10 +80,10 @@ function setupCommand(
8080
): CommandContext | undefined {
8181
const flags: { [key: string]: any } = {
8282
__proto__: null,
83-
...outputFlags,
84-
...getDiffScanFlags
83+
...commonFlags,
84+
...getDiffScanFlags,
85+
...outputFlags
8586
}
86-
8787
const cli = meow(
8888
`
8989
Usage
@@ -102,42 +102,32 @@ function setupCommand(
102102
flags
103103
}
104104
)
105-
106-
const {
107-
json: outputJson,
108-
markdown: outputMarkdown,
109-
before,
110-
after,
111-
preview,
112-
file
113-
} = cli.flags
114-
105+
const { before, after } = cli.flags
106+
let showHelp = cli.flags['help']
115107
if (!before || !after) {
108+
showHelp = true
116109
console.error(
117110
`${chalk.bgRed.white('Input error')}: Please specify a before and after full scan ID. To get full scans IDs, you can run the command "socket scan list <your org slug>".\n`
118111
)
119-
cli.showHelp()
120-
return
121-
}
122-
123-
if (cli.input.length < 1) {
112+
} else if (cli.input.length < 1) {
113+
showHelp = true
124114
console.error(
125115
`${chalk.bgRed.white('Input error')}: Please provide an organization slug\n`
126116
)
117+
}
118+
if (showHelp) {
127119
cli.showHelp()
128120
return
129121
}
130-
131122
const [orgSlug = ''] = cli.input
132-
133123
return <CommandContext>{
134-
outputJson,
135-
outputMarkdown,
124+
outputJson: cli.flags['json'],
125+
outputMarkdown: cli.flags['markdown'],
136126
before,
137127
after,
138-
preview,
128+
preview: cli.flags['preview'],
139129
orgSlug,
140-
file
130+
file: cli.flags['file']
141131
}
142132
}
143133

src/commands/info.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from 'chalk'
22
import meow from 'meow'
33
import ora from 'ora'
44

5-
import { outputFlags, validationFlags } from '../flags'
5+
import { commonFlags, outputFlags, validationFlags } from '../flags'
66
import {
77
handleApiCall,
88
handleUnsuccessfulApiResponse
@@ -72,10 +72,10 @@ function setupCommand(
7272
): void | CommandContext {
7373
const flags: { [key: string]: any } = {
7474
__proto__: null,
75+
...commonFlags,
7576
...outputFlags,
7677
...validationFlags
7778
}
78-
7979
const cli = meow(
8080
`
8181
Usage
@@ -95,39 +95,30 @@ function setupCommand(
9595
flags
9696
}
9797
)
98-
99-
const {
100-
all: includeAllIssues,
101-
json: outputJson,
102-
markdown: outputMarkdown,
103-
strict
104-
} = cli.flags
105-
10698
if (cli.input.length > 1) {
10799
throw new InputError('Only one package lookup supported at once')
108100
}
109-
110101
const { 0: rawPkgName = '' } = cli.input
111-
102+
let showHelp = cli.flags['help']
112103
if (!rawPkgName) {
104+
showHelp = true
105+
}
106+
if (showHelp) {
113107
cli.showHelp()
114108
return
115109
}
116-
117110
const versionSeparator = rawPkgName.lastIndexOf('@')
118-
119111
const pkgName =
120112
versionSeparator < 1 ? rawPkgName : rawPkgName.slice(0, versionSeparator)
121113
const pkgVersion =
122114
versionSeparator < 1 ? 'latest' : rawPkgName.slice(versionSeparator + 1)
123-
124115
return {
125-
includeAllIssues,
126-
outputJson,
127-
outputMarkdown,
116+
includeAllIssues: cli.flags['all'],
117+
outputJson: cli.flags['json'],
118+
outputMarkdown: cli.flags['markdown'],
128119
pkgName,
129120
pkgVersion,
130-
strict
121+
strict: cli.flags['strict']
131122
} as CommandContext
132123
}
133124

src/commands/login.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,19 @@ export const login: CliSubcommand = {
7272
flags
7373
}
7474
)
75-
75+
let showHelp = cli.flags['help']
7676
if (cli.input.length) {
77+
showHelp = true
78+
}
79+
if (showHelp) {
7780
cli.showHelp()
81+
return
7882
}
79-
8083
if (!isInteractive()) {
8184
throw new InputError(
8285
'Cannot prompt for credentials in a non-interactive shell'
8386
)
8487
}
85-
8688
const apiKey =
8789
(await password({
8890
message: `Enter your ${terminalLink(

src/commands/logout.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ export const logout: CliSubcommand = {
2727
importMeta
2828
}
2929
)
30-
30+
let showHelp = cli.flags['help']
3131
if (cli.input.length) {
32+
showHelp = true
33+
}
34+
if (showHelp) {
3235
cli.showHelp()
36+
return
3337
}
34-
3538
updateSetting('apiKey', null)
3639
updateSetting('apiBaseUrl', null)
3740
updateSetting('apiProxy', null)

src/commands/optimize.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ora from 'ora'
99
import pacote from 'pacote'
1010
import semver from 'semver'
1111

12+
import { commonFlags } from '../flags'
1213
import { printFlagList } from '../utils/formatting'
1314
import { hasOwn } from '../utils/objects'
1415
import { detect } from '../utils/package-manager-detector'
@@ -230,12 +231,12 @@ async function addOverrides(
230231
await pEach(overridesDataObjects, 3, async ({ overrides, type }) => {
231232
const overrideExists = hasOwn(overrides, origPkgName)
232233
if (overrideExists || lockIncludes(lockSrc, origPkgName)) {
233-
// With npm you may not set an override for a package that you directly
234-
// depend on unless both the dependency and the override itself share
234+
// With npm one may not set an override for a package that one directly
235+
// depends on unless both the dependency and the override itself share
235236
// the exact same spec. To make this limitation easier to deal with,
236237
// overrides may also be defined as a reference to a spec for a direct
237-
// dependency by prefixing the name of the package you wish the version
238-
// to match with a $.
238+
// dependency by prefixing the name of the package to match the version
239+
// of with a $.
239240
// https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides
240241
const oldSpec = overrides[origPkgName]
241242
const depAlias = depAliasMap.get(origPkgName)
@@ -305,7 +306,7 @@ export const optimize: CliSubcommand = {
305306
description: 'Optimize dependencies with @socketregistry overrides',
306307
async run(argv, importMeta, { parentName }) {
307308
const commandContext = setupCommand(
308-
`${parentName} dependency optimize`,
309+
`${parentName} optimize`,
309310
optimize.description,
310311
argv,
311312
importMeta
@@ -441,13 +442,14 @@ function setupCommand(
441442
importMeta: ImportMeta
442443
): CommandContext | undefined {
443444
const flags: { [key: string]: any } = {
445+
__proto__: null,
446+
...commonFlags,
444447
pin: {
445448
type: 'boolean',
446449
default: false,
447450
description: 'Pin overrides to their latest version'
448451
}
449452
}
450-
451453
const cli = meow(
452454
`
453455
Usage
@@ -466,9 +468,11 @@ function setupCommand(
466468
flags
467469
}
468470
)
469-
470-
const { pin } = cli.flags
471-
471+
const { help, pin } = cli.flags
472+
if (help) {
473+
cli.showHelp()
474+
return
475+
}
472476
return <CommandContext>{
473477
pin
474478
}

0 commit comments

Comments
 (0)