Skip to content

Commit 2542d46

Browse files
committed
Tweak wasSpinning
1 parent d54ce54 commit 2542d46

6 files changed

Lines changed: 112 additions & 51 deletions

File tree

src/commands/fix/npm-fix.mts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ async function install(
7171
...(extraArgs ?? []),
7272
]
7373

74-
const isSpinning = spinner?.isSpinning
74+
const wasSpinning = !!spinner?.isSpinning
75+
7576
spinner?.stop()
7677

7778
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
@@ -91,14 +92,16 @@ async function install(
9192

9293
const treeResult = await getActualTree(cwd)
9394
if (treeResult.actualTree) {
94-
if (isSpinning) {
95+
if (wasSpinning) {
9596
spinner.start()
9697
}
9798
return treeResult
9899
}
100+
99101
debugFn('error', 'caught: await arb.loadActual() error')
100102
debugDir('inspect', treeResult)
101-
if (isSpinning) {
103+
104+
if (wasSpinning) {
102105
spinner.start()
103106
}
104107
return treeResult

src/commands/fix/pnpm-fix.mts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ async function install(
5858
...(extraArgs ?? []),
5959
]
6060

61-
const isSpinning = spinner?.isSpinning
61+
const wasSpinning = !!spinner?.isSpinning
62+
6263
spinner?.stop()
6364

6465
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
@@ -78,14 +79,16 @@ async function install(
7879

7980
const treeResult = await getActualTree(cwd)
8081
if (treeResult.actualTree) {
81-
if (isSpinning) {
82+
if (wasSpinning) {
8283
spinner.start()
8384
}
8485
return treeResult
8586
}
87+
8688
debugFn('error', 'caught: await arb.loadActual() error')
8789
debugDir('inspect', treeResult)
88-
if (isSpinning) {
90+
91+
if (wasSpinning) {
8992
spinner.start()
9093
}
9194
return treeResult

src/commands/optimize/update-lockfile.mts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ export async function updateLockfile(
2929
__proto__: null,
3030
...options,
3131
} as UpdateLockfileOptions
32-
const isSpinning = !!spinner?.isSpinning
33-
if (!isSpinning) {
34-
spinner?.start()
35-
}
36-
spinner?.setText(`Updating ${pkgEnvDetails.lockName}...`)
32+
33+
const wasSpinning = !!spinner?.isSpinning
34+
35+
spinner?.start(`Updating ${pkgEnvDetails.lockName}...`)
36+
3737
try {
3838
await runAgentInstall(pkgEnvDetails, { spinner })
3939
if (pkgEnvDetails.features.npmBuggyOverrides) {
@@ -48,6 +48,10 @@ export async function updateLockfile(
4848
debugFn('error', 'fail: update')
4949
debugDir('inspect', { error: e })
5050

51+
if (wasSpinning) {
52+
spinner.start()
53+
}
54+
5155
return {
5256
ok: false,
5357
message: 'Update failed',
@@ -57,10 +61,11 @@ export async function updateLockfile(
5761
),
5862
}
5963
}
60-
if (isSpinning) {
61-
spinner?.start()
62-
} else {
63-
spinner?.stop()
64+
65+
spinner?.stop()
66+
67+
if (wasSpinning) {
68+
spinner.start()
6469
}
6570

6671
return { ok: true, data: undefined }

src/commands/scan/cmd-scan-create.mts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ async function run(
321321
message: 'Canceled by user',
322322
cause: 'Org selector was canceled by user',
323323
},
324-
outputKind,
325-
false,
324+
{
325+
interactive: false,
326+
outputKind,
327+
},
326328
)
327329
return
328330
}

src/commands/scan/handle-create-new-scan.mts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ export async function handleCreateNewScan({
7777

7878
const supportedFilesCResult = await fetchSupportedScanFileNames()
7979
if (!supportedFilesCResult.ok) {
80-
await outputCreateNewScan(supportedFilesCResult, outputKind, interactive)
80+
await outputCreateNewScan(supportedFilesCResult, {
81+
interactive,
82+
outputKind,
83+
})
8184
return
8285
}
8386

@@ -141,7 +144,7 @@ export async function handleCreateNewScan({
141144
spinner.stop()
142145

143146
if (!reachResult.ok) {
144-
await outputCreateNewScan(reachResult, outputKind, interactive)
147+
await outputCreateNewScan(reachResult, { interactive, outputKind })
145148
return
146149
}
147150

@@ -199,12 +202,17 @@ export async function handleCreateNewScan({
199202
cause: 'Server did not respond with a scan ID',
200203
data: fullScanCResult.data,
201204
},
202-
outputKind,
203-
interactive,
205+
{
206+
interactive,
207+
outputKind,
208+
},
204209
)
205210
}
206211
} else {
207-
await outputCreateNewScan(fullScanCResult, outputKind, interactive)
212+
spinner.stop()
213+
spinner.clear()
214+
215+
await outputCreateNewScan(fullScanCResult, { interactive, outputKind })
208216
}
209217
}
210218

@@ -250,7 +258,7 @@ async function performReachabilityAnalysis(
250258

251259
const sockSdk = sockSdkCResult.data
252260

253-
const wasSpinning = spinner?.isSpinning ?? false
261+
const wasSpinning = !!spinner?.isSpinning
254262

255263
// Upload manifests to get tar hash
256264
spinner?.start('Uploading manifests for reachability analysis...')
@@ -266,17 +274,20 @@ async function performReachabilityAnalysis(
266274
spinner,
267275
},
268276
)
277+
278+
spinner?.stop()
279+
269280
if (!uploadCResult.ok) {
270-
if (!wasSpinning) {
271-
spinner?.stop()
281+
if (wasSpinning) {
282+
spinner.start()
272283
}
273284
return uploadCResult
274285
}
275286

276287
const tarHash = (uploadCResult.data as { tarHash?: string })?.tarHash
277288
if (!tarHash) {
278-
if (!wasSpinning) {
279-
spinner?.stop()
289+
if (wasSpinning) {
290+
spinner.start()
280291
}
281292
return {
282293
ok: false,
@@ -285,11 +296,11 @@ async function performReachabilityAnalysis(
285296
}
286297
}
287298

299+
spinner?.start()
288300
spinner?.success(`Manifests uploaded successfully. Tar hash: ${tarHash}`)
301+
spinner?.infoAndStop('Running reachability analysis with Coana...')
289302

290303
// Run Coana with the manifests tar hash.
291-
spinner?.info('Running reachability analysis with Coana...')
292-
293304
const coanaResult = await spawnCoana(
294305
[
295306
'run',
@@ -304,29 +315,29 @@ async function performReachabilityAnalysis(
304315
],
305316
{
306317
cwd,
307-
stdio: 'inherit',
308318
env: {
309319
...process.env,
310320
SOCKET_REPO_NAME: repoName,
311321
SOCKET_BRANCH_NAME: branchName,
312322
},
323+
spinner,
324+
stdio: 'inherit',
313325
},
314326
)
315327

316-
if (!wasSpinning) {
317-
spinner?.stop()
318-
}
319-
if (!coanaResult.ok) {
320-
return coanaResult
321-
}
322-
// Use the DOT_SOCKET_DOT_FACTS_JSON file for the scan.
323-
return {
324-
ok: true,
325-
data: {
326-
scanPaths: [constants.DOT_SOCKET_DOT_FACTS_JSON],
327-
tier1ReachabilityScanId: extractTier1ReachabilityScanId(
328-
constants.DOT_SOCKET_DOT_FACTS_JSON,
329-
),
330-
},
328+
if (wasSpinning) {
329+
spinner.start()
331330
}
331+
return coanaResult.ok
332+
? {
333+
ok: true,
334+
data: {
335+
// Use the DOT_SOCKET_DOT_FACTS_JSON file for the scan.
336+
scanPaths: [constants.DOT_SOCKET_DOT_FACTS_JSON],
337+
tier1ReachabilityScanId: extractTier1ReachabilityScanId(
338+
constants.DOT_SOCKET_DOT_FACTS_JSON,
339+
),
340+
},
341+
}
342+
: coanaResult
332343
}

src/commands/scan/output-create-new-scan.mts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,52 @@ import colors from 'yoctocolors-cjs'
44
import { logger } from '@socketsecurity/registry/lib/logger'
55
import { confirm } from '@socketsecurity/registry/lib/prompts'
66

7+
import constants from '../../constants.mts'
78
import { failMsgWithBadge } from '../../utils/fail-msg-with-badge.mts'
89
import { serializeResultJson } from '../../utils/serialize-result-json.mts'
910

1011
import type { CResult, OutputKind } from '../../types.mts'
12+
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
1113
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
1214

15+
export type CreateNewScanOptions = {
16+
interactive?: boolean | undefined
17+
outputKind?: OutputKind | undefined
18+
spinner?: Spinner | undefined
19+
}
20+
1321
export async function outputCreateNewScan(
1422
result: CResult<SocketSdkSuccessResult<'CreateOrgFullScan'>['data']>,
15-
outputKind: OutputKind,
16-
interactive: boolean,
23+
options?: CreateNewScanOptions | undefined,
1724
) {
25+
const {
26+
interactive = false,
27+
outputKind = 'text',
28+
// Lazily access constants.spinner.
29+
spinner = constants.spinner,
30+
} = { __proto__: null, ...options } as CreateNewScanOptions
31+
1832
if (!result.ok) {
1933
process.exitCode = result.code ?? 1
2034
}
2135

36+
const wasSpinning = !!spinner?.isSpinning
37+
38+
spinner?.stop()
39+
2240
if (outputKind === 'json') {
2341
logger.log(serializeResultJson(result))
42+
if (wasSpinning) {
43+
spinner.start()
44+
}
2445
return
2546
}
47+
2648
if (!result.ok) {
2749
logger.fail(failMsgWithBadge(result.message, result.cause))
50+
if (wasSpinning) {
51+
spinner.start()
52+
}
2853
return
2954
}
3055

@@ -47,19 +72,31 @@ export async function outputCreateNewScan(
4772
)
4873
}
4974
logger.log('')
75+
if (wasSpinning) {
76+
spinner.start()
77+
}
5078
return
5179
}
5280

5381
const link = colors.underline(colors.cyan(`${result.data.html_report_url}`))
82+
83+
logger.log('')
5484
logger.log(`Available at: ${link}`)
5585

5686
if (
5787
interactive &&
58-
(await confirm({
59-
message: 'Would you like to open it in your browser?',
60-
default: false,
61-
}))
88+
(await confirm(
89+
{
90+
message: 'Would you like to open it in your browser?',
91+
default: false,
92+
},
93+
{ spinner },
94+
))
6295
) {
6396
await open(`${result.data.html_report_url}`)
6497
}
98+
99+
if (wasSpinning) {
100+
spinner.start()
101+
}
65102
}

0 commit comments

Comments
 (0)