Skip to content

Commit 1882c46

Browse files
committed
refactor: replace getSpinner with getDefaultSpinner
Remove getSpinner() from constants/process to resolve circular dependency between constants/process and spinner modules. All spinner instances now use getDefaultSpinner() directly, which provides lazy loading without circular import issues. Changes: - Remove getSpinner() and related imports from constants/process.ts - Update debug.ts to use getDefaultSpinner() instead of getSpinner() - Update spawn.ts to use getDefaultSpinner() instead of getSpinner() - Update stdio/prompts.ts to use getDefaultSpinner() instead of getSpinner() - Remove obsolete getSpinner tests from process.test.ts
1 parent 6b35b1f commit 1882c46

File tree

5 files changed

+11
-58
lines changed

5 files changed

+11
-58
lines changed

src/constants/process.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
* Process control: abort signals and UI utilities.
33
*/
44

5-
import type { Spinner } from '../spinner'
6-
import { Spinner as SpinnerFn } from '../spinner'
7-
85
// Abort controller and signal.
96
let _abortController: AbortController
107
export function getAbortController(): AbortController {
@@ -17,12 +14,3 @@ export function getAbortController(): AbortController {
1714
export function getAbortSignal(): AbortSignal {
1815
return getAbortController().signal
1916
}
20-
21-
// Spinner instance.
22-
let _spinner: Spinner | null | undefined
23-
export function getSpinner(): Spinner | null {
24-
if (_spinner === undefined) {
25-
_spinner = SpinnerFn() ?? null
26-
}
27-
return _spinner ?? null
28-
}

src/debug.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* Provides Socket CLI specific debug functionality and logging formatters.
44
*/
55

6-
import { getSpinner } from './constants/process'
76
import { getDebug } from './env/debug'
87
import { getSocketDebug } from './env/socket'
98
import isUnicodeSupported from './external/@socketregistry/is-unicode-supported'
109
import debugJs from './external/debug'
1110

1211
import { getDefaultLogger } from './logger'
1312
import { hasOwn } from './objects'
13+
import { getDefaultSpinner } from './spinner'
1414
import { applyLinePrefix } from './strings'
1515

1616
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
@@ -232,7 +232,7 @@ function debugDirNs(
232232
} as InspectOptions
233233
}
234234
}
235-
const spinnerInstance = options.spinner || getSpinner()
235+
const spinnerInstance = options.spinner || getDefaultSpinner()
236236
const wasSpinning = spinnerInstance?.isSpinning
237237
spinnerInstance?.stop()
238238
logger.info(`[DEBUG] ${callerName} ${pointingTriangle} object inspection:`)
@@ -269,7 +269,7 @@ function debugNs(namespacesOrOpts: NamespacesOrOptions, ...args: unknown[]) {
269269
...args.slice(1),
270270
]
271271
: args
272-
const spinnerInstance = options.spinner || getSpinner()
272+
const spinnerInstance = options.spinner || getDefaultSpinner()
273273
const wasSpinning = spinnerInstance?.isSpinning
274274
spinnerInstance?.stop()
275275
ReflectApply(logger.info, logger, logArgs)
@@ -307,7 +307,7 @@ function debugLogNs(namespacesOrOpts: NamespacesOrOptions, ...args: unknown[]) {
307307
]
308308
: [`[DEBUG] ${callerName} ${pointingTriangle}`, ...args]
309309

