From fdacf68269e4e28346dd1298ac5783641d5fa41a Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:57:30 +0900 Subject: [PATCH] feat: validate css option --- deno.json | 1 + src/cli.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/deno.json b/deno.json index 4ead951..e8f9266 100644 --- a/deno.json +++ b/deno.json @@ -10,6 +10,7 @@ "dev": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts -- README.md", "dev:help": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --help", "dev:css": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --css=src/testdata/bluecode.css -- README.md", + "dev:invalidcss": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --css=src/testdata/redcode.css -- README.md", "dev:watch": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --watch -- README.md" }, "imports": { diff --git a/src/cli.ts b/src/cli.ts index 801e1af..90ceec9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -13,12 +13,26 @@ import { Spinner } from "@std/cli/spinner"; import { parseArgs } from "@std/cli/parse-args"; -import { bgBlue, gray, green, underline, yellow } from "@std/fmt/colors"; +import { bgBlue, gray, green, red, underline, yellow } from "@std/fmt/colors"; import { exists } from "@std/fs/exists"; import { mdToPdf } from "./md-to-pdf.ts"; import { getFilename } from "./utils/filename.ts"; import type { MdToPdfOptions } from "./types.ts"; +async function validateArgs( + args: { + css?: string; + }, +): Promise { + if (typeof args.css === "string") { + if (!(await exists(args.css, { isFile: true, isReadable: true }))) { + console.error(`${red("error")}: Set CSS file is not found: ${args.css}`); + return false; + } + } + return true; +} + function printHelp(): void { const help = `md2pdf: ${ green("A simple CLI tool for converting markdown to PDF.") @@ -50,12 +64,16 @@ async function generatePdfFromMarkdown(path: string, options?: MdToPdfOptions) { ); } +// Inline + const args = await parseArgs(Deno.args, { boolean: ["w", "watch", "h", "help"], string: ["css"], }); -if (args.h || args.help) { +if (!(await validateArgs(args))) { + Deno.exit(1); +} else if (args.h || args.help) { printHelp(); Deno.exit(0); }