Skip to content

Commit 78b7610

Browse files
committed
Simplify command imports to help maintainability
1 parent f1bd316 commit 78b7610

File tree

24 files changed

+121
-98
lines changed

24 files changed

+121
-98
lines changed

src/cli.ts

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,35 @@ import { messageWithCauses, stackWithCauses } from 'pony-cause'
77
import updateNotifier from 'tiny-updater'
88
import colors from 'yoctocolors-cjs'
99

10-
import * as cliCommands from './commands'
10+
import { actionCommand } from './commands/action'
11+
import { analyticsCommand } from './commands/analytics'
12+
import { auditLogCommand } from './commands/audit-log'
13+
import { cdxgenCommand } from './commands/cdxgen'
14+
import { dependenciesCommand } from './commands/dependencies'
15+
import { diffScanCommand } from './commands/diff-scan'
16+
import { fixCommand } from './commands/fix'
17+
import { infoCommand } from './commands/info'
18+
import { loginCommand } from './commands/login'
19+
import { logoutCommand } from './commands/logout'
20+
import { manifestCommand } from './commands/manifest'
21+
import { npmCommand } from './commands/npm'
22+
import { npxCommand } from './commands/npx'
23+
import { optimizeCommand } from './commands/optimize'
24+
import { organizationCommand } from './commands/organization'
25+
import { rawNpmCommand } from './commands/raw-npm'
26+
import { rawNpxCommand } from './commands/raw-npx'
27+
import { reportCommand } from './commands/report'
28+
import { reposCommand } from './commands/repos'
29+
import { scanCommand } from './commands/scan'
30+
import { threatFeedCommand } from './commands/threat-feed'
31+
import { wrapperCommand } from './commands/wrapper'
1132
import constants from './constants'
1233
import { AuthError, InputError } from './utils/errors'
1334
import { logSymbols } from './utils/logging'
1435
import { meowWithSubcommands } from './utils/meow-with-subcommands'
1536

1637
const { rootPkgJsonPath } = constants
1738

