Skip to content

Commit 49d54fd

Browse files
authored
refactor: hoist remaining getDefaultLogger() calls and fix test isolation (#875)
- Hoist getDefaultLogger() calls to module scope in integration and check scripts - Fix flaky getBaseBranch test by properly isolating environment variables - Clear GITHUB_BASE_REF in test as it takes priority over GITHUB_REF_NAME
1 parent 229cb0f commit 49d54fd

29 files changed

+176
-338
lines changed

packages/cli/scripts/build.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { WIN32 } from '@socketsecurity/lib/constants/platform'
2020
import { getDefaultLogger } from '@socketsecurity/lib/logger'
2121
import { spawn } from '@socketsecurity/lib/spawn'
2222

23+
const logger = getDefaultLogger()
24+
2325
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2426
const packageRoot = path.resolve(__dirname, '..')
2527
const repoRoot = path.resolve(__dirname, '../../..')
@@ -28,7 +30,6 @@ const repoRoot = path.resolve(__dirname, '../../..')
2830
const isQuiet = () => process.argv.includes('--quiet')
2931
const isVerbose = () => process.argv.includes('--verbose')
3032
const log = {
31-
const logger = getDefaultLogger()
3233
info: msg => logger.info(msg),
3334
step: msg => logger.step(msg),
3435
success: msg => logger.success(msg),

packages/cli/scripts/check.mjs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getDefaultLogger } from '@socketsecurity/lib/logger'
99
import { spawn } from '@socketsecurity/lib/spawn'
1010
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
1111

12+
const logger = getDefaultLogger()
13+
1214
/**
1315
* Run ESLint check via lint script.
1416
*/
@@ -21,7 +23,6 @@ async function runEslintCheck(options = {}) {
2123
} = options
2224

2325
if (!quiet) {
24-
const logger = getDefaultLogger()
2526
logger.progress('Checking ESLint')
2627
}
2728

@@ -152,28 +153,16 @@ async function main() {
152153
logger.log(' --help Show this help message')
153154
logger.log(' --lint Run ESLint check only')
154155
logger.log(' --types Run TypeScript check only')
155-
logger.log(
156-
' --all Check all files (passes to lint)',
157-
)
158-
logger.log(
159-
' --staged Check staged files (passes to lint)',
160-
)
161-
logger.log(
162-
' --changed Check changed files (passes to lint)',
163-
)
156+
logger.log(' --all Check all files (passes to lint)')
157+
logger.log(' --staged Check staged files (passes to lint)')
158+
logger.log(' --changed Check changed files (passes to lint)')
164159
logger.log(' --quiet, --silent Suppress progress messages')
165160
logger.log('\nExamples:')
166-
logger.log(
167-
' pnpm check # Run all checks on changed files',
168-
)
169-
logger.log(
170-
' pnpm check --all # Run all checks on all files',
171-
)
161+
logger.log(' pnpm check # Run all checks on changed files')
162+
logger.log(' pnpm check --all # Run all checks on all files')
172163
logger.log(' pnpm check --lint # Run ESLint only')
173164
logger.log(' pnpm check --types # Run TypeScript only')
174-
logger.log(
175-
' pnpm check --lint --staged # Run ESLint on staged files',
176-
)
165+
logger.log(' pnpm check --lint --staged # Run ESLint on staged files')
177166
process.exitCode = 0
178167
return
179168
}

packages/cli/scripts/claude.mjs

Lines changed: 38 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { parseArgs } from '@socketsecurity/lib/argv/parse'
2222
import { safeDelete } from '@socketsecurity/lib/fs'
2323
import { getDefaultLogger } from '@socketsecurity/lib/logger'
2424

25+
const logger = getDefaultLogger()
26+
2527
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2628
const rootPath = path.join(__dirname, '..')
2729
const parentPath = path.join(rootPath, '..')
@@ -92,7 +94,6 @@ const PRICING = {
9294

9395
// Simple inline logger.
9496
const log = {
95-
const logger = getDefaultLogger()
9697
info: msg => logger.log(msg),
9798
error: msg => logger.error(`${colors.red('✗')} ${msg}`),
9899
success: msg => logger.log(`${colors.green('✓')} ${msg}`),
@@ -255,31 +256,21 @@ class CostTracker {
255256
showSessionSummary() {
256257
const duration = Date.now() - this.startTime
257258
logger.log(colors.cyan('\n💰 Cost Summary:'))
258-
logger.log(
259-
` Input tokens: ${this.session.input.toLocaleString()}`,
260-
)
261-
logger.log(
262-
` Output tokens: ${this.session.output.toLocaleString()}`,
263-
)
259+
logger.log(` Input tokens: ${this.session.input.toLocaleString()}`)
260+
logger.log(` Output tokens: ${this.session.output.toLocaleString()}`)
264261
if (this.session.cacheWrite > 0) {
265-
logger.log(
266-
` Cache write: ${this.session.cacheWrite.toLocaleString()}`,
267-
)
262+
logger.log(` Cache write: ${this.session.cacheWrite.toLocaleString()}`)
268263
}
269264
if (this.session.cacheRead > 0) {
270-
logger.log(
271-
` Cache read: ${this.session.cacheRead.toLocaleString()}`,
272-
)
265+
logger.log(` Cache read: ${this.session.cacheRead.toLocaleString()}`)
273266
}
274267
logger.log(
275268
` Session cost: ${colors.green(`$${this.session.cost.toFixed(4)}`)}`,
276269
)
277270
logger.log(
278271
` Monthly total: ${colors.yellow(`$${this.monthly.cost.toFixed(2)}`)}`,
279272
)
280-
logger.log(
281-
` Duration: ${colors.gray(formatDuration(duration))}`,
282-
)
273+
logger.log(` Duration: ${colors.gray(formatDuration(duration))}`)
283274
}
284275
}
285276

@@ -1261,9 +1252,7 @@ async function ensureClaudeAuthenticated(claudeCmd) {
12611252

12621253
// Not authenticated, provide instructions for manual authentication
12631254
log.warn('Claude Code login required')
1264-
logger.log(
1265-
colors.yellow('\nClaude Code needs to be authenticated.'),
1266-
)
1255+
logger.log(colors.yellow('\nClaude Code needs to be authenticated.'))
12671256
logger.log('\nTo authenticate:')
12681257
logger.log(' 1. Open a new terminal')
12691258
logger.log(` 2. Run: ${colors.green('claude')}`)
@@ -1314,9 +1303,7 @@ async function ensureGitHubAuthenticated() {
13141303

13151304
// Not authenticated, prompt for login
13161305
log.warn('GitHub authentication required')
1317-
logger.log(
1318-
colors.yellow('\nYou need to authenticate with GitHub.'),
1319-
)
1306+
logger.log(colors.yellow('\nYou need to authenticate with GitHub.'))
13201307
logger.log('Follow the prompts to complete authentication.\n')
13211308

13221309
// Run gh auth login interactively
@@ -1334,9 +1321,7 @@ async function ensureGitHubAuthenticated() {
13341321
logger.log(colors.red('\nLogin failed. Please try again.'))
13351322

13361323
if (attempts < maxAttempts) {
1337-
logger.log(
1338-
colors.yellow(`\nAttempt ${attempts + 1} of ${maxAttempts}`),
1339-
)
1324+
logger.log(colors.yellow(`\nAttempt ${attempts + 1} of ${maxAttempts}`))
13401325
await new Promise(resolve => setTimeout(resolve, 1000))
13411326
}
13421327
}
@@ -2699,9 +2684,7 @@ Apply the fix and return ONLY the fixed code snippet.`
26992684

27002685
// Report issues that need review
27012686
if (toReview.length > 0) {
2702-
logger.log(
2703-
`\n${colors.yellow('Issues requiring manual review:')}`,
2704-
)
2687+
logger.log(`\n${colors.yellow('Issues requiring manual review:')}`)
27052688
toReview.forEach((issue, i) => {
27062689
logger.log(
27072690
`${i + 1}. [${issue.severity}] ${issue.file}:${issue.line} - ${issue.description}`,
@@ -4327,18 +4310,14 @@ Let's work through this together to get CI passing.`
43274310
logger.log(` macOS: ${colors.green('brew install gh')}`)
43284311
logger.log(` Ubuntu: ${colors.green('sudo apt install gh')}`)
43294312
logger.log(` Fedora: ${colors.green('sudo dnf install gh')}`)
4330-
logger.log(
4331-
` Windows: ${colors.green('winget install --id GitHub.cli')}`,
4332-
)
4313+
logger.log(` Windows: ${colors.green('winget install --id GitHub.cli')}`)
43334314
logger.log(
43344315
` Other: ${colors.gray('https://github.com/cli/cli/blob/trunk/docs/install_linux.md')}`,
43354316
)
43364317
logger.log(`\n${colors.yellow('After installation:')}`)
43374318
logger.log(` 1. Run: ${colors.green('gh auth login')}`)
43384319
logger.log(' 2. Follow the prompts to authenticate')
4339-
logger.log(
4340-
` 3. Try again: ${colors.green('pnpm claude --green')}`,
4341-
)
4320+
logger.log(` 3. Try again: ${colors.green('pnpm claude --green')}`)
43424321
return false
43434322
}
43444323

@@ -4349,9 +4328,7 @@ Let's work through this together to get CI passing.`
43494328
logger.log(
43504329
colors.red('\nGitHub authentication is required for CI monitoring.'),
43514330
)
4352-
logger.log(
4353-
'Please ensure you can login to GitHub CLI and try again.',
4354-
)
4331+
logger.log('Please ensure you can login to GitHub CLI and try again.')
43554332
return false
43564333
}
43574334

@@ -4449,9 +4426,7 @@ Let's work through this together to get CI passing.`
44494426
logger.log('\n2. If not authenticated, login:')
44504427
logger.log(` ${colors.green('gh auth login')}`)
44514428
logger.log('\n3. Test repository access:')
4452-
logger.log(
4453-
` ${colors.green(`gh api repos/${owner}/${repo}`)}`,
4454-
)
4429+
logger.log(` ${colors.green(`gh api repos/${owner}/${repo}`)}`)
44554430
logger.log('\n4. Check if workflows exist:')
44564431
logger.log(
44574432
` ${colors.green(`gh workflow list --repo ${owner}/${repo}`)}`,
@@ -4578,9 +4553,7 @@ Let's work through this together to get CI passing.`
45784553
)
45794554
})
45804555
if (snapshotList.length > 5) {
4581-
logger.log(
4582-
colors.gray(` ... and ${snapshotList.length - 5} more`),
4583-
)
4556+
logger.log(colors.gray(` ... and ${snapshotList.length - 5} more`))
45844557
}
45854558
}
45864559

