Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 58 additions & 23 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,35 @@ 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'
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({
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commands/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down
8 changes: 5 additions & 3 deletions src/commands/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/commands/audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cdxgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <any>{
Expand Down
10 changes: 6 additions & 4 deletions src/commands/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diff-scan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 0 additions & 22 deletions src/commands/index.ts

This file was deleted.

13 changes: 5 additions & 8 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function nonNullish<T>(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`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/npx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
13 changes: 5 additions & 8 deletions src/commands/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/commands/raw-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 5 additions & 3 deletions src/commands/raw-npx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/repos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/scan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading
Loading