-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
89 lines (73 loc) · 2.65 KB
/
Copy pathmain.ts
File metadata and controls
89 lines (73 loc) · 2.65 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @module cli/main
*
* CLI entry logic — parses argv and auto-routes to compress or decompress.
*/
import { readSplitInput } from "../split/parts.js"
import { printVersion } from "./analytics.js"
import { type Args, parseArgs, resolveEncodingOptional, resolveInputArgs } from "./args.js"
import { runCompress } from "./commands/compress.js"
import { runDecompress } from "./commands/decompress.js"
import { detectCompressedPayload } from "./detect.js"
import { printUsage } from "./usage.js"
function wantsHelp(argv: string[]): boolean {
return argv.length === 0 || argv.includes("-h") || argv.includes("--help")
}
function wantsVersion(argv: string[]): boolean {
return argv.includes("-V") || argv.includes("--version")
}
/** Strip an optional legacy leading compress/decompress command. */
function normalizeArgv(argv: string[]): { argv: string[]; forcedMode?: Args["mode"] } {
const [first, ...rest] = argv
if (first === "compress" || first === "c") return { argv: rest, forcedMode: "compress" }
if (first === "decompress" || first === "d") return { argv: rest, forcedMode: "decompress" }
return { argv }
}
function readEncodedInput(args: Args): string {
if (args.file) return readSplitInput(args.file).content
if (args.text !== undefined) return args.text
throw new Error("No input provided. Pass a path, or use -t <text>.")
}
function resolveCliMode(args: Args): "compress" | "decompress" {
if (args.mode === "compress") return "compress"
if (args.mode === "decompress") return "decompress"
if (args.dir) return "compress"
const encoded = readEncodedInput(args)
const detection = detectCompressedPayload(encoded, resolveEncodingOptional(args), args.password)
if (detection === "password-required") {
throw new Error("This payload is password-protected. Pass -p/--password to decompress.")
}
if (detection === "compressed") return "decompress"
return "compress"
}
/** Main CLI dispatcher. */
export function main() {
let argv = process.argv.slice(2)
if (wantsHelp(argv)) {
printUsage()
process.exit(argv.length === 0 ? 1 : 0)
}
if (wantsVersion(argv)) {
printVersion()
process.exit(0)
}
const legacy = normalizeArgv(argv)
argv = legacy.argv
const args = parseArgs(argv)
if (legacy.forcedMode && !args.mode) {
args.mode = legacy.forcedMode
}
const run = async () => {
resolveInputArgs(args, args.mode === "decompress" ? "decompress" : "compress")
const mode = resolveCliMode(args)
if (mode === "decompress") {
runDecompress(args)
return
}
await runCompress(args)
}
run().catch((err) => {
console.error(`Error: ${(err as Error).message}`)
process.exit(1)
})
}