@@ -5371,26 +5344,20 @@ async function runWatchMode(claudeCmd, options = {}) {
53715344
*/
53725345
function showOperations() {
53735346
logger.log('\nCore operations:')
5374-
logger.log(
5375-
' --commit Create commits with Claude assistance',
5376-
)
5347+
logger.log(' --commit Create commits with Claude assistance')
53775348
logger.log(
53785349
' --green Ensure all tests pass, push, monitor CI until green',
53795350
)
53805351
logger.log(' --push Create commits and push to remote')
5381-
logger.log(
5382-
' --sync Synchronize CLAUDE.md files across projects',
5383-
)
5352+
logger.log(' --sync Synchronize CLAUDE.md files across projects')
53845353

53855354
logger.log('\nCode quality:')
53865355
logger.log(' --audit Security and quality audit')
53875356
logger.log(' --clean Find unused code and imports')
53885357
logger.log(' --fix Scan for bugs and security issues')
53895358
logger.log(' --optimize Performance optimization analysis')
53905359
logger.log(' --refactor Suggest code improvements')
5391-
logger.log(
5392-
' --review Review staged changes before committing',
5393-
)
5360+
logger.log(' --review Review staged changes before committing')
53945361

53955362
logger.log('\nDevelopment:')
53965363
logger.log(' --debug Help debug errors')
@@ -5567,50 +5534,30 @@ async function main() {
55675534

55685535
// Show help if requested or no operation specified.
55695536
if (values.help || !hasOperation) {
5570-
logger.log(
5571-
'\nUsage: pnpm claude [operation] [options] [files...]',
5572-
)
5537+
logger.log('\nUsage: pnpm claude [operation] [options] [files...]')
55735538
logger.log('\nClaude-powered utilities for Socket projects.')
55745539
showOperations()
55755540
logger.log('\nOptions:')
55765541
logger.log(
55775542
' --cross-repo Operate on all Socket projects (default: current only)',
55785543
)
5579-
logger.log(
5580-
' --dry-run Preview changes without writing files',
5581-
)
5544+
logger.log(' --dry-run Preview changes without writing files')
55825545
logger.log(
55835546
' --max-auto-fixes N Max auto-fix attempts (--green, default: 10)',
55845547
)
5585-
logger.log(
5586-
' --max-retries N Max CI fix attempts (--green, default: 3)',
5587-
)
5588-
logger.log(
5589-
' --no-darkwing Disable "Let\'s get dangerous!" mode',
5590-
)
5591-
logger.log(
5592-
' --no-report Skip generating scan report (--fix)',
5593-
)
5594-
logger.log(
5595-
' --no-verify Use --no-verify when committing',
5596-
)
5597-
logger.log(
5598-
' --pinky Use default model (Claude 3.5 Sonnet)',
5599-
)
5600-
logger.log(
5601-
' --prompt Prompt for approval before fixes (--fix)',
5602-
)
5603-
logger.log(
5604-
' --seq Run sequentially (default: parallel)',
5605-
)
5548+
logger.log(' --max-retries N Max CI fix attempts (--green, default: 3)')
5549+
logger.log(' --no-darkwing Disable "Let\'s get dangerous!" mode')
5550+
logger.log(' --no-report Skip generating scan report (--fix)')
5551+
logger.log(' --no-verify Use --no-verify when committing')
5552+
logger.log(' --pinky Use default model (Claude 3.5 Sonnet)')
5553+
logger.log(' --prompt Prompt for approval before fixes (--fix)')
5554+
logger.log(' --seq Run sequentially (default: parallel)')
56065555
logger.log(" --skip-commit Update files but don't commit")
56075556
logger.log(
56085557
' --the-brain Use ultrathink mode - "Try to take over the world!"',
56095558
)
56105559
logger.log(' --watch Continuous monitoring mode')
5611-
logger.log(
5612-
' --workers N Number of parallel workers (default: 3)',
5613-
)
5560+
logger.log(' --workers N Number of parallel workers (default: 3)')
56145561
logger.log('\nExamples:')
56155562
logger.log(
56165563
' pnpm claude --fix # Auto-fix issues (careful mode)',
@@ -5621,30 +5568,20 @@ async function main() {
56215568
logger.log(
56225569
' pnpm claude --fix --watch # Continuous monitoring & fixing',
56235570
)
5624-
logger.log(
5625-
' pnpm claude --review # Review staged changes',
5626-
)
5627-
logger.log(
5628-
' pnpm claude --green # Ensure CI passes',
5629-
)
5571+
logger.log(' pnpm claude --review # Review staged changes')
5572+
logger.log(' pnpm claude --green # Ensure CI passes')
56305573
logger.log(
56315574
' pnpm claude --green --dry-run # Test green without real CI',
56325575
)
56335576
logger.log(
56345577
' pnpm claude --fix --the-brain # Deep analysis with ultrathink mode',
56355578
)
5636-
logger.log(
5637-
' pnpm claude --fix --workers 5 # Use 5 parallel workers',
5638-
)
5579+
logger.log(' pnpm claude --fix --workers 5 # Use 5 parallel workers')
56395580
logger.log(
56405581
' pnpm claude --test lib/utils.js # Generate tests for a file',
56415582
)
5642-
logger.log(
5643-
' pnpm claude --refactor src/index.js # Suggest refactoring',
5644-
)
5645-
logger.log(
5646-
' pnpm claude --push # Commit and push changes',
5647-
)
5583+
logger.log(' pnpm claude --refactor src/index.js # Suggest refactoring')
5584+
logger.log(' pnpm claude --push # Commit and push changes')
56485585
logger.log(' pnpm claude --help # Show this help')
56495586
logger.log('\nRequires:')
56505587
logger.log(' - Claude Code CLI (claude) installed')
@@ -5661,17 +5598,13 @@ async function main() {
56615598
log.failed('Claude Code CLI not found')
56625599
log.error('Please install Claude Code to use these utilities')
56635600
logger.log(`\n${colors.cyan('Installation Instructions:')}`)
5664-
logger.log(
5665-
' 1. Visit: https://docs.claude.com/en/docs/claude-code',
5666-
)
5601+
logger.log(' 1. Visit: https://docs.claude.com/en/docs/claude-code')
56675602
logger.log(' 2. Or install via npm:')
56685603
logger.log(
56695604
` ${colors.green('npm install -g @anthropic/claude-desktop')}`,
56705605
)
56715606
logger.log(' 3. Or download directly:')
5672-
logger.log(
5673-
` macOS: ${colors.gray('brew install claude')}`,
5674-
)
5607+
logger.log(` macOS: ${colors.gray('brew install claude')}`)
56755608
logger.log(
56765609
` Linux: ${colors.gray('curl -fsSL https://docs.claude.com/install.sh | sh')}`,
56775610
)
@@ -5680,12 +5613,8 @@ async function main() {
56805613
)
56815614
logger.log(`\n${colors.yellow('After installation:')}`)
56825615
logger.log(` 1. Run: ${colors.green('claude')}`)
5683-
logger.log(
5684-
' 2. Sign in with your Anthropic account when prompted',
5685-
)
5686-
logger.log(
5687-
` 3. Try again: ${colors.green('pnpm claude --help')}`,
5688-
)
5616+
logger.log(' 2. Sign in with your Anthropic account when prompted')
5617+
logger.log(` 3. Try again: ${colors.green('pnpm claude --help')}`)
56895618
process.exitCode = 1
56905619
return
56915620
}

packages/cli/scripts/compress-cli.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ const checksumPath = path.join(distPath, 'cli.js.bz.sha256')
5656
writeFileSync(checksumPath, `${sha256} cli.js.bz\n`)
5757

5858
logger.success(`SHA256: ${sha256}`)
59-
logger.log(
60-
`Checksum written to: ${path.relative(rootPath, checksumPath)}`,
61-
)
59+
logger.log(`Checksum written to: ${path.relative(rootPath, checksumPath)}`)
6260

6361
logger.log('')

0 commit comments

Comments
 (0)