forked from bootoffav/deno-zip
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.ts
46 lines (43 loc) · 1.33 KB
/
compress.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
import { exists, join } from "./deps.ts";
interface CompressOptions {
overwrite?: boolean;
flags: string[];
}
const compressProcess = async (
files: string | string[],
archiveName: string = "./archive.zip",
options?: CompressOptions,
): Promise<boolean> => {
if (await exists(archiveName) && !(options?.overwrite)) {
throw `The archive file ${
join(Deno.cwd(), archiveName)
}.zip already exists, Use the {overwrite: true} option to overwrite the existing archive file`;
}
const runtimeOS = Deno.build.os;
const filesList = typeof files === "string"
? files
: files.join(runtimeOS === "windows" ? ", " : " ");
const compressCommandProcess = new Deno.Command(
runtimeOS === "windows" ? "PowerShell" : "zip",
{
args: runtimeOS === "windows"
? [
"Compress-Archive",
"-Path",
filesList,
"-DestinationPath",
archiveName,
options?.overwrite ? "-Force" : "",
]
: ["-r", ...options?.flags ?? [], archiveName, ...filesList.split(" ")],
},
);
return (await compressCommandProcess.output()).success;
};
export const compress = async (
files: string | string[],
archiveName: string = "./archive.zip",
options?: CompressOptions,
): Promise<boolean> => {
return await compressProcess(files, archiveName, options);
};