Skip to content

Commit 0829dac

Browse files
committed
refactor(scripts): use logger instead of console throughout scripts/
Replace all console.log/console.error/console.warn calls with logger methods: **Build Scripts** (6 files): - build-externals/: Added logger, replaced console calls - build/: Removed colors import, replaced console with logger methods **Fix Scripts** (4 files): - Removed colors imports where only used for console - Replaced printCompletedHeader with logger.success() - Consistent logger.log/logger.error usage **Test Scripts** (2 files): - Replaced all console.log with logger.log - Updated error handlers to use logger.error **Validate Scripts** (3 files): - Added logger imports where missing - Replaced console with logger.success/logger.error/logger.log - Use logger.info() for informational messages (ℹ) **Other** (2 files): - lint.mjs: Full logger migration - claude.mjs: Added logger, updated inline log.info to use logger.info() This ensures consistent output formatting and color usage across all scripts in the repository. Updated 17 files total.
1 parent 921902d commit 0829dac

17 files changed

+394
-371
lines changed

scripts/build-externals/bundler.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { createRequire } from 'node:module'
77
import path from 'node:path'
88

99
import esbuild from 'esbuild'
10+
import { getDefaultLogger } from '#socketsecurity/lib/logger'
11+
1012
import {
1113
getEsbuildConfig,
1214
getPackageSpecificOptions,
@@ -17,6 +19,7 @@ import {
1719
} from './local-packages.mjs'
1820

1921
const require = createRequire(import.meta.url)
22+
const logger = getDefaultLogger()
2023

2124
/**
2225
* Bundle a single package with esbuild.
@@ -32,7 +35,7 @@ export async function bundlePackage(packageName, outputPath, options = {}) {
3235
const { quiet = false, rootDir } = options
3336

3437
if (!quiet) {
35-
console.log(` Bundling ${packageName}...`)
38+
logger.log(` Bundling ${packageName}...`)
3639
}
3740

3841
try {
@@ -51,7 +54,7 @@ export async function bundlePackage(packageName, outputPath, options = {}) {
5154
await fs.access(srcExternalPath)
5255
packagePath = srcExternalPath
5356
if (!quiet) {
54-
console.log(
57+
logger.log(
5558
` Using entry point ${path.relative(rootDir, srcExternalPath)}`,
5659
)
5760
}
@@ -60,7 +63,7 @@ export async function bundlePackage(packageName, outputPath, options = {}) {
6063
const localPath = await getLocalPackagePath(packageName, rootDir)
6164
if (localPath) {
6265
if (!quiet) {
63-
console.log(
66+
logger.log(
6467
` Using local version from ${path.relative(rootDir, localPath)}`,
6568
)
6669
}
@@ -103,12 +106,12 @@ ${contentWithoutStrict}`
103106
const stats = await fs.stat(outputPath)
104107
const sizeKB = Math.round(stats.size / 1024)
105108
if (!quiet) {
106-
console.log(` ✓ Bundled ${packageName} (${sizeKB}KB)`)
109+
logger.log(` ✓ Bundled ${packageName} (${sizeKB}KB)`)
107110
}
108111
return sizeKB
109112
} catch (error) {
110113
if (!quiet) {
111-
console.error(` ✗ Failed to bundle ${packageName}:`, error.message)
114+
logger.log(` ✗ Failed to bundle ${packageName}: ${error.message}`)
112115
}
113116
// Create error stub.
114117
const stubContent = `'use strict'

scripts/build-externals/copy-files.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import { promises as fs } from 'node:fs'
66
import path from 'node:path'
77

8+
import { getDefaultLogger } from '#socketsecurity/lib/logger'
9+
10+
const logger = getDefaultLogger()
11+
812
/**
913
* Ensure directory exists.
1014
*
@@ -37,7 +41,7 @@ export async function copyLocalFiles(srcDir, destDir, quiet = false) {
3741

3842
await fs.copyFile(srcPath, destPath)
3943
if (!quiet) {
40-
console.log(` Copied ${file}`)
44+
logger.log(` Copied ${file}`)
4145
}
4246
count++
4347
}
@@ -82,7 +86,7 @@ export async function copyRecursive(
8286
// File doesn't exist, copy it.
8387
await fs.copyFile(srcEntry, destEntry)
8488
if (!quiet) {
85-
console.log(` Copied ${relPath}`)
89+
logger.log(` Copied ${relPath}`)
8690
}
8791
count++
8892
}

scripts/build/clean.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async function cleanDirectories(tasks, options = {}) {
6060
} catch (error) {
6161
if (!quiet) {
6262
logger.error(`Failed to clean ${name}`)
63-
console.error(error.message)
63+
logger.error(error.message)
6464
}
6565
return 1
6666
}
@@ -211,4 +211,7 @@ async function main() {
211211
}
212212
}
213213

214-
main().catch(console.error)
214+
main().catch(error => {
215+
logger.error(error.message || error)
216+
process.exitCode = 1
217+
})

scripts/build/externals.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
* Entry point that wraps the modular build-externals system.
66
*/
77

8-
import colors from 'yoctocolors-cjs'
9-
108
import { isQuiet } from '#socketsecurity/lib/argv/flags'
119
import { getDefaultLogger } from '#socketsecurity/lib/logger'
1210
import { pluralize } from '#socketsecurity/lib/words'
1311

1412
import { buildExternals } from '../build-externals/orchestrator.mjs'
1513

1614
const logger = getDefaultLogger()
17-
const printCompletedHeader = title => console.log(colors.green(`✓ ${title}`))
1815

1916
async function main() {
2017
// Check for verbose mode via isVerbose or manual check
@@ -29,7 +26,7 @@ async function main() {
2926
bundledCount > 0
3027
? `External Bundles (${bundledCount} ${pluralize('package', { count: bundledCount })})`
3128
: 'External Bundles (no packages)'
32-
printCompletedHeader(title)
29+
logger.success(title)
3330
}
3431
} catch (error) {
3532
logger.error(`Build failed: ${error.message || error}`)

scripts/build/js.mjs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const isWatch = process.argv.includes('--watch')
2424
async function buildJS() {
2525
try {
2626
if (!isQuiet) {
27-
console.log('→ Building JavaScript with esbuild')
27+
logger.step('Building JavaScript with esbuild')
2828
}
2929

3030
const startTime = Date.now()
@@ -36,19 +36,19 @@ async function buildJS() {
3636
const buildTime = Date.now() - startTime
3737

3838
if (!isQuiet) {
39-
console.log(` JavaScript built in ${buildTime}ms`)
39+
logger.log(` JavaScript built in ${buildTime}ms`)
4040

4141
if (result?.metafile && isVerbose) {
4242
const analysis = analyzeMetafile(result.metafile)
43-
console.log(` Total size: ${analysis.totalSize}`)
43+
logger.log(` Total size: ${analysis.totalSize}`)
4444
}
4545
}
4646

4747
return 0
4848
} catch (error) {
4949
if (!isQuiet) {
5050
logger.error('JavaScript build failed')
51-
console.error(error)
51+
logger.error(error)
5252
}
5353
return 1
5454
}
@@ -60,8 +60,8 @@ async function buildJS() {
6060
async function watchJS() {
6161
try {
6262
if (!isQuiet) {
63-
console.log('→ Starting watch mode with incremental builds')
64-
console.log(' Watching for file changes...')
63+
logger.step('Starting watch mode with incremental builds')
64+
logger.log(' Watching for file changes...')
6565
}
6666

6767
const ctx = await context({
@@ -83,7 +83,7 @@ async function watchJS() {
8383

8484
if (result?.metafile && isVerbose) {
8585
const analysis = analyzeMetafile(result.metafile)
86-
console.log(` Total size: ${analysis.totalSize}`)
86+
logger.log(` Total size: ${analysis.totalSize}`)
8787
}
8888
}
8989
}
@@ -98,7 +98,7 @@ async function watchJS() {
9898
// Keep process alive
9999
process.on('SIGINT', async () => {
100100
if (!isQuiet) {
101-
console.log('\nStopping watch mode...')
101+
logger.log('\nStopping watch mode...')
102102
}
103103
await ctx.dispose()
104104
process.exit(0)
@@ -109,7 +109,7 @@ async function watchJS() {
109109
} catch (error) {
110110
if (!isQuiet) {
111111
logger.error('Watch mode failed')
112-
console.error(error)
112+
logger.error(error)
113113
}
114114
return 1
115115
}
@@ -118,7 +118,7 @@ async function watchJS() {
118118
// Main
119119
if (isWatch) {
120120
watchJS().catch(error => {
121-
console.error(error)
121+
logger.error(error)
122122
process.exit(1)
123123
})
124124
} else {
@@ -127,7 +127,7 @@ if (isWatch) {
127127
process.exitCode = code
128128
})
129129
.catch(error => {
130-
console.error(error)
130+
logger.error(error)
131131
process.exitCode = 1
132132
})
133133
}

scripts/build/main.mjs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import path from 'node:path'
77
import { fileURLToPath } from 'node:url'
88

99
import { build, context } from 'esbuild'
10-
import colors from 'yoctocolors-cjs'
1110

1211
import {
1312
analyzeMetafile,
@@ -23,9 +22,6 @@ import { runSequence } from '../utils/run-command.mjs'
2322

2423
const logger = getDefaultLogger()
2524

26-
// Helper for completed headers (simple wrapper)
27-
const printCompletedHeader = title => console.log(colors.green(`✓ ${title}`))
28-
2925
const rootPath = path.resolve(
3026
path.dirname(fileURLToPath(import.meta.url)),
3127
'..',
@@ -69,7 +65,7 @@ async function buildSource(options = {}) {
6965
} catch (error) {
7066
if (!quiet) {
7167
logger.error('Source build failed')
72-
console.error(error)
68+
logger.error(error)
7369
}
7470
return { exitCode: 1, buildTime: 0, result: null }
7571
}
@@ -300,28 +296,28 @@ async function main() {
300296

301297
// Show help if requested
302298
if (values.help) {
303-
console.log('Build Runner')
304-
console.log('\nUsage: pnpm build [options]')
305-
console.log('\nOptions:')
306-
console.log(' --help Show this help message')
307-
console.log(' --src Build source code only')
308-
console.log(' --types Build TypeScript declarations only')
309-
console.log(
299+
logger.log('Build Runner')
300+
logger.log('\nUsage: pnpm build [options]')
301+
logger.log('\nOptions:')
302+
logger.log(' --help Show this help message')
303+
logger.log(' --src Build source code only')
304+
logger.log(' --types Build TypeScript declarations only')
305+
logger.log(
310306
' --watch Watch mode with incremental builds (68% faster rebuilds)',
311307
)
312-
console.log(' --needed Only build if dist files are missing')
313-
console.log(' --analyze Show bundle size analysis')
314-
console.log(' --quiet, --silent Suppress progress messages')
315-
console.log(' --verbose Show detailed build output')
316-
console.log('\nExamples:')
317-
console.log(' pnpm build # Full build (source + types)')
318-
console.log(' pnpm build --src # Build source only')
319-
console.log(' pnpm build --types # Build types only')
320-
console.log(
308+
logger.log(' --needed Only build if dist files are missing')
309+
logger.log(' --analyze Show bundle size analysis')
310+
logger.log(' --quiet, --silent Suppress progress messages')
311+
logger.log(' --verbose Show detailed build output')
312+
logger.log('\nExamples:')
313+
logger.log(' pnpm build # Full build (source + types)')
314+
logger.log(' pnpm build --src # Build source only')
315+
logger.log(' pnpm build --types # Build types only')
316+
logger.log(
321317
' pnpm build --watch # Watch mode with incremental builds',
322318
)
323-
console.log(' pnpm build --analyze # Build with size analysis')
324-
console.log(
319+
logger.log(' pnpm build --analyze # Build with size analysis')
320+
logger.log(
325321
'\nNote: Watch mode uses esbuild context API for 68% faster rebuilds',
326322
)
327323
process.exitCode = 0
@@ -404,7 +400,7 @@ async function main() {
404400
}
405401

406402
if (!quiet) {
407-
printCompletedHeader('Build Cleaned')
403+
logger.success('Build Cleaned')
408404
}
409405

410406
// Run source, externals, and types builds in parallel
@@ -447,9 +443,9 @@ async function main() {
447443
// Print final status and footer
448444
if (!quiet) {
449445
if (exitCode === 0) {
450-
console.log(colors.green('✓ Build completed successfully!'))
446+
logger.success('Build completed successfully!')
451447
} else {
452-
console.error(colors.red('✗ Build failed'))
448+
logger.error('Build failed')
453449
}
454450
printFooter()
455451
}
@@ -463,4 +459,7 @@ async function main() {
463459
}
464460
}
465461

466-
main().catch(console.error)
462+
main().catch(error => {
463+
logger.error(error.message || error)
464+
process.exitCode = 1
465+
})

0 commit comments

Comments
 (0)