1 |
|
2 | var ConnectApp, connect, es, fs, http, https, liveReload, lr, opt, path, server, tiny_lr, util;
|
3 |
|
4 | path = require("path");
|
5 |
|
6 | es = require("event-stream");
|
7 |
|
8 | util = require("gulp-util");
|
9 |
|
10 | http = require("http");
|
11 |
|
12 | https = require("https");
|
13 |
|
14 | fs = require("fs");
|
15 |
|
16 | connect = require("connect");
|
17 |
|
18 | liveReload = require("connect-livereload");
|
19 |
|
20 | tiny_lr = require("tiny-lr");
|
21 |
|
22 | opt = {};
|
23 |
|
24 | server = void 0;
|
25 |
|
26 | lr = void 0;
|
27 |
|
28 | ConnectApp = (function() {
|
29 | function ConnectApp(options) {
|
30 | opt = options;
|
31 | opt.port = opt.port || "8080";
|
32 | opt.root = opt.root || path.dirname(module.parent.id);
|
33 | opt.host = opt.host || "localhost";
|
34 | opt.debug = opt.debug || false;
|
35 | if (opt.open) {
|
36 | this.oldMethod("open");
|
37 | }
|
38 | this.server();
|
39 | }
|
40 |
|
41 | ConnectApp.prototype.server = function() {
|
42 | var app;
|
43 | app = connect();
|
44 | this.middleware().forEach(function(middleware) {
|
45 | if (typeof middleware === "object") {
|
46 | return app.use(middleware[0], middleware[1]);
|
47 | } else {
|
48 | return app.use(middleware);
|
49 | }
|
50 | });
|
51 | if (opt.https != null) {
|
52 | server = https.createServer({
|
53 | key: opt.https.key || fs.readFileSync(__dirname + '/certs/server.key'),
|
54 | cert: opt.https.cert || fs.readFileSync(__dirname + '/certs/server.crt'),
|
55 | ca: opt.https.ca || fs.readFileSync(__dirname + '/certs/ca.crt'),
|
56 | passphrase: opt.https.passphrase || 'gulp'
|
57 | }, app);
|
58 | } else {
|
59 | server = http.createServer(app);
|
60 | }
|
61 | app.use(connect.directory(typeof opt.root === "object" ? opt.root[0] : opt.root));
|
62 | return server.listen(opt.port, (function(_this) {
|
63 | return function(err) {
|
64 | var sockets, stopServer, stoped;
|
65 | if (err) {
|
66 | return _this.log("Error on starting server: " + err);
|
67 | } else {
|
68 | _this.log("Server started http" + (opt.https != null ? 's' : '') + "://" + opt.host + ":" + opt.port);
|
69 | stoped = false;
|
70 | sockets = [];
|
71 | server.on('close', function() {
|
72 | if (!stoped) {
|
73 | stoped = true;
|
74 | return _this.log("Server stopped");
|
75 | }
|
76 | });
|
77 | server.on("connection", function(socket) {
|
78 | sockets.push(socket);
|
79 | return socket.on("close", function() {
|
80 | return sockets.splice(sockets.indexOf(socket), 1);
|
81 | });
|
82 | });
|
83 | server.on("request", function(request, response) {
|
84 | return _this.logDebug("Received request " + request.method + " " + request.url);
|
85 | });
|
86 | stopServer = function() {
|
87 | if (!stoped) {
|
88 | sockets.forEach(function(socket) {
|
89 | return socket.destroy();
|
90 | });
|
91 | server.close();
|
92 | return process.nextTick(function() {
|
93 | return process.exit(0);
|
94 | });
|
95 | }
|
96 | };
|
97 | process.on("SIGINT", stopServer);
|
98 | process.on("exit", stopServer);
|
99 | if (opt.livereload) {
|
100 | tiny_lr.Server.prototype.error = function() {};
|
101 | if (opt.https != null) {
|
102 | lr = tiny_lr({
|
103 | key: opt.https.key || fs.readFileSync(__dirname + '/certs/server.key'),
|
104 | cert: opt.https.cert || fs.readFileSync(__dirname + '/certs/server.crt')
|
105 | });
|
106 | } else {
|
107 | lr = tiny_lr();
|
108 | }
|
109 | lr.listen(opt.livereload.port);
|
110 | return _this.log("LiveReload started on port " + opt.livereload.port);
|
111 | }
|
112 | }
|
113 | };
|
114 | })(this));
|
115 | };
|
116 |
|
117 | ConnectApp.prototype.middleware = function() {
|
118 | var middleware;
|
119 | middleware = opt.middleware ? opt.middleware.call(this, connect, opt) : [];
|
120 | if (opt.livereload) {
|
121 | if (typeof opt.livereload === "boolean") {
|
122 | opt.livereload = {};
|
123 | }
|
124 | if (!opt.livereload.port) {
|
125 | opt.livereload.port = 35729;
|
126 | }
|
127 | middleware.push(liveReload({
|
128 | port: opt.livereload.port
|
129 | }));
|
130 | }
|
131 | if (typeof opt.root === "object") {
|
132 | opt.root.forEach(function(path) {
|
133 | return middleware.push(connect["static"](path));
|
134 | });
|
135 | } else {
|
136 | middleware.push(connect["static"](opt.root));
|
137 | }
|
138 | if (opt.fallback) {
|
139 | middleware.push(function(req, res) {
|
140 | return require('fs').createReadStream(opt.fallback).pipe(res);
|
141 | });
|
142 | }
|
143 | return middleware;
|
144 | };
|
145 |
|
146 | ConnectApp.prototype.log = function(text1) {
|
147 | this.text = text1;
|
148 | if (!opt.silent) {
|
149 | return util.log(util.colors.green(this.text));
|
150 | }
|
151 | };
|
152 |
|
153 | ConnectApp.prototype.logWarning = function(text1) {
|
154 | this.text = text1;
|
155 | if (!opt.silent) {
|
156 | return util.log(util.colors.yellow(this.text));
|
157 | }
|
158 | };
|
159 |
|
160 | ConnectApp.prototype.logDebug = function(text1) {
|
161 | this.text = text1;
|
162 | if (opt.debug) {
|
163 | return util.log(util.colors.blue(this.text));
|
164 | }
|
165 | };
|
166 |
|
167 | ConnectApp.prototype.oldMethod = function(type) {
|
168 | var text;
|
169 | text = 'does not work in gulp-connect v 2.*. Please read "readme" https://github.com/AveVlad/gulp-connect';
|
170 | switch (type) {
|
171 | case "open":
|
172 | return this.logWarning("Option open " + text);
|
173 | }
|
174 | };
|
175 |
|
176 | return ConnectApp;
|
177 |
|
178 | })();
|
179 |
|
180 | module.exports = {
|
181 | server: function(options) {
|
182 | if (options == null) {
|
183 | options = {};
|
184 | }
|
185 | return new ConnectApp(options);
|
186 | },
|
187 | reload: function() {
|
188 | return es.map(function(file, callback) {
|
189 | if (opt.livereload && typeof lr === "object") {
|
190 | lr.changed({
|
191 | body: {
|
192 | files: file.path
|
193 | }
|
194 | });
|
195 | }
|
196 | return callback(null, file);
|
197 | });
|
198 | },
|
199 | lr: lr,
|
200 | serverClose: function() {
|
201 | return server.close();
|
202 | }
|
203 | };
|