Skip to content
Closed
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
37 changes: 25 additions & 12 deletions packages/cli/src/cli-dispatch.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*
* This single file handles all Socket CLI commands by detecting how it was invoked:
* - socket (main CLI)
* - socket-npm (npm wrapper)
* - socket-npx (npx wrapper)
* - socket-pnpm (pnpm wrapper)
* - socket-yarn (yarn wrapper)
* - socket-npm (calls socket npm command directly)
* - socket-npx (calls socket npx command directly)
* - socket-pnpm (calls socket pnpm command directly)
* - socket-yarn (calls socket yarn command directly)
*
* Perfect for SEA packaging and single-file distribution.
*
Expand All @@ -20,6 +20,10 @@ import path from 'node:path'

import { getDefaultLogger } from '@socketsecurity/lib-internal/logger'

import { cmdNpm } from './commands/npm/cmd-npm.mts'
import { cmdNpx } from './commands/npx/cmd-npx.mts'
import { cmdPnpm } from './commands/pnpm/cmd-pnpm.mts'
import { cmdYarn } from './commands/yarn/cmd-yarn.mts'
import { waitForBootstrapHandshake } from './utils/sea/boot.mjs'

const logger = getDefaultLogger()
Expand Down Expand Up @@ -102,28 +106,37 @@ async function main() {
// Import and run the appropriate CLI function.
switch (mode) {
case 'npm': {
const { default: runNpmCli } = await import('./npm-cli.mjs')
await runNpmCli()
// Call socket npm command directly.
await cmdNpm.run(process.argv.slice(2), import.meta, {
parentName: 'socket',
})
break
}

case 'npx': {
const { default: runNpxCli } = await import('./npx-cli.mjs')
await runNpxCli()
// Call socket npx command directly.
await cmdNpx.run(process.argv.slice(2), import.meta, {
parentName: 'socket',
})
break
}

case 'pnpm': {
const { default: runPnpmCli } = await import('./pnpm-cli.mjs')
await runPnpmCli()
// Call socket pnpm command directly.
await cmdPnpm.run(process.argv.slice(2), import.meta, {
parentName: 'socket',
})
break
}

case 'yarn': {
const { default: runYarnCli } = await import('./yarn-cli.mjs')
await runYarnCli()
// Call socket yarn command directly.
await cmdYarn.run(process.argv.slice(2), import.meta, {
parentName: 'socket',
})
break
}

default:
await import('./cli-entry.mjs')
break
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/commands/npm/cmd-npm.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createRequire } from 'node:module'

import { NPM } from '@socketsecurity/lib-internal/constants/agents'
import { getDefaultLogger } from '@socketsecurity/lib-internal/logger'

Expand All @@ -9,8 +7,8 @@ import {
FLAG_HELP,
FLAG_JSON,
} from '../../constants/cli.mts'
import { getShadowNpmBinPath } from '../../constants/paths.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import shadowNpmBin from '../../shadow/npm/bin.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { getFlagApiRequirementsOutput } from '../../utils/output/formatting.mts'
import { filterFlags } from '../../utils/process/cmd.mts'
Expand All @@ -20,7 +18,6 @@ import type {
CliCommandContext,
} from '../../utils/cli/with-subcommands.mjs'

const require = createRequire(import.meta.url)
const logger = getDefaultLogger()

export const CMD_NAME = NPM
Expand Down Expand Up @@ -81,8 +78,6 @@ async function run(
return
}

const shadowNpmBin = /*@__PURE__*/ require(getShadowNpmBinPath())

process.exitCode = 1

// Filter Socket flags from argv but keep --json for npm.
Expand All @@ -94,7 +89,7 @@ async function run(
})

// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
;(spawnPromise as any).process.on(
'exit',
(code: string | null, signalName: NodeJS.Signals | null) => {
if (signalName) {
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/commands/npx/cmd-npx.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createRequire } from 'node:module'

import { NPX } from '@socketsecurity/lib-internal/constants/agents'
import { getDefaultLogger } from '@socketsecurity/lib-internal/logger'

Expand All @@ -8,8 +6,8 @@ import {
FLAG_DRY_RUN,
FLAG_HELP,
} from '../../constants/cli.mts'
import { getShadowNpxBinPath } from '../../constants/paths.mts'
import { commonFlags } from '../../flags.mts'
import shadowNpxBin from '../../shadow/npx/bin.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { getFlagApiRequirementsOutput } from '../../utils/output/formatting.mts'

Expand All @@ -18,7 +16,6 @@ import type {
CliCommandContext,
} from '../../utils/cli/with-subcommands.mjs'

const require = createRequire(import.meta.url)
const logger = getDefaultLogger()

const CMD_NAME = NPX
Expand Down Expand Up @@ -77,14 +74,12 @@ async function run(
return
}

const shadowNpxBin = /*@__PURE__*/ require(getShadowNpxBinPath())

process.exitCode = 1

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

// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on(
;(spawnPromise as any).process.on(
'exit',
(code: string | null, signalName: NodeJS.Signals | null) => {
if (signalName) {
Expand Down
15 changes: 6 additions & 9 deletions packages/cli/src/commands/pnpm/cmd-pnpm.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { createRequire } from 'node:module'

import { PNPM } from '@socketsecurity/lib-internal/constants/agents'
import { getDefaultLogger } from '@socketsecurity/lib-internal/logger'
import { WIN32 } from '@socketsecurity/lib/constants/platform'
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { spawn } from '@socketsecurity/lib/spawn'

import { PNPM } from '../../constants/agents.mts'
import {
DRY_RUN_BAILING_NOW,
FLAG_DRY_RUN,
FLAG_HELP,
} from '../../constants/cli.mts'
import { getShadowPnpmBinPath } from '../../constants/paths.mts'
import { commonFlags } from '../../flags.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { getFlagApiRequirementsOutput } from '../../utils/output/formatting.mts'
Expand All @@ -19,7 +18,6 @@ import type {
CliCommandContext,
} from '../../utils/cli/with-subcommands.mjs'

const require = createRequire(import.meta.url)
const logger = getDefaultLogger()

export const CMD_NAME = PNPM
Expand Down Expand Up @@ -81,14 +79,13 @@ async function run(
return
}

const shadowPnpmBin = /*@__PURE__*/ require(getShadowPnpmBinPath())

process.exitCode = 1

// Filter Socket flags from argv.
const filteredArgv = filterFlags(argv, config.flags)

const { spawnPromise } = await shadowPnpmBin(filteredArgv, {
const spawnPromise = spawn(PNPM, filteredArgv, {
shell: WIN32,
stdio: 'inherit',
})

Expand Down
17 changes: 7 additions & 10 deletions packages/cli/src/commands/yarn/cmd-yarn.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { createRequire } from 'node:module'

import { YARN } from '@socketsecurity/lib-internal/constants/agents'
import { getDefaultLogger } from '@socketsecurity/lib-internal/logger'
import { WIN32 } from '@socketsecurity/lib/constants/platform'
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { spawn } from '@socketsecurity/lib/spawn'

import { YARN } from '../../constants/agents.mts'
import {
DRY_RUN_BAILING_NOW,
FLAG_DRY_RUN,
FLAG_HELP,
} from '../../constants/cli.mts'
import { getShadowYarnBinPath } from '../../constants/paths.mts'
import { commonFlags } from '../../flags.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { getFlagApiRequirementsOutput } from '../../utils/output/formatting.mts'
Expand All @@ -19,7 +18,7 @@ import type {
CliCommandContext,
} from '../../utils/cli/with-subcommands.mjs'

const require = createRequire(import.meta.url)
const logger = getDefaultLogger()

export const CMD_NAME = YARN

Expand Down Expand Up @@ -76,19 +75,17 @@ async function run(
const dryRun = !!cli.flags['dryRun']

if (dryRun) {
const logger = getDefaultLogger()
logger.log(DRY_RUN_BAILING_NOW)
return
}

const shadowYarnBin = /*@__PURE__*/ require(getShadowYarnBinPath())

process.exitCode = 1

// Filter Socket flags from argv.
const filteredArgv = filterFlags(argv, config.flags)

const { spawnPromise } = await shadowYarnBin(filteredArgv, {
const spawnPromise = spawn(YARN, filteredArgv, {
shell: WIN32,
stdio: 'inherit',
})

Expand Down
35 changes: 0 additions & 35 deletions packages/cli/src/pnpm-cli.mts

This file was deleted.

Loading
Loading