|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Wrapper for @umbrelladocs/linkspector |
| 4 | + * Fixes CommonJS import of @umbrelladocs/rdformat-validator for Node 20+. |
| 5 | + * Works when dependencies are installed in the caller repository. |
| 6 | + */ |
| 7 | + |
| 8 | +import { fileURLToPath } from "url"; |
| 9 | +import { dirname, resolve } from "path"; |
| 10 | +import { createRequire } from "module"; |
| 11 | + |
| 12 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 13 | + |
| 14 | +async function importValidator() { |
| 15 | + try { |
| 16 | + // Try local import first |
| 17 | + return await import("@umbrelladocs/rdformat-validator"); |
| 18 | + } catch { |
| 19 | + console.warn("⚠️ validator not found locally; resolving from caller repo..."); |
| 20 | + try { |
| 21 | + // Dynamically resolve from the parent repository |
| 22 | + const require = createRequire(import.meta.url); |
| 23 | + const path = require.resolve("@umbrelladocs/rdformat-validator", { |
| 24 | + paths: [resolve(process.cwd(), "node_modules")], |
| 25 | + }); |
| 26 | + return await import(`file://${path}`); |
| 27 | + } catch (err) { |
| 28 | + console.error("❌ Could not resolve @umbrelladocs/rdformat-validator", err); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function main() { |
| 35 | + // Import linkspector CLI |
| 36 | + let linkspector; |
| 37 | + try { |
| 38 | + linkspector = await import("@umbrelladocs/linkspector/lib/cli.js"); |
| 39 | + } catch (err) { |
| 40 | + console.error("❌ Could not import @umbrelladocs/linkspector CLI:", err); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + // Import validator safely |
| 45 | + const validatorPkg = await importValidator(); |
| 46 | + const { validateAndFix } = validatorPkg; |
| 47 | + globalThis.validateAndFix = validateAndFix; |
| 48 | + |
| 49 | + // Run linkspector’s CLI |
| 50 | + const cli = linkspector.default || linkspector.run; |
| 51 | + if (typeof cli === "function") { |
| 52 | + await cli(); |
| 53 | + } else { |
| 54 | + console.error("⚠️ No valid entry point found in linkspector."); |
| 55 | + process.exit(1); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +main().catch((err) => { |
| 60 | + console.error("❌ linkspector wrapper failed:", err); |
| 61 | + process.exit(1); |
| 62 | +}); |
0 commit comments