Skip to content

Commit

Permalink
chore: Change cli.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
redpeacock78 committed Nov 20, 2021
1 parent ecf739d commit a3a0c5a
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { readAllSync } from "https://deno.land/[email protected]/streams/conversion.ts";
import { getStdinBufferSync } from "https://deno.land/x/[email protected]/mod.ts";
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
import { base85encode, base85decode } from "./mod.ts";
import { version } from "./version.ts";
// deno-lint-ignore-file camelcase no-dupe-else-if
import {
base85decode,
base85encode,
Command,
getStdinBufferSync,
readAllSync,
version,
} from "./deps.ts";

try {
let file: Uint8Array;
Expand Down Expand Up @@ -35,7 +39,9 @@ try {
}
}
} else if (Deno.args[0] !== undefined) {
args_judge = Deno.args[0].match(/-d|--decode|-h|--help|-V|--version/) ? true : false;
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]));
Expand All @@ -56,18 +62,35 @@ try {
}
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.")
.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(",")]);
.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 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 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);
typeof result === "string"
? console.log(result)
: Deno.stdout.writeSync(result);
};
if (filled!) {
convert();
Expand Down

0 comments on commit a3a0c5a

Please sign in to comment.