Skip to content
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@socketregistry/packageurl-js": "1.0.9",
"@socketsecurity/config": "3.0.1",
"@socketsecurity/registry": "1.1.17",
"@socketsecurity/sdk": "1.4.94",
"@socketsecurity/sdk": "file:///Users/billli/code/socketdev/socket-sdk-js/socketsecurity-sdk-1.4.94.tgz",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to point to the newly published sdk from SocketDev/socket-sdk-js#446

"@types/blessed": "0.1.25",
"@types/cmd-shim": "5.0.2",
"@types/js-yaml": "4.0.9",
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 61 additions & 1 deletion src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ import { AuthError, InputError, captureException } from './utils/errors.mts'
import { failMsgWithBadge } from './utils/fail-msg-with-badge.mts'
import { meowWithSubcommands } from './utils/meow-with-subcommands.mts'
import { serializeResultJson } from './utils/serialize-result-json.mts'
import {
finalizeTelemetry,
trackCliComplete,
trackCliError,
trackCliStart,
} from './utils/telemetry/integration.mts'
import { socketPackageLink } from './utils/terminal-link.mts'

const __filename = fileURLToPath(import.meta.url)

void (async () => {
// Track CLI start for telemetry.
const cliStartTime = await trackCliStart(process.argv)

const registryUrl = lookupRegistryUrl()
await updateNotifier({
authInfo: lookupRegistryAuthToken(registryUrl, { recursive: true }),
Expand Down Expand Up @@ -50,8 +59,14 @@ void (async () => {
},
{ aliases: rootAliases },
)

// Track successful CLI completion.
await trackCliComplete(process.argv, cliStartTime, process.exitCode)
} catch (e) {
process.exitCode = 1

// Track CLI error for telemetry.
await trackCliError(process.argv, cliStartTime, e, process.exitCode)
debugFn('error', 'CLI uncaught error')
debugDir('error', e)

Expand Down Expand Up @@ -104,5 +119,50 @@ void (async () => {
}

await captureException(e)
} finally {
// Finalize telemetry to ensure all events are sent.
// This runs on both success and error paths.
await finalizeTelemetry()
}
})()
})().catch(async err => {
// Fatal error in main async function.
console.error('Fatal error:', err)

// Track CLI error for fatal exceptions.
await trackCliError(process.argv, Date.now(), err, 1)

// Finalize telemetry before fatal exit.
await finalizeTelemetry()

// eslint-disable-next-line n/no-process-exit
process.exit(1)
})

// Handle uncaught exceptions.
process.on('uncaughtException', async err => {
console.error('Uncaught exception:', err)

// Track CLI error for uncaught exception.
await trackCliError(process.argv, Date.now(), err, 1)

// Finalize telemetry before exit.
await finalizeTelemetry()

// eslint-disable-next-line n/no-process-exit
process.exit(1)
})

