Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/package/src/util/utils.ts
6450 views
1
import { writeAll } from "io/write-all";
2
import { CmdResult, runCmd } from "./cmd.ts";
3
import { isWindows } from "../../../src/deno_ral/platform.ts";
4
5
// Read an environment variable
6
export function getEnv(name: string, defaultValue?: string) {
7
const value = Deno.env.get(name);
8
if (!value) {
9
if (defaultValue === undefined) {
10
throw new Error("Missing environment variable: " + name);
11
} else {
12
return defaultValue;
13
}
14
} else {
15
return value;
16
}
17
}
18
19
export async function download(src: string, dest: string): Promise<void> {
20
const response = await fetch(src);
21
const data = await response.blob();
22
23
const buffer = await data.arrayBuffer();
24
const contents = new Uint8Array(buffer);
25
26
const file = await Deno.create(dest);
27
await writeAll(file, contents);
28
file.close();
29
}
30
31
export async function unzip(
32
zipFile: string,
33
dest: string,
34
): Promise<CmdResult> {
35
if (isWindows) {
36
return await runCmd("PowerShell", [
37
"Expand-Archive",
38
"-Path",
39
`"${zipFile}"`,
40
"-DestinationPath",
41
`"${dest}"`,
42
]);
43
} else {
44
return await runCmd("unzip", [zipFile, "-d", dest]);
45
}
46
}
47
48