Skip to content

Commit 6df00cc

Browse files
committed
Adjust spinner use
1 parent 1bf9673 commit 6df00cc

File tree

16 files changed

+83
-86
lines changed

16 files changed

+83
-86
lines changed

src/commands/diff-scan/get-diff-scan.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export async function getDiffScan(
2525
},
2626
apiToken: string
2727
): Promise<void> {
28-
const spinnerText = 'Getting diff scan... \n'
29-
const spinner = new Spinner({ text: spinnerText }).start()
28+
const spinner = new Spinner()
29+
30+
spinner.start('Getting diff scan...')
3031

3132
const response = await queryAPI(
3233
`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`,

src/commands/fix/run-fix.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export async function runFix() {
5555
const tree = arb.idealTree!
5656
const hasUpgrade = !!getManifestData(NPM, name)
5757
if (hasUpgrade) {
58-
spinner.stop()
59-
console.log(`Skipping ${name}. Socket Optimize package exists.`)
60-
spinner.start()
58+
spinner.info(`Skipping ${name}. Socket Optimize package exists.`)
6159
continue
6260
}
6361
const nodes = findPackageNodes(tree, name)
@@ -91,9 +89,8 @@ export async function runFix() {
9189
) {
9290
try {
9391
// eslint-disable-next-line no-await-in-loop
94-
await runScript('test', [], { stdio: 'pipe' })
92+
await runScript('test', [], { spinner, stdio: 'ignore' })
9593
spinner.info(`Patched ${name} ${oldVersion} -> ${node.version}`)
96-
spinner.start()
9794
if (isTopLevel(tree, node)) {
9895
for (const depField of [
9996
'dependencies',
@@ -113,14 +110,11 @@ export async function runFix() {
113110
// eslint-disable-next-line no-await-in-loop
114111
await editablePkgJson.save()
115112
} catch {
116-
spinner.errorAndStop(`Reverting ${name} to ${oldVersion}`)
117-
spinner.start()
113+
spinner.error(`Reverting ${name} to ${oldVersion}`)
118114
arb.idealTree = revertToIdealTree
119115
}
120116
} else {
121-
spinner.stop()
122-
console.log(`Could not patch ${name} ${oldVersion}`)
123-
spinner.start()
117+
spinner.error(`Could not patch ${name} ${oldVersion}`)
124118
}
125119
}
126120
}

src/commands/login/attempt-login.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export async function attemptLogin(
4343
apiBaseUrl ??= getSetting('apiBaseUrl') ?? undefined
4444
apiProxy ??= getSetting('apiProxy') ?? undefined
4545

46-
const spinner = new Spinner({ text: 'Verifying API key...' }).start()
46+
const spinner = new Spinner()
47+
48+
spinner.start('Verifying API key...')
4749

4850
let orgs: SocketSdkReturnType<'getOrganizations'>['data']
4951
try {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Spinner } from '@socketsecurity/registry/lib/spinner'
2-
31
import { applyLogout } from './apply-logout'
2+
import { logger } from '../../utils/logging'
43

54
export function attemptLogout() {
65
try {
76
applyLogout()
8-
new Spinner().success('Successfully logged out')
7+
logger.success('Successfully logged out')
98
} catch {
10-
new Spinner().success('Failed to complete logout steps')
9+
logger.error('Failed to complete logout steps')
1110
}
1211
}

src/commands/manifest/convert_gradle_to_maven.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,16 @@ export async function convertGradleToMaven(
4747
const commandArgs = ['--init-script', initLocation, ...gradleOpts, 'pom']
4848

4949
if (verbose) {
50-
console.log('\n[VERBOSE] Executing:', bin, commandArgs)
50+
spinner.log('[VERBOSE] Executing:', bin, commandArgs)
5151
}
52-
5352
const output = await spawn(bin, commandArgs, {
5453
cwd: target || '.'
5554
})
56-
spinner.stop()
5755
if (verbose) {
5856
console.group('[VERBOSE] gradle stdout:')
5957
console.log(output)
6058
console.groupEnd()
6159
}
62-
6360
if (output.stderr) {
6461
spinner.errorAndStop('There were errors while running gradle')
6562
// (In verbose mode, stderr was printed above, no need to repeat it)
@@ -70,10 +67,7 @@ export async function convertGradleToMaven(
7067
}
7168
process.exit(1)
7269
}
73-
74-
spinner.start()
7570
spinner.successAndStop('Executed gradle successfully')
76-
7771
console.log('Reported exports:')
7872
output.stdout.replace(
7973
/^POM file copied to: (.*)/gm,
@@ -85,30 +79,30 @@ export async function convertGradleToMaven(
8579

8680
// const loc = output.stdout?.match(/Wrote (.*?.pom)\n/)?.[1]?.trim()
8781
// if (!loc) {
88-
// spinner.errorAndStop(
82+
// console.error(
8983
// 'There were no errors from sbt but could not find the location of resulting .pom file either'
9084
// )
9185
// process.exit(1)
9286
// }
9387
//
9488
// // Move the pom file to ...? initial cwd? loc will be an absolute path, or dump to stdout
9589
// if (out === '-') {
96-
// spinner.start('Result:\n```').success()
90+
// spinner.start('Result:\n```')
9791
// console.log(await safeReadFile(loc, 'utf8'))
9892
// console.log('```')
99-
// spinner.start().success(`OK`)
93+
// spinner.successAndStop(`OK`)
10094
// } else {
95+
// spinner.start()
10196
// if (verbose) {
102-
// spinner.start(
97+
// spinner.log(
10398
// `Moving manifest file from \`${loc.replace(/^\/home\/[^/]*?\//, '~/')}\` to \`${out}\``
10499
// )
105100
// } else {
106-
// spinner.start('Moving output pom file')
101+
// spinner.log('Moving output pom file')
107102
// }
108103
// // TODO: do we prefer fs-extra? renaming can be gnarly on windows and fs-extra's version is better
109104
// await renamep(loc, out)
110-
// spinner.successAndStop()
111-
// spinner.start().success(`OK. File should be available in \`${out}\``)
105+
// spinner.successAndStop(`OK. File should be available in \`${out}\``)
112106
// }
113107
} catch (e) {
114108
spinner.errorAndStop(

src/commands/manifest/convert_sbt_to_maven.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import spawn from '@npmcli/promise-spawn'
55
import { Spinner } from '@socketsecurity/registry/lib/spinner'
66

77
import { safeReadFile } from '../../utils/fs'
8+
import { logger } from '../../utils/logging'
89

910
export async function convertSbtToMaven(
1011
target: string,
@@ -36,23 +37,22 @@ export async function convertSbtToMaven(
3637
spinner.start(`Converting sbt to maven from \`${bin}\` on \`${target}\`...`)
3738

3839
try {
39-
// Run sbt with the init script we provide which should yield zero or more pom files.
40-
// We have to figure out where to store those pom files such that we can upload them and predict them through the GitHub API.
41-
// We could do a .socket folder. We could do a socket.pom.gz with all the poms, although I'd prefer something plain-text if it is to be committed.
42-
40+
// Run sbt with the init script we provide which should yield zero or more
41+
// pom files. We have to figure out where to store those pom files such that
42+
// we can upload them and predict them through the GitHub API. We could do a
43+
// .socket folder. We could do a socket.pom.gz with all the poms, although
44+
// I'd prefer something plain-text if it is to be committed.
4345
const output = await spawn(bin, ['makePom'].concat(sbtOpts), {
4446
cwd: target || '.'
4547
})
46-
spinner.successAndStop()
48+
spinner.stop()
4749
if (verbose) {
4850
console.group('[VERBOSE] sbt stdout:')
4951
console.log(output)
5052
console.groupEnd()
5153
}
52-
5354
if (output.stderr) {
54-
spinner.start()
55-
spinner.errorAndStop('There were errors while running sbt')
55+
logger.error('There were errors while running sbt')
5656
// (In verbose mode, stderr was printed above, no need to repeat it)
5757
if (!verbose) {
5858
console.group('[VERBOSE] stderr:')
@@ -61,50 +61,45 @@ export async function convertSbtToMaven(
6161
}
6262
process.exit(1)
6363
}
64-
6564
const poms: Array<string> = []
6665
output.stdout.replace(/Wrote (.*?.pom)\n/g, (_all: string, fn: string) => {
6766
poms.push(fn)
6867
return fn
6968
})
70-
7169
if (!poms.length) {
72-
spinner.errorAndStop(
70+
logger.error(
7371
'There were no errors from sbt but it seems to not have generated any poms either'
7472
)
7573
process.exit(1)
7674
}
77-
7875
// Move the pom file to ...? initial cwd? loc will be an absolute path, or dump to stdout
7976
// TODO: what to do with multiple output files? Do we want to dump them to stdout? Raw or with separators or ?
8077
// TODO: maybe we can add an option to target a specific file to dump to stdout
8178
if (out === '-' && poms.length === 1) {
82-
spinner.start('Result:\n```').success()
79+
console.log('Result:\n```')
8380
console.log(await safeReadFile(poms[0] as string, 'utf8'))
8481
console.log('```')
85-
spinner.start().success(`OK`)
82+
logger.success(`OK`)
8683
} else if (out === '-') {
87-
spinner
88-
.start()
89-
.error(
90-
'Requested out target was stdout but there are multiple generated files'
91-
)
84+
logger.error(
85+
'Requested out target was stdout but there are multiple generated files'
86+
)
9287
poms.forEach(fn => console.error('-', fn))
9388
console.error('Exiting now...')
9489
process.exit(1)
9590
} else {
9691
// if (verbose) {
97-
// spinner.start(
92+
// console.log(
9893
// `Moving manifest file from \`${loc.replace(/^\/home\/[^/]*?\//, '~/')}\` to \`${out}\``
9994
// )
10095
// } else {
101-
// spinner.start('Moving output pom file')
96+
// console.log('Moving output pom file')
10297
// }
10398
// TODO: do we prefer fs-extra? renaming can be gnarly on windows and fs-extra's version is better
10499
// await renamep(loc, out)
105-
spinner.start().success(`Generated ${poms.length} pom files`)
100+
logger.success(`Generated ${poms.length} pom files`)
106101
poms.forEach(fn => console.log('-', fn))
107-
spinner.start().success(`OK`)
102+
logger.success(`OK`)
108103
}
109104
} catch (e) {
110105
spinner.errorAndStop(

src/commands/optimize/apply-optimization.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export async function applyOptimization(
7272
return
7373
}
7474
const spinner = new Spinner({ text: 'Socket optimizing...' })
75+
7576
spinner.start()
7677

7778
const state = await addOverrides(pkgEnvDetails.pkgPath, pkgEnvDetails, {
@@ -186,9 +187,10 @@ async function addOverrides(
186187
overridesDataByAgent.get(YARN_CLASSIC)!(pkgJson)
187188
)
188189
}
189-
if (spinner) {
190-
spinner.text = `Adding overrides${workspaceName ? ` to ${workspaceName}` : ''}...`
191-
}
190+
spinner?.setText(
191+
`Adding overrides${workspaceName ? ` to ${workspaceName}` : ''}...`
192+
)
193+
192194
const depAliasMap = new Map<string, string>()
193195

194196
const nodeRange = `>=${pkgEnvDetails.minimumNodeVersion}`

src/commands/organization/get-organization.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ async function printOrganizationsFromToken(
2626
apiToken: string,
2727
format: 'text' | 'json' | 'markdown' = 'text'
2828
) {
29-
const spinner = new Spinner({ text: 'Fetching organizations...' }).start()
29+
const spinner = new Spinner()
30+
31+
spinner.start('Fetching organizations...')
32+
3033
const socketSdk = await setupSdk(apiToken)
3134
const result = await handleApiCall(
3235
socketSdk.getOrganizations(),

src/commands/report/fetch-report-data.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export async function fetchReportData(
2222
includeAllIssues: boolean,
2323
strict: boolean
2424
): Promise<void | ReportData> {
25-
const socketSdk = await setupSdk()
26-
const spinner = new Spinner({
27-
text: `Fetching report with ID ${reportId} (this could take a while)`
28-
}).start()
25+
const spinner = new Spinner()
26+
27+
spinner.start(`Fetching report with ID ${reportId} (this could take a while)`)
2928

29+
const socketSdk = await setupSdk()
3030
let result: SocketSdkResultType<'getReport'> | undefined
3131
for (let retry = 1; !result; ++retry) {
3232
try {
@@ -41,6 +41,7 @@ export async function fetchReportData(
4141
!(err instanceof Error) ||
4242
(err.cause as any)?.cause?.response?.statusCode !== HTTP_CODE_TIMEOUT
4343
) {
44+
spinner.stop()
4445
throw err
4546
}
4647
}
@@ -50,24 +51,24 @@ export async function fetchReportData(
5051
return handleUnsuccessfulApiResponse('getReport', result, spinner)
5152
}
5253

53-
// Conclude the status of the API call
54-
54+
// Conclude the status of the API call.
5555
if (strict) {
5656
if (result.data.healthy) {
57-
spinner.successAndStop('Report result is healthy and great!')
57+
spinner.success('Report result is healthy and great!')
5858
} else {
59-
spinner.errorAndStop('Report result deemed unhealthy for project')
59+
spinner.error('Report result deemed unhealthy for project')
6060
}
6161
} else if (!result.data.healthy) {
6262
const severityCount = getSeverityCount(
6363
result.data.issues,
6464
includeAllIssues ? undefined : 'high'
6565
)
6666
const issueSummary = formatSeverityCount(severityCount)
67-
spinner.successAndStop(`Report has these issues: ${issueSummary}`)
67+
spinner.success(`Report has these issues: ${issueSummary}`)
6868
} else {
69-
spinner.successAndStop('Report has no issues')
69+
spinner.success('Report has no issues')
7070
}
71+
spinner.stop()
7172

7273
return result.data
7374
}

src/commands/repos/create-repo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export async function createRepo({
2424
default_branch: string
2525
visibility: string
2626
}): Promise<void> {
27-
const spinnerText = 'Creating repository... \n'
28-
const spinner = new Spinner({ text: spinnerText }).start()
27+
const spinner = new Spinner()
28+
29+
spinner.start('Creating repository...')
2930

3031
const socketSdk = await setupSdk(apiToken)
3132
const result = await handleApiCall(

0 commit comments

Comments
 (0)