-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindowsService.js
More file actions
164 lines (134 loc) · 3.29 KB
/
WindowsService.js
File metadata and controls
164 lines (134 loc) · 3.29 KB
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
// -----------------------------------------------------------------------------
// Dependencies
// -----------------------------------------------------------------------------
var path = require('path');
var Service = require('node-windows').Service;
var parseArgs = require('minimist')
// -----------------------------------------------------------------------------
// Initialization
// -----------------------------------------------------------------------------
// Create service object
var svc = new Service({
name: 'PollBot',
description: 'HipChat bot for simple question polls and restaurant polls.',
script: path.join(__dirname, 'PollBot.js'),
});
// Parse arguments
var argv = parseArgs(process.argv.slice(2))
var validArg = false;
var reinstallFlag = false;
var restartFlag = false;
// Install
if (argv.install) {
validArg = true;
install();
}
// Uninstall
else if (argv.uninstall) {
validArg = true;
uninstall();
}
// Reinstall / update
else if (argv.reinstall || argv.update) {
validArg = true;
reinstallFlag = true;
uninstall();
}
// Start
else if (argv.start) {
validArg = true;
start();
}
// Stop
else if (argv.stop) {
validArg = true;
stop();
}
// Restart
else if (argv.restart) {
validArg = true;
stop();
restartFlag = true;
}
// Invalid arguments
else if (validArg === false) {
var usage = `Invalid arguments.
Usage:
node WindowsService.js --install
node WindowsService.js --uninstall
node WindowsService.js --reinstall
node WindowsService.js --update
node WindowsService.js --start
node WindowsService.js --stop
node WindowsService.js --restart`
console.log(usage);
}
// -----------------------------------------------------------------------------
// Event handlers
// -----------------------------------------------------------------------------
svc.on('install', function() {
console.log('Install complete!');
if (svc.exists) {
console.log('Starting service...');
svc.start();
}
});
svc.on('alreadyinstalled', function() {
console.log('Service is already installed.');
console.log('Starting service...');
svc.start();
});
svc.on('uninstall', function() {
console.log('Uninstall complete!');
if (reinstallFlag) {
install();
}
});
svc.on('start', function() {
console.log('Service up and running!');
});
svc.on('stop', function() {
console.log('Service stopped!');
if (restartFlag) {
start();
}
});
svc.on('error', function() {
console.log('An error occurred!');
});
// -----------------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------------
function install() {
// Safeguard
if (svc.exists) {
console.log('Service already installed, nothing to do.!');
return;
}
console.log('Installing service...');
svc.install();
}
function uninstall() {
if (!svc.exists) {
console.log('Service does not exist, nothing to do.');
return;
}
console.log('Uninstalling service...');
svc.uninstall();
}
function start() {
// Safeguard
if (!svc.exists) {
console.log('Service does not exist, install it first!');
return;
}
svc.start();
}
function stop() {
// Safeguard
if (!svc.exists) {
console.log('Service does not exist, install it first!');
return;
}
svc.stop();
}