@@ -2,12 +2,15 @@ import { createRequire } from 'node:module'
22
33import { getOwn } from '@socketsecurity/registry/lib/objects'
44
5- import constants , { NPM , PNPM , YARN } from '../constants.mts'
5+ import { getDefaultOrgSlug } from '../commands/ci/fetch-default-org-slug.mts'
6+ import constants , { NPM , PNPM , UNKNOWN_ERROR , YARN } from '../constants.mts'
67import { findUp } from './fs.mts'
8+ import { getDefaultApiToken , getDefaultProxyUrl } from './sdk.mts'
79import { isYarnBerry } from './yarn-version.mts'
810import shadowBin from '../shadow/npm/bin.mts'
911
1012import type { ShadowBinOptions , ShadowBinResult } from '../shadow/npm/bin.mts'
13+ import type { CResult } from '../types.mts'
1114import type { SpawnExtra } from '@socketsecurity/registry/lib/spawn'
1215
1316const require = createRequire ( import . meta. url )
@@ -150,21 +153,84 @@ export async function spawnDlx(
150153/**
151154 * Helper to spawn coana with dlx.
152155 * Automatically uses force and silent when version is not pinned exactly.
156+ * Returns a CResult with stdout extraction for backward compatibility.
153157 */
154158export async function spawnCoanaDlx (
155159 args : string [ ] | readonly string [ ] ,
160+ orgSlug ?: string ,
156161 options ?: DlxOptions | undefined ,
157162 spawnExtra ?: SpawnExtra | undefined ,
158- ) : Promise < ShadowBinResult > {
159- return await spawnDlx (
160- {
161- name : '@coana-tech/cli' ,
162- version : `~${ constants . ENV . INLINED_SOCKET_CLI_COANA_TECH_CLI_VERSION } ` ,
163- } ,
164- args ,
165- { force : true , silent : true , ...options } ,
166- spawnExtra ,
167- )
163+ ) : Promise < CResult < string > > {
164+ const {
165+ env : spawnEnv ,
166+ ipc,
167+ ...dlxOptions
168+ } = {
169+ __proto__ : null ,
170+ ...options ,
171+ } as DlxOptions
172+
173+ const mixinsEnv : Record < string , string > = {
174+ SOCKET_CLI_VERSION : constants . ENV . INLINED_SOCKET_CLI_VERSION ,
175+ }
176+ const defaultApiToken = getDefaultApiToken ( )
177+ if ( defaultApiToken ) {
178+ mixinsEnv [ 'SOCKET_CLI_API_TOKEN' ] = defaultApiToken
179+ }
180+
181+ if ( orgSlug ) {
182+ mixinsEnv [ 'SOCKET_ORG_SLUG' ] = orgSlug
183+ } else {
184+ const orgSlugCResult = await getDefaultOrgSlug ( )
185+ if ( orgSlugCResult . ok ) {
186+ mixinsEnv [ 'SOCKET_ORG_SLUG' ] = orgSlugCResult . data
187+ }
188+ }
189+
190+ const proxyUrl = getDefaultProxyUrl ( )
191+ if ( proxyUrl ) {
192+ mixinsEnv [ 'SOCKET_CLI_API_PROXY' ] = proxyUrl
193+ }
194+
195+ try {
196+ const result = await spawnDlx (
197+ {
198+ name : '@coana-tech/cli' ,
199+ version : `~${ constants . ENV . INLINED_SOCKET_CLI_COANA_TECH_CLI_VERSION } ` ,
200+ } ,
201+ args ,
202+ {
203+ force : true ,
204+ silent : true ,
205+ ...dlxOptions ,
206+ env : {
207+ ...process . env ,
208+ ...constants . processEnv ,
209+ ...mixinsEnv ,
210+ ...spawnEnv ,
211+ } ,
212+ ipc : {
213+ [ constants . SOCKET_CLI_SHADOW_ACCEPT_RISKS ] : true ,
214+ [ constants . SOCKET_CLI_SHADOW_API_TOKEN ] :
215+ constants . SOCKET_PUBLIC_API_TOKEN ,
216+ [ constants . SOCKET_CLI_SHADOW_SILENT ] : true ,
217+ ...ipc ,
218+ } ,
219+ } ,
220+ spawnExtra ,
221+ )
222+ const output = await result . spawnPromise
223+ return { ok : true , data : output . stdout }
224+ } catch ( e ) {
225+ const stderr = ( e as any ) ?. stderr
226+ const cause = ( e as Error ) ?. message || UNKNOWN_ERROR
227+ const message = stderr ? stderr : cause
228+ return {
229+ ok : false ,
230+ data : e ,
231+ message,
232+ }
233+ }
168234}
169235
170236/**
0 commit comments