From 1d4b3ce3071f47b7699e176828bc1dfd5ac41940 Mon Sep 17 00:00:00 2001 From: Aiden <68633820+awils27@users.noreply.github.com> Date: Thu, 11 Jun 2026 08:30:52 +1000 Subject: [PATCH] deploy script --- .env.r2.example | 2 ++ scripts/upload-r2.mjs | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.env.r2.example b/.env.r2.example index 0da002f..315374b 100644 --- a/.env.r2.example +++ b/.env.r2.example @@ -7,7 +7,9 @@ R2_SECRET_ACCESS_KEY=your_r2_secret_access_key R2_BUCKET=your-r2-bucket-name # Optional settings. +# Local folder to upload after running npm run build. R2_SOURCE_DIR=vr180player +# Object key prefix inside the bucket. R2_PREFIX=vr180player R2_ENDPOINT= R2_CACHE_CONTROL=public, max-age=31536000, immutable diff --git a/scripts/upload-r2.mjs b/scripts/upload-r2.mjs index cf580ef..5a07594 100644 --- a/scripts/upload-r2.mjs +++ b/scripts/upload-r2.mjs @@ -1,6 +1,6 @@ import { createHmac, createHash } from 'node:crypto'; -import { readdir, readFile } from 'node:fs/promises'; -import { basename, dirname, join, relative, sep } from 'node:path'; +import { readdir, readFile, stat } from 'node:fs/promises'; +import { basename, dirname, join, relative, resolve, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; const rootDir = dirname(dirname(fileURLToPath(import.meta.url))); @@ -9,6 +9,7 @@ const args = new Set(process.argv.slice(2)); await loadEnvFile(join(rootDir, '.env.r2')); const config = getConfig(); +await assertReadableSourceDir(config.sourceDir); const files = await listFiles(config.sourceDir); if (files.length === 0) { @@ -45,10 +46,32 @@ function getConfig() { endpoint: endpoint.replace(/\/+$/, ''), prefix: trimSlashes(process.env.R2_PREFIX ?? 'vr180player'), secretAccessKey, - sourceDir: join(rootDir, process.env.R2_SOURCE_DIR?.trim() || 'vr180player') + sourceDir: resolve(rootDir, process.env.R2_SOURCE_DIR?.trim() || 'vr180player') }; } +async function assertReadableSourceDir(sourceDir) { + try { + const sourceStats = await stat(sourceDir); + if (!sourceStats.isDirectory()) { + console.error(`R2_UPLOAD: R2_SOURCE_DIR is not a directory: ${sourceDir}`); + process.exit(1); + } + } catch (error) { + if (error?.code === 'ENOENT') { + console.error([ + `R2_UPLOAD: Source directory not found: ${sourceDir}`, + 'R2_SOURCE_DIR is a local folder to upload, not the R2 bucket name.', + 'If "VR-180" is your bucket name, set R2_BUCKET=VR-180 and leave R2_SOURCE_DIR=vr180player.', + 'If vr180player/ is missing, run npm run build first.' + ].join('\n')); + process.exit(1); + } + + throw error; + } +} + async function loadEnvFile(filePath) { let contents; try {