Skip to content

Commit

Permalink
feat: validate css option
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp committed Aug 15, 2024
1 parent 289f395 commit fdacf68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 20 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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.")
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit fdacf68

Please sign in to comment.