-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
166 lines (139 loc) · 4.36 KB
/
upload.js
File metadata and controls
166 lines (139 loc) · 4.36 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { readdir, stat, readFile } from 'fs/promises';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { join, extname, relative, basename } from 'path';
import { execFile } from 'child_process';
import { promisify } from 'util';
import { createHash } from 'crypto';
const exec = promisify(execFile);
const BUCKET = 'nullvariable-com';
const ACCOUNT_ID = '20cf47b39d871fc8f4faeeb895520ddc';
const DIST = 'dist';
const MANIFEST_PATH = '.upload-manifest.json';
const FORCE = process.argv.includes('--force');
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.xml': 'application/xml',
'.webmanifest': 'application/manifest+json',
'.txt': 'text/plain',
'.map': 'application/json',
};
// --- Manifest helpers ---
function loadManifest() {
if (FORCE) return {};
try {
return JSON.parse(readFileSync(MANIFEST_PATH, 'utf8'));
} catch {
return {};
}
}
function saveManifest(manifest) {
writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2));
}
async function hashFileAsync(filePath) {
const content = await readFile(filePath);
return createHash('md5').update(content).digest('hex');
}
async function walk(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...await walk(full));
} else {
files.push(full);
}
}
return files;
}
function cacheControl(filePath) {
const ext = extname(filePath);
if (ext === '.html' || filePath.endsWith('site.webmanifest')) {
return 'no-cache';
}
// Blog HTML and unhashed assets get short cache
if (filePath.includes('blog/') && !basename(filePath).match(/\.[a-f0-9]{8}\./)) {
return 'public, max-age=3600';
}
return 'public, max-age=31536000, immutable';
}
function contentType(filePath) {
return MIME_TYPES[extname(filePath)] || 'application/octet-stream';
}
async function upload(filePath) {
const key = relative(DIST, filePath);
const args = [
'wrangler', 'r2', 'object', 'put',
`${BUCKET}/${key}`,
'--remote',
'--file', filePath,
'--content-type', contentType(filePath),
'--cache-control', cacheControl(filePath),
];
try {
const { stdout, stderr } = await exec('npx', args);
console.log(` ✓ ${key}`);
} catch (err) {
console.error(` ✗ ${key}: ${err.stderr || err.message}`);
throw err;
}
}
async function main() {
console.log(`${FORCE ? 'Force uploading' : 'Uploading'} to R2 bucket "${BUCKET}"...\n`);
const manifest = loadManifest();
const files = await walk(DIST);
// Build a set of current file keys for cleanup
const currentKeys = new Set();
// Compute hashes and determine which files need uploading
const toUpload = [];
let skippedCount = 0;
for (const file of files) {
const key = relative(DIST, file);
currentKeys.add(key);
const hash = await hashFileAsync(file);
if (!FORCE && manifest[key] && manifest[key] === hash) {
skippedCount++;
continue;
}
toUpload.push({ file, key, hash });
}
// Remove manifest entries for files that no longer exist in dist/
for (const key of Object.keys(manifest)) {
if (!currentKeys.has(key)) {
delete manifest[key];
}
}
if (toUpload.length === 0) {
console.log(` No changes detected. All ${skippedCount} files unchanged.`);
saveManifest(manifest);
console.log('\nDone — nothing to upload.');
return;
}
console.log(` ${toUpload.length} files to upload, ${skippedCount} unchanged.\n`);
// Upload HTML files last so assets are available first
const assets = toUpload.filter(f => extname(f.file) !== '.html');
const html = toUpload.filter(f => extname(f.file) === '.html');
for (const { file, key, hash } of [...assets, ...html]) {
await upload(file);
manifest[key] = hash;
}
// Save manifest after successful upload
saveManifest(manifest);
console.log(`\nDone — uploaded ${toUpload.length} files, skipped ${skippedCount} unchanged.`);
}
main().catch(err => {
console.error('\nUpload failed.');
process.exit(1);
});