forked from bobheadxi/readable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readable.ts
69 lines (61 loc) · 1.71 KB
/
readable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Document, DOMParser } from "deno_dom/deno-dom-wasm.ts";
import { cac } from "cac/mod.ts";
import { processArgs } from "cac/deno/deno.ts";
import { getLogger } from "log/mod.ts";
import { READABLE_VERSION } from "./version.ts";
import check from "./cmd/check.ts";
import fmt, { FmtOptions } from "./cmd/fmt.ts";
import setupLogger from "./lib/setupLogger.ts";
/**
* Workaround for document reference error during parsing.
*/
declare module globalThis {
var document: any;
}
const parser = new DOMParser();
const doc = parser.parseFromString("<html></html>", "text/html");
globalThis.document = doc;
const cli = cac("readable")
.version(READABLE_VERSION)
.help()
.option("--log-level <level>", "Set a log level", { default: "INFO" });
/**
* readable fmt
*/
cli
.command("fmt [...globs]", "Format Markdown")
.option("--to-stdout", "Output results to stdout instead of modifying files")
.action(async (globs: string[], opts: FmtOptions) => {
const log = getLogger("fmt");
await fmt(log, globs, opts);
});
/**
* readable check
*/
cli
.command("check [...globs]", "Check Markdown formatting")
.action(async (globs: string[]) => {
const log = getLogger("check");
await check(log, globs);
});
/**
* Run input commands if run as a script.
*
* See https://deno.land/manual@master/tools/script_installer
*/
if (import.meta.main) {
// Show help if no args are provided
if (Deno.args.length == 0) {
cli.outputHelp();
Deno.exit(1);
}
try {
const parsed = cli.parse(processArgs, { run: false });
await setupLogger(parsed.options.logLevel);
await cli.runMatchedCommand();
} catch (err) {
getLogger().error(`${err.message}`);
Deno.exit(1);
}
}
export default cli;