Skip to content

Commit 3e0017a

Browse files
committed
refactor(scripts): reorganize and standardize script infrastructure
- Reorganize scripts into focused categories: npm/, validation/, maintenance/, ci/ - Migrate from console to logger (@socketsecurity/lib) for consistent output - Standardize error handling with descriptive messages and proper exit codes - Extract validation boilerplate into shared validation-runner utility - Convert Promise.all to Promise.allSettled for better error handling - Add type annotations to improve type coverage - Correct root path resolution in validation scripts - Remove unused scripts and utilities (get-local-package-aliases, etc.) - Add @fileoverview headers to all script files - Standardize logger method usage across all scripts
1 parent 3030017 commit 3e0017a

File tree

85 files changed

+1751
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1751
-1278
lines changed

.config/vitest.config.mts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export default defineConfig({
171171
reportsDirectory: 'coverage',
172172
reporter: ['text', 'json', 'html', 'lcov', 'clover'],
173173
// ONLY include registry/src production code (not types.ts)
174-
include: ['registry/src/index.{ts,mts,cts}'],
174+
include: ['registry/src/**/*.{ts,mts,cts}'],
175175
// Exclude everything else (must include vitest defaults)
176176
exclude: [
177177
// Vitest defaults
@@ -193,16 +193,16 @@ export default defineConfig({
193193
'registry/plugins/**',
194194
'registry/dist/**',
195195
],
196-
// Set to false to ONLY report files in include list that are executed
197-
all: false,
196+
// Set to true to report ALL files in include list, even if not executed
197+
all: true,
198198
clean: true,
199199
skipFull: false,
200200
ignoreClassMethods: ['constructor'],
201201
thresholds: {
202-
lines: 1,
203-
functions: 80,
204-
branches: 80,
205-
statements: 1,
202+
lines: 98,
203+
functions: 100,
204+
branches: 100,
205+
statements: 98,
206206
},
207207
},
208208
},

perf/npm/json-stable-stringify.perf.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { getDefaultLogger } from '@socketsecurity/lib/logger'
77
import fastJsonStableStringify from 'fast-json-stable-stringify'
88
import { Bench } from 'tinybench'
99

10-
import constants from '../../scripts/constants.mjs'
10+
import { PERF_NPM_FIXTURES_PATH } from '../../scripts/constants/paths.mjs'
1111

1212
const logger = getDefaultLogger()
1313

1414
void (async () => {
1515
const sampleData2MbJson = require(
16-
path.join(constants.perfNpmFixturesPath, 'sample_data_2mb.json'),
16+
path.join(PERF_NPM_FIXTURES_PATH, 'sample_data_2mb.json'),
1717
)
1818
const sampleData6MbJson = {
1919
a: sampleData2MbJson,

registry/.config/esbuild.config.mjs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from 'node:path'
77
import { fileURLToPath } from 'node:url'
88
import fg from 'fast-glob'
99

10-
import { getLocalPackageAliases } from '../scripts/utils/get-local-package-aliases.mjs'
10+
import { envAsBoolean } from '@socketsecurity/lib/env/helpers'
1111

1212
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313
const rootPath = path.join(__dirname, '..')
@@ -22,38 +22,6 @@ const entryPoints = fg.sync('**/*.{ts,mts,cts}', {
2222
ignore: ['**/*.d.ts', '**/types/**'],
2323
})
2424

25-
/**
26-
* Plugin to handle local package aliases when bundle: false
27-
* esbuild's built-in alias only works with bundle: true, so we need a custom plugin
28-
*/
29-
function createAliasPlugin() {
30-
const aliases = getLocalPackageAliases(rootPath)
31-
32-
// Only create plugin if we have local aliases
33-
if (Object.keys(aliases).length === 0) {
34-
return null
35-
}
36-
37-
return {
38-
name: 'local-package-aliases',
39-
setup(build) {
40-
// Intercept imports for aliased packages
41-
for (const [packageName, aliasPath] of Object.entries(aliases)) {
42-
build.onResolve({ filter: new RegExp(`^${packageName}$`) }, () => {
43-
// Return the path to the local package dist
44-
return { path: aliasPath, external: true }
45-
})
46-
47-
// Handle subpath imports like '@socketsecurity/lib/spinner'
48-
build.onResolve({ filter: new RegExp(`^${packageName}/`) }, args => {
49-
const subpath = args.path.slice(packageName.length + 1)
50-
return { path: path.join(aliasPath, subpath), external: true }
51-
})
52-
}
53-
},
54-
}
55-
}
56-
5725
// Build configuration for CommonJS output
5826
export const buildConfig = {
5927
entryPoints,
@@ -64,7 +32,8 @@ export const buildConfig = {
6432
format: 'cjs',
6533
platform: 'node',
6634
target: 'node18',
67-
sourcemap: true,
35+
// Enable source maps for coverage (set COVERAGE=true env var)
36+
sourcemap: envAsBoolean(process.env.COVERAGE),
6837
// Disable minification for better Node ESM interop
6938
// Node ESM requires clear exports: module.exports = { foo, bar }
7039
minify: false,
@@ -73,9 +42,6 @@ export const buildConfig = {
7342
metafile: true,
7443
logLevel: 'info',
7544

76-
// Use plugin for local package aliases (built-in alias requires bundle: true)
77-
plugins: [createAliasPlugin()].filter(Boolean),
78-
7945
// Note: Cannot use "external" with bundle: false
8046
// esbuild automatically treats all imports as external when not bundling
8147

registry/scripts/build-js.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { fileURLToPath } from 'node:url'
99
import { build, context } from 'esbuild'
1010
import fg from 'fast-glob'
1111

12-
import { printError, printSuccess } from '../../scripts/utils/cli-helpers.mjs'
12+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
13+
1314
import {
1415
analyzeMetafile,
1516
buildConfig,
1617
watchConfig,
1718
} from '../.config/esbuild.config.mjs'
1819

20+
const logger = getDefaultLogger()
21+
1922
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2023
const rootPath = path.join(__dirname, '..')
2124
const srcPath = path.join(rootPath, 'src')
@@ -112,7 +115,7 @@ async function buildJS() {
112115
return 0
113116
} catch (error) {
114117
if (!isQuiet) {
115-
printError('JavaScript build failed')
118+
logger.error('JavaScript build failed')
116119
console.error(error)
117120
}
118121
return 1
@@ -140,11 +143,11 @@ async function watchJS() {
140143
build.onEnd(result => {
141144
if (result.errors.length > 0) {
142145
if (!isQuiet) {
143-
printError('Rebuild failed')
146+
logger.error('Rebuild failed')
144147
}
145148
} else {
146149
if (!isQuiet) {
147-
printSuccess('Rebuild succeeded')
150+
logger.success('Rebuild succeeded')
148151

149152
if (result?.metafile && isVerbose) {
150153
const analysis = analyzeMetafile(result.metafile)
@@ -173,7 +176,7 @@ async function watchJS() {
173176
await new Promise(() => {})
174177
} catch (error) {
175178
if (!isQuiet) {
176-
printError('Watch mode failed')
179+
logger.error('Watch mode failed')
177180
console.error(error)
178181
}
179182
return 1

registry/scripts/build.mjs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ 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
import fg from 'fast-glob'
11+
12+
import { isQuiet } from '@socketsecurity/lib/argv/flags'
13+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
14+
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
15+
1216
import {
1317
analyzeMetafile,
1418
buildConfig,
1519
watchConfig,
1620
} from '../.config/esbuild.config.mjs'
17-
import { isQuiet } from '../../scripts/utils/flags.mjs'
18-
import {
19-
logger,
20-
printCompletedHeader,
21-
printFooter,
22-
printHeader,
23-
} from '../../scripts/utils/helpers.mjs'
2421
import { parseArgs } from '../../scripts/utils/parse-args.mjs'
2522
import { runSequence } from '../../scripts/utils/run-command.mjs'
2623

24+
const logger = getDefaultLogger()
25+
2726
const rootPath = path.resolve(
2827
path.dirname(fileURLToPath(import.meta.url)),
2928
'..',
@@ -432,7 +431,7 @@ async function main() {
432431
}
433432

434433
if (!quiet) {
435-
printCompletedHeader('Build Cleaned')
434+
logger.success('Build Cleaned')
436435
}
437436

438437
// Run source and types builds in parallel.
@@ -463,9 +462,9 @@ async function main() {
463462
// Print final status and footer.
464463
if (!quiet) {
465464
if (exitCode === 0) {
466-
console.log(colors.green('✓ Build completed successfully!'))
465+
logger.success('Build completed successfully!')
467466
} else {
468-
console.error(colors.red('✗ Build failed'))
467+
logger.fail('Build failed')
469468
}
470469
printFooter()
471470
}

registry/scripts/post-build-transform.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { readFileSync, writeFileSync } from 'node:fs'
88
import path from 'node:path'
99
import { fileURLToPath } from 'node:url'
1010

11+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
12+
13+
const logger = getDefaultLogger()
14+
1115
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1216
const rootPath = path.join(__dirname, '..')
1317
const indexPath = path.join(rootPath, 'dist', 'index.js')
@@ -41,8 +45,8 @@ try {
4145
// Write back the transformed content
4246
writeFileSync(indexPath, transformedContent, 'utf8')
4347

44-
console.log('✓ Transformed exports for Node ESM interop')
48+
logger.success('Transformed exports for Node ESM interop')
4549
} catch (error) {
46-
console.error('Post-build transform failed:', error.message)
50+
logger.error(`Post-build transform failed: ${error.message}`)
4751
process.exit(1)
4852
}

registry/scripts/utils/get-local-package-aliases.mjs

Lines changed: 0 additions & 60 deletions
This file was deleted.

scripts/build.mjs

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

99
import colors from 'yoctocolors-cjs'
10+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
11+
1012
import { runCommand } from './utils/run-command.mjs'
1113

14+
const logger = getDefaultLogger()
15+
1216
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1317
const rootPath = path.resolve(__dirname, '..')
1418
const registryPath = path.join(rootPath, 'registry')
@@ -20,7 +24,6 @@ const isQuiet = args.includes('--quiet') || args.includes('--silent')
2024
async function main() {
2125
// Build the @socketsecurity/registry package.
2226
// This is required before running tests that import from it.
23-
2427
// Pass all arguments through to the registry build script.
2528
const buildArgs = ['run', 'build']
2629
if (args.length > 0) {
@@ -33,7 +36,7 @@ async function main() {
3336

3437
if (exitCode !== 0) {
3538
if (!isQuiet) {
36-
console.error(colors.red('✗ Failed to build @socketsecurity/registry'))
39+
logger.error(colors.red('✗ Failed to build @socketsecurity/registry'))
3740
}
3841
process.exitCode = exitCode
3942
return
@@ -43,6 +46,6 @@ async function main() {
4346
}
4447

4548
main().catch(error => {
46-
console.error(colors.red('✗ Build failed:'), error)
49+
logger.error(colors.red('✗ Build failed:'), error)
4750
process.exitCode = 1
4851
})

0 commit comments

Comments
 (0)