-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-host.js
More file actions
executable file
·161 lines (135 loc) · 3.7 KB
/
Copy pathjs-host.js
File metadata and controls
executable file
·161 lines (135 loc) · 3.7 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
#!/usr/bin/env node
'use strict';
var argv = require('yargs')
.usage('Usage: $0 <file>')
.demand(1)
.example('$0 host.config.js', 'Start a host using the specified config file')
.option('p', {
alias: 'port',
description: 'Run the host or manager at the specified port'
})
.option('c', {
alias: 'config',
description: 'Read in the config file and output its generated config as JSON'
})
.option('j', {
alias: 'json',
description: 'Once the process has started, output its generated config as JSON'
})
.option('m', {
alias: 'manager',
description: 'Run a manager process'
})
.option('d', {
alias: 'detached',
description: 'Run in a detached process'
})
.option('l', {
alias: 'logfile',
description: 'Adds a logger file transport that writes to the specified file'
})
.version(function() {
return require('../package').version;
}).alias('v', 'version')
.help('h').alias('h', 'help')
.strict()
.argv;
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var absolutePath = require('absolute-path'); // node 0.10.x support
var tmp = require('tmp');
var _ = require('lodash');
var winston = require('winston');
var Host = require('../lib/Host');
var Manager = require('../lib/Manager');
var configFile = argv._[0];
if (!absolutePath(configFile)) {
configFile = path.join(process.cwd(), configFile);
}
var config = require(configFile);
if (!_.isObject(config) || _.isEqual({}, config)) {
throw Error('Config file does not export an object. "' + configFile + '"');
}
var runAsDetachedProcess = false;
if (argv.detached) {
argv.j = argv.json = true;
runAsDetachedProcess = true;
}
var server;
if (argv.manager) {
server = new Manager(config);
} else {
server = new Host(config);
}
if (argv.port !== undefined) {
config.port = argv.port;
}
if (argv['logfile']) {
var logFile = argv.logfile;
if (!_.isString(logFile)) {
throw new Error('-l --logfile should only be specified once');
}
if (!absolutePath(logFile)) {
logFile = path.join(process.cwd(), logFile);
}
server.logger.add(winston.transports.File, {
filename: logFile,
handleExceptions: true,
colorize: true,
timestamp: true,
prettyPrint: true,
showLevel: true,
json: false
});
}
if (argv.config) {
var status = JSON.stringify(server.getStatus());
return console.log(status);
}
var onListen;
if (argv.json) {
config.outputOnListen = false;
onListen = function(server) {
console.log(JSON.stringify(server.getStatus()));
};
}
if (!runAsDetachedProcess) {
return server.listen(onListen);
}
// Run the host/manager in a detached process which is called
// with a similar command + argument combination as this process
// was. The detached process writes its stdout and stderr to temp
// files which we poll for content and look for the process's
// expected output before exiting this process
var outputFile = tmp.tmpNameSync();
var command = _.first(process.argv);
var args = _.rest(process.argv);
args = _.without(args, '-d', '--detached');
args.push('--json');
var detached = child_process.spawn(
command,
args,
{
detached: true,
stdio: ['ignore', fs.openSync(outputFile, 'a'), fs.openSync(outputFile, 'a')]
}
);
var expectedOutput = JSON.stringify(server.getStatus());
var pollDelay = 100;
var pollOutput = function() {
var output;
try {
output = fs.readFileSync(outputFile).toString().trim();
} catch(err) {}
if (output) {
if (output === expectedOutput) {
process.stdout.write(output);
return detached.unref();
}
process.stderr.write(output);
return detached.kill();
}
setTimeout(pollOutput, pollDelay);
};
setTimeout(pollOutput, pollDelay);