|
1
|
/* eslint-disable no-console */ |
|
2
|
/** |
|
3
|
* Uploads references/ (the JSON doc-model, page.json) to Cloudflare R2. |
|
4
|
* |
|
5
|
* Usage: |
|
6
|
* node ./scripts/upload-references-to-r2.mjs |
|
7
|
* Full upload of the references/ directory. |
|
8
|
* |
|
9
|
* node ./scripts/upload-references-to-r2.mjs --upload references/js-sdk/foo/page.json |
|
10
|
* Upload only the listed files (paths relative to app root; page.json). |
|
11
|
* |
|
12
|
* node ./scripts/upload-references-to-r2.mjs --remove references/js-sdk/old/page.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 = mimeLookup(path.basename(localPath)) || "text/plain" |
|
92
|
await client.send( |
|
93
|
new PutObjectCommand({ |
|
94
|
Bucket: bucket, |
|
95
|
Key: r2Key, |
|
96
|
Body: body, |
|
97
|
ContentType: contentType, |
|
98
|
}) |
|
99
|
) |
|
100
|
console.log(` uploaded: ${r2Key}`) |
|
101
|
} |
|
102
|
|
|
103
|
async function removeFile(r2Key) { |
|
104
|
await client.send(new DeleteObjectCommand({ Bucket: bucket, Key: r2Key })) |
|
105
|
console.log(` removed: ${r2Key}`) |
|
106
|
} |
|
107
|
|
|
108
|
// The reference doc-model content file uploaded to R2. |
|
109
|
const CONTENT_FILES = ["page.json"] |
|
110
|
|
|
111
|
async function uploadDir(localDir, r2Prefix, targetBasename) { |
|
112
|
let entries |
|
113
|
try { |
|
114
|
entries = await readdir(localDir, { withFileTypes: true }) |
|
115
|
} catch { |
|
116
|
console.log(` skipped (missing): ${localDir}`) |
|
117
|
return |
|
118
|
} |
|
119
|
for (const entry of entries) { |
|
120
|
if (entry.isFile() && entry.name !== targetBasename) { |
|
121
|
continue |
|
122
|
} |
|
123
|
const localPath = path.join(localDir, entry.name) |
|
124
|
const r2Key = `${r2Prefix}/${entry.name}` |
|
125
|
if (entry.isDirectory()) { |
|
126
|
await uploadDir(localPath, r2Key, targetBasename) |
|
127
|
} else { |
|
128
|
await uploadFile(localPath, r2Key) |
|
129
|
} |
|
130
|
} |
|
131
|
} |
|
132
|
|
|
133
|
// --- main --- |
|
134
|
|
|
135
|
if (isSelective) { |
|
136
|
for (const relPath of filesToUpload) { |
|
137
|
if (!CONTENT_FILES.includes(path.basename(relPath))) { |
|
138
|
console.log(` skipped (not a page.json): ${relPath}`) |
|
139
|
continue |
|
140
|
} |
|
141
|
await uploadFile(path.join(process.cwd(), relPath), `resources/${relPath}`) |
|
142
|
} |
|
143
|
for (const relPath of filesToRemove) { |
|
144
|
if (!CONTENT_FILES.includes(path.basename(relPath))) { |
|
145
|
console.log(` skipped (not a page.json): ${relPath}`) |
|
146
|
continue |
|
147
|
} |
|
148
|
await removeFile(`resources/${relPath}`) |
|
149
|
} |
|
150
|
} else { |
|
151
|
// Full upload of the JSON doc-model references. |
|
152
|
const referencesDir = path.join(process.cwd(), "references") |
|
153
|
console.log( |
|
154
|
`Uploading ${referencesDir} → r2://${bucket}/resources/references` |
|
155
|
) |
|
156
|
await uploadDir(referencesDir, "resources/references", "page.json") |
|
157
|
} |
|
158
|
|
|
159
|
console.log("Done.") |