Skip to content

Commit d7c92b0

Browse files
committed
refactor(scripts): hoist getDefaultLogger and getDefaultSpinner
Refactor all scripts to use hoisted logger/spinner pattern instead of inline getDefaultLogger() and getDefaultSpinner() calls. Changed pattern from: getDefaultLogger().log('message') To: const logger = getDefaultLogger() // hoisted at module level logger.log('message') This improves code readability and reduces function call overhead. Files modified: 40 scripts - scripts/*.mjs - scripts/*/*.mjs
1 parent 8aa849c commit d7c92b0

40 files changed

+1287
-1205
lines changed

scripts/apply-socket-mods.mjs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { fileURLToPath } from 'node:url'
1414
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1515
import colors from 'yoctocolors-cjs'
1616

17+
18+
const logger = getDefaultLogger()
1719
const __filename = fileURLToPath(import.meta.url)
1820
const __dirname = dirname(__filename)
1921

@@ -25,7 +27,7 @@ const NODE_DIR = join(BUILD_DIR, 'node-smol')
2527
* Fix V8 include paths
2628
*/
2729
async function fixV8IncludePaths() {
28-
getDefaultLogger().log('🔧 Fixing V8 include paths...')
30+
logger.log('🔧 Fixing V8 include paths...')
2931

3032
const fixes = [
3133
{
@@ -85,27 +87,27 @@ async function fixV8IncludePaths() {
8587
if (content.includes(from)) {
8688
content = content.replace(from, to)
8789
modified = true
88-
getDefaultLogger().log(` ✓ Fixed: ${file}`)
90+
logger.log(` ✓ Fixed: ${file}`)
8991
}
9092
}
9193

9294
if (modified) {
9395
await writeFile(filePath, content, 'utf8')
9496
}
9597
} catch (e) {
96-
getDefaultLogger().warn(` ${colors.yellow('⚠')} Could not fix ${file}: ${e.message}`)
98+
logger.warn(` ${colors.yellow('⚠')} Could not fix ${file}: ${e.message}`)
9799
}
98100
}
99101

100-
getDefaultLogger().log(`${colors.green('✓')} V8 include paths fixed`)
101-
getDefaultLogger().log('')
102+
logger.log(`${colors.green('✓')} V8 include paths fixed`)
103+
logger.log('')
102104
}
103105

104106
/**
105107
* Enable SEA detection for pkg binaries
106108
*/
107109
async function enableSeaForPkg() {
108-
getDefaultLogger().log('🔧 Enabling SEA detection for pkg binaries...')
110+
logger.log('🔧 Enabling SEA detection for pkg binaries...')
109111

110112
const filePath = join(NODE_DIR, 'lib', 'sea.js')
111113

@@ -121,38 +123,38 @@ const { getAsset: getAssetInternal, getAssetKeys: getAssetKeysInternal } = inter
121123
if (content.includes(oldImport)) {
122124
content = content.replace(oldImport, newImport)
123125
await writeFile(filePath, content, 'utf8')
124-
getDefaultLogger().log(' ✓ Modified: lib/sea.js')
126+
logger.log(' ✓ Modified: lib/sea.js')
125127
} else {
126-
getDefaultLogger().log(' ℹ️ lib/sea.js already modified or structure changed')
128+
logger.log(' ℹ️ lib/sea.js already modified or structure changed')
127129
}
128130
} catch (e) {
129-
getDefaultLogger().warn(` ${colors.yellow('⚠')} Could not modify lib/sea.js: ${e.message}`)
131+
logger.warn(` ${colors.yellow('⚠')} Could not modify lib/sea.js: ${e.message}`)
130132
}
131133

132-
getDefaultLogger().log(`${colors.green('✓')} SEA detection enabled`)
133-
getDefaultLogger().log('')
134+
logger.log(`${colors.green('✓')} SEA detection enabled`)
135+
logger.log('')
134136
}
135137

