-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupload-assets.js
More file actions
45 lines (37 loc) · 1.33 KB
/
upload-assets.js
File metadata and controls
45 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')
// Path to your build folder
const directoryPath = path.resolve('./out/_next/static')
const kvNamespace = '7e4963ca204a4783bd576a3b361017eb'
function uploadToKV(filePath, kvKey) {
return new Promise((resolve, reject) => {
const command = `npx wrangler@latest kv key put "${kvKey}" --path "${filePath}" --namespace-id ${kvNamespace}`
console.log(command)
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Failed to upload ${kvKey}:`, error.message);
reject(error);
return;
}
console.log(`Uploaded: ${kvKey}`);
resolve();
});
});
}
// Modified processDirectory function for serial uploads
async function processDirectory(directory) {
const items = fs.readdirSync(directory)
for (const item of items) {
const fullPath = path.join(directory, item)
if (fs.statSync(fullPath).isDirectory()) {
await processDirectory(fullPath)
} else {
let kvKey = '_next/' + path.relative('./out/_next', fullPath).replace(/\\/g, '/')
// Upload serially - wait for each upload to complete before starting the next
await uploadToKV(fullPath, kvKey)
}
}
}
// Start processing from the static directory
processDirectory(directoryPath)