From ae4895fff35d92950c723efa2d31eb7f6ad8a3de Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 29 Nov 2022 19:14:48 +0900 Subject: [PATCH] add progrossbar --- deno.lock | 15 ++++++++ validator.ts | 101 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 87 insertions(+), 29 deletions(-) diff --git a/deno.lock b/deno.lock index 93fd286..c87ceb6 100644 --- a/deno.lock +++ b/deno.lock @@ -74,6 +74,14 @@ "https://deno.land/std@0.161.0/path/posix.ts": "6b63de7097e68c8663c84ccedc0fd977656eb134432d818ecd3a4e122638ac24", "https://deno.land/std@0.161.0/path/separator.ts": "fe1816cb765a8068afb3e8f13ad272351c85cbc739af56dacfc7d93d710fe0f9", "https://deno.land/std@0.161.0/path/win32.ts": "ee8826dce087d31c5c81cd414714e677eb68febc40308de87a2ce4b40e10fb8d", + "https://deno.land/std@0.162.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", + "https://deno.land/std@0.162.0/bytes/bytes_list.ts": "aba5e2369e77d426b10af1de0dcc4531acecec27f9b9056f4f7bfbf8ac147ab4", + "https://deno.land/std@0.162.0/bytes/equals.ts": "3c3558c3ae85526f84510aa2b48ab2ad7bdd899e2e0f5b7a8ffc85acb3a6043a", + "https://deno.land/std@0.162.0/bytes/mod.ts": "b2e342fd3669176a27a4e15061e9d588b89c1aaf5008ab71766e23669565d179", + "https://deno.land/std@0.162.0/fmt/colors.ts": "9e36a716611dcd2e4865adea9c4bec916b5c60caad4cdcdc630d4974e6bb8bd4", + "https://deno.land/std@0.162.0/io/buffer.ts": "fae02290f52301c4e0188670e730cd902f9307fb732d79c4aa14ebdc82497289", + "https://deno.land/std@0.162.0/io/types.d.ts": "107e1e64834c5ba917c783f446b407d33432c5d612c4b3430df64fc2b4ecf091", + "https://deno.land/std@0.162.0/streams/conversion.ts": "555c6c249f3acf85655f2d0af52d1cb3168e40b1c1fa26beefea501b333abe28", "https://deno.land/std@0.166.0/_util/asserts.ts": "d0844e9b62510f89ce1f9878b046f6a57bf88f208a10304aab50efcb48365272", "https://deno.land/std@0.166.0/_util/os.ts": "8a33345f74990e627b9dfe2de9b040004b08ea5146c7c9e8fe9a29070d193934", "https://deno.land/std@0.166.0/bytes/bytes_list.ts": "aba5e2369e77d426b10af1de0dcc4531acecec27f9b9056f4f7bfbf8ac147ab4", @@ -276,6 +284,13 @@ "https://deno.land/x/fresh@1.1.2/src/server/types.ts": "dde992ab4ee635df71a7fc96fe4cd85943c1a9286ea8eb586563d5f5ca154955", "https://deno.land/x/importmap@0.2.1/_util.ts": "ada9a9618b537e6c0316c048a898352396c882b9f2de38aba18fd3f2950ede89", "https://deno.land/x/importmap@0.2.1/mod.ts": "ae3d1cd7eabd18c01a4960d57db471126b020f23b37ef14e1359bbb949227ade", + "https://deno.land/x/progress@v1.3.4/deps.ts": "83050e627263931d853ba28b7c15c80bf4be912bea7e0d3d13da2bc0aaf7889d", + "https://deno.land/x/progress@v1.3.4/mod.ts": "ca65cf56c63d48ac4806f62a6ee5e5889dc19b8bd9a3be2bfeee6c8c4a483786", + "https://deno.land/x/progress@v1.3.4/multi.ts": "755f05ce3d1f859142c6a1e67972f8765ee29eac7bfdec8126008c312addbeef", + "https://deno.land/x/progress@v1.3.4/time.ts": "e7ab1975dadf519c8881a333cbc661d6048da2b1c2f4a7d3e995065d53340144", + "https://deno.land/x/progressbar@v0.2.0/progressbar.ts": "d571c5505c2afd792d729f2467a017dd3a108c82ecb4421280fa86eec6d25a05", + "https://deno.land/x/progressbar@v0.2.0/styles.ts": "2ba8cfb41688c0ca497ed740bda342f55e629043397eef43180665aa34ef2f01", + "https://deno.land/x/progressbar@v0.2.0/widgets.ts": "73aa6d06b6a89a4dfd8d15e97ca634bc1e82c7993cd95ba56459f900d2e02838", "https://deno.land/x/rutt@0.0.13/mod.ts": "af981cfb95131152bf50fc9140fc00cb3efb6563df2eded1d408361d8577df20", "https://deno.land/x/ts_morph@16.0.0/common/DenoRuntime.ts": "537800e840d0994f9055164e11bf33eadf96419246af0d3c453793c3ae67bdb3", "https://deno.land/x/ts_morph@16.0.0/common/mod.ts": "01985d2ee7da8d1caee318a9d07664774fbee4e31602bc2bb6bb62c3489555ed", diff --git a/validator.ts b/validator.ts index 297c753..3bfc8a2 100644 --- a/validator.ts +++ b/validator.ts @@ -1,6 +1,7 @@ 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"; +import ProgressBar from "https://deno.land/x/progress@v1.3.4/mod.ts"; function checkAndPrint(doc: Doc, another: Doc): [boolean, string[]] { const diffs = []; @@ -44,6 +45,40 @@ function checkAndPrint(doc: Doc, another: Doc): [boolean, string[]] { return [diffs.length === 0, diffs]; } +async function validateDoc(path: string, parser: DocParser, fix?: boolean, log = console.log): Promise { + try { + const content = await Deno.readTextFile(path); + const doc = parser.parseAlternative(content); + if (fix) { + await writeDoc(path, doc); + } + else { + try { + parser.parse(content); + const another = parser.parse(content); + const [isSame, errors] = checkAndPrint(doc, another); + if (!isSame) { + log(`File ${path} is not valid.`); + for (const error of errors) { + log("\t",error); + } + } + } + catch (e) { + if ( e instanceof Error) { + log(`File ${path} is not valid. error: ${e.name}`); + } + else { + throw e; + } + } + } + } catch (error) { + log(`File ${path} is not valid. error: ${error.name}`); + throw error; + } +} + if(import.meta.main){ const app = new Command() .name("validator") @@ -51,40 +86,48 @@ if(import.meta.main){ .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.") + .option("-p, --progress", "Show progress.") .arguments("[pathes...:string]") .action(async (options, ...pathes) => { - const { verbose, fix } = options; + const { verbose, fix, progress } = options; const parser = new DocParser(); - for (const path of pathes) { - for await (const dir of expandGlob(path)) { - if (verbose) { - console.log(`Processing ${dir.path}`); + if(pathes.length === 0){ + console.log("No pathes specified. Use --help to see the usage."); + Deno.exit(1); + } + if (verbose) { + console.log("Verbose mode enabled."); + } + if (progress) { + console.log("Progress mode enabled."); + const targets = (await Promise.all(pathes.map(async (path) => { + const paths = []; + for await (const entry of expandGlob(path)) { + paths.push(entry.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; - } + return paths; + }))).flat(); + + const bar = new ProgressBar({ + total: targets.length, + title: "Validating", + }); + let count = 0; + for (const path of targets) { + await validateDoc(path, parser, fix, verbose ? (...args)=>{ + bar.console(args.join(" ")); + } : () => { }); + bar.render(++count); + } + bar.end(); + } + else { + for (const path of pathes) { + for await (const dir of expandGlob(path)) { + if (verbose) { + console.log(`Processing ${dir.path}`); } + validateDoc(dir.path, parser, fix); } } }