diff --git a/src/cli.ts b/src/cli.ts index 67ddfcb09..c847b1820 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,7 +7,28 @@ import { messageWithCauses, stackWithCauses } from 'pony-cause' import updateNotifier from 'tiny-updater' import colors from 'yoctocolors-cjs' -import * as cliCommands from './commands' +import { actionCommand } from './commands/action' +import { analyticsCommand } from './commands/analytics' +import { auditLogCommand } from './commands/audit-log' +import { cdxgenCommand } from './commands/cdxgen' +import { dependenciesCommand } from './commands/dependencies' +import { diffScanCommand } from './commands/diff-scan' +import { fixCommand } from './commands/fix' +import { infoCommand } from './commands/info' +import { loginCommand } from './commands/login' +import { logoutCommand } from './commands/logout' +import { manifestCommand } from './commands/manifest' +import { npmCommand } from './commands/npm' +import { npxCommand } from './commands/npx' +import { optimizeCommand } from './commands/optimize' +import { organizationCommand } from './commands/organization' +import { rawNpmCommand } from './commands/raw-npm' +import { rawNpxCommand } from './commands/raw-npx' +import { reportCommand } from './commands/report' +import { reposCommand } from './commands/repos' +import { scanCommand } from './commands/scan' +import { threatFeedCommand } from './commands/threat-feed' +import { wrapperCommand } from './commands/wrapper' import constants from './constants' import { AuthError, InputError } from './utils/errors' import { logSymbols } from './utils/logging' @@ -15,18 +36,6 @@ import { meowWithSubcommands } from './utils/meow-with-subcommands' const { rootPkgJsonPath } = constants -const formattedCliCommands = Object.fromEntries( - Object.entries(cliCommands).map(entry => { - const key = entry[0] - entry[0] = camelToHyphen(key) - return entry - }) -) - -function camelToHyphen(str: string): string { - return str.replace(/[A-Z]+/g, '-$&').toLowerCase() -} - // TODO: Add autocompletion using https://socket.dev/npm/package/omelette void (async () => { await updateNotifier({ @@ -36,17 +45,43 @@ void (async () => { }) try { - await meowWithSubcommands(formattedCliCommands, { - aliases: { - ci: { - description: 'Alias for "report create --view --strict"', - argv: ['report', 'create', '--view', '--strict'] - } + await meowWithSubcommands( + { + action: actionCommand, + cdxgen: cdxgenCommand, + fix: fixCommand, + info: infoCommand, + login: loginCommand, + logout: logoutCommand, + npm: npmCommand, + npx: npxCommand, + optimize: optimizeCommand, + organization: organizationCommand, + 'raw-npm': rawNpmCommand, + 'raw-npx': rawNpxCommand, + report: reportCommand, + wrapper: wrapperCommand, + scan: scanCommand, + 'audit-log': auditLogCommand, + repos: reposCommand, + dependencies: dependenciesCommand, + analytics: analyticsCommand, + 'diff-scan': diffScanCommand, + 'threat-feed': threatFeedCommand, + manifest: manifestCommand }, - argv: process.argv.slice(2), - name: 'socket', - importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta - }) + { + aliases: { + ci: { + description: 'Alias for "report create --view --strict"', + argv: ['report', 'create', '--view', '--strict'] + } + }, + argv: process.argv.slice(2), + name: 'socket', + importMeta: { url: `${pathToFileURL(__filename)}` } as ImportMeta + } + ) } catch (err) { let errorBody: string | undefined let errorTitle: string diff --git a/src/commands/action/index.ts b/src/commands/action/index.ts index d3e53ce75..d33d2beff 100644 --- a/src/commands/action/index.ts +++ b/src/commands/action/index.ts @@ -15,7 +15,7 @@ import { getDefaultToken } from '../../utils/sdk' const socket = new SocketSdk(getDefaultToken()!) -export const action: CliSubcommand = { +export const actionCommand: CliSubcommand = { description: 'Socket action command', hidden: true, async run(args: readonly string[]) { diff --git a/src/commands/analytics.ts b/src/commands/analytics.ts index 2154dd3c2..6c877206b 100644 --- a/src/commands/analytics.ts +++ b/src/commands/analytics.ts @@ -21,12 +21,14 @@ import { getDefaultToken, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -export const analytics: CliSubcommand = { - description: `Look up analytics data\n Default parameters are set to show the organization-level analytics over the last 7 days.`, +const description = `Look up analytics data\n Default parameters are set to show the organization-level analytics over the last 7 days.` + +export const analyticsCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const name = parentName + ' analytics' - const input = setupCommand(name, analytics.description, argv, importMeta) + const input = setupCommand(name, description, argv, importMeta) if (input) { const apiToken = getDefaultToken() if (!apiToken) { diff --git a/src/commands/audit-log.ts b/src/commands/audit-log.ts index 39ad0f9cc..aab22968b 100644 --- a/src/commands/audit-log.ts +++ b/src/commands/audit-log.ts @@ -12,12 +12,14 @@ import { getDefaultToken, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -export const auditLog: CliSubcommand = { - description: 'Look up the audit log for an organization', +const description = 'Look up the audit log for an organization' + +export const auditLogCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const name = parentName + ' audit-log' - const input = setupCommand(name, auditLog.description, argv, importMeta) + const input = setupCommand(name, description, argv, importMeta) if (input) { const apiToken = getDefaultToken() if (!apiToken) { diff --git a/src/commands/cdxgen.ts b/src/commands/cdxgen.ts index 6b62de6ee..f4e8d2186 100644 --- a/src/commands/cdxgen.ts +++ b/src/commands/cdxgen.ts @@ -149,7 +149,7 @@ function argvToArray(argv: { return result } -export const cdxgen: CliSubcommand = { +export const cdxgenCommand: CliSubcommand = { description: 'Create an SBOM with CycloneDX generator (cdxgen)', async run(argv_) { const yargv = { diff --git a/src/commands/dependencies.ts b/src/commands/dependencies.ts index fd1abeceb..f3466c77f 100644 --- a/src/commands/dependencies.ts +++ b/src/commands/dependencies.ts @@ -13,13 +13,15 @@ import { getDefaultToken, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -export const dependencies: CliSubcommand = { - description: - 'Search for any dependency that is being used in your organization', +const description = + 'Search for any dependency that is being used in your organization' + +export const dependenciesCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const name = parentName + ' dependencies' - const input = setupCommand(name, dependencies.description, argv, importMeta) + const input = setupCommand(name, description, argv, importMeta) if (input) { await searchDeps(input) } diff --git a/src/commands/diff-scan/index.ts b/src/commands/diff-scan/index.ts index 671d6978a..b8e9da2bd 100644 --- a/src/commands/diff-scan/index.ts +++ b/src/commands/diff-scan/index.ts @@ -5,7 +5,7 @@ import type { CliSubcommand } from '../../utils/meow-with-subcommands' const description = 'Diff scans related commands' -export const diffScan: CliSubcommand = { +export const diffScanCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { await meowWithSubcommands( diff --git a/src/commands/fix.ts b/src/commands/fix.ts index 5164585dd..03b645ca9 100644 --- a/src/commands/fix.ts +++ b/src/commands/fix.ts @@ -34,7 +34,7 @@ const { SOCKET_CLI_IN_FIX_CMD, SOCKET_IPC_HANDSHAKE } = constants // /* eslint-enable no-await-in-loop */ // return ret! -export const fix: CliSubcommand = { +export const fixCommand: CliSubcommand = { description: 'Fix "fixable" Socket alerts', hidden: true, async run() { diff --git a/src/commands/index.ts b/src/commands/index.ts deleted file mode 100644 index 16f95bc05..000000000 --- a/src/commands/index.ts +++ /dev/null @@ -1,22 +0,0 @@ -export * from './action' -export * from './cdxgen' -export * from './fix' -export * from './info' -export * from './login' -export * from './logout' -export * from './npm' -export * from './npx' -export * from './optimize' -export * from './organization' -export * from './raw-npm' -export * from './raw-npx' -export * from './report' -export * from './wrapper' -export * from './scan' -export * from './audit-log' -export * from './repos' -export * from './dependencies' -export * from './analytics' -export * from './diff-scan' -export * from './threat-feed' -export * from './manifest' diff --git a/src/commands/info.ts b/src/commands/info.ts index 1941a9cc2..b3be24772 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -25,17 +25,14 @@ import type { SocketSdkReturnType } from '@socketsecurity/sdk' const { NPM } = constants -export const info: CliSubcommand = { - description: 'Look up info regarding a package', +const description = 'Look up info regarding a package' + +export const infoCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const name = parentName + ' info' - const commandContext = setupCommand( - name, - info.description, - argv, - importMeta - ) + const commandContext = setupCommand(name, description, argv, importMeta) if (commandContext) { const spinnerText = commandContext.pkgVersion === 'latest' diff --git a/src/commands/login.ts b/src/commands/login.ts index 8459d20d9..81eb904db 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -46,7 +46,7 @@ function nonNullish(value: T | null | undefined): value is T { return value !== null && value !== undefined } -export const login: CliSubcommand = { +export const loginCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { const name = `${parentName} login` diff --git a/src/commands/logout.ts b/src/commands/logout.ts index 5c30bb4f1..69590ed27 100644 --- a/src/commands/logout.ts +++ b/src/commands/logout.ts @@ -8,7 +8,7 @@ import type { CliSubcommand } from '../utils/meow-with-subcommands' const description = 'Socket API logout' -export const logout: CliSubcommand = { +export const logoutCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { const name = `${parentName} logout` diff --git a/src/commands/manifest/index.ts b/src/commands/manifest/index.ts index 6cc5a4625..cbc178069 100644 --- a/src/commands/manifest/index.ts +++ b/src/commands/manifest/index.ts @@ -31,7 +31,7 @@ const help = (name: string) => ` $ ${name} yolo ` -export const manifest: CliSubcommand = { +export const manifestCommand: CliSubcommand = { description, hidden: true, async run(argv, importMeta, { parentName }) { diff --git a/src/commands/npm.ts b/src/commands/npm.ts index dc7be24c7..2251d6eb0 100644 --- a/src/commands/npm.ts +++ b/src/commands/npm.ts @@ -4,7 +4,7 @@ import type { CliSubcommand } from '../utils/meow-with-subcommands' const { NPM } = constants -export const npm: CliSubcommand = { +export const npmCommand: CliSubcommand = { description: `${NPM} wrapper functionality`, async run(argv) { // Lazily access constants.distPath. diff --git a/src/commands/npx.ts b/src/commands/npx.ts index 680cbef11..c2da405af 100644 --- a/src/commands/npx.ts +++ b/src/commands/npx.ts @@ -4,7 +4,7 @@ import type { CliSubcommand } from '../utils/meow-with-subcommands' const { NPX } = constants -export const npx: CliSubcommand = { +export const npxCommand: CliSubcommand = { description: `${NPX} wrapper functionality`, async run(argv) { // Lazily access constants.distPath. diff --git a/src/commands/optimize.ts b/src/commands/optimize.ts index bed216fd6..fcbbc274f 100644 --- a/src/commands/optimize.ts +++ b/src/commands/optimize.ts @@ -64,6 +64,8 @@ const PNPM_WORKSPACE = `${PNPM}-workspace` const manifestNpmOverrides = getManifestData(NPM) +const description = 'Optimize dependencies with @socketregistry overrides' + type NpmOverrides = { [key: string]: string | StringKeyValueObject } type PnpmOrYarnOverrides = { [key: string]: string } type Overrides = NpmOverrides | PnpmOrYarnOverrides @@ -810,12 +812,12 @@ async function addOverrides( return state } -export const optimize: CliSubcommand = { - description: 'Optimize dependencies with @socketregistry overrides', +export const optimizeCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const commandContext = setupCommand( `${parentName} optimize`, - optimize.description, + description, argv, importMeta ) diff --git a/src/commands/organization.ts b/src/commands/organization.ts index f3c125913..9ca9de5ab 100644 --- a/src/commands/organization.ts +++ b/src/commands/organization.ts @@ -9,15 +9,12 @@ import { getDefaultToken, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -export const organizations: CliSubcommand = { - description: 'List organizations associated with the API key used', +const description = 'List organizations associated with the API key used' + +export const organizationCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { - setupCommand( - `${parentName} organizations`, - organizations.description, - argv, - importMeta - ) + setupCommand(`${parentName} organizations`, description, argv, importMeta) await fetchOrganizations() } } diff --git a/src/commands/raw-npm.ts b/src/commands/raw-npm.ts index d2fa54a2e..216b9873a 100644 --- a/src/commands/raw-npm.ts +++ b/src/commands/raw-npm.ts @@ -14,12 +14,14 @@ const { NPM, abortSignal } = constants const binName = NPM -export const rawNpm: CliSubcommand = { - description: `Temporarily disable the Socket ${binName} wrapper`, +const description = `Temporarily disable the Socket ${binName} wrapper` + +export const rawNpmCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { await setupCommand( `${parentName} raw-${binName}`, - rawNpm.description, + description, argv, importMeta ) diff --git a/src/commands/raw-npx.ts b/src/commands/raw-npx.ts index e593f7a50..affdd534c 100644 --- a/src/commands/raw-npx.ts +++ b/src/commands/raw-npx.ts @@ -14,12 +14,14 @@ const { NPX, abortSignal } = constants const binName = NPX -export const rawNpx: CliSubcommand = { - description: `Temporarily disable the Socket ${binName} wrapper`, +const description = `Temporarily disable the Socket ${binName} wrapper` + +export const rawNpxCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { await setupCommand( `${parentName} raw-${binName}`, - rawNpx.description, + description, argv, importMeta ) diff --git a/src/commands/report/index.ts b/src/commands/report/index.ts index 45c4c4984..8274a9635 100644 --- a/src/commands/report/index.ts +++ b/src/commands/report/index.ts @@ -6,7 +6,7 @@ import type { CliSubcommand } from '../../utils/meow-with-subcommands' const description = '[Deprecated] Project report related commands' -export const report: CliSubcommand = { +export const reportCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { await meowWithSubcommands( diff --git a/src/commands/repos/index.ts b/src/commands/repos/index.ts index b04d42a95..54f1d965e 100644 --- a/src/commands/repos/index.ts +++ b/src/commands/repos/index.ts @@ -9,7 +9,7 @@ import type { CliSubcommand } from '../../utils/meow-with-subcommands' const description = 'Repositories related commands' -export const repo: CliSubcommand = { +export const reposCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { await meowWithSubcommands( diff --git a/src/commands/scan/index.ts b/src/commands/scan/index.ts index 3a743e4c7..6583f5b65 100644 --- a/src/commands/scan/index.ts +++ b/src/commands/scan/index.ts @@ -9,7 +9,7 @@ import type { CliSubcommand } from '../../utils/meow-with-subcommands' const description = 'Scans related commands' -export const scan: CliSubcommand = { +export const scanCommand: CliSubcommand = { description, async run(argv, importMeta, { parentName }) { await meowWithSubcommands( diff --git a/src/commands/threat-feed.ts b/src/commands/threat-feed.ts index 68c3a7c9a..6e2b2deeb 100644 --- a/src/commands/threat-feed.ts +++ b/src/commands/threat-feed.ts @@ -16,12 +16,14 @@ import { getDefaultToken } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -export const threatFeed: CliSubcommand = { - description: 'Look up the threat feed', +const description = 'Look up the threat feed' + +export const threatFeedCommand: CliSubcommand = { + description, async run(argv, importMeta, { parentName }) { const name = `${parentName} threat-feed` - const input = setupCommand(name, threatFeed.description, argv, importMeta) + const input = setupCommand(name, description, argv, importMeta) if (input) { const apiToken = getDefaultToken() if (!apiToken) { diff --git a/src/commands/wrapper.ts b/src/commands/wrapper.ts index 2b56f1861..75abd3da6 100644 --- a/src/commands/wrapper.ts +++ b/src/commands/wrapper.ts @@ -14,14 +14,16 @@ const HOME_DIR = os.homedir() const BASH_FILE = `${HOME_DIR}/.bashrc` const ZSH_BASH_FILE = `${HOME_DIR}/.zshrc` -export const wrapper: CliSubcommand = { - description: 'Enable or disable the Socket npm/npx wrapper', +const description = 'Enable or disable the Socket npm/npx wrapper' + +export const wrapperCommand: CliSubcommand = { + description, async run( argv: readonly string[], importMeta: ImportMeta, { parentName }: { parentName: string } ) { - setupCommand(`${parentName} wrapper`, wrapper.description, argv, importMeta) + setupCommand(`${parentName} wrapper`, description, argv, importMeta) } }