import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { diagnoseTemplate } from "./lib/template-doctor.mjs"; try { const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const args = process.argv.slice(2); const options = {}; for (let i = 0; i < args.length; i += 2) { if (!["--template", "--output"].includes(args[i]) || !args[i + 1]) throw new Error("Usage: template-doctor [--template key] [--output path]"); options[args[i]] = args[i + 1]; } const reports = []; for (const dir of fs.readdirSync(path.join(root, "packages")).sort()) { const manifestPath = path.join(root, "packages", dir, "template.json"); if (!dir.startsWith("template-") || !fs.existsSync(manifestPath)) continue; const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); if (options["--template"] && manifest.packageKey !== options["--template"]) continue; const renderer = path.join(root, "packages", dir, "taro/index.tsx"); if (!fs.existsSync(renderer)) throw new Error("Missing renderer: " + renderer); reports.push( diagnoseTemplate(manifest, fs.readFileSync(renderer, "utf8"), path.relative(root, renderer)), ); } if (!reports.length) throw new Error("No matching templates found"); const report = { schemaVersion: 1, completionStatus: "unverified", templates: reports }; const output = JSON.stringify(report, null, 2) + "\n"; if (options["--output"]) { fs.mkdirSync(path.dirname(path.resolve(options["--output"])), { recursive: true }); fs.writeFileSync(options["--output"], output); } process.stdout.write(output); process.exitCode = reports.some((r) => r.staticStatus === "fail") ? 1 : 0; } catch (error) { console.error(error.message); process.exitCode = 2; }