-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
209 lines (173 loc) · 6.31 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const FormData = require('form-data');
const dotenv = require('dotenv');
dotenv.config();
const workbookID = process.env.WORKBOOK_ID;
const API_KEY = process.env.API_KEY;
axios.defaults.baseURL = "https://sheet.rocks/api/v1";
axios.defaults.headers.common['Authorization'] = `Bearer ${API_KEY}`;
let containsIndex = false;
async function uploadFiles(workbookId) {
try {
const folderPath = 'web/dist';
const files = await fs.promises.readdir(folderPath);
// Get the list of existing files
const res = await axios.get(`/workbook/${workbookId}/files`);
const existingFiles = res.data;
const uploadPromises = files.map(async (file) => {
// if file ends with.map, skip it
if (file.endsWith('.map')) {
return;
}
if (file === 'index.html') {
containsIndex = true;
}
const filePath = path.join(folderPath, file);
const content = await fs.promises.readFile(filePath);
const existingFile = existingFiles.find((f) => f.Name === file);
if (existingFile) {
// Update existing file
const fileId = existingFile.ID;
console.log('Updating file:', existingFile.Name);
const res = await axios.put(`/workbook/${workbookId}/files/${fileId}`, {
Content: Buffer.from(content).toString('base64'),
});
console.log('File updated:', existingFile.Name);
} else {
// Upload new file
console.log('Uploading file:', file);
const res = await axios.post(`/workbook/${workbookId}/files`, {
Name: file,
Content: Buffer.from(content).toString('base64'),
IsPublic: true,
});
}
return res.data;
});
console.log('Awaiting uploads...');
await Promise.all(uploadPromises);
console.log('All files uploaded.');
} catch (error) {
console.error('Error uploading files:', error);
}
}
async function uploadWebhooks(workbookId) {
try {
const folderPath = 'api/dist';
const files = await fs.promises.readdir(folderPath);
// Get the list of existing webhooks
const res = await axios.get(`/workbook/${workbookId}/webhooks`);
const existingWebhooks = res.data;
const uploadPromises = files.map(async (file) => {
if (file.endsWith('.map')) {
return;
}
const filePath = path.join(folderPath, file);
const fileStream = fs.createReadStream(filePath);
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const form = new FormData();
form.append('file', fileStream, { knownLength: fileSizeInBytes });
const route = file.replace('.js', '');
const existingWebhook = existingWebhooks.find((w) => w.Name === route);
if (existingWebhook) {
console.log('Updating webhook:', existingWebhook.Name);
// Update existing webhook
const webhookId = existingWebhook.ID;
await axios.patch(
`/workbook/${workbookId}/webhooks?webhookId=${webhookId}&name=${route}&route=${route}&filesChanged=true&runtime=javascript`,
form,
{
headers: {
...form.getHeaders(),
'Content-length': form.getLengthSync(),
},
}
);
} else {
console.log('Uploading webhook:', route);
// Create a new webhook
await axios.put(
`/workbook/${workbookId}/webhooks?name=${route}&route=${route}&runtime=javascript`,
form,
{
headers: {
...form.getHeaders(),
'Content-length': form.getLengthSync(),
},
}
);
}
return { success: true, route };
});
await Promise.all(uploadPromises);
console.log('All webhooks uploaded.');
} catch (error) {
console.error('Error uploading webhooks:', error);
}
}
async function uploadAutomations(workbookId) {
try {
const folderPath = 'automations/dist';
const files = await fs.promises.readdir(folderPath);
// Get the list of existing automations
const res = await axios.get(`/workbook/${workbookId}/automations`);
const existingAutomations = res.data;
const uploadPromises = files.map(async (file) => {
if (!file.endsWith('.js')) {
return;
}
const filePath = path.join(folderPath, file);
const automationScript = await fs.promises.readFile(filePath, 'utf8');
const automationName = file.replace('.js', '');
const existingAutomation = existingAutomations.find((a) => a.Name === automationName);
if (existingAutomation) {
console.log('Updating automation:', existingAutomation.Name);
// Update existing automation
const automationId = existingAutomation.ID;
await axios.patch(
`/workbook/${workbookId}/automations/${automationId}`,
{
Name: automationName,
Runtime: 'javascript',
AutomationText: automationScript,
RunOnSchedule: false
}
);
} else {
console.log('Uploading new automation:', automationName);
// Create a new automation
await axios.post(
`/workbook/${workbookId}/automations`,
{
Name: automationName,
Runtime: 'javascript',
AutomationText: automationScript,
RunOnSchedule: false
}
);
}
return { success: true, automationName };
});
await Promise.all(uploadPromises);
console.log('All automations uploaded.');
} catch (error) {
console.error('Error uploading automations:', error);
}
}
// Usage:
async function deploy() {
await uploadFiles(workbookID);
await uploadWebhooks(workbookID);
await uploadAutomations(workbookID);
if(containsIndex) {
if(process.env.ROOT_URL) {
console.log(`Success! Your app is live at ${process.env.ROOT_URL}`);
} else {
console.log(`Success! Your app is live at https://sheet.rocks/apps/${workbookID}/index.html`);
}
}
};
deploy();