-
Notifications
You must be signed in to change notification settings - Fork 13
/
shelly-builder.js
501 lines (405 loc) · 14.5 KB
/
shelly-builder.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
* shelly-porssisahko
*
* (c) Jussi isotalo - http://jisotalo.fi
* https://github.com/jisotalo/shelly-porssisahko
*
* License: GNU Affero General Public License v3.0
*
* This file is used for the build/minify process only.
* NOTE: work-in-progress
*
* Usage:
* npm run build : builds and creates files to ./dist
* npm start : runs build and then uploads codes to shelly
* npm serve : serves static file from ./src/statics at local http server port 3000
* npm debug : starts listening to UDP data at port 8001
*/
const fs = require('fs').promises;
const path = require('path');
const UglifyJS = require("uglify-js");
const minify = require('html-minifier').minify;
const zlib = require('node:zlib');
const { promisify } = require('node:util');
const dgram = require('node:dgram');
//Settings
const BASE_URL = "http://192.168.68.100";
const RPC_URL = `${BASE_URL}/rpc`;
const MAX_CODE_CHUNK_SIZE = 1024;
const GZIP = true;
const UDP_DEBUG_PORT = 8001;
const MAX_STATIC_FILE_SIZE = -1; //Max file size served with shelly HTTP in bytes
const MAX_SCRIPT_SIZE = -1; //Max script size allowed in bytes
const log = (...args) => {
let time = new Date().toISOString();
time = time.substring(time.indexOf("T") + 1);
console.log(`${time}:`, ...args);
}
/**
* Returns all available scripts
* @returns
*/
const getScripts = async () => {
log(`getScripts(): Reading all scripts from shelly...`);
const res = await fetch(`${RPC_URL}/Script.List`);
if (res.status !== 200) {
throw new Error(`getScripts(): Failed to read all scripts: ${JSON.stringify(await res.json())}`)
}
const scripts = (await res.json()).scripts;
log(`getScripts(): Scripts read - found ${scripts.length} scripts`);
return scripts;
}
/**
* Returns script id by given script name
*
* If not found -> throws an error!
* @param {*} name
* @returns
*/
const getScriptId = async (name) => {
log(`getScriptId(): Reading scripts to find script named "${name}"...`);
const scripts = await getScripts();
const found = scripts.find(s => s.name.toLowerCase() === name.toLowerCase());
if (found) {
log(`getScriptId(): Found script "${found.name}" (id: ${found.id})`);
return Number(found.id);
}
log(`getScriptId(): Script "${name}" was not found`);
throw new Error(`getScriptId(): Script "${name}" was not found`);
}
/**
* Creates a new script with given name
* @param {*} name
*/
const createScript = async (name) => {
log(`createScript(): Creating script with name ${name}`);
const res = await fetch(`${RPC_URL}/Script.Create?name="${encodeURI(name)}"`);
if (res.status !== 200) {
throw new Error(`createScript(): Failed to create script "${name}": ${JSON.stringify(await res.json())}`)
}
log(`createScript(): Script "${name}" created: ${JSON.stringify(await res.json())}`);
}
/**
* Deletes script by given script name or id
* @param {*} name name or id
*/
const deleteScript = async (nameOrId, throwIfNotFound = false) => {
if (typeof nameOrId === 'string') {
try {
nameOrId = await getScriptId(nameOrId);
} catch (err) {
if (throwIfNotFound) {
throw err;
}
return;
}
}
log(`deleteScript(): Deleting script with id ${nameOrId}`);
const res = await fetch(`${RPC_URL}/Script.Delete?id=${nameOrId}`);
if (res.status !== 200) {
throw new Error(`deleteScript(): Failed to delete script with id ${nameOrId}: ${JSON.stringify(await res.json())}`)
}
log(`deleteScript(): Script with id ${nameOrId} deleted: ${JSON.stringify(await res.json())}`);
}
/**
* Starts script by given script name or id
* @param {*} name name or id
*/
const startScript = async (nameOrId) => {
if (typeof nameOrId === 'string') {
nameOrId = await getScriptId(nameOrId);
}
log(`startScript(): Starting script with id ${nameOrId}`);
const res = await fetch(`${RPC_URL}/Script.Start?id=${nameOrId}`);
if (res.status !== 200) {
throw new Error(`startScript(): Failed to start script with id ${nameOrId}: ${JSON.stringify(await res.json())}`)
}
log(`startScript(): Script with id ${nameOrId} started: ${JSON.stringify(await res.json())}`);
}
/**
* Sets script enable to false/true
* @param {*} name
*/
const setScriptEnable = async (nameOrId, enable) => {
if (typeof nameOrId === 'string') {
nameOrId = await getScriptId(nameOrId);
}
log(`setScriptEnable(): Setting script with id ${nameOrId} to enable: ${enable}`);
const res = await fetch(`${RPC_URL}/Script.SetConfig?id=${nameOrId}&config={"enable":${enable}}`);
if (res.status !== 200) {
throw new Error(`setScriptEnable(): Failed to set script with id ${nameOrId} enable to ${enable}: ${JSON.stringify(await res.json())}`)
}
log(`setScriptEnable(): Script with id ${nameOrId} enable set to ${enable}: ${JSON.stringify(await res.json())}`);
}
/**
* Uploads file contents to a script with given name
* @param {*} name
* @param {*} filePath
*/
const uploadScript = async (name, id, filePath) => {
log(`uploadScript(): Starting upload of script "${name}" with id ${id} from file "${filePath}"`);
const data = await fs.readFile(filePath);
let pos = 0;
let firstTime = true;
while (pos < data.byteLength) {
const codeChunk = data.subarray(pos, pos + MAX_CODE_CHUNK_SIZE);
//log(`uploadScript(): Uploading script "${name}" ${pos}...${pos + codeChunk.byteLength}/${data.byteLength}... (${RPC_URL}/Script.PutCode?id=${id}&code="${encodeURIComponent(codeChunk.toString())}"&append=${!firstTime})`);
log(`uploadScript(): Uploading script "${name}" ${pos}...${pos + codeChunk.byteLength}/${data.byteLength}...`);
const res = await fetch(`${RPC_URL}/Script.PutCode?id=${id}&code="${encodeURIComponent(codeChunk.toString())}"&append=${!firstTime}`);
if (res.status !== 200) {
throw new Error(`uploadScript(): Failed to upload script chunk ${pos}...${pos + codeChunk.byteLength}/${data.byteLength}: ${JSON.stringify(await res.text())}`)
}
log(`uploadScript(): Uploading script "${name}" ${pos}...${pos + codeChunk.byteLength}/${data.byteLength} successful: ${JSON.stringify(await res.json())}"`);
pos += codeChunk.byteLength;
firstTime = false;
}
log(`uploadScript(): Upload of script "${name}" with id ${id} from file "${filePath} done!"`);
}
/**
* Minifies the script file and writes to ./dist directory
* @param {*} filePath
* @param {*} distPath
*/
const createDistFile = async (filePath, distPath, isShellyScript) => {
log(`createDistFile(): Minifying script "${filePath}"...`);
let data = (await fs.readFile(filePath)).toString();
if (isShellyScript) {
let staticFileMatches = data.matchAll(/\#\[(.*)\]/gm);
for (const staticFileMatch of staticFileMatches) {
data = data.replace(staticFileMatch[0], ((await fs.readFile(path.join('./dist/statics', staticFileMatch[1]))).toString()));
}
}
let outputBuffer = Buffer.alloc(0);
let gzippedBuffer = Buffer.alloc(0);
if (filePath.endsWith(".js")) {
const minified = UglifyJS.minify(data, {
toplevel: true,
output: {
comments: "some" //leaving @license comment
},
mangle: {
toplevel: false, //if mangling toplevel, we get some rrors
reserved: [
//Some issues atm. with global constants -> setting so that names will not be mangled
"CNST",
"_" //Keeping state variable name as _ for user scripts
]
},
compress: {
pure_funcs: [
'DBG',
'me'
],
unsafe: true
},
});
if (minified.error) {
log(`createDistFile(): Minifying script "${filePath}" failed: ${minified.error}`);
throw new Error(`createDistFile(): Minifying script "${filePath}" failed: ${minified.error}`);
}
outputBuffer = minified.code;
if (isShellyScript) {
outputBuffer += "\n//end";
}
if (GZIP && !isShellyScript) {
const gzip = promisify(zlib.gzip);
const zippedData = await gzip(outputBuffer);
gzippedBuffer = Buffer.from(zippedData.toString('base64'), 'utf8');
}
log(`createDistFile(): Minifying script "${filePath}" done - size is now ${(100.0 - (minified.code.length / data.length * 100.0)).toFixed(0)}% smaller`);
} else if (filePath.endsWith(".html") || filePath.endsWith(".css")) {
const minified = minify(data, {
removeAttributeQuotes: true,
preserveLineBreaks: false,
removeTagWhitespace: true,
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeScriptTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true,
removeRedundantAttributes: true,
html5: true,
});
let trimmedData = minified.replace(/[\n\r]/g, '');
trimmedData = trimmedData.replace(/\s+/g, " ");
/*
if (filePath.endsWith(".css")) {
trimmedData = trimmedData.replaceAll("{ ", "{");
trimmedData = trimmedData.replaceAll(" {", "{");
trimmedData = trimmedData.replaceAll(" }", "}");
trimmedData = trimmedData.replaceAll("} ", "}");
trimmedData = trimmedData.replaceAll(": ", ":");
trimmedData = trimmedData.replaceAll("; ", ";");
}
*/
outputBuffer = Buffer.from(trimmedData, 'utf8');
if (GZIP) {
const gzip = promisify(zlib.gzip);
const zippedData = await gzip(outputBuffer);
gzippedBuffer = Buffer.from(zippedData.toString('base64'), 'utf8');
}
} else {
outputBuffer = Buffer.from(data, 'utf8');
}
//log(data);
//log(minified.code);
log(`createDistFile(): Creating dist file for "${filePath}" done`);
await fs.writeFile(distPath, GZIP && !isShellyScript ? gzippedBuffer : outputBuffer);
if (GZIP && !isShellyScript) {
const fileExt = path.extname(distPath);
await fs.writeFile(distPath.replace(fileExt, `.nozip${fileExt}`), outputBuffer);
}
}
/**
* Uploads script file/s() from ./dist to the Shelly
* Orders by name
*/
const upload = async (files = []) => {
if (!files || files.length === 0) {
files = await fs.readdir('./dist', { recursive: false });
}
for (let file of files.sort()) {
const distPath = path.join('./dist', file);
const fileInfo = await fs.stat(distPath);
if (!fileInfo.isFile()) {
continue;
}
await deleteScript(file);
await createScript(file);
const id = await getScriptId(file);
await uploadScript(file, id, distPath);
await setScriptEnable(id, true);
await startScript(id);
}
log(`All files uploaded!`);
printStats();
}
/**
* Builds and creates ./dist files from all ./src files
*/
const build = async (files = []) => {
await fs.rm('./dist', { recursive: true, force: true });
await fs.mkdir('./dist');
await fs.mkdir('./dist/statics');
//Static files (build all)
let staticFiles = await fs.readdir('./src/statics', { recursive: false });
for (let file of staticFiles.sort()) {
const filePath = path.join('./src/statics', file);
const distPath = path.join('./dist/statics', file);
const fileInfo = await fs.stat(filePath);
if (!fileInfo.isFile()) {
continue;
}
await createDistFile(filePath, distPath, false);
}
//Shelly files (build all or provided ones)
if (!files || files.length === 0) {
files = await fs.readdir('./src', { recursive: false });
}
for (let file of files.sort()) {
const filePath = path.join('./src', file);
const distPath = path.join('./dist', file);
const fileInfo = await fs.stat(filePath);
if (!fileInfo.isFile()) {
continue;
}
await createDistFile(filePath, distPath, true);
}
log(`All files built!`);
printStats();
}
const printStats = async () => {
log('--------------------------------------------------');
log('Static files:');
log('');
let statics = await fs.readdir('./dist/statics', { recursive: false });
for (let file of statics.sort()) {
if (file.includes(".nozip.")) {
continue;
}
const distPath = path.join('./dist/statics', file);
const fileInfo = await fs.stat(distPath);
if (!fileInfo.isFile()) {
continue;
}
log(` ${file}`);
if (MAX_STATIC_FILE_SIZE > 0 && MAX_STATIC_FILE_SIZE - fileInfo.size < 100) {
log(` ${(fileInfo.size / 1024.0).toFixed(1)} KB\t${(MAX_STATIC_FILE_SIZE - fileInfo.size)} bytes left (used ${(fileInfo.size / MAX_STATIC_FILE_SIZE * 100.0).toFixed(0)} %) <---- WARNING!!!`);
} else if (MAX_STATIC_FILE_SIZE > 0) {
log(` ${(fileInfo.size / 1024.0).toFixed(1)} KB\t${(MAX_STATIC_FILE_SIZE - fileInfo.size)} bytes left (used ${(fileInfo.size / MAX_STATIC_FILE_SIZE * 100.0).toFixed(0)} %)`);
} else {
log(` ${(fileInfo.size / 1024.0).toFixed(1)} KB`);
}
}
log('--------------------------------------------------');
let files = await fs.readdir('./dist', { recursive: false });
log('File stats:');
log('');
let count = 0;
for (let file of files.sort()) {
const distPath = path.join('./dist', file);
const fileInfo = await fs.stat(distPath);
if (!fileInfo.isFile()) {
continue;
}
log(` ${file}`);
if (MAX_SCRIPT_SIZE > 0) {
log(` ${(fileInfo.size / 1024.0).toFixed(1)} KB\t${(MAX_SCRIPT_SIZE - fileInfo.size)} bytes left (used ${(fileInfo.size / MAX_SCRIPT_SIZE * 100.0).toFixed(0)} %)`);
} else {
log(` ${(fileInfo.size / 1024.0).toFixed(1)} KB`);
}
count++;
}
log('');
log(`Scripts in use ${count}/3`);
log('--------------------------------------------------');
}
const uploadAndBuildAll = async () => {
await buildAll();
await uploadAll();
}
const listenUdp = async () => {
fs.unlink("./log.txt").catch(err => { });
const socket = dgram.createSocket('udp4');
socket.addListener('message', (msg, rinfo) => {
let str = msg.toString('utf-8');
str = str.substring(str.indexOf("|") + 1);
str = str.substring(str.indexOf(" ") + 1);
str = str.trim();
if (str.includes("1 clears ")) {
return;
}
log(str);
let time = new Date().toISOString();
time = time.substring(time.indexOf("T") + 1);
fs.appendFile("./log.txt", `${time}: ${str} \n`).catch();
});
socket.bind(UDP_DEBUG_PORT, () => {
log(`Listening to UDP port ${UDP_DEBUG_PORT}`);
});
}
//Handling command line parameters
const args = process.argv.splice(2);
switch (args[0]) {
case '--buildAll':
build();
break;
case '--build':
build([args[1]]);
break;
case '--uploadAll':
upload();
break;
case '--upload':
upload([args[1]]);
break;
case '--debug':
listenUdp();
break;
case undefined:
default:
console.log(`Error: Unknown command-line arguments: ${args}`);
}
//