-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
73 lines (65 loc) · 1.99 KB
/
utils.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createWriteStream } from "fs";
import archiver from "archiver";
export async function post(
url: string,
options: RequestInit,
) {
try {
const response = await fetch(url, {
method: "POST",
headers: options.headers,
body: options.body,
duplex: options.duplex || 'half'
});
if (!response.ok) {
console.error("Response is not ok", response);
throw new Error("Response is not ok");
}
if (!(options.headers?.["Content-Type"] || options.headers?.["Accept"])) {
return response;
}
const json = await response.json();
return json;
} catch (error) {
console.error(error);
throw new Error(error);
}
}
export function getEnv(env: Record<string, string> = {}) {
return Object.entries(env).map(([key, value]) => ({
key,
scopes: ["builds", "functions", "runtime", "post_processing"],
values: [
{
context: "all",
value: value as string,
},
],
}));
}
export function zipFiles({zipPath, glob}: {zipPath: string, glob: string}) {
return new Promise((resolve) => {
// create a file to stream archive data to.
const output = createWriteStream(zipPath);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function () {
resolve({ zipPath });
});
// good practice to catch this error explicitly
archive.on('error', function (err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
// append a file from stream
// archive.append(readFileSync('index.html'), { name: 'index.html' });
archive.glob(glob);
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
});
}