-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyFiles.ts
52 lines (41 loc) · 1.48 KB
/
copyFiles.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
import { ensureDir, exists } from "https://deno.land/[email protected]/fs/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
const partytownUrlPrefix = "https://unpkg.com/@builder.io/partytown@0/lib/";
const partytownFiles = [
"partytown.js",
"partytown-sw.js",
"partytown-media.js",
"partytown-atomics.js",
];
const partytownDebugFiles = [
...partytownFiles.map((filename) => `/debug/${filename}`),
"/debug/partytown-sandbox-sw.js",
"/debug/partytown-ww-atomics.js",
"/debug/partytown-ww-sw.js",
];
const fetchFileText = async (file: string) => {
const response = await fetch(file);
if (!response.ok) {
throw new Error(`Error while fetching ${file}`);
}
return response.text();
};
const fetchAndWriteFiles = (filenames: string[], dest: string) =>
Promise.all(filenames.map(async (filename) => {
const remoteUrl = `${partytownUrlPrefix}/${filename}`;
const localUrl = join(dest, filename);
const pathExists = await exists(localUrl);
if (pathExists) {
return;
}
const txt = await fetchFileText(remoteUrl);
return Deno.writeTextFile(localUrl, txt, { create: true });
}));
export const copyLibFiles = async () => {
const fullPathDest = join(Deno.cwd(), "static/~partytown");
await ensureDir(fullPathDest);
await fetchAndWriteFiles(partytownFiles, fullPathDest);
const debugFolder = join(fullPathDest, "./debug/");
await ensureDir(debugFolder);
await fetchAndWriteFiles(partytownDebugFiles, fullPathDest);
};