Skip to content

Commit 439dfab

Browse files
committed
Flatten spawnCoana into spanCoanaDlx
1 parent 10652ea commit 439dfab

4 files changed

Lines changed: 84 additions & 101 deletions

File tree

src/commands/fix/coana-fix.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getSocketFixPrs, openSocketFixPr } from './pull-request.mts'
1111
import { GQL_PR_STATE_OPEN, UNKNOWN_ERROR } from '../../constants.mts'
1212
import { handleApiCall } from '../../utils/api.mts'
1313
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
14-
import { spawnCoana } from '../../utils/coana.mts'
14+
import { spawnCoanaDlx } from '../../utils/dlx.mts'
1515
import {
1616
gitCheckoutBranch,
1717
gitCommit,
@@ -106,7 +106,7 @@ export async function coanaFix(
106106
return { ok: true, data: { fixed: false } }
107107
}
108108

109-
const fixCResult = await spawnCoana(
109+
const fixCResult = await spawnCoanaDlx(
110110
[
111111
'compute-fixes-and-upgrade-purls',
112112
cwd,
@@ -160,7 +160,7 @@ export async function coanaFix(
160160
let ids: string[] | undefined
161161

162162
if (shouldSpawnCoana && isAll) {
163-
const foundCResult = await spawnCoana(
163+
const foundCResult = await spawnCoanaDlx(
164164
[
165165
'compute-fixes-and-upgrade-purls',
166166
cwd,
@@ -215,7 +215,7 @@ export async function coanaFix(
215215

216216
// Apply fix for single GHSA ID.
217217
// eslint-disable-next-line no-await-in-loop
218-
const fixCResult = await spawnCoana(
218+
const fixCResult = await spawnCoanaDlx(
219219
[
220220
'compute-fixes-and-upgrade-purls',
221221
cwd,

src/commands/scan/perform-reachability-analysis.mts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import terminalLink from 'terminal-link'
44

55
import constants, { SOCKET_WEBSITE_URL } from '../../constants.mts'
66
import { handleApiCall } from '../../utils/api.mts'
7-
import {
8-
extractTier1ReachabilityScanId,
9-
spawnCoana,
10-
} from '../../utils/coana.mts'
7+
import { extractTier1ReachabilityScanId } from '../../utils/coana.mts'
8+
import { spawnCoanaDlx } from '../../utils/dlx.mts'
119
import { hasEnterpriseOrgPlan } from '../../utils/organization.mts'
1210
import { setupSdk } from '../../utils/sdk.mts'
1311
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
@@ -177,7 +175,7 @@ export async function performReachabilityAnalysis(
177175
}
178176

179177
// Run Coana with the manifests tar hash.
180-
const coanaResult = await spawnCoana(coanaArgs, orgSlug, {
178+
const coanaResult = await spawnCoanaDlx(coanaArgs, orgSlug, {
181179
cwd,
182180
env: coanaEnv,
183181
spinner,

src/utils/coana.mts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { readJsonSync } from '@socketsecurity/registry/lib/fs'
22

3-
import { getDefaultOrgSlug } from '../commands/ci/fetch-default-org-slug.mts'
4-
import constants, { UNKNOWN_ERROR } from '../constants.mts'
5-
import { spawnCoanaDlx } from './dlx.mts'
6-
import { getDefaultApiToken, getDefaultProxyUrl } from './sdk.mts'
7-
8-
import type { ShadowBinOptions } from '../shadow/npm/bin.mts'
9-
import type { CResult } from '../types.mts'
10-
import type { SpawnExtra } from '@socketsecurity/registry/lib/spawn'
11-
123
export function extractTier1ReachabilityScanId(
134
socketFactsFile: string,
145
): string | undefined {
@@ -20,75 +11,3 @@ export function extractTier1ReachabilityScanId(
2011
? tier1ReachabilityScanId
2112
: undefined
2213
}
23-
24-
export async function spawnCoana(
25-
args: string[] | readonly string[],
26-
orgSlug?: string,
27-
options?: ShadowBinOptions | undefined,
28-
extra?: SpawnExtra | undefined,
29-
): Promise<CResult<string>> {
30-
const {
31-
env: spawnEnv,
32-
ipc,
33-
...spawnOpts
34-
} = {
35-
__proto__: null,
36-
...options,
37-
} as ShadowBinOptions
38-
39-
const mixinsEnv: Record<string, string> = {
40-
SOCKET_CLI_VERSION: constants.ENV.INLINED_SOCKET_CLI_VERSION,
41-
}
42-
const defaultApiToken = getDefaultApiToken()
43-
if (defaultApiToken) {
44-
mixinsEnv['SOCKET_CLI_API_TOKEN'] = defaultApiToken
45-
}
46-
47-
if (orgSlug) {
48-
mixinsEnv['SOCKET_ORG_SLUG'] = orgSlug
49-
} else {
50-
const orgSlugCResult = await getDefaultOrgSlug()
51-
if (orgSlugCResult.ok) {
52-
mixinsEnv['SOCKET_ORG_SLUG'] = orgSlugCResult.data
53-
}
54-
}
55-
56-
const proxyUrl = getDefaultProxyUrl()
57-
if (proxyUrl) {
58-
mixinsEnv['SOCKET_CLI_API_PROXY'] = proxyUrl
59-
}
60-
61-
try {
62-
const { spawnPromise } = await spawnCoanaDlx(
63-
args,
64-
{
65-
...spawnOpts,
66-
env: {
67-
...process.env,
68-
...constants.processEnv,
69-
...mixinsEnv,
70-
...spawnEnv,
71-
},
72-
ipc: {
73-
[constants.SOCKET_CLI_SHADOW_ACCEPT_RISKS]: true,
74-
[constants.SOCKET_CLI_SHADOW_API_TOKEN]:
75-
constants.SOCKET_PUBLIC_API_TOKEN,
76-
[constants.SOCKET_CLI_SHADOW_SILENT]: true,
77-
...ipc,
78-
},
79-
},
80-
extra,
81-
)
82-
const output = await spawnPromise
83-
return { ok: true, data: output.stdout }
84-
} catch (e) {
85-
const stderr = (e as any)?.stderr
86-
const cause = (e as Error)?.message || UNKNOWN_ERROR
87-
const message = stderr ? stderr : cause
88-
return {
89-
ok: false,
90-
data: e,
91-
message,
92-
}
93-
}
94-
}

src/utils/dlx.mts

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { createRequire } from 'node:module'
22

33
import { 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'
67
import { findUp } from './fs.mts'
8+
import { getDefaultApiToken, getDefaultProxyUrl } from './sdk.mts'
79
import { isYarnBerry } from './yarn-version.mts'
810
import shadowBin from '../shadow/npm/bin.mts'
911

1012
import type { ShadowBinOptions, ShadowBinResult } from '../shadow/npm/bin.mts'
13+
import type { CResult } from '../types.mts'
1114
import type { SpawnExtra } from '@socketsecurity/registry/lib/spawn'
1215

1316
const 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
*/
154158
export 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

Comments
 (0)