-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
129 lines (103 loc) · 2.77 KB
/
utils.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
"use strict";
var commands = {
help: null,
nodaemon: null,
reload: null,
restart: null,
start: null,
status: null,
stop: null,
};
var hooks = {
argv: null,
help: null,
kill: null,
reload: null,
running: null,
start: null,
starting: null,
status: null,
stop: null,
syntax: null,
term: null,
wait: null,
};
function _init() {
if(! this.daemon)
throw new Error("DaemonControl: missing daemon parameter");
if("function" !== typeof this.daemon)
throw new Error("DaemonControl: daemon parameter is not a function");
if(! this.filename)
throw new Error("DaemonControl: missing filename parameter");
if("string" !== typeof this.filename)
throw new Error("DaemonControl: filename parameter is not a string");
if("undefined" === typeof this.options)
this.options = {};
else
if("object" !== typeof this.options)
throw new Error("DaemonControl: options parameter is not an object");
this._init_options();
}
function _init_options() {
var i;
if(! this.options.hooks)
this.hooks = {};
else {
this.hooks = this.options.hooks;
delete this.options.hooks;
if("object" !== typeof this.hooks)
throw new Error("DaemonControl: options.hooks is not an object");
}
for(i in this.hooks) {
if(! (i in hooks))
throw new Error("DaemonControl: unknow hook options.hooks." + i);
if("function" !== typeof this.hooks[i])
throw new Error("DaemonControl: options.hooks." + i + " is not a function");
}
if("reload" in this.options) {
this.reload = this.options.reload;
delete this.options.reload;
}
if("timeout" in this.options) {
this.timeout = parseInt(this.options.timeout, 10);
delete this.options.timeout;
if(isNaN(this.timeout))
throw new Error("DaemonControl: timeout parameter is not an integer number");
if(this.timeout <= 0)
throw new Error("DaemonControl: timeout parameter is not a non zero positive integer number");
}
else
this.timeout = 5;
this._init_spawn();
}
function _init_spawn() {
var i;
if(! ("cwd" in this.options))
this.options.cwd = process.cwd();
else
if("string" !== typeof this.options.cwd)
throw new Error("DaemonControl: cwd option is not a string");
if(! ("env" in this.options)) {
this.options.env = {};
for(i in process.env)
this.options.env[i] = process.env[i];
}
else
if("object" !== typeof this.options.env)
throw new Error("DaemonControl: env option is not an object");
if(! ("stdio" in this.options))
this.options.stdio = "ignore";
if(! ("detached" in this.options))
this.options.detached = true;
}
function _write(msg) {
process.stdout.write(msg);
}
module.exports = {
_init: _init,
_init_options: _init_options,
_init_spawn: _init_spawn,
_write: _write,
commands: commands,
hooks: hooks,
};