-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomusto.js
executable file
·383 lines (278 loc) · 9.91 KB
/
domusto.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
#!/usr/bin/env node
/*
* DOMUSTO Home Automation Manager script
*
* Author: Bas van Dijk
*
*/
let util = require('util');
let spawn = require('child_process').spawn;
let spawnSync = require('child_process').spawnSync;
let fs = require('fs');
let jsonfile = require('jsonfile');
let fetchUrl = require('fetch').fetchUrl;
const pluginFolder = './src/domusto-plugins';
switch (process.argv[2]) {
case 'start':
spawn('npm', ['run', 'start'], { stdio: ['inherit', 'inherit', 'inherit'] });
break;
case 'dev':
spawn('npm', ['run', 'dev'], { stdio: ['inherit', 'inherit', 'inherit'] });
break;
case 'upgrade':
// Get the latest DOMUSTO server version from GitHub
spawnSync('git', ['pull'], { stdio: ['inherit', 'inherit', 'inherit'] });
// Install all of the new version's dependencies
spawnSync('npm', ['install'], { stdio: ['inherit', 'inherit', 'inherit'] });
// Upgrade all DOMUSTO plugins
pluginUpgrade();
// Reinstall all plugin dependencies
pluginReInstallDeps();
break;
case 'plugin':
switch (process.argv[3]) {
case 'add':
if (!process.argv[4]) {
console.log('Usage: no github plugin name specified');
process.exit()
}
repoClone(process.argv[4]);
break;
case 'remove':
if (!process.argv[4]) {
console.log('Usage: no github plugin name specified');
process.exit()
}
pluginRemove(process.argv[4]);
break;
case 'upgrade':
pluginUpgrade();
pluginReInstallDeps();
break;
case 'list':
pluginList();
break;
case 'installed':
pluginListInstalled();
break;
case 'install-deps':
pluginReInstallDeps();
break;
default:
console.log('Usage: domusto plugin add <DOMUSTO plugin name>');
console.log('Usage: domusto plugin remove <DOMUSTO repo name>');
break;
}
break;
default:
console.log(`
-------------------------------------------------------------------------------
🔈 💡 🚨 ⛅ ️🌡 🔌 🏠 DOMUSTO Home Automation manager 🏠 🔌 🌡️ ⛅ 🚨 💡 🔈
-------------------------------------------------------------------------------
DOMUSTO
Use ctrl+c to stop server
npm run start start server
npm run dev start server with live refresh on .ts changes
./domusto.js upgrade upgrade DOMUSTO
PLUGIN COMMANDS:
Example: ./domusto.js plugin add basvdijk/domusto-marantz
./domusto.js plugin add [REPO] install plugin by Git repo name
./domusto.js plugin remove [REPO] remove plugin by Git repo name
./domusto.js plugin upgrade upgrade all installed DOMUSTO plugins
./domusto.js plugin install-deps re-install all plugin dependencies
./domusto.js plugin list list all available DOMUSTO plugins
./domusto.js plugin installed list all installed DOMUSTO plugins
-------------------------------------------------------------------------------
`);
break;
}
/**
* Upgrades installed plugins
*
*/
function pluginUpgrade() {
let plugins = fs.readdirSync(pluginFolder);
// Remove all non-directories
plugins = plugins.filter(plugin => fs.statSync(`${pluginFolder}/${plugin}`).isDirectory());
console.log('Upgrading all DOMUSTO server plugins...');
for (plugin of plugins) {
repoUpgrade(`${pluginFolder}/${plugin}`);
}
}
/**
* Lists installed plugins
*
*/
function pluginListInstalled() {
let plugins = fs.readdirSync(pluginFolder);
console.log('Currently installed DOMUSTO plugins:')
for (pluginName of plugins) {
let packageJson = `${pluginFolder}/${pluginName}/package.json`;
jsonfile.readFile(packageJson, function (err, obj) {
if (obj) {
console.log(` ├ ${obj.name}`);
for (package in obj.dependencies) {
console.log(` | └ ${package}@${obj.dependencies[package]}`);
}
}
});
}
}
function pluginList() {
fetchUrl('https://api.github.com/search/repositories?q=topic:domusto-plugin+domusto', {
headers: {
accept: 'Accept: application/vnd.github.json'
}
}, function(error, meta, body){
console.log('\nCurrent available DOMUSTO plugins:\n')
for (plugin of JSON.parse(body).items) {
let date = new Date(plugin.updated_at);
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
let month = date.getMonth()+1 < 10 ? '0' + (date.getMonth()+1) : date.getMonth()+1;
let dateString = `${day}-${month}-${date.getFullYear()}`;
console.log(`\x1b[33m${plugin.full_name}\x1b[0m - ${plugin.html_url}
Author: ${plugin.owner.login}
Last update: ${dateString}
Description: ${plugin.description}
`);
}
console.log(`Use:
./domusto.js plugin add [REPO NAME]
to install a DOMUSTO plugin
`);
});
// curl -H "Accept: application/vnd.github.json" https://api.github.com/search/repositories?q=topic:domusto-plugin
}
/**
* Remove installed plugin
*
* @param {any} pluginRepo Github repository name e.g. basvdijk/domusto-marantz
* @returns
*/
function pluginRemove(pluginRepo) {
let pluginName = pluginRepo.split('/').reverse()[0];
if (!fs.existsSync(`${pluginFolder}/${pluginName}`)) {
warning(`Cannot remove DOMUSTO plugin '${pluginName}', the plugin is not installed`);
} else {
let rm = spawn('rm', ['-r', `${pluginFolder}/${pluginName}`], { stdio: ['inherit', 'inherit', 'inherit'] });
rm.on('exit', function (code) {
success(`Successfully removed DOMUSTO plugin ${pluginName}`);
});
return rm;
}
}
/**
* Install dependencies from package.json
*
* @param {any} pluginName Name of the plugin e.g. domusto-marantz
*/
function getDependencies(pluginName) {
let packageJson = `${pluginFolder}/${pluginName}/package.json`;
let packages = [];
let obj = jsonfile.readFileSync(packageJson);
for (package in obj.dependencies) {
packages.push(`${package}@${obj.dependencies[package]}`);
}
return packages;
}
/**
* Install npm packages
*
* @param {any} packages Array with packages to install
*/
function installNpmPackages(packages) {
log('Installing plugin dependencies:');
log(packages.join(' '));
let npm = spawn('npm', ['install', '--no-save', ...packages], { stdio: ['inherit', 'inherit', 'inherit'] });
npm.on('exit', function (code) {
if (code === 0) {
success('Done installing dependencies');
} else {
console.log('child process exited with code ' + code);
}
});
}
/**
* Upgrade repo to latest version
*
* @param {any} pluginRepo Repository path e.g. src/plugins/domusto-marantz
*/
function pluginReInstallDeps(pluginRepo) {
let plugins = fs.readdirSync(pluginFolder).filter(function (file) {
return (file.indexOf('.AppleDouble') === -1) && (file.indexOf('.MD') === -1);
});
let dependencies = new Set();
for (plugin of plugins) {
dependencies = [...new Set([...dependencies, ...getDependencies(plugin)])];
}
installNpmPackages(dependencies);
}
/**
* Clone repository in plugin folder
*
* @param {any} pluginRepo Repository of plugin on Git e.g. basvdijk/domusto-marantz
* @returns
*/
function repoClone(pluginRepo) {
log(`Installing DOMUSTO plugin ${pluginRepo}`);
let repo = `https://github.com/${pluginRepo}.git`;
let pluginName = pluginRepo.split('/').reverse()[0];
if (fs.existsSync(`${pluginFolder}/${pluginName}`)) {
warning(`Cannot add DOMUSTO plugin '${pluginRepo}', the plugin is already installed`);
} else {
let git = spawn('git', ['clone', repo, `${pluginFolder}/${pluginName}`], { stdio: ['inherit', 'inherit', 'inherit'] });
git.on('exit', function (code) {
if (code === 0) {
let packages = getDependencies(pluginName);
if (packages.length > 0) {
installNpmPackages(packages);
}
} else {
console.log('child process exited with code ' + code);
}
});
return git;
}
}
/**
* Upgrade repo to latest version
*
* @param {any} pluginRepo Repository path e.g. src/plugins/domusto-marantz
*/
function repoUpgrade(pluginRepo) {
const repoName = pluginRepo.split('/').reverse()[0];
console.log('');
console.log(`Upgrading ${repoName}...`);
process.chdir(pluginRepo);
let git = spawnSync('git', ['pull', '--verbose'], { stdio: ['inherit', 'inherit', 'inherit'] });
process.chdir('../../..');
if (git.error) {
error(`${repoName} upgrade failed`);
}
}
function debug(...args) {
Array.prototype.unshift.call(args);
console.log.apply(this, args);
}
function log(...args) {
Array.prototype.unshift.call(args);
console.log.apply(this, args);
}
function warning(...args) {
Array.prototype.unshift.call(args, '\x1b[33m' + args[0]);
Array.prototype.splice.call(args, 1, 1);
Array.prototype.push.call(args, '\x1b[0m');
console.log.apply(this, args);
}
function success(...args) {
Array.prototype.unshift.call(args, '\x1b[32m' + args[0]);
Array.prototype.splice.call(args, 1, 1);
Array.prototype.push.call(args, '\x1b[0m');
console.log.apply(this, args);
}
function error(...args) {
Array.prototype.unshift.call(args, '\x1b[31m' + args[0]);
Array.prototype.splice.call(args, 1, 1);
Array.prototype.push.call(args, '\x1b[0m');
console.log.apply(this, args);
}