UNPKG

tsdown-jsr-exports-lint

Version:
45 lines (42 loc) 1.6 kB
import fs from "node:fs"; import path from "node:path"; import pc from "picocolors"; //#region src/validator.ts function validateJsrExports(entries, jsr) { const result = {}; for (const [key, value] of Object.entries(entries)) { const exportKey = key === "index" ? "." : `./${key}`; if (jsr[exportKey]) result[exportKey] = jsr[exportKey] === `./${value}` ? true : `jsr.exports["${exportKey}"] is ./${value}, but it does not exist in jsr file.`; else result[exportKey] = `jsr.exports["${exportKey}"] does not define.`; } return result; } //#endregion //#region src/index.ts function jsrExportsLint(jsrPath) { const targetJsrPath = jsrPath ?? path.resolve(process.cwd(), "jsr.json"); if (!fs.existsSync(targetJsrPath)) throw new Error(`jsr manifest file not found at ${targetJsrPath}`); return async (config) => { const jsr = await import(targetJsrPath, { with: { type: "json" } }).then((m) => m.default || m); if (!jsr.exports) { error(pc.dim("No jsr.exports found in jsr.json")); process.exit(1); } const result = validateJsrExports(config.entry, jsr.exports); const messages = []; for (const [_, value] of Object.entries(result)) if (typeof value === "string") messages.push(value); if (messages.length > 0) { for (const msg of messages) { console.log(""); error(pc.whiteBright(msg)); console.log(""); } process.exit(1); } else if (!config.silent) console.log(pc.green("✔"), "No JSR exports issues found"); }; } function error(message) { console.error(pc.bgRed(pc.black(" ERROR ")), message); } //#endregion export { error, jsrExportsLint };