Skip to content

Commit 864d4aa

Browse files
committed
Sync shared scripts from canonical source
Brings in improved build, test, lint, and clean scripts from socket-registry along with new utility modules. Keeps existing build.mjs and package.json configuration that works with socket-cli's build system.
1 parent 996dca3 commit 864d4aa

File tree

10 files changed

+887
-799
lines changed

10 files changed

+887
-799
lines changed

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if [ -z "${DISABLE_PRECOMMIT_LINT}" ]; then
2-
pnpm run precommit
2+
pnpm lint --staged
33
else
44
echo "Skipping lint due to DISABLE_PRECOMMIT_LINT env var"
55
fi

scripts/clean.mjs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/**
2+
* @fileoverview Unified clean runner with flag-based configuration.
3+
* Removes build artifacts, caches, and other generated files.
4+
*/
5+
6+
import path from 'node:path'
7+
import { fileURLToPath } from 'node:url'
8+
9+
import { deleteAsync } from 'del'
10+
import fastGlob from 'fast-glob'
11+
12+
import { isQuiet } from '@socketsecurity/lib/argv/flags'
13+
import { parseArgs } from '@socketsecurity/lib/argv/parse'
14+
import { logger } from '@socketsecurity/lib/logger'
15+
import { createSectionHeader } from '@socketsecurity/lib/stdio/header'
16+
17+
const rootPath = path.resolve(
18+
path.dirname(fileURLToPath(import.meta.url)),
19+
'..',
20+
)
21+
22+
/**
23+
* Clean specific directories.
24+
*/
25+
async function cleanDirectories(tasks, options = {}) {
26+
const { quiet = false } = options
27+
28+
for (const task of tasks) {
29+
const { name, pattern, patterns } = task
30+
const patternsToDelete = patterns || [pattern]
31+
32+
if (!quiet) {
33+
logger.progress(`Cleaning ${name}`)
34+
}
35+
36+
try {
37+
// Find all files/dirs matching the patterns
38+
const files = await fastGlob(patternsToDelete, {
39+
cwd: rootPath,
40+
absolute: true,
41+
dot: true,
42+
onlyFiles: false,
43+
markDirectories: true,
44+
})
45+
46+
// Delete each file/directory
47+
await deleteAsync(files)
48+
49+
if (!quiet) {
50+
if (files.length > 0) {
51+
logger.done(`Cleaned ${name} (${files.length} items)`)
52+
} else {
53+
logger.done(`Cleaned ${name} (already clean)`)
54+
}
55+
}
56+
} catch (error) {
57+
if (!quiet) {
58+
logger.error(`Failed to clean ${name}`)
59+
console.error(error.message)
60+
}
61+
return 1
62+
}
63+
}
64+
65+
return 0
66+
}
67+
68+
async function main() {
69+
try {
70+
// Parse arguments
71+
const { values } = parseArgs({
72+
options: {
73+
help: {
74+
type: 'boolean',
75+
default: false,
76+
},
77+
all: {
78+
type: 'boolean',
79+
default: false,
80+
},
81+
cache: {
82+
type: 'boolean',
83+
default: false,
84+
},
85+
coverage: {
86+
type: 'boolean',
87+
default: false,
88+
},
89+
dist: {
90+
type: 'boolean',
91+
default: false,
92+
},
93+
types: {
94+
type: 'boolean',
95+
default: false,
96+
},
97+
modules: {
98+
type: 'boolean',
99+
default: false,
100+
},
101+
quiet: {
102+
type: 'boolean',
103+
default: false,
104+
},
105+
silent: {
106+
type: 'boolean',
107+
default: false,
108+
},
109+
},
110+
allowPositionals: false,
111+
strict: false,
112+
})
113+
114+
// Show help if requested
115+
if (values.help) {
116+
console.log('Clean Runner')
117+
console.log('\nUsage: pnpm clean [options]')
118+
console.log('\nOptions:')
119+
console.log(' --help Show this help message')
120+
console.log(
121+
' --all Clean everything (default if no flags)',
122+
)
123+
console.log(' --cache Clean cache directories')
124+
console.log(' --coverage Clean coverage reports')
125+
console.log(' --dist Clean build output')
126+
console.log(' --types Clean TypeScript declarations only')
127+
console.log(' --modules Clean node_modules')
128+
console.log(' --quiet, --silent Suppress progress messages')
129+
console.log('\nExamples:')
130+
console.log(
131+
' pnpm clean # Clean everything except node_modules',
132+
)
133+
console.log(' pnpm clean --dist # Clean build output only')
134+
console.log(' pnpm clean --cache --coverage # Clean cache and coverage')
135+
console.log(
136+
' pnpm clean --all --modules # Clean everything including node_modules',
137+
)
138+
process.exitCode = 0
139+
return
140+
}
141+
142+
const quiet = isQuiet(values)
143+
144+
// Determine what to clean
145+
const cleanAll =
146+
values.all ||
147+
(!values.cache &&
148+
!values.coverage &&
149+
!values.dist &&
150+
!values.types &&
151+
!values.modules)
152+
153+
const tasks = []
154+
155+
// Build task list
156+
if (cleanAll || values.cache) {
157+
tasks.push({ name: 'cache', pattern: '**/.cache' })
158+
}
159+
160+
if (cleanAll || values.coverage) {
161+
tasks.push({ name: 'coverage', pattern: 'coverage' })
162+
}
163+
164+
if (cleanAll || values.dist) {
165+
tasks.push({
166+
name: 'dist',
167+
patterns: ['dist', '*.tsbuildinfo', '.tsbuildinfo'],
168+
})
169+
} else if (values.types) {
170+
tasks.push({ name: 'dist/types', patterns: ['dist/types'] })
171+
}
172+
173+
if (values.modules) {
174+
tasks.push({ name: 'node_modules', pattern: '**/node_modules' })
175+
}
176+
177+
// Check if there's anything to clean
178+
if (tasks.length === 0) {
179+
if (!quiet) {
180+
logger.info('Nothing to clean')
181+
}
182+
process.exitCode = 0
183+
return
184+
}
185+
186+
if (!quiet) {
187+
console.log(
188+
createSectionHeader('Clean Runner', { width: 56, borderChar: '=' }),
189+
)
190+
logger.step('Cleaning project directories')
191+
}
192+
193+
// Clean directories
194+
const exitCode = await cleanDirectories(tasks, { quiet })
195+
196+
if (exitCode !== 0) {
197+
if (!quiet) {
198+
logger.error('Clean failed')
199+
}
200+
process.exitCode = exitCode
201+
} else {
202+
if (!quiet) {
203+
logger.success('Clean completed successfully!')
204+
}
205+
}
206+
} catch (error) {
207+
logger.error(`Clean runner failed: ${error.message}`)
208+
process.exitCode = 1
209+
}
210+
}
211+
212+
main().catch(console.error)

