-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
107 lines (106 loc) · 3.31 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// deno-lint-ignore-file camelcase no-dupe-else-if
import {
base85decode,
base85encode,
Command,
getStdinBufferSync,
readAllSync,
version,
} from "./deps.ts";
try {
let file: Uint8Array;
let stdin: Uint8Array;
let filled: boolean;
let args_judge: boolean;
const isatty: boolean = Deno.isatty(Deno.stdin.rid);
if (!isatty) {
const input = getStdinBufferSync({ exitOnEnter: false });
if (input.length !== 0) {
stdin = input;
filled = stdin.length! !== 0 ? true : false;
} else if (!Deno.args[0].match(/-d|--decode|-h|--help|-V|--version/)) {
try {
args_judge = true;
stdin = readAllSync(Deno.openSync(Deno.args[0]));
Deno.close(Deno.openSync(Deno.args[0]).rid);
filled = stdin.length !== 0 ? true : false;
} catch {
throw new Error("base85: No such file or directory");
}
} else if (Deno.args[1]) {
try {
args_judge = false;
stdin = readAllSync(Deno.openSync(Deno.args[1]));
Deno.close(Deno.openSync(Deno.args[1]).rid);
filled = stdin.length !== 0 ? true : false;
} catch {
throw new Error("base85: No such file or directory");
}
}
} else if (Deno.args[0] !== undefined) {
args_judge = Deno.args[0].match(/-d|--decode|-h|--help|-V|--version/)
? true
: false;
if (!args_judge) {
try {
file = readAllSync(Deno.openSync(Deno.args[0]));
Deno.close(Deno.openSync(Deno.args[0]).rid);
filled = file.length !== 0 ? true : false;
} catch {
throw new Error("base85: No such file or directory");
}
} else if (Deno.args[1]) {
try {
file = readAllSync(Deno.openSync(Deno.args[1]));
Deno.close(Deno.openSync(Deno.args[1]).rid);
filled = file.length !== 0 ? true : false;
} catch {
throw new Error("base85: No such file or directory");
}
}
}
const { options, args } = await new Command()
.name("base85")
.description(
"Base85 (Ascii85 with Adobe Escape Sequence) encode or decode FILE, or standard input, to standard output.",
)
.version(version())
.option("-d, --decode", "Decode data")
.arguments("<option>")
.parse(
!isatty
? (args_judge!
? [[...stdin!].join(",")]
: [Deno.args[0], [...stdin!].join(",")].filter(Boolean))
: !file!
? Deno.args
: !args_judge!
? [[...file!].join(",")]
: [Deno.args[0], [...file!].join(",")],
);
const { decode } = options;
const runner: ((str: string) => Uint8Array) | ((byte: Uint8Array) => string) =
decode ? base85decode : base85encode;
const convert: () => void = (): void => {
const args2Unit8Array: Uint8Array = new Uint8Array(args[0].split(","));
const input: Uint8Array | string = decode
? new TextDecoder().decode(args2Unit8Array).replace(/\n$/g, "")
: args2Unit8Array;
const result: string | Uint8Array = runner(input! as Uint8Array & string);
typeof result === "string"
? console.log(result)
: Deno.stdout.writeSync(result);
};
if (filled!) {
convert();
} else if (!isatty && filled!) {
convert();
} else if (file! && filled!) {
convert();
} else {
throw new Error("base85: No such file or directory");
}
} catch (e) {
console.error(e.message);
Deno.exit(1);
}