136138
/**
137139
* Main function
138140
*/
139141
async function main() {
140-
getDefaultLogger().log('🔨 Applying Socket modifications to Node.js source')
141-
getDefaultLogger().log('')
142+
logger.log('🔨 Applying Socket modifications to Node.js source')
143+
logger.log('')
142144

143145
await fixV8IncludePaths()
144146
await enableSeaForPkg()
145147

146-
getDefaultLogger().log('🎉 All modifications applied!')
147-
getDefaultLogger().log('')
148-
getDefaultLogger().log('📝 To generate patches:')
149-
getDefaultLogger().log(' cd build/node-smol')
150-
getDefaultLogger().log(' git diff > ../../build/patches/socket/my-changes.patch')
151-
getDefaultLogger().log('')
148+
logger.log('🎉 All modifications applied!')
149+
logger.log('')
150+
logger.log('📝 To generate patches:')
151+
logger.log(' cd build/node-smol')
152+
logger.log(' git diff > ../../build/patches/socket/my-changes.patch')
153+
logger.log('')
152154
}
153155

154156
// Run main function
155157
main().catch(error => {
156-
getDefaultLogger().error(`${colors.red('✗')} Failed to apply modifications:`, error.message)
158+
logger.error(`${colors.red('✗')} Failed to apply modifications:`, error.message)
157159
process.exitCode = 1
158160
})

scripts/build-all-from-source.mjs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { fileURLToPath } from 'node:url'
2121
import { getDefaultLogger } from '@socketsecurity/lib/logger'
2222
import { spawn } from '@socketsecurity/lib/spawn'
2323

24+
25+
const logger = getDefaultLogger()
2426
const __filename = fileURLToPath(import.meta.url)
2527
const __dirname = path.dirname(__filename)
2628

