-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.ts
More file actions
68 lines (56 loc) · 1.61 KB
/
Copy pathbin.ts
File metadata and controls
68 lines (56 loc) · 1.61 KB
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
import * as mod from "https://deno.land/std@0.218.2/cli/parse_args.ts";
import * as path from "https://deno.land/std@0.201.0/path/mod.ts";
import Marshal from "./mod.ts";
const args = mod.parseArgs(Deno.args);
if (!args._[0] || typeof args._[0] !== "string") {
console.error("No command provided");
Deno.exit(1);
}
const baseDir = Deno.cwd();
switch (args._[0]) {
case "encode":
await encode();
break;
case "decode":
decode();
break;
default:
console.error("Invalid command: ", args[0]);
Deno.exit(1);
}
async function encode() {
if (typeof args._[1] !== "string") {
console.error("No input file provided");
Deno.exit(1);
}
const input = path.join(baseDir, args._[1]);
const output = args.out
? path.join(baseDir, args.out)
: path.parse(input).name + ".bin";
try {
const content = await Deno.readTextFile(input);
const [encoded] = Marshal.encodeWithClasses(JSON.parse(content));
await Deno.writeFile(output, encoded);
} catch (error) {
console.error(`Failed to marhal file: ${error}`);
Deno.exit(1);
}
}
async function decode() {
if (typeof args._[1] !== "string") {
console.error("No input file provided");
Deno.exit(1);
}
const input = path.join(baseDir, args._[1]);
const output = args.out
? path.join(baseDir, args.out)
: path.parse(input).name + ".json";
try {
const content = await Deno.readFile(input);
const decoded = Marshal.decode(content);
await Deno.writeTextFile(output, JSON.stringify(decoded));
} catch (error) {
console.error(`Failed to unmarshal file: ${error}`);
Deno.exit(1);
}
}