-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
74 lines (64 loc) · 2.01 KB
/
cli.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
70
71
72
73
74
import { parseArgs } from "@std/cli/parse-args";
import * as colors from "@std/fmt/colors";
import { readAll } from "@std/io";
import scrapboxToReView from "./mod.ts";
const options = parseArgs(Deno.args, {
string: ["_"],
boolean: ["help", "version"],
alias: { "h": "help", "v": "version" },
});
const usage = `sb2re - Convert Scrapbox text to Re:VIEW
${colors.bold("Usage")}
cat input | sb2re [options] > out.re
sb2re input [options] > out.re
sb2re input out.re [options]
${colors.bold("Options")}
-h, --help Show this help.
-v, --version Print version info
--base-heading-level Specify the largest heading level (Default: 3. This means \`[*** ]\` corresponds to Re:VIEW's \`==\`)`;
if (options.help) {
console.log(usage);
Deno.exit(0);
}
if (options.version) {
const { default: denoConfig } = await import("./deno.json", {
with: { type: "json" },
});
console.log(denoConfig.version);
Deno.exit(0);
}
const baseHeadingLevel = options["base-heading-level"] as unknown;
if (baseHeadingLevel !== undefined && typeof baseHeadingLevel !== "number") {
console.error("Error: base-heading-level option should be number");
Deno.exit(1);
}
const src = await readSource();
const result = scrapboxToReView(src, {
baseHeadingLevel,
logger: {
error(message: string) {
console.error(colors.bold(colors.red(message)));
},
warn(message: string) {
console.warn(message);
},
},
});
if (options._[1] === "-" || options._[1] === undefined) {
console.log(result.replace(/\n$/, ""));
} else {
await Deno.writeTextFile(options._[1].toString(), result);
}
async function readSource() {
if (options._.length === 0 || options._[0] === "-") {
// read from stdin
if (Deno.stdin.isTerminal()) {
console.log(colors.bold(colors.red("Specify input file.")));
Deno.exit(1);
}
const buf = await readAll(Deno.stdin);
return new TextDecoder().decode(buf);
} else {
return await Deno.readTextFile(options._[0].toString());
}
}