-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
58 lines (46 loc) · 1.54 KB
/
deploy.js
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
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const {Storage} = require('@google-cloud/storage');
const stat = promisify(fs.stat);
const readdir = promisify(fs.readdir);
const bucketName = 'vpnlove';
const directoryPath = './build';
const storage = new Storage({
keyFilename: '.google.json',
});
async function* getFiles(directory = '.') {
for (const file of await readdir(directory)) {
const fullPath = path.join(directory, file);
const stats = await stat(fullPath);
// Recursively dive deeper into subdirectories
if (stats.isDirectory()) {
yield* getFiles(fullPath);
}
// Only yield paths to files
if (stats.isFile()) {
yield fullPath;
}
}
}
async function uploadDirectory() {
const bucket = storage.bucket(bucketName);
let successfulUploads = 0;
for await (const filePath of getFiles(directoryPath)) {
try {
const dirname = path.dirname(directoryPath);
const destination = path.relative(dirname, filePath);
await bucket.upload(filePath, {
destination: destination.replace('build/', '')
});
console.log(`Successfully uploaded: ${filePath}`);
successfulUploads++;
} catch (e) {
console.error(`Error uploading ${filePath}:`, e);
}
}
console.log(
`${successfulUploads} files uploaded to ${bucketName} successfully.`
);
}
uploadDirectory().catch(console.error);