-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.js
88 lines (77 loc) · 2.13 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
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
const Storage = require('@google-cloud/storage');
const fs = require('fs');
const projectId = 'brianfoggcom';
const bucketName = 'harmonyoscillators.brianfogg.com';
const keyFilename = 'auth/uploadtoken.json';
const buildFileLocation = './dist/';
console.log(`deploying to ${projectId}/${bucketName}`);
bucket = Storage({
projectId,
keyFilename,
}).bucket(bucketName);
const emptyBucket = (cb) => {
bucket.getFiles()
.then((results) => {
let index = 1;
const files = results[0];
if (files.length) {
files.forEach((file) => {
bucket.file(file.name).delete()
.then(() => {
console.log(`deleted ${file.name}`);
index++;
if (index === files.length) {
cb();
}
});
});
} else {
console.log(`empty bucket`);
cb();
}
})
.catch(console.error);
};
const uploadOptions = {
'index.html': {
public: true,
metadata: {
cacheControl: 'no-cache'
}
},
};
const uploadFolder = (folderName) => {
const folderToUploadFrom2 = fs.readdirSync(`${buildFileLocation}/${folderName}/`);
folderToUploadFrom2.forEach((file) => {
let destination = `${folderName}/${file}`
console.log(`upload ${folderName}`, file, destination);
console.log('')
bucket.upload(`${buildFileLocation}${folderName}/${file}`, { public: true, destination: destination })
.then(() => {
console.log(`uploaded ${file}`);
})
.catch((err) => {
console.log(`upload ${folderName} err`, file, err)
});
});
}
const uploadDist = () => {
console.log('upload dist')
const distFiles = fs.readdirSync(buildFileLocation);
distFiles.forEach((file) => {
if(file === 'css' || file === 'js' || file === '.DS_Store') {
return
}
const options = uploadOptions[file] || { public: true };
bucket.upload(`${buildFileLocation}${file}`,options)
.then(() => {
console.log(`uploaded ${file}`);
})
.catch((err) => {
console.log('upload 1 err', file, err)
});
});
uploadFolder('js')
uploadFolder('css')
};
emptyBucket(uploadDist);