1 |
|
2 | var EventEmitter, yaml, fs, N, portReservation, ObservableProcess, path, ServiceRunner;
|
3 | EventEmitter = require('events').EventEmitter;
|
4 | yaml = require('js-yaml');
|
5 | fs = require('fs');
|
6 | N = require('nitroglycerin');
|
7 | portReservation = require('port-reservation');
|
8 | ObservableProcess = require('observable-process');
|
9 | path = require('path');
|
10 | ServiceRunner = (function(superclass){
|
11 | var prototype = extend$((import$(ServiceRunner, superclass).displayName = 'ServiceRunner', ServiceRunner), superclass).prototype, constructor = ServiceRunner;
|
12 | function ServiceRunner(name, config){
|
13 | this.name = name;
|
14 | this.config = config;
|
15 | this._log = bind$(this, '_log', prototype);
|
16 | this.start = bind$(this, 'start', prototype);
|
17 | this.serviceConfig = yaml.safeLoad(fs.readFileSync(path.join(this.config.root, 'config.yml'), 'utf8'));
|
18 | }
|
19 | prototype.start = function(done){
|
20 | var this$ = this;
|
21 | return portReservation.getPort(N(function(port){
|
22 | var x$;
|
23 | this$.config.SERVICE_NAME = this$.name;
|
24 | this$.config.EXORELAY_PORT = port;
|
25 | x$ = new ObservableProcess(this$._createCommand(this$.serviceConfig.startup.command), {
|
26 | cwd: this$.config.root,
|
27 | env: this$.config,
|
28 | verbose: true,
|
29 | console: {
|
30 | log: this$._log,
|
31 | error: this$._log
|
32 | }
|
33 | });
|
34 | x$.wait(this$.serviceConfig.startup['online-text'], function(){
|
35 | this$.emit('online', this$.name);
|
36 | return done();
|
37 | });
|
38 | return x$;
|
39 | }));
|
40 | };
|
41 | prototype._createCommand = function(template){
|
42 | var key, ref$, value;
|
43 | template = this.config.root + "/" + template;
|
44 | for (key in ref$ = this.config) {
|
45 | value = ref$[key];
|
46 | template = template.replace("{{" + key + "}}", value);
|
47 | }
|
48 | return template;
|
49 | };
|
50 | prototype._log = function(text){
|
51 | return this.emit('output', {
|
52 | name: this.name,
|
53 | text: text
|
54 | });
|
55 | };
|
56 | return ServiceRunner;
|
57 | }(EventEmitter));
|
58 | module.exports = ServiceRunner;
|
59 | function bind$(obj, key, target){
|
60 | return function(){ return (target || obj)[key].apply(obj, arguments) };
|
61 | }
|
62 | function extend$(sub, sup){
|
63 | function fun(){} fun.prototype = (sub.superclass = sup).prototype;
|
64 | (sub.prototype = new fun).constructor = sub;
|
65 | if (typeof sup.extended == 'function') sup.extended(sub);
|
66 | return sub;
|
67 | }
|
68 | function import$(obj, src){
|
69 | var own = {}.hasOwnProperty;
|
70 | for (var key in src) if (own.call(src, key)) obj[key] = src[key];
|
71 | return obj;
|
72 | } |
\ | No newline at end of file |