// Handle unhandled promise rejections.
process.on('unhandledRejection', async (reason, promise) => {
console.error('Unhandled rejection at:', promise, 'reason:', reason)

// Track CLI error for unhandled rejection.
const error = reason instanceof Error ? reason : new Error(String(reason))
await trackCliError(process.argv, Date.now(), error, 1)

// Finalize telemetry before exit.
await finalizeTelemetry()

// eslint-disable-next-line n/no-process-exit
process.exit(1)
})
14 changes: 13 additions & 1 deletion src/commands/npm/cmd-npm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { commonFlags, outputFlags } from '../../flags.mts'
import { filterFlags } from '../../utils/cmd.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagApiRequirementsOutput } from '../../utils/output-formatting.mts'
import {
trackSubprocessExit,
trackSubprocessStart,
} from '../../utils/telemetry/integration.mts'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -86,14 +90,22 @@ async function run(
const argsToForward = filterFlags(argv, { ...commonFlags, ...outputFlags }, [
FLAG_JSON,
])

// Track subprocess start.
const subprocessStartTime = await trackSubprocessStart(NPM)

const { spawnPromise } = await shadowNpmBin(argsToForward, {
stdio: 'inherit',
})

// Handle exit codes and signals using event-based pattern.
// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
'exit',
(code: string | null, signalName: NodeJS.Signals | null) => {
async (code: number | null, signalName: NodeJS.Signals | null) => {
// Track subprocess exit and flush telemetry.
await trackSubprocessExit(NPM, subprocessStartTime, code)

if (signalName) {
process.kill(process.pid, signalName)
} else if (typeof code === 'number') {
Expand Down
13 changes: 12 additions & 1 deletion src/commands/npx/cmd-npx.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import constants, { FLAG_DRY_RUN, FLAG_HELP, NPX } from '../../constants.mts'
import { commonFlags } from '../../flags.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagApiRequirementsOutput } from '../../utils/output-formatting.mts'
import {
trackSubprocessExit,
trackSubprocessStart,
} from '../../utils/telemetry/integration.mts'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -74,12 +78,19 @@ async function run(

process.exitCode = 1

// Track subprocess start.
const subprocessStartTime = await trackSubprocessStart(NPX)

const { spawnPromise } = await shadowNpxBin(argv, { stdio: 'inherit' })

// Handle exit codes and signals using event-based pattern.
// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
'exit',
(code: string | null, signalName: NodeJS.Signals | null) => {
async (code: number | null, signalName: NodeJS.Signals | null) => {
// Track subprocess exit and flush telemetry.
await trackSubprocessExit(NPX, subprocessStartTime, code)

if (signalName) {
process.kill(process.pid, signalName)
} else if (typeof code === 'number') {
Expand Down
25 changes: 24 additions & 1 deletion src/commands/pnpm/cmd-pnpm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { commonFlags } from '../../flags.mts'
import { filterFlags } from '../../utils/cmd.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagApiRequirementsOutput } from '../../utils/output-formatting.mts'
import {
trackSubprocessExit,
trackSubprocessStart,
} from '../../utils/telemetry/integration.mts'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -81,10 +85,29 @@ async function run(
// Filter Socket flags from argv.
const filteredArgv = filterFlags(argv, config.flags)

// Track subprocess start.
const subprocessStartTime = await trackSubprocessStart(PNPM)

const { spawnPromise } = await shadowPnpmBin(filteredArgv, {
stdio: 'inherit',
})

// Handle exit codes and signals using event-based pattern.
// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
'exit',
async (code: number | null, signalName: NodeJS.Signals | null) => {
// Track subprocess exit and flush telemetry.
await trackSubprocessExit(PNPM, subprocessStartTime, code)

if (signalName) {
process.kill(process.pid, signalName)
} else if (typeof code === 'number') {
// eslint-disable-next-line n/no-process-exit
process.exit(code)
}
},
)

await spawnPromise
process.exitCode = 0
}
5 changes: 1 addition & 4 deletions src/commands/scan/output-scan-reach.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import type { CResult, OutputKind } from '../../types.mts'

export async function outputScanReach(
result: CResult<ReachabilityAnalysisResult>,
{
outputKind,
outputPath,
}: { outputKind: OutputKind; outputPath: string },
{ outputKind, outputPath }: { outputKind: OutputKind; outputPath: string },
): Promise<void> {
if (!result.ok) {
process.exitCode = result.code ?? 1
Expand Down
25 changes: 24 additions & 1 deletion src/commands/yarn/cmd-yarn.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { commonFlags } from '../../flags.mts'
import { filterFlags } from '../../utils/cmd.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagApiRequirementsOutput } from '../../utils/output-formatting.mts'
import {
trackSubprocessExit,
trackSubprocessStart,
} from '../../utils/telemetry/integration.mts'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -81,10 +85,29 @@ async function run(
// Filter Socket flags from argv.
const filteredArgv = filterFlags(argv, config.flags)

// Track subprocess start.
const subprocessStartTime = await trackSubprocessStart(YARN)

const { spawnPromise } = await shadowYarnBin(filteredArgv, {
stdio: 'inherit',
})

// Handle exit codes and signals using event-based pattern.
// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
'exit',
async (code: number | null, signalName: NodeJS.Signals | null) => {
// Track subprocess exit and flush telemetry.
await trackSubprocessExit(YARN, subprocessStartTime, code)

if (signalName) {
process.kill(process.pid, signalName)
} else if (typeof code === 'number') {
// eslint-disable-next-line n/no-process-exit
process.exit(code)
}
},
)

await spawnPromise
process.exitCode = 0
}
Loading
Loading