This repository was archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessFile.ts
56 lines (47 loc) · 1.55 KB
/
ProcessFile.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
import * as fs from "fs";
import { extname } from "path";
import { promisify } from "util";
import { LZWTransformCompress, LZWTransformDecompress } from "./LZWstream";
import { SingleBar, Presets } from "cli-progress";
import Progress from "progress-stream";
import { questionYN } from "./CLI";
export const processFile = async (
input: string,
outputOptional?: string,
operation?: "C" | "D",
forceOverwrite?: boolean
) => {
if (!await promisify(fs.exists)(input))
throw new Error(`Input file doesn't exist! ${input}`);
const isDecompress = (operation === "D")
|| (operation !== "C" && extname(input).toLowerCase() === ".lzw")
;
const output = outputOptional || (isDecompress
? input.split(".").slice(-1)[0] === "lzw"
? input.split(".").slice(0, -1).join(".")
: input
: input + ".lzw"
);
if (await promisify(fs.exists)(output)
&& !forceOverwrite
&& !await questionYN(`Output file ${output} already exists. Override ?`)
) return;
const length = (await promisify(fs.stat)(input)).size;
const progressBar = new SingleBar({ clearOnComplete: true }, Presets.shades_classic);
progressBar.start(length, 0);
await new Promise(done =>
fs.createReadStream(input)
.pipe(
Progress({ length })
.on("progress", ({ transferred }) => progressBar.update(transferred))
)
.pipe(
isDecompress
? new LZWTransformDecompress()
: new LZWTransformCompress()
)
.pipe(fs.createWriteStream(output))
.on("close", done)
);
progressBar.stop();
};