18-
const formattedCliCommands = Object.fromEntries(
19-
Object.entries(cliCommands).map(entry => {
20-
const key = entry[0]
21-
entry[0] = camelToHyphen(key)
22-
return entry
23-
})
24-
)
25-
26-
function camelToHyphen(str: string): string {
27-
return str.replace(/[A-Z]+/g, '-$&').toLowerCase()
28-
}
29-
3039
// TODO: Add autocompletion using https://socket.dev/npm/package/omelette
3140
void (async () => {
3241
await updateNotifier({
@@ -36,17 +45,43 @@ void (async () => {
3645
})
3746

3847
try {
39-
await meowWithSubcommands(formattedCliCommands, {
40-
aliases: {
41-
ci: {
42-
description: 'Alias for "report create --view --strict"',
43-
argv: ['report', 'create', '--view', '--strict']
44-
}
48+
await meowWithSubcommands(
49+
{
50+
action: actionCommand,
51+
cdxgen: cdxgenCommand,
52+
fix: fixCommand,
53+
info: infoCommand,
54+
login: loginCommand,
55+
logout: logoutCommand,
56+
npm: npmCommand,
57+
npx: npxCommand,
58+
optimize: optimizeCommand,
59+
organization: organizationCommand,
60+
'raw-npm': rawNpmCommand,
61+
'raw-npx': rawNpxCommand,
62+
report: reportCommand,
63+
wrapper: wrapperCommand,
64+
scan: scanCommand,
65+
'audit-log': auditLogCommand,
66+
repos: reposCommand,
67+
dependencies: dependenciesCommand,
68+
analytics: analyticsCommand,
69+
'diff-scan': diffScanCommand,
70+
'threat-feed': threatFeedCommand,
71+
manifest: manifestCommand
4572
},
46-
argv: process.argv.slice(2),
47-
name: 'socket',
48-
importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta
49-
})
73+
{
74+
aliases: {
75+
ci: {
76+
description: 'Alias for "report create --view --strict"',
77+
argv: ['report', 'create', '--view', '--strict']
78+
}
79+
},
80+
argv: process.argv.slice(2),
81+
name: 'socket',
82+
importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta
83+
}
84+
)
5085
} catch (err) {
5186
let errorBody: string | undefined
5287
let errorTitle: string

src/commands/action/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getDefaultToken } from '../../utils/sdk'
1515

1616
const socket = new SocketSdk(getDefaultToken()!)
1717

18-
export const action: CliSubcommand = {
18+
export const actionCommand: CliSubcommand = {
1919
description: 'Socket action command',
2020
hidden: true,
2121
async run(args: readonly string[]) {

src/commands/analytics.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import { getDefaultToken, setupSdk } from '../utils/sdk'
2121

2222
import type { CliSubcommand } from '../utils/meow-with-subcommands'
2323

24-
export const analytics: CliSubcommand = {
25-
description: `Look up analytics data\n Default parameters are set to show the organization-level analytics over the last 7 days.`,
24+
const description = `Look up analytics data\n Default parameters are set to show the organization-level analytics over the last 7 days.`
25+
26+
export const analyticsCommand: CliSubcommand = {
27+
description,
2628
async run(argv, importMeta, { parentName }) {
2729
const name = parentName + ' analytics'
2830

29-
const input = setupCommand(name, analytics.description, argv, importMeta)
31+
const input = setupCommand(name, description, argv, importMeta)
3032
if (input) {
3133
const apiToken = getDefaultToken()
3234
if (!apiToken) {

src/commands/audit-log.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import { getDefaultToken, setupSdk } from '../utils/sdk'
1212

1313
import type { CliSubcommand } from '../utils/meow-with-subcommands'
1414

15-
export const auditLog: CliSubcommand = {
16-
description: 'Look up the audit log for an organization',
15+
const description = 'Look up the audit log for an organization'
16+
17+
export const auditLogCommand: CliSubcommand = {
18+
description,
1719
async run(argv, importMeta, { parentName }) {
1820
const name = parentName + ' audit-log'
1921

20-
const input = setupCommand(name, auditLog.description, argv, importMeta)
22+
const input = setupCommand(name, description, argv, importMeta)
2123
if (input) {
2224
const apiToken = getDefaultToken()
2325
if (!apiToken) {

src/commands/cdxgen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function argvToArray(argv: {
149149
return result
150150
}
151151

152-
export const cdxgen: CliSubcommand = {
152+
export const cdxgenCommand: CliSubcommand = {
153153
description: 'Create an SBOM with CycloneDX generator (cdxgen)',
154154
async run(argv_) {
155155
const yargv = <any>{

src/commands/dependencies.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import { getDefaultToken, setupSdk } from '../utils/sdk'
1313

1414
import type { CliSubcommand } from '../utils/meow-with-subcommands'
1515

16-
export const dependencies: CliSubcommand = {
17-
description:
18-
'Search for any dependency that is being used in your organization',
16+
const description =
17+
'Search for any dependency that is being used in your organization'
18+
19+
export const dependenciesCommand: CliSubcommand = {
20+
description,
1921
async run(argv, importMeta, { parentName }) {
2022
const name = parentName + ' dependencies'
2123

22-
const input = setupCommand(name, dependencies.description, argv, importMeta)
24+
const input = setupCommand(name, description, argv, importMeta)
2325
if (input) {
2426
await searchDeps(input)
2527
}

src/commands/diff-scan/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CliSubcommand } from '../../utils/meow-with-subcommands'
55

66
const description = 'Diff scans related commands'
77

8-
export const diffScan: CliSubcommand = {
8+
export const diffScanCommand: CliSubcommand = {
99
description,
1010
async run(argv, importMeta, { parentName }) {
1111
await meowWithSubcommands(

src/commands/fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const { SOCKET_CLI_IN_FIX_CMD, SOCKET_IPC_HANDSHAKE } = constants
3434
// /* eslint-enable no-await-in-loop */
3535
// return ret!
3636

37-
export const fix: CliSubcommand = {
37+
export const fixCommand: CliSubcommand = {
3838
description: 'Fix "fixable" Socket alerts',
3939
hidden: true,
4040
async run() {

src/commands/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/commands/info.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@ import type { SocketSdkReturnType } from '@socketsecurity/sdk'
2525

2626
const { NPM } = constants
2727

28-
export const info: CliSubcommand = {
29-
description: 'Look up info regarding a package',
28+
const description = 'Look up info regarding a package'
29+
30+
export const infoCommand: CliSubcommand = {
31+
description,
3032
async run(argv, importMeta, { parentName }) {
3133
const name = parentName + ' info'
3234

33-
const commandContext = setupCommand(
34-
name,
35-
info.description,
36-
argv,
37-
importMeta
38-
)
35+
const commandContext = setupCommand(name, description, argv, importMeta)
3936
if (commandContext) {
4037
const spinnerText =
4138
commandContext.pkgVersion === 'latest'

0 commit comments

Comments
 (0)