-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunzip.ts
55 lines (49 loc) · 1.37 KB
/
unzip.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
import { path } from "../deps.ts";
import { AppError } from "../errors.ts";
import { asserts } from "./asserts.ts";
/**
* Decompress a zip file at a given path.
*
* @param filePath - Path to the zip file
* @param destinationPath - Path to the destination folder
* @param options - Configuration options
*/
export async function unzip(
filePath: string,
destinationPath = Deno.cwd(),
): Promise<string> {
try {
Deno.statSync(filePath);
} catch (_err) {
throw new AppError({ message: `A file "${filePath}" does not exist` });
}
const filename = path.basename(filePath, path.extname(filePath));
const destination = path.join(destinationPath, filename);
asserts(
await decompressProcess(filePath, destination),
new AppError({ message: `A file "${filePath}" does not exist` }),
);
return destination;
}
async function decompressProcess(
source: string,
destination: string,
): Promise<boolean> {
// deno-lint-ignore no-deprecated-deno-api
const unzipProc = Deno.run({
cmd: Deno.build.os === "windows"
? [
"PowerShell",
"Expand-Archive",
"-Path",
`"${source}"`,
"-DestinationPath",
`"${destination}"`,
"-Force",
]
: ["unzip", "-o", source, "-d", destination],
});
const processStatus = (await unzipProc.status()).success;
unzipProc.close();
return processStatus;
}