import { Command } from "cliffy"; import { DocParser, readDoc, writeDoc, Doc } from "./doc_load/load.ts"; import { expandGlob } from "https://deno.land/std@0.166.0/fs/mod.ts"; function checkAndPrint(doc: Doc, another: Doc): [boolean, string[]] { const diffs = []; if (doc.name !== another.name) { diffs.push(`name: ${doc.name} => ${another.name}`); } if (doc.author !== another.author) { diffs.push(`author: ${doc.author} => ${another.author}`); } if (doc.star !== another.star) { diffs.push(`star: ${doc.star} => ${another.star}`); } if (doc.fork !== another.fork) { diffs.push(`fork: ${doc.fork} => ${another.fork}`); } if (doc.desc !== another.desc) { diffs.push(`desc: "${doc.desc}" => ${another.desc}`); } if (doc.url !== another.url) { diffs.push(`url: ${doc.url} => ${another.url}`); } if (doc.tags.length !== another.tags.length) { diffs.push(`tags: ${doc.tags} => ${another.tags}`); } else { doc.tags = doc.tags.sort(); another.tags = another.tags.sort(); for (let i = 0; i < doc.tags.length; i++) { if (doc.tags[i] !== another.tags[i]) { diffs.push(`tags: ${doc.tags} => ${another.tags}`); break; } } } if (doc.readme !== another.readme) { diffs.push(`readme: ${doc.readme} => ${another.readme}`); } if (doc.from_url !== another.from_url) { diffs.push(`from_url: ${doc.from_url} => ${another.from_url}`); } return [diffs.length === 0, diffs]; } if(import.meta.main){ const app = new Command() .name("validator") .version("0.1.0") .description("Validate and fix the doc files in the sample directory. glob is supported.") .option("-v, --verbose", "Enable verbose mode.") .option("-f, --fix", "Fix the doc files.") .arguments("[pathes...:string]") .action(async (options, ...pathes) => { const { verbose, fix } = options; const parser = new DocParser(); for (const path of pathes) { for await (const dir of expandGlob(path)) { if (verbose) { console.log(`Processing ${dir.path}`); } const content = await Deno.readTextFile(dir.path); const doc = parser.parseAlternative(content); if (fix) { await writeDoc(dir.path, doc); } else { try { parser.parse(content); const another = parser.parse(content); const [isSame, errors] = checkAndPrint(doc, another); if (!isSame) { console.log(`File ${dir.path} is not valid.`); for (const error of errors) { console.log("\t",error); } } } catch (e) { if ( e instanceof Error) { console.log(`File ${dir.path} is not valid. error: ${e.name}`); } else { throw e; } } } } } }); await app.parse(Deno.args); }