This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish.js
83 lines (78 loc) · 2.04 KB
/
publish.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
'use strict';
/* eslint-disable no-console */
require('dotenv').config();
const fetch = require('node-fetch');
const fs = require('fs');
const WebSocket = require('ws');
function deserialize(value) {
if (typeof value === 'object' && value !== null) {
switch (value['@t']) {
case '[[undefined]]':
return undefined;
case 'Function': {
const f = function renameMe() {};
Object.defineProperty(f, 'name', {
value: value.data.name,
});
return f;
}
case 'BigInt':
return BigInt(value.data.value);
default:
break;
}
if (Array.isArray(value)) {
return value.map(deserialize);
}
return Object.fromEntries(Object.entries(value)
.map(([k, v]) => {
if (typeof k === 'string' && k.startsWith('#') && k.endsWith('@t')) {
k = k.slice(1);
}
return [k, deserialize(v)];
}));
}
return value;
}
function workbenchWs(url) {
const ws = new WebSocket(url);
ws.onopen = () => console.log('WS Open');
ws.onmessage = (e) => {
const data = JSON.parse(e.data);
console[data[0].method]('PYLON LOG:', ...data[0].data.map(deserialize));
};
ws.onerror = console.error;
ws.onclose = () => workbenchWs(url);
}
fetch(`https://pylon.bot/api/deployments/${process.env.DEPLOYMENT_ID}`, {
method: 'POST',
headers: {
'Authorization': process.env.API_TOKEN,
'Content-Type': 'application/json',
},
body: JSON.stringify({
script: {
contents: fs.readFileSync('./dist/bundle.js', 'utf8'),
project: {
files: [{ path: '/main.ts', content: 'don\'t edit this lol' }],
},
},
}),
}).then((r) => r.json())
.then((v) => {
if (!v.workbench_url) {
return Promise.reject(v);
}
return v;
})
.then(({ workbench_url: workbenchEndpoint, errors }) => {
if (errors) {
errors.forEach((error) => {
console.error(new Error(error));
});
} else {
console.log('Published!');
}
workbenchWs(workbenchEndpoint);
})
.catch(console.error);