forked from moncefplastin07/deno-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.ts
43 lines (42 loc) · 1.32 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
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 filesList = typeof files === "string"
? files
: files.join(Deno.build.os === "windows" ? ", " : " ");
const compressCommandProcess = Deno.run({
cmd: Deno.build.os === "windows"
? [
"PowerShell",
"Compress-Archive",
"-Path",
filesList,
"-DestinationPath",
archiveName,
options?.overwrite ? "-Force" : "",
]
: ["zip", "-r", ...options?.flags ?? [], archiveName, ...filesList.split(" ")],
});
const processStatus = (await compressCommandProcess.status()).success;
Deno.close(compressCommandProcess.rid);
return processStatus;
};
export const compress = async (
files: string | string[],
archiveName: string = "./archive.zip",
options?: CompressOptions,
): Promise<boolean> => {
return await compressProcess(files, archiveName, options);
};