|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @ts-check |
| 4 | + |
| 5 | +// This script will publish the compiler.js bundle / packages cmij.js files to our Cloudclare R2. |
| 6 | +// The target folder on R2 bucket will be the compiler.js' version number. |
| 7 | +// This script requires `rclone` and `zstd` to be installed. |
| 8 | + |
| 9 | +import * as fs from "node:fs"; |
| 10 | +import * as path from "node:path"; |
| 11 | +import * as readline from "node:readline/promises"; |
| 12 | +import { createRequire } from "node:module"; |
| 13 | + |
| 14 | +import { |
| 15 | + exec, |
| 16 | + playgroundDir, |
| 17 | + playgroundPackagesDir, |
| 18 | +} from "./common.mjs"; |
| 19 | + |
| 20 | +const require = createRequire(import.meta.url); |
| 21 | +// @ts-ignore |
| 22 | +const { rescript_compiler } = require('../compiler.js'); |
| 23 | + |
| 24 | +/** |
| 25 | + * @type {number} |
| 26 | + */ |
| 27 | +const version = rescript_compiler.make().rescript.version; |
| 28 | +const tag = "v" + version; |
| 29 | + |
| 30 | +console.log("Uploading playground artifacts for %s", tag); |
| 31 | +if (!process.env.CI) { |
| 32 | + const rl = readline.createInterface({ |
| 33 | + input: process.stdin, |
| 34 | + output: process.stdout, |
| 35 | + }); |
| 36 | + const answer = await rl.question("Do you want to proceed? [y/N]: "); |
| 37 | + rl.close(); |
| 38 | + |
| 39 | + if (answer.toLowerCase() !== "y") { |
| 40 | + console.log("Cancelled"); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const rcloneOpts = (process.env.CI |
| 46 | + ? [ |
| 47 | + "--stats 5", |
| 48 | + "--checkers 5000", |
| 49 | + "--transfers 8", |
| 50 | + "--buffer-size 128M", |
| 51 | + "--s3-chunk-size 128M", |
| 52 | + "--s3-upload-concurrency 8", |
| 53 | + ] |
| 54 | + : [ |
| 55 | + "--progress", |
| 56 | + "--checkers 5000", |
| 57 | + "--transfers 16", |
| 58 | + "--buffer-size 128M", |
| 59 | + "--s3-chunk-size 128M", |
| 60 | + "--s3-upload-concurrency 16", |
| 61 | + ]).join(" "); |
| 62 | + |
| 63 | +const remote = process.env.RCLONE_REMOTE || "rescript"; |
| 64 | +const bucket = "cdn-assets"; |
| 65 | +const dest = `${remote}:${bucket}/${tag}`; |
| 66 | + |
| 67 | +// Create a temporary directory for bundling |
| 68 | +const tmpDir = path.join(playgroundDir, ".tmp"); |
| 69 | +const artifactsDir = path.join(tmpDir, tag); |
| 70 | +const archivePath = path.join(tmpDir, `${tag}.tar.zst`); |
| 71 | +fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 72 | +fs.mkdirSync(artifactsDir, { recursive: true }); |
| 73 | + |
| 74 | +console.log("Copying compiler.js"); |
| 75 | +fs.copyFileSync( |
| 76 | + path.join(playgroundDir, "compiler.js"), |
| 77 | + path.join(artifactsDir, "compiler.js"), |
| 78 | +); |
| 79 | + |
| 80 | +console.log("Copying packages"); |
| 81 | +fs.cpSync(playgroundPackagesDir, artifactsDir, { recursive: true }); |
| 82 | + |
| 83 | +// Create tar.zst archive |
| 84 | +console.log("Creating archive..."); |
| 85 | +exec(`tar \\ |
| 86 | + --use-compress-program="zstd -T0 --adapt --exclude-compressed" \\ |
| 87 | + -cf "${archivePath}" \\ |
| 88 | + "${artifactsDir}" |
| 89 | +`); |
| 90 | + |
| 91 | +console.log(`Uploading v${version} artifacts...`); |
| 92 | +exec(`rclone sync ${rcloneOpts} --fast-list \\ |
| 93 | + "${artifactsDir}" \\ |
| 94 | + "${dest}" |
| 95 | +`); |
| 96 | + |
| 97 | +console.log("Uploading archive..."); |
| 98 | +exec(`rclone copyto ${rcloneOpts} \\ |
| 99 | + "${archivePath}" \\ |
| 100 | + "${dest}.tar.zst" |
| 101 | +`); |
0 commit comments