310-
const spinnerInstance = options.spinner || getSpinner()
310+
const spinnerInstance = options.spinner || getDefaultSpinner()
311311
const wasSpinning = spinnerInstance?.isSpinning
312312
spinnerInstance?.stop()
313313
ReflectApply(logger.info, logger, logArgs)
@@ -344,7 +344,7 @@ function debugCacheNs(
344344
const prefix = `[CACHE] ${callerName} ${pointingTriangle} ${operation}: ${key}`
345345
const logArgs = meta !== undefined ? [prefix, meta] : [prefix]
346346

347-
const spinnerInstance = options.spinner || getSpinner()
347+
const spinnerInstance = options.spinner || getDefaultSpinner()
348348
const wasSpinning = spinnerInstance?.isSpinning
349349
spinnerInstance?.stop()
350350
ReflectApply(logger.info, logger, logArgs)

src/spawn.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* - https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html
2727
*/
2828

29-
import { getAbortSignal, getSpinner } from './constants/process'
29+
import { getAbortSignal } from './constants/process'
3030

3131
import npmCliPromiseSpawn from './external/@npmcli/promise-spawn'
3232
import path from 'node:path'
@@ -35,10 +35,11 @@ import { isArray } from './arrays'
3535
import { whichSync } from './bin'
3636
import { isPath } from './paths/normalize'
3737
import { getOwn, hasOwn } from './objects'
38+
import { getDefaultSpinner } from './spinner'
3839
import { stripAnsi } from './strings'
3940

4041
const abortSignal = getAbortSignal()
41-
const spinner = getSpinner()
42+
const spinner = getDefaultSpinner()
4243

4344
// Define BufferEncoding type for TypeScript compatibility.
4445
type BufferEncoding = globalThis.BufferEncoding

src/stdio/prompts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Provides inquirer.js integration with spinner support, context handling, and theming.
44
*/
55

6-
import { getAbortSignal, getSpinner } from '../constants/process'
6+
import { getAbortSignal } from '../constants/process'
77

88
import checkboxRaw from '../external/@inquirer/checkbox'
99
import confirmRaw from '../external/@inquirer/confirm'
@@ -14,13 +14,14 @@ import * as selectModuleImport from '../external/@inquirer/select'
1414
import yoctocolorsCjs from '../external/yoctocolors-cjs'
1515

1616
import type { ColorValue } from '../colors'
17+
import { getDefaultSpinner } from '../spinner'
1718
import { getTheme } from '../themes/context'
1819
import { THEMES, type ThemeName } from '../themes/themes'
1920
import type { Theme } from '../themes/types'
2021
import { resolveColor } from '../themes/utils'
2122

2223
const abortSignal = getAbortSignal()
23-
const spinner = getSpinner()
24+
const spinner = getDefaultSpinner()
2425

2526
// Modules imported at the top - extract default and Separator
2627
const searchRaw = searchModule.default

test/unit/constants/process.test.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { describe, expect, it } from 'vitest'
1313
import {
1414
getAbortController,
1515
getAbortSignal,
16-
getSpinner,
1716
} from '@socketsecurity/lib/constants/process'
1817

1918
describe('constants/process', () => {
@@ -74,30 +73,6 @@ describe('constants/process', () => {
7473
})
7574
})
7675

77-
describe('getSpinner', () => {
78-
it('should return null or a Spinner object', () => {
79-
const spinner = getSpinner()
80-
expect(spinner === null || typeof spinner === 'object').toBe(true)
81-
})
82-
83-
it('should return same instance on multiple calls (cached)', () => {
84-
const first = getSpinner()
85-
const second = getSpinner()
86-
expect(first).toBe(second)
87-
})
88-
89-
it('should handle spinner module not being available', () => {
90-
// Should not throw even if spinner module is unavailable
91-
expect(() => getSpinner()).not.toThrow()
92-
})
93-
94-
it('should return null when spinner cannot be loaded', () => {
95-
const spinner = getSpinner()
96-
// In test environment, spinner might not be available
97-
expect([null, 'object'].includes(typeof spinner)).toBe(true)
98-
})
99-
})
100-
10176
describe('integration', () => {
10277
it('should allow AbortController and Signal to work together', () => {
10378
const controller = getAbortController()
@@ -137,14 +112,6 @@ describe('constants/process', () => {
137112
const allSame = signals.every(sig => sig === signals[0])
138113
expect(allSame).toBe(true)
139114
})
140-
141-
it('should cache spinner result', () => {
142-
const first = getSpinner()
143-
const second = getSpinner()
144-
const third = getSpinner()
145-
expect(first).toBe(second)
146-
expect(second).toBe(third)
147-
})
148115
})
149116

150117
describe('error handling', () => {
@@ -155,10 +122,6 @@ describe('constants/process', () => {
155122
it('should not throw when getting AbortSignal', () => {
156123
expect(() => getAbortSignal()).not.toThrow()
157124
})
158-
159-
it('should gracefully handle spinner loading errors', () => {
160-
expect(() => getSpinner()).not.toThrow()
161-
})
162125
})
163126

164127
describe('real-world usage', () => {

0 commit comments

Comments
 (0)