UNPKG

jsr-exports-lint

Version:
61 lines (58 loc) 2.37 kB
import fs from "node:fs"; import path from "node:path"; import pc from "picocolors"; //#region src/validator.ts /** * @author kazuya kawaguchi (a.k.a. @kazupon) * @license MIT */ function validateJsrExports(entries, jsr, cwd = ".") { const result = {}; for (const [key, value] of Object.entries(entries)) { const isBarrelFile = key.endsWith("/index"); const shortenedKey = isBarrelFile ? key.slice(0, -6) : key; const shortenedExportKey = shortenedKey === "index" ? "." : `./${shortenedKey}`; const fullExportKey = key === "index" ? "." : `./${key}`; const exportKey = isBarrelFile && !jsr[shortenedExportKey] && jsr[fullExportKey] ? fullExportKey : shortenedExportKey; const resolvedValue = path.isAbsolute(value) ? value : path.resolve(cwd, value); if (jsr[exportKey]) result[exportKey] = path.resolve(cwd, jsr[exportKey]) === resolvedValue ? true : `jsr.exports["${exportKey}"] is ${jsr[exportKey]}, but it's miss-matched in your tsdown entry.`; else result[exportKey] = `jsr.exports["${exportKey}"] does not define.`; } return result; } //#endregion //#region src/lint.ts /** * @author kazuya kawaguchi (a.k.a. @kazupon) * @license MIT */ /** * Lint the `exports` field of the JSR manifest file. * @param options An lint options, about details, see {@link LintOptions} */ async function lint({ jsrPath = path.resolve(process.cwd(), "jsr.json"), entries = {}, cwd = ".", silent = false } = {}) { if (!fs.existsSync(jsrPath)) throw new Error(`jsr manifest file not found at ${jsrPath}`); const jsr = await import(jsrPath, { 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 startTime = performance.now(); const result = validateJsrExports(entries, jsr.exports, cwd); const endTime = performance.now(); 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 (!silent) console.log(pc.green("✔"), "No JSR exports issues found", pc.dim(`(${(endTime - startTime).toFixed(2)}ms)`)); } function error(message) { console.error(pc.bgRed(pc.black(" ERROR ")), message); } //#endregion export { lint as t };