UNPKG

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