forked from ioBroker/ioBroker.vis-google-fonts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (68 loc) · 2.16 KB
/
main.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
/**
*
* ioBroker vis-google-fonts Adapter
*
* Copyright 2015-2022 bluefox<[email protected]>
*
* OFL License
*
*/
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const writeFile = require('./lib/install.js');
const adapterName = require('./package.json').name.split('.').pop();
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: adapterName,
ready: () => checkFiles()
});
adapter = new utils.Adapter(options);
return adapter;
}
function upload(callback) {
adapter.log.info(`Upload ${adapter.name} anew, while changes detected...`);
const file = utils.controllerDir + '/lib/setup.js';
const child = require('child_process').spawn('node', [file, 'upload', adapter.name, 'widgets']);
let count = 0;
child.stdout.on('data', data => {
count++;
adapter.log.debug(data.toString().replace('\n', ''));
if ((count % 100) === 0) {
adapter.log.info(count + ' files uploaded...');
}
});
child.stderr.on('data', data =>
adapter.log.error(data.toString().replace('\n', '')));
child.on('exit', exitCode => {
adapter.log.info('Uploaded.');
callback(exitCode);
});
}
// Update google-fonts.html
function checkFiles() {
writeFile(adapter, changed => {
if (changed) {
upload(async () => {
adapter.log.info('Changes in widgets/google-fonts.html detected => restart vis');
const obj = await adapter.getForeignObjectAsync('system.adapter.vis.0');
await adapter.getForeignObjectAsync('system.adapter.vis.0', obj);
adapter.stop();
});
} else {
adapter.stop();
}
});
}
// If started as allInOne mode => return function to create instance
// @ts-ignore
if (module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}