This introduces CI-enforced checks that compare index.js pearcord-* requires against declared dependencies and smoke-resolve imported modules, preventing undeclared module regressions in local file-link and attached runtime environments. Co-authored-by: Cursor <[email protected]>
70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict'
|
|
|
|
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
|
|
const ROOT = path.resolve(__dirname, '..')
|
|
const PACKAGE_JSON = path.join(ROOT, 'package.json')
|
|
const ENTRYPOINT = path.join(ROOT, 'index.js')
|
|
const SHOULD_RESOLVE = process.argv.includes('--resolve')
|
|
|
|
function readJson (filePath) {
|
|
return JSON.parse(fs.readFileSync(filePath, 'utf8'))
|
|
}
|
|
|
|
function extractPearcordModuleImports (source) {
|
|
const imports = new Set()
|
|
const regex = /require\('([^']+)'\)/g
|
|
let match = regex.exec(source)
|
|
while (match) {
|
|
const raw = String(match[1] || '')
|
|
if (raw.startsWith('pearcord-')) {
|
|
imports.add(raw.split('/')[0])
|
|
}
|
|
match = regex.exec(source)
|
|
}
|
|
return [...imports].sort()
|
|
}
|
|
|
|
function run () {
|
|
const pkg = readJson(PACKAGE_JSON)
|
|
const source = fs.readFileSync(ENTRYPOINT, 'utf8')
|
|
const imported = extractPearcordModuleImports(source)
|
|
const declared = new Set(Object.keys(pkg.dependencies || {}).filter((name) => name.startsWith('pearcord-')))
|
|
|
|
const missingDeps = imported.filter((name) => !declared.has(name))
|
|
const unusedDeps = [...declared].filter((name) => !imported.includes(name)).sort()
|
|
|
|
const report = {
|
|
ok: missingDeps.length === 0,
|
|
importedCount: imported.length,
|
|
declaredCount: declared.size,
|
|
missingDependencies: missingDeps,
|
|
unusedDependencies: unusedDeps
|
|
}
|
|
|
|
if (SHOULD_RESOLVE) {
|
|
const unresolved = []
|
|
for (const name of imported) {
|
|
try {
|
|
require.resolve(name)
|
|
} catch (err) {
|
|
unresolved.push({ module: name, error: err && err.message ? err.message : String(err) })
|
|
}
|
|
}
|
|
report.resolveOk = unresolved.length === 0
|
|
report.unresolved = unresolved
|
|
if (!report.resolveOk) report.ok = false
|
|
}
|
|
|
|
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`)
|
|
|
|
if (!report.ok) {
|
|
process.stderr.write('pearcord-platform dependency check failed.\n')
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
run()
|