Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/package/src/util/tar.ts
6450 views
1
/*
2
* tar.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
8
import { info } from "../../../src/deno_ral/log.ts";
9
import { dirname, extname } from "../../../src/deno_ral/path.ts";
10
11
export async function makeTarball(
12
input: string,
13
output: string,
14
changewd = false,
15
) {
16
info("Make Tarball");
17
info(`Input: ${input}`);
18
info(`Output: ${output}\n`);
19
const tarCmd: string[] = [];
20
tarCmd.push("tar");
21
tarCmd.push("czvf");
22
tarCmd.push(output);
23
if (changewd) {
24
tarCmd.push("-C");
25
}
26
tarCmd.push(input);
27
if (changewd) {
28
tarCmd.push(".");
29
}
30
31
info(tarCmd);
32
const p = Deno.run({
33
cmd: tarCmd,
34
});
35
const status = await p.status();
36
if (status.code !== 0) {
37
throw Error("Failure to make tarball");
38
}
39
}
40
41
export async function unTar(input: string, directory?: string) {
42
info("Untar");
43
info(`Input: ${input}`);
44
45
const cwd = dirname(input);
46
info(`Cwd: ${cwd}`);
47
48
// Properly process the compressions
49
let compressFlag = "z"; // zip by default
50
const ext = extname(input);
51
if (ext === ".xz") {
52
compressFlag = "J";
53
} else if (ext === ".bz2") {
54
compressFlag = "j";
55
}
56
57
const tarCmd: string[] = [];
58
tarCmd.push("tar");
59
tarCmd.push(`-xv${compressFlag}f`);
60
tarCmd.push(input);
61
if (directory) {
62
tarCmd.push("--directory");
63
tarCmd.push(directory);
64
}
65
66
const p = Deno.run({
67
cmd: tarCmd,
68
cwd,
69
});
70
const status = await p.status();
71
if (status.code !== 0) {
72
throw Error("Failure to untar");
73
}
74
}
75
76