Skip to content

Commit 20943ef

Browse files
committed
refactor(validation): use normalizePath from @socketsecurity/lib
- Replace inline normalizePath implementations with library utility - Updated 4 validation scripts: - dist-exports.mjs - external-exports.mjs - esm-named-exports.mjs - external-esm-cjs.mjs - Removes duplicate code and uses more robust path normalization - Library version handles edge cases (win32 namespaces, etc.)
1 parent c074bbd commit 20943ef

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

scripts/validate/dist-exports.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1212
const distDir = path.resolve(__dirname, '..', '..', 'dist')
1313
const require = createRequire(import.meta.url)
1414

15-
// Normalize path for cross-platform (converts backslashes to forward slashes)
16-
const normalizePath = p => p.split(path.sep).join('/')
17-
1815
// Import CommonJS modules using require
1916
const { isQuiet } = require('#socketsecurity/lib/argv/flags')
2017
const { getDefaultLogger } = require('#socketsecurity/lib/logger')
18+
const { normalizePath } = require('#socketsecurity/lib/path')
2119
const { pluralize } = require('#socketsecurity/lib/words')
2220

2321
const logger = getDefaultLogger()

scripts/validate/esm-named-exports.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313
const distDir = path.resolve(__dirname, '..', '..', 'dist')
1414
const require = createRequire(import.meta.url)
1515

16-
// Normalize path for cross-platform (converts backslashes to forward slashes)
17-
const normalizePath = p => p.split(path.sep).join('/')
18-
1916
// Import CommonJS modules using require
2017
const { isQuiet } = require('#socketsecurity/lib/argv/flags')
2118
const { getDefaultLogger } = require('#socketsecurity/lib/logger')
19+
const { normalizePath } = require('#socketsecurity/lib/path')
2220
const { pluralize } = require('#socketsecurity/lib/words')
2321

2422
const logger = getDefaultLogger()

scripts/validate/external-esm-cjs.mjs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import { createRequire } from 'node:module'
15-
import { readdirSync, statSync } from 'node:fs'
15+
import { readdirSync } from 'node:fs'
1616
import path from 'node:path'
1717
import { fileURLToPath } from 'node:url'
1818
import { pathToFileURL } from 'node:url'
@@ -21,12 +21,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
2121
const externalDir = path.resolve(__dirname, '..', '..', 'dist', 'external')
2222
const require = createRequire(import.meta.url)
2323

24-
// Normalize path for cross-platform (converts backslashes to forward slashes)
25-
const normalizePath = p => p.split(path.sep).join('/')
26-
2724
// Import CommonJS modules using require
2825
const { isQuiet } = require('#socketsecurity/lib/argv/flags')
2926
const { getDefaultLogger } = require('#socketsecurity/lib/logger')
27+
const { normalizePath } = require('#socketsecurity/lib/path')
3028
const { pluralize } = require('#socketsecurity/lib/words')
3129

3230
const logger = getDefaultLogger()
@@ -88,7 +86,8 @@ async function checkModuleExports(filePath) {
8886

8987
// Validate CJS export structure
9088
const cjsType = typeof cjsModule
91-
const cjsKeys = cjsType === 'object' && cjsModule !== null ? Object.keys(cjsModule) : []
89+
const cjsKeys =
90+
cjsType === 'object' && cjsModule !== null ? Object.keys(cjsModule) : []
9291

9392
// Check for problematic CJS patterns
9493
if (cjsType === 'object' && cjsModule !== null) {
@@ -101,7 +100,9 @@ async function checkModuleExports(filePath) {
101100

102101
// Empty object is suspicious
103102
if (cjsKeys.length === 0) {
104-
issues.push('CJS: Module exports empty object - may indicate bundling issue')
103+
issues.push(
104+
'CJS: Module exports empty object - may indicate bundling issue',
105+
)
105106
}
106107

107108
// Check if .default shadows the main export
@@ -112,7 +113,9 @@ async function checkModuleExports(filePath) {
112113
// If there are other exports, this might be intentional (like @inquirer modules)
113114
// We'll check ESM compatibility below
114115
if (nonDefaultKeys.length === 0) {
115-
issues.push('CJS: Module has .default but no other exports - may be wrapped')
116+
issues.push(
117+
'CJS: Module has .default but no other exports - may be wrapped',
118+
)
116119
}
117120
}
118121
}
@@ -150,7 +153,9 @@ async function checkModuleExports(filePath) {
150153
if ('default' in cjsModule && cjsModule.default !== cjsModule) {
151154
// ESM should have the default export
152155
if (esmDefault === undefined) {
153-
issues.push('ESM: Missing default export, but CJS has .default property')
156+
issues.push(
157+
'ESM: Missing default export, but CJS has .default property',
158+
)
154159
}
155160

156161
// Named exports should be accessible in ESM's default import
@@ -170,7 +175,9 @@ async function checkModuleExports(filePath) {
170175
const esmDefaultKeys = Object.keys(esmDefault)
171176
for (const key of cjsKeys) {
172177
if (!esmDefaultKeys.includes(key)) {
173-
issues.push(`ESM: Named export '${key}' missing from default object`)
178+
issues.push(
179+
`ESM: Named export '${key}' missing from default object`,
180+
)
174181
}
175182
}
176183
}
@@ -278,7 +285,9 @@ async function main() {
278285
// Summary statistics
279286
const totalCjsKeys = successes.reduce((sum, r) => sum + r.cjsKeys, 0)
280287
const modulesWithDefault = successes.filter(r => r.hasEsmDefault).length
281-
const functionExports = successes.filter(r => r.cjsType === 'function').length
288+
const functionExports = successes.filter(
289+
r => r.cjsType === 'function',
290+
).length
282291
const objectExports = successes.filter(r => r.cjsType === 'object').length
283292

284293
logger.success(

scripts/validate/external-exports.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313
const externalDir = path.resolve(__dirname, '..', '..', 'dist', 'external')
1414
const require = createRequire(import.meta.url)
1515

16-
// Normalize path for cross-platform (converts backslashes to forward slashes)
17-
const normalizePath = p => p.split(path.sep).join('/')
18-
1916
// Import CommonJS modules using require
2017
const { isQuiet } = require('#socketsecurity/lib/argv/flags')
2118
const { getDefaultLogger } = require('#socketsecurity/lib/logger')
19+
const { normalizePath } = require('#socketsecurity/lib/path')
2220
const { pluralize } = require('#socketsecurity/lib/words')
2321

2422
const logger = getDefaultLogger()

0 commit comments

Comments
 (0)