-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.ts
More file actions
88 lines (80 loc) · 2.99 KB
/
Copy pathcompress.ts
File metadata and controls
88 lines (80 loc) · 2.99 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
/**
* @module cli/commands/compress
*
* `text-compress` command — Brotli-compress input and write encoded output.
*/
import { compress } from "../../api/text.js"
import { assertDirectory } from "../../fs/paths.js"
import { compressFolderToPath } from "../../streaming/folder.js"
import { formatBytes, formatCount, printRunSummary, splitAnalytics } from "../analytics.js"
import { type Args, readInput, resolveEncoding } from "../args.js"
import { writeCompressedOutput } from "../output.js"
import { resolveOutputPath } from "../paths.js"
/** Execute the compress subcommand. */
export async function runCompress(args: Args): Promise<void> {
const encoding = resolveEncoding(args)
if (args.dir) {
assertDirectory(args.dir)
const outputPath = resolveOutputPath(args, "compressed.txt", ".txt")
const start = process.hrtime.bigint()
const {
fileCount,
dirCount,
originalBytes,
archiveBytes,
compressedBytes,
outputPaths,
splitChunkSize,
} = await compressFolderToPath(args.dir, outputPath, encoding, args.split, args.password)
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6
const ratio = originalBytes === 0 ? 0 : compressedBytes / originalBytes
printRunSummary({
title:
outputPaths.length === 1
? "Compressed folder"
: `Compressed folder → ${outputPaths.length} files`,
outputPaths,
stats: {
Encoding: `base${encoding}`,
Files: formatCount(fileCount),
Directories: formatCount(dirCount),
"Original size": formatBytes(originalBytes),
"Archive (pre-Brotli)": formatBytes(archiveBytes),
[`Compressed (base${encoding})`]: formatBytes(compressedBytes),
...splitAnalytics(splitChunkSize, outputPaths.length),
"Space saved": `${((1 - ratio) * 100).toFixed(1)}%`,
Time: `${elapsedMs.toFixed(0)} ms`,
},
})
return
}
const outputPath = resolveOutputPath(args, "compressed.txt", ".txt")
const input = readInput(args)
const inputBytes = Buffer.byteLength(input, "utf-8")
const start = process.hrtime.bigint()
const result = compress(input, encoding, args.password)
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6
const { paths: outputPaths, splitChunkSize } = writeCompressedOutput(
result,
outputPath,
args.split,
)
const outputBytes = Buffer.byteLength(result, "utf-8")
const ratio = inputBytes === 0 ? 0 : outputBytes / inputBytes
printRunSummary({
title:
outputPaths.length === 1
? "Compressed text"
: `Compressed text → ${outputPaths.length} files`,
outputPaths,
stats: {
Encoding: `base${encoding}`,
"Original size": formatBytes(inputBytes),
[`Compressed (base${encoding})`]: formatBytes(outputBytes),
...splitAnalytics(splitChunkSize, outputPaths.length),
"Size ratio": ratio.toFixed(3),
"Space saved": `${((1 - ratio) * 100).toFixed(1)}%`,
Time: `${elapsedMs.toFixed(0)} ms`,
},
})
}