1 /* eslint-disable no-console */
2 /**
3 * Uploads specs/ to Cloudflare R2.
4 *
5 * Usage:
6 * node ./scripts/upload-specs-to-r2.mjs
7 * Full upload of specs/ directory.
8 *
9 * node ./scripts/upload-specs-to-r2.mjs --upload specs/components/foo.json
10 * Upload only the listed files (paths relative to app root).
11 *
12 * node ./scripts/upload-specs-to-r2.mjs --remove specs/components/old.json
13 * Remove the listed keys from R2 (paths relative to app root).
14 *
15 * --upload and --remove can be combined in a single invocation.
16 *
17 * Required env vars:
18 * CLOUDFLARE_ACCOUNT_ID
19 * CLOUDFLARE_R2_ACCESS_KEY_ID
20 * CLOUDFLARE_R2_SECRET_ACCESS_KEY
21 * R2_BUCKET_NAME (default: docs-assets)
22 */
23
24 // Load .env if present; silently skip if not (e.g. CI injects vars directly)
25 try {
26 process.loadEnvFile()
27 } catch {
28 /* no .env file */
29 }
30
31 import {
32 S3Client,
33 PutObjectCommand,
34 DeleteObjectCommand,
35 } from "@aws-sdk/client-s3"
36 import { readFile, readdir } from "fs/promises"
37 import path from "path"
38 import { lookup as mimeLookup } from "mime-types"
39
40 const accountId = process.env.CLOUDFLARE_ACCOUNT_ID
41 const accessKeyId = process.env.CLOUDFLARE_R2_ACCESS_KEY_ID
42 const secretAccessKey = process.env.CLOUDFLARE_R2_SECRET_ACCESS_KEY
43 const bucket = process.env.R2_BUCKET_NAME || "docs-assets"
44
45 if (!accountId || !accessKeyId || !secretAccessKey) {
46 console.error(
47 "Missing required env vars: CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_R2_ACCESS_KEY_ID, CLOUDFLARE_R2_SECRET_ACCESS_KEY"
48 )
49 process.exit(1)
50 }
51
52 const client = new S3Client({
53 region: "auto",
54 endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
55 credentials: { accessKeyId, secretAccessKey },
56 })
57
58 // Parse --upload and --remove CLI flags
59 const args = process.argv.slice(2)
60 const filesToUpload = []
61 const filesToRemove = []
62 let mode = null
63
64 for (const arg of args) {
65 if (arg === "--upload") {
66 mode = "upload"
67 continue
68 }
69 if (arg === "--remove") {
70 mode = "remove"
71 continue
72 }
73 if (arg.startsWith("--")) {
74 mode = null
75 continue
76 }
77 if (mode === "upload") {
78 filesToUpload.push(arg)
79 }
80 if (mode === "remove") {
81 filesToRemove.push(arg)
82 }
83 }
84
85 const isSelective = filesToUpload.length > 0 || filesToRemove.length > 0
86
87 // --- helpers ---
88
89 async function uploadFile(localPath, r2Key) {
90 const body = await readFile(localPath)
91 const contentType =
92 mimeLookup(path.basename(localPath)) || "application/octet-stream"
93 await client.send(
94 new PutObjectCommand({
95 Bucket: bucket,
96 Key: r2Key,
97 Body: body,
98 ContentType: contentType,
99 })
100 )
101 console.log(` uploaded: ${r2Key}`)
102 }
103
104 async function removeFile(r2Key) {
105 await client.send(new DeleteObjectCommand({ Bucket: bucket, Key: r2Key }))
106 console.log(` removed: ${r2Key}`)
107 }
108
109 async function uploadDir(localDir, r2Prefix) {
110 const entries = await readdir(localDir, { withFileTypes: true })
111 for (const entry of entries) {
112 const localPath = path.join(localDir, entry.name)
113 const r2Key = `${r2Prefix}/${entry.name}`
114 if (entry.isDirectory()) {
115 await uploadDir(localPath, r2Key)
116 } else if (entry.isFile()) {
117 await uploadFile(localPath, r2Key)
118 }
119 }
120 }
121
122 // --- main ---
123
124 if (isSelective) {
125 for (const relPath of filesToUpload) {
126 await uploadFile(path.join(process.cwd(), relPath), `ui/${relPath}`)
127 }
128 for (const relPath of filesToRemove) {
129 await removeFile(`ui/${relPath}`)
130 }
131 } else {
132 const specsDir = path.join(process.cwd(), "specs")
133 console.log(`Uploading ${specsDir} → r2://${bucket}/ui/specs`)
134 await uploadDir(specsDir, "ui/specs")
135 }
136
137 console.log("Done.")