11/** @fileoverview Shadow bin runner with IPC support and error handling. */
22
3- import { createRequire } from 'node:module'
4-
53import { SOCKET_PUBLIC_API_TOKEN } from '@socketsecurity/lib/constants/socket'
64import { Spinner as createSpinner } from '@socketsecurity/lib/spinner'
75
8- import { NPM , PNPM , YARN } from '../../constants/agents.mts'
96import { FLAG_SILENT } from '../../constants/cli.mts'
107import {
11- PACKAGE_LOCK_JSON ,
12- PNPM_LOCK_YAML ,
13- YARN_LOCK ,
14- } from '../../constants/packages.mts'
15- import {
16- getShadowNpmBinPath ,
17- getShadowNpxBinPath ,
18- getShadowPnpmBinPath ,
19- getShadowYarnBinPath ,
208 SOCKET_CLI_SHADOW_ACCEPT_RISKS ,
219 SOCKET_CLI_SHADOW_API_TOKEN ,
2210 SOCKET_CLI_SHADOW_SILENT ,
2311} from '../../constants/shadow.mts'
12+ import shadowNpmBin from '../../shadow/npm/bin.mts'
13+ import shadowNpxBin from '../../shadow/npx/bin.mts'
2414import { getErrorCause } from '../error/errors.mts'
25- import { findUp } from '../fs/find-up.mts'
26- import { isYarnBerry } from '../yarn/version.mts'
2715
2816import type { IpcObject } from '../../constants/shadow.mts'
2917import type {
@@ -34,10 +22,7 @@ import type { CResult } from '../../types.mts'
3422import type { SpawnExtra } from '@socketsecurity/lib/spawn'
3523import type { Spinner } from '@socketsecurity/lib/spinner'
3624
37- const require = createRequire ( import . meta. url )
38-
3925export type ShadowRunnerOptions = {
40- agent ?: 'npm' | 'pnpm' | 'yarn' | undefined
4126 bufferOutput ?: boolean | undefined
4227 cwd ?: string | undefined
4328 env ?: Record < string , string > | undefined
@@ -48,39 +33,9 @@ export type ShadowRunnerOptions = {
4833}
4934
5035/**
51- * Auto-detect package manager based on lockfiles.
52- */
53- export async function detectPackageManager (
54- cwd ?: string | undefined ,
55- ) : Promise < 'npm' | 'pnpm' | 'yarn' > {
56- const pnpmLockPath = await findUp ( PNPM_LOCK_YAML , {
57- cwd,
58- onlyFiles : true ,
59- } )
60- const yarnLockPath = pnpmLockPath
61- ? undefined
62- : await findUp ( YARN_LOCK , { cwd, onlyFiles : true } )
63- const npmLockPath =
64- pnpmLockPath || yarnLockPath
65- ? undefined
66- : await findUp ( PACKAGE_LOCK_JSON , { cwd, onlyFiles : true } )
67-
68- if ( pnpmLockPath ) {
69- return PNPM
70- }
71- if ( yarnLockPath ) {
72- return YARN
73- }
74- if ( npmLockPath ) {
75- return NPM
76- }
77- // Default to npm if no lockfile found.
78- return NPM
79- }
80-
81- /**
82- * Run a command via package manager dlx/npx with shadow bin wrapping.
36+ * Run a command via npx with shadow bin wrapping.
8337 * Handles IPC for secure config passing and provides unified error handling.
38+ * Note: Only supports npm/npx. For pnpm/yarn, use the direct commands instead.
8439 */
8540export async function runShadowCommand (
8641 packageSpec : string ,
@@ -89,7 +44,6 @@ export async function runShadowCommand(
8944 spawnExtra ?: SpawnExtra | undefined ,
9045) : Promise < CResult < string > > {
9146 const opts = { __proto__ : null , ...options } as ShadowRunnerOptions
92- const agent = opts . agent ?? ( await detectPackageManager ( opts . cwd ) )
9347
9448 const shadowOpts : ShadowBinOptions = {
9549 cwd : opts . cwd ,
@@ -116,30 +70,11 @@ export async function runShadowCommand(
11670 spinner . start ( opts . spinnerMessage )
11771 }
11872
119- let result : ShadowBinResult
120-
121- if ( agent === PNPM ) {
122- const shadowPnpmBin = /*@__PURE__ */ require ( getShadowPnpmBinPath ( ) )
123- result = await shadowPnpmBin (
124- [ 'dlx' , FLAG_SILENT , packageSpec , ...args ] ,
125- shadowOpts ,
126- finalSpawnExtra ,
127- )
128- } else if ( agent === YARN && isYarnBerry ( ) ) {
129- const shadowYarnBin = /*@__PURE__ */ require ( getShadowYarnBinPath ( ) )
130- result = await shadowYarnBin (
131- [ 'dlx' , '--quiet' , packageSpec , ...args ] ,
132- shadowOpts ,
133- finalSpawnExtra ,
134- )
135- } else {
136- const shadowNpxBin = /*@__PURE__ */ require ( getShadowNpxBinPath ( ) )
137- result = await shadowNpxBin (
138- [ '--yes' , '--force' , FLAG_SILENT , packageSpec , ...args ] ,
139- shadowOpts ,
140- finalSpawnExtra ,
141- )
142- }
73+ const result : ShadowBinResult = await shadowNpxBin (
74+ [ '--yes' , '--force' , FLAG_SILENT , packageSpec , ...args ] ,
75+ shadowOpts ,
76+ finalSpawnExtra ,
77+ )
14378
14479 if ( spinner ) {
14580 spinner . stop ( )
@@ -196,7 +131,6 @@ export async function runShadowNpm(
196131 spinner . start ( opts . spinnerMessage )
197132 }
198133
199- const shadowNpmBin = /*@__PURE__ */ require ( getShadowNpmBinPath ( ) )
200134 const result : ShadowBinResult = await shadowNpmBin (
201135 args ,
202136 shadowOpts ,
0 commit comments