@@ -75,13 +77,13 @@ const PACKAGES = [
7577
* Build a specific package.
7678
*/
7779
async function buildPackage(pkg) {
78-
getDefaultLogger().step(`Building ${pkg.name}`)
79-
getDefaultLogger().info(` ${pkg.description}`)
80-
getDefaultLogger().info('')
80+
logger.step(`Building ${pkg.name}`)
81+
logger.info(` ${pkg.description}`)
82+
logger.info('')
8183

8284
if (!pkg.build) {
83-
getDefaultLogger().info(` Skipping ${pkg.name} (no build needed)`)
84-
getDefaultLogger().info('')
85+
logger.info(` Skipping ${pkg.name} (no build needed)`)
86+
logger.info('')
8587
return
8688
}
8789

@@ -107,10 +109,10 @@ async function buildPackage(pkg) {
107109
}
108110

109111
const duration = Math.round((Date.now() - startTime) / 1000)
110-
getDefaultLogger().success(`${pkg.name} built successfully in ${duration}s`)
111-
getDefaultLogger().info('')
112+
logger.success(`${pkg.name} built successfully in ${duration}s`)
113+
logger.info('')
112114
} catch (e) {
113-
getDefaultLogger().fail(`Failed to build ${pkg.name}: ${e.message}`)
115+
logger.fail(`Failed to build ${pkg.name}: ${e.message}`)
114116
throw e
115117
}
116118
}
@@ -121,13 +123,13 @@ async function buildPackage(pkg) {
121123
async function main() {
122124
const totalStart = Date.now()
123125

124-
getDefaultLogger().log('')
125-
getDefaultLogger().log('🔨 Building All From-Source Packages')
126-
getDefaultLogger().log('')
126+
logger.log('')
127+
logger.log('🔨 Building All From-Source Packages')
128+
logger.log('')
127129

128130
if (FORCE_BUILD) {
129-
getDefaultLogger().warn('Force rebuild enabled (ignoring checkpoints)')
130-
getDefaultLogger().log('')
131+
logger.warn('Force rebuild enabled (ignoring checkpoints)')
132+
logger.log('')
131133
}
132134

133135
// Filter packages if specific package requested.
@@ -136,25 +138,25 @@ async function main() {
136138
if (specificPackage) {
137139
const pkg = PACKAGES.find((p) => p.name === specificPackage)
138140
if (!pkg) {
139-
getDefaultLogger().fail(`Unknown package: ${specificPackage}`)
140-
getDefaultLogger().info('')
141-
getDefaultLogger().info('Available packages:')
141+
logger.fail(`Unknown package: ${specificPackage}`)
142+
logger.info('')
143+
logger.info('Available packages:')
142144
for (const p of PACKAGES) {
143-
getDefaultLogger().info(` - ${p.name}: ${p.description}`)
145+
logger.info(` - ${p.name}: ${p.description}`)
144146
}
145147
process.exit(1)
146148
}
147149
packagesToBuild = [pkg]
148-
getDefaultLogger().info(`Building specific package: ${pkg.name}`)
149-
getDefaultLogger().info('')
150+
logger.info(`Building specific package: ${pkg.name}`)
151+
logger.info('')
150152
} else {
151-
getDefaultLogger().info('Building all packages in order:')
153+
logger.info('Building all packages in order:')
152154
for (const pkg of PACKAGES) {
153155
if (pkg.build) {
154-
getDefaultLogger().info(` ${pkg.name} - ${pkg.description}`)
156+
logger.info(` ${pkg.name} - ${pkg.description}`)
155157
}
156158
}
157-
getDefaultLogger().info('')
159+
logger.info('')
158160
}
159161

160162
// Build packages in order.
@@ -167,27 +169,27 @@ async function main() {
167169
const totalMinutes = Math.floor(totalDuration / 60)
168170
const totalSeconds = totalDuration % 60
169171

170-
getDefaultLogger().log('━'.repeat(60))
171-
getDefaultLogger().log('')
172-
getDefaultLogger().success('🎉 All packages built successfully!')
173-
getDefaultLogger().log('')
174-
getDefaultLogger().info(`Total time: ${totalMinutes}m ${totalSeconds}s`)
175-
getDefaultLogger().log('')
176-
getDefaultLogger().info('Build artifacts:')
177-
getDefaultLogger().info(' node-smol-builder: packages/node-smol-builder/build/out/Release/node')
178-
getDefaultLogger().info(' onnx-runtime: packages/onnx-runtime/build/wasm/')
179-
getDefaultLogger().info(' codet5-models: packages/codet5-models/build/models/')
180-
getDefaultLogger().info(' yoga-layout: packages/yoga-layout/build/wasm/')
181-
getDefaultLogger().log('')
182-
getDefaultLogger().info('Next steps:')
183-
getDefaultLogger().info(' 1. Test built artifacts')
184-
getDefaultLogger().info(' 2. Integrate with Socket CLI build')
185-
getDefaultLogger().info(' 3. Run Socket CLI build: pnpm run build')
186-
getDefaultLogger().log('')
172+
logger.log('━'.repeat(60))
173+
logger.log('')
174+
logger.success('🎉 All packages built successfully!')
175+
logger.log('')
176+
logger.info(`Total time: ${totalMinutes}m ${totalSeconds}s`)
177+
logger.log('')
178+
logger.info('Build artifacts:')
179+
logger.info(' node-smol-builder: packages/node-smol-builder/build/out/Release/node')
180+
logger.info(' onnx-runtime: packages/onnx-runtime/build/wasm/')
181+
logger.info(' codet5-models: packages/codet5-models/build/models/')
182+
logger.info(' yoga-layout: packages/yoga-layout/build/wasm/')
183+
logger.log('')
184+
logger.info('Next steps:')
185+
logger.info(' 1. Test built artifacts')
186+
logger.info(' 2. Integrate with Socket CLI build')
187+
logger.info(' 3. Run Socket CLI build: pnpm run build')
188+
logger.log('')
187189
}
188190

189191
// Run main function.
190192
main().catch((e) => {
191-
getDefaultLogger().fail(`Build failed: ${e.message}`)
193+
logger.fail(`Build failed: ${e.message}`)
192194
process.exit(1)
193195
})

0 commit comments

Comments
 (0)