-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy.js
More file actions
95 lines (75 loc) · 2.45 KB
/
deploy.js
File metadata and controls
95 lines (75 loc) · 2.45 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
const fs = require('fs');
const util = require('util');
const firebase = require('firebase');
global.XMLHttpRequest = require('xhr2');
require('firebase/storage');
require('dotenv').config();
const readDir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: 'javascript-9cdb9.firebaseapp.com',
databaseURL: 'https://javascript-9cdb9.firebaseio.com',
projectId: 'javascript-9cdb9',
storageBucket: 'javascript-9cdb9.appspot.com',
messagingSenderId: '593134678665',
appId: '1:593134678665:web:b3dd9b4d842295aa4c4c58',
measurementId: 'G-83DM989VK5',
};
const app = firebase.initializeApp(firebaseConfig);
app.auth().signInWithEmailAndPassword(process.env.FIREBASE_EMAIL, process.env.FIREBASE_PASSWORD).then(processFiles);
async function processFiles() {
const fileExtensions = ['.exe', '.dmg', '.AppImage'];
const files = [];
const filesName = [];
try {
const dist = await readDir('./dist');
dist.forEach(fileName => {
if (fileExtensions.some(ext => fileName.endsWith(ext))) {
files.push(readFile('./dist/' + fileName));
filesName.push(fileName);
}
});
const filesData = await Promise.all(files);
deployToFirebase(filesName, filesData);
} catch {}
}
async function deployToFirebase(filesName, filesData) {
try {
console.log(`${getCurrentTime()} - Start deploying to firebase...`);
// Deploy files
const deployedFiles = await Promise.all(
filesData.map((data, idx) => app.storage().ref().child(filesName[idx]).put(data)),
);
// Get download URLS
const downloadUrls = await Promise.all(deployedFiles.map(file => file.ref.getDownloadURL()));
// Save download urls in butlerUrls collection (firestore)
await Promise.all(
downloadUrls.map((downloadUrl, idx) =>
app
.firestore()
.collection('butlerUrls')
.doc(getDocName(filesName[idx]))
.set({ [getDocName(filesName[idx])]: downloadUrl }),
),
);
console.log(`${getCurrentTime()} - Files successfully deployed`);
process.exit();
} catch (error) {
console.log(error);
}
}
function getDocName(fileName) {
if (fileName.includes('.exe')) {
return 'windows';
}
if (fileName.includes('.dmg')) {
return 'macOS';
}
if (fileName.includes('.AppImage')) {
return 'linux';
}
}
function getCurrentTime() {
return new Date().toLocaleTimeString();
}