-
Notifications
You must be signed in to change notification settings - Fork 31
/
index-compat.js
268 lines (218 loc) · 9.21 KB
/
index-compat.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* The following JavaScript file was preprocessed from index.js via Babel for support on older Node installations. */
'use strict';
var _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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
(function _gulp_connect_php_module_scoping(OPTIONS_SPAWN_OBJ, OPTIONS_PHP_CLI_ARR) {
var childProcess = require('child_process');
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var http = require('http');
var open = require('opn');
var binVersionCheck = require('bin-version-check');
var fs = require('fs');
//let counter = 0;
function EnumSet() {
var _this = this;
[].concat(Array.prototype.slice.call(arguments)).forEach(function (x) {
_this[x] = Symbol(x);
});
}
var PhpDevelopmentServerConnection = function _PhpDevelopmentServerConnection_private_scope() {
var Status = new EnumSet('NEW', 'STARTING', 'STARTED', 'FINISHED');
/**
* Private: Check wherther the server is running.
* @param hostname
* @param port
* @param cb
*/
function checkServer(hostname, port, cb) {
var self = this;
//console.log(`[${this.counter}] checkServer`);
if (self.status !== Status.STARTING) return;
setTimeout(function _checkServer_fire() {
http.request({
method: 'HEAD',
hostname: hostname,
port: port
}, function _checkServer_httpCallback(res) {
var statusCodeType = Number(res.statusCode.toString()[0]);
if ([2, 3, 4].indexOf(statusCodeType) !== -1) {
return cb(true);
} else if (statusCodeType === 5) {
console.log('Server docroot returned 500-level response. Please check ' + 'your configuration for possible errors.');
return cb(true);
}
checkServer.call(self, hostname, port, cb);
}).on('error', function _checkServer_httpError(err) {
// back off after 1s
if (++self.checkServerTries > 20) {
console.log('PHP server not started. Retrying...');
return cb(false);
}
checkServer.call(self, hostname, port, cb);
}).end();
}, 15);
}
/**
* PHP Development Server Connection Instance
*
* {@link http://php.net/manual/en/features.commandline.webserver.php}
*/
var PhpDevelopmentServerConnection = function () {
/**
* Create a new Instance
* @param opts Default Options. Will be merged with our own internal set of default options. Can be overwridden in the connect ('server') call.
* @returns {PhpDevelopmentServerConnection}
*/
function PhpDevelopmentServerConnection(opts) {
_classCallCheck(this, PhpDevelopmentServerConnection);
//this.counter = ++counter;
//console.log(`[${this.counter}] constructor`);
this.status = Status.NEW;
this.checkServerTries = 0;
this.workingPort = 8000;
this.defaults = Object.assign({
port: 8000,
hostname: '127.0.0.1',
base: '.',
open: false,
bin: 'php',
root: '/',
stdio: 'inherit',
configCallback: null,
debug: false
}, opts || {});
return this; // `new` bug
}
/**
* 'Close'/Shutdown the PHP Development Server
* @param cb Optional single parameter Callback. Parameter is the return (if any) of the node `ChildProcess.kill(...)` call or nothing if not started.
*/
_createClass(PhpDevelopmentServerConnection, [{
key: 'closeServer',
value: function closeServer(cb) {
cb = cb || function _closeServerCb_noop() {};
//console.log(`[${this.counter}] closeServer`);
var self = this;
if (this.loading) {
setTimeout(function () {
return self.closeServer(cb);
}, 5);
return;
}
if (this.childProcess) {
cb(this.childProcess.kill('SIGKILL'));
this.status = Status.FINISHED;
return;
}
cb();
}
/**
* Get the port the server is running on.
* @returns {number|*} Port number.
*/
}, {
key: 'server',
/**
* Start the Server
* @param options Optional Server Options to overwrite the defaults in the CTor.
* @param cb Optional Callback for Completion. May pass in an error when there is a fault.
*/
value: function server(options, cb) {
//console.log(`[${this.counter}] server`);
cb = cb || function _serverCB_noop() {};
var self = this;
if (this.status !== Status.NEW && this.status !== Status.FINISHED) {
return cb(new Error('You may not start a server that is starting or started.'));
}
options = Object.assign({}, this.defaults, options);
this.workingPort = options.port;
var host = options.hostname + ':' + options.port;
var args = ['-S', host, '-t', options.base];
if (options.ini) {
args.push('-c', options.ini);
}
if (options.router) {
args.push(require('path').resolve(options.router));
}
if (options.debug) {
spawn = function _debugSpawn(outerSpawn) {
return function debugSpawnWrapper(file, args, options) {
console.log('Invoking Spawn with:');
console.log(file);
console.log(args);
console.log(options);
return outerSpawn(file, args, options);
};
}(spawn);
}
if (options.configCallback === null || options.configCallback === undefined) {
options.configCallback = function noOpConfigCallback(type, collection) {
return collection;
};
}
spawn = function _configCallbackSpawn(outerSpawn) {
return function configCallbackSpawnWrapper(file, spawnArgs, spawnOptions) {
return outerSpawn(file, options.configCallback(OPTIONS_PHP_CLI_ARR, spawnArgs) || spawnArgs, options.configCallback(OPTIONS_SPAWN_OBJ, spawnOptions) || spawnOptions);
};
}(spawn);
binVersionCheck('"' + options.bin + '"', '>=5.4', function _binVerCheck(err) {
if (err) {
cb(err);
return;
}
var checkPath = function _checkPath() {
var exists = fs.existsSync(options.base);
if (exists === true) {
self.status = Status.STARTING;
self.childProcess = spawn(options.bin, args, {
cwd: '.',
stdio: options.stdio
});
} else {
setTimeout(checkPath, 100);
}
};
checkPath();
// check when the server is ready. tried doing it by listening
// to the child process `data` event, but it's not triggered...
checkServer.call(self, options.hostname, options.port, function _server_checkServer() {
self.status = Status.STARTED;
if (options.open) {
open('http://' + host + options.root);
}
cb();
}.bind(this));
}.bind(this));
}
}, {
key: 'port',
get: function get() {
return this.workingPort;
}
}]);
return PhpDevelopmentServerConnection;
}();
return PhpDevelopmentServerConnection;
}();
module.exports = function _export_scoping() {
var returnStructure = PhpDevelopmentServerConnection;
var adopterBinder = function adopterBinder(adopter, inst, method) {
return adopter[method] = inst[method].bind(inst);
};
returnStructure.compat = function _naught_version_compatibility() {
// This is segregated beacuse in the future around v1.5 we will make it emit a warning.
// In v2.0 we will gut it completely.
var inst = new PhpDevelopmentServerConnection();
inst.OPTIONS_SPAWN_OBJ = OPTIONS_SPAWN_OBJ;
inst.OPTIONS_PHP_CLI_ARR = OPTIONS_PHP_CLI_ARR;
return inst;
}();
// You cannot actually bind a function to a method directly... so... lets manually bind to get a function that calls the right instance.
adopterBinder(returnStructure, returnStructure.compat, 'server');
adopterBinder(returnStructure, returnStructure.compat, 'closeServer');
returnStructure.OPTIONS_SPAWN_OBJ = OPTIONS_SPAWN_OBJ;
returnStructure.OPTIONS_PHP_CLI_ARR = OPTIONS_PHP_CLI_ARR;
return returnStructure;
}();
})('spawn', 'php_args');