scripts/lint.mjs

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { existsSync } from 'node:fs'
77
import path from 'node:path'
8-
import { fileURLToPath } from 'node:url'
98

109
import { isQuiet } from '@socketsecurity/lib/argv/flags'
1110
import { parseArgs } from '@socketsecurity/lib/argv/parse'
@@ -15,18 +14,16 @@ import { printHeader } from '@socketsecurity/lib/stdio/header'
1514

1615
import { runCommandQuiet } from './utils/run-command.mjs'
1716

18-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
19-
2017
// Files that trigger a full lint when changed
2118
const CORE_FILES = new Set([
22-
'src/logger.ts',
23-
'src/spawn.ts',
24-
'src/fs.ts',
25-
'src/promises.ts',
19+
'src/constants.ts',
20+
'src/error.ts',
21+
'src/helpers.ts',
22+
'src/lang.ts',
2623
'src/objects.ts',
27-
'src/arrays.ts',
2824
'src/strings.ts',
29-
'src/types.ts',
25+
'src/validate.ts',
26+
'src/purl-type.ts',
3027
])
3128

3229
// Config patterns that trigger a full lint
@@ -36,7 +33,6 @@ const CONFIG_PATTERNS = [
3633
'pnpm-lock.yaml',
3734
'tsconfig*.json',
3835
'eslint.config.*',
39-
'.config/biome.json',
4036
]
4137

4238
/**
@@ -121,20 +117,6 @@ async function runLintOnFiles(files, options = {}) {
121117
'-c',
122118
'.config/eslint.config.mjs',
123119
'--report-unused-disable-directives',
124-
'--ignore-pattern',
125-
'build/',
126-
'--ignore-pattern',
127-
'binaries/',
128-
'--ignore-pattern',
129-
'dist/',
130-
'--ignore-pattern',
131-
'external/',
132-
'--ignore-pattern',
133-
'.cache/',
134-
'--ignore-pattern',
135-
'.claude/',
136-
'--ignore-pattern',
137-
'pkg-binaries/',
138120
...(fix ? ['--fix'] : []),
139121
...files,
140122
],
@@ -169,7 +151,8 @@ async function runLintOnFiles(files, options = {}) {
169151

170152
if (!quiet) {
171153
logger.clearLine().done('Linting passed')
172-
console.log()
154+
// Add newline after message (use error to write to same stream)
155+
logger.error('')
173156
}
174157

175158
return 0
@@ -203,26 +186,8 @@ async function runLintOnAll(options = {}) {
203186
'-c',
204187
'.config/eslint.config.mjs',
205188
'--report-unused-disable-directives',
206-
'--no-warn-ignored',
207-
'--ignore-pattern',
208-
'build/',
209-
'--ignore-pattern',
210-
'binaries/',
211-
'--ignore-pattern',
212-
'dist/',
213-
'--ignore-pattern',
214-
'external/',
215-
'--ignore-pattern',
216-
'.cache/',
217-
'--ignore-pattern',
218-
'.claude/',
219-
'--ignore-pattern',
220-
'pkg-binaries/',
221189
...(fix ? ['--fix'] : []),
222-
'src/',
223-
'scripts/',
224-
'test/',
225-
'.config/',
190+
'.',
226191
],
227192
name: 'eslint',
228193
},
@@ -250,7 +215,8 @@ async function runLintOnAll(options = {}) {
250215

251216
if (!quiet) {
252217
logger.clearLine().done('Linting passed')
253-
console.log()
218+
// Add newline after message (use error to write to same stream)
219+
logger.error('')
254220
}
255221

256222
return 0
@@ -373,6 +339,7 @@ async function main() {
373339

374340
if (!quiet) {
375341
printHeader('Lint Runner')
342+
console.log('')
376343
}
377344

378345
let exitCode = 0
@@ -419,13 +386,13 @@ async function main() {
419386

420387
if (exitCode !== 0) {
421388
if (!quiet) {
422-
console.log()
389+
logger.error('')
423390
console.log('Lint failed')
424391
}
425392
process.exitCode = exitCode
426393
} else {
427394
if (!quiet) {
428-
console.log()
395+
console.log('')
429396
logger.success('All lint checks passed!')
430397
}
431398
}

0 commit comments

Comments
 (0)