|
| 1 | +import * as path from "node:path"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as childProcess from "node:child_process"; |
| 4 | +import { Readable } from "node:stream"; |
| 5 | +import * as stream from "node:stream/promises"; |
| 6 | + |
| 7 | +const bucketUrl = new URL("https://cdn.rescript-lang.org"); |
| 8 | + |
| 9 | +const bundlesDir = path.join(import.meta.dirname, "../public/playground-bundles"); |
| 10 | +const versions = await fetch(new URL("/playground-bundles/versions.json", bucketUrl)) |
| 11 | + .then(res => res.json()); |
| 12 | + |
| 13 | +for (const version of versions) { |
| 14 | + const versionDir = path.join(bundlesDir, version); |
| 15 | + const compilerFile = path.join(versionDir, "compiler.js"); |
| 16 | + if (fs.existsSync(compilerFile)) { |
| 17 | + console.log(`%s has already been synced.`, version); |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + console.group(`Syncing %s...`, version); |
| 22 | + { |
| 23 | + console.log(`Downloading archive file...`); |
| 24 | + const res = await fetch(new URL(`/playground-bundles/${version}.tar.zst`, bucketUrl)); |
| 25 | + if (!res.ok) { |
| 26 | + console.error(await res.text()); |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const archiveFile = path.join(bundlesDir, `${version}.tar.zst`); |
| 31 | + const fileStream = fs.createWriteStream(archiveFile); |
| 32 | + await stream.finished(Readable.fromWeb(res.body).pipe(fileStream)); |
| 33 | + |
| 34 | + console.log("Extracting archive..."); |
| 35 | + fs.mkdirSync(versionDir, { recursive: true }); |
| 36 | + childProcess.execSync(`tar --zstd -xf "${archiveFile}" -C "${versionDir}"`); |
| 37 | + |
| 38 | + console.log("Cleaning up..."); |
| 39 | + fs.unlinkSync(archiveFile); |
| 40 | + |
| 41 | + console.groupEnd(); |
| 42 | + } |
| 43 | +} |
0 commit comments