UNPKG

3.92 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _nodeFetch = require('node-fetch');
10
11var _nodeFetch2 = _interopRequireDefault(_nodeFetch);
12
13var _child_process = require('child_process');
14
15var _promisePoller = require('promise-poller');
16
17var _promisePoller2 = _interopRequireDefault(_promisePoller);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23var Target = function () {
24 function Target(config) {
25 _classCallCheck(this, Target);
26
27 this.process = null;
28
29 this.config = {
30 verbose: config.verbose,
31 command: config.command,
32 cwd: config.cwd,
33 checkUrl: config.checkUrl,
34 checkInterval: config.checkInterval || 1000,
35 checkTimeout: config.checkTimeout || 1000 * 60 * 5
36 };
37
38 if (!this.config.command) {
39 throw new Error('No command or file set to run target');
40 }
41
42 if (!this.config.checkUrl) {
43 throw new Error('No url set to check target');
44 }
45 }
46
47 _createClass(Target, [{
48 key: 'start',
49 value: function start() {
50 var _this = this;
51
52 return this.check().then(function () {
53 return _this.alreadyRunning();
54 }).catch(function () {
55 return _this.buildTarget();
56 });
57 }
58 }, {
59 key: 'stop',
60 value: function stop() {
61 if (this.process) {
62 this.process.stdin.pause();
63 this.process.kill('SIGINT');
64 }
65 }
66 }, {
67 key: 'check',
68 value: function check() {
69 if (this.config.verbose) {
70 console.log('Checking target at: ' + this.config.checkUrl);
71 }
72 return (0, _nodeFetch2.default)(this.config.checkUrl);
73 }
74 }, {
75 key: 'alreadyRunning',
76 value: function alreadyRunning() {
77 if (this.config.verbose) {
78 console.log('Target already running');
79 }
80 return Promise.resolve();
81 }
82 }, {
83 key: 'startProcess',
84 value: function startProcess() {
85 var splitted = this.config.command.split(' ');
86 var command = splitted[0];
87 var args = splitted.slice(1);
88 var options = {
89 cwd: this.config.cwd
90 };
91 this.process = (0, _child_process.spawn)(command, args, options);
92 }
93 }, {
94 key: 'buildTarget',
95 value: function buildTarget() {
96 var _this2 = this;
97
98 return new Promise(function (resolve, reject) {
99 if (_this2.config.verbose) {
100 console.log('Building target...');
101 }
102
103 _this2.startProcess();
104
105 _this2.process.on('close', function (code) {
106 reject(new Error('Could not build Target: ' + code));
107 });
108
109 if (_this2.config.verbose) {
110 _this2.process.stdout.pipe(process.stdout);
111 _this2.process.stderr.pipe(process.stderr);
112 }
113
114 (0, _promisePoller2.default)({
115 taskFn: function taskFn() {
116 return _this2.check();
117 },
118 interval: _this2.config.checkInterval,
119 timeout: _this2.config.checkTimeout,
120 // timeout: this.config.checkTimeout,
121 retries: _this2.config.checkTimeout / _this2.config.checkInterval }).then(resolve, reject);
122 });
123 }
124 }]);
125
126 return Target;
127}();
128
129exports.default = Target;
\No newline at end of file