-
Notifications
You must be signed in to change notification settings - Fork 0
/
win-service.js
98 lines (86 loc) · 2.64 KB
/
win-service.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
var Service = require('node-windows').Service;
var jsonfile = require('jsonfile');
var async = require('async');
// Create a new service object
var svc = new Service({
name:'FoxWebAlarmPlayer',
description: 'The web server for FoxWebAlarmPlayer.',
script: __dirname + '\\main.js',
env: [
{name: "NODE_ENV", value: "production"},
{name: "NODE_CONFIG_DIR", value: __dirname + "\\config"}
]
});
if (process.argv[2] && process.argv[2] === '--uninstall'){
removeService(svc);
} else if (process.argv[2] && process.argv[2] === '--start'){
startService(svc, function(){});
} else if (process.argv[2] && process.argv[2] === '--stop'){
stopService(svc);
} else {
async.series([updateConfFile, function(done){ createService(svc, done); }]);
}
function updateConfFile(done){
console.log("Updating 'dbHost' key in config/production.json...");
jsonfile.readFile('./config/production.json', function(err, config){
if (err){
console.log(err);
process.exit(1);
}
config["dbHost"] = __dirname + "\\server\\data\\alarmplayer.db";
config["logFile"] = __dirname + "\\server\\data\\alarmplayer.log";
jsonfile.writeFile('./config/production.json', config, {spaces: 2}, (err) => {
if (err){
console.log("Unabl to update config file");
console.error(err);
process.exit(1);
} else {
console.log("Config file updated");
done();
}
})
});
}
function createService(service, done){
console.log('Installing service...')
service.on('install', function(){
console.log('Starting service...')
startService(service, done);
});
service.on('invalidinstallation', function(){
console.log('Invalid installation');
});
service.on('error', function(){
console.log('Error')
});
service.on('alreadyinstalled', function(){
console.log('Service already installed');
startService(service, done);
});
service.install();
}
function startService(service, done){
service.on('start', function(){
console.log('Service started.');
});
service.on('error', function(){
console.log('Error')
});
service.start();
done();
}
function stopService(service){
service.on('stop', function(){
console.log('Service stopped.');
});
service.on('error', function(){
console.log('Error')
});
service.stop();
}
function removeService(service){
service.on('uninstall',function(){
console.log('Uninstall complete.');
});
service.uninstall();
}