|
| 1 | +import { mkdir, writeFile } from "node:fs/promises"; |
| 2 | +import { extname } from "node:path"; |
| 3 | +import process from "node:process"; |
| 4 | + |
| 5 | +const root = new URL("../data-files/", import.meta.url); |
| 6 | + |
| 7 | +interface Entry { |
| 8 | + type: "directory" | "file"; |
| 9 | + name: string; |
| 10 | + path: string; |
| 11 | +} |
| 12 | + |
| 13 | +async function run() { |
| 14 | + const rootResponse = await fetch("https://unicode-proxy.mojis.dev/proxy/emoji/"); |
| 15 | + |
| 16 | + if (!rootResponse.ok) { |
| 17 | + throw new Error("failed to fetch root entry"); |
| 18 | + } |
| 19 | + |
| 20 | + const rootEntries: Entry[] = await rootResponse.json() as Entry[]; |
| 21 | + |
| 22 | + await mkdir(root, { recursive: true }); |
| 23 | + |
| 24 | + async function processDirectory(entry: Entry, basePath: string) { |
| 25 | + const dirResponse = await fetch(`https://unicode-proxy.mojis.dev/proxy/emoji/${entry.path}`); |
| 26 | + const dirEntries: Entry[] = await dirResponse.json() as Entry[]; |
| 27 | + |
| 28 | + const fileEntries = dirEntries.filter( |
| 29 | + (e) => |
| 30 | + e.type === "file" |
| 31 | + && e.name !== "ReadMe.txt" |
| 32 | + && !e.path.includes("latest") |
| 33 | + && !e.path.includes("draft"), |
| 34 | + ); |
| 35 | + |
| 36 | + await Promise.all( |
| 37 | + fileEntries.map(async (fileEntry) => { |
| 38 | + const fullPath = basePath + fileEntry.path; |
| 39 | + |
| 40 | + if (fullPath.includes("latest") || fullPath.includes("draft")) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const [_, version, ...rest] = fullPath.replace(/^\//, "").split("/"); |
| 45 | + |
| 46 | + if (!version) { |
| 47 | + throw new Error(`version is undefined, ${fullPath}`); |
| 48 | + } |
| 49 | + |
| 50 | + const fileExt = extname(fileEntry.name); |
| 51 | + const type = rest.join("").replace(fileExt, ""); |
| 52 | + |
| 53 | + await mkdir(new URL(type, root), { recursive: true }); |
| 54 | + |
| 55 | + const content = await fetch(`https://unicode-proxy.mojis.dev/proxy/${fullPath}`).then((res) => res.text()); |
| 56 | + await writeFile( |
| 57 | + new URL(`${type}/v${version.toString()}${fileExt}`, root), |
| 58 | + content, |
| 59 | + ); |
| 60 | + }), |
| 61 | + ); |
| 62 | + |
| 63 | + const dirEntriesToProcess = dirEntries.filter((e) => e.type === "directory"); |
| 64 | + await Promise.all( |
| 65 | + dirEntriesToProcess.map((dir) => processDirectory(dir, basePath + entry.path)), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + const directories = rootEntries.filter((e) => e.type === "directory"); |
| 70 | + await Promise.all(directories.map((dir) => processDirectory(dir, `emoji/${dir.path}/`))); |
| 71 | + |
| 72 | + console.log("done"); |
| 73 | +} |
| 74 | + |
| 75 | +run().catch((err) => { |
| 76 | + console.error(err); |
| 77 | + process.exit(1); |
| 78 | +}); |
0 commit comments