UNPKG

3.94 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.7.1
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 server.listen(opt.port);
58 this.log("Server started http://" + opt.host + ":" + opt.port);
59 if (opt.livereload) {
60 tiny_lr.Server.prototype.error = function() {};
61 if (opt.https != null) {
62 lr = tiny_lr({
63 key: opt.https.key || fs.readFileSync(__dirname + '/certs/server.key'),
64 cert: opt.https.cert || fs.readFileSync(__dirname + '/certs/server.crt')
65 });
66 } else {
67 lr = tiny_lr();
68 }
69 lr.listen(opt.livereload.port);
70 return this.log("LiveReload started on port " + opt.livereload.port);
71 }
72 };
73
74 ConnectApp.prototype.middleware = function() {
75 var middleware;
76 middleware = opt.middleware ? opt.middleware.call(this, connect, opt) : [];
77 if (opt.livereload) {
78 if (typeof opt.livereload === "boolean") {
79 opt.livereload = {};
80 }
81 if (!opt.livereload.port) {
82 opt.livereload.port = 35729;
83 }
84 middleware.push(liveReload({
85 port: opt.livereload.port
86 }));
87 }
88 if (typeof opt.root === "object") {
89 opt.root.forEach(function(path) {
90 return middleware.push(connect["static"](path));
91 });
92 } else {
93 middleware.push(connect["static"](opt.root));
94 }
95 if (opt.fallback) {
96 middleware.push(function(req, res) {
97 return require('fs').createReadStream(opt.fallback).pipe(res);
98 });
99 }
100 return middleware;
101 };
102
103 ConnectApp.prototype.log = function(text) {
104 this.text = text;
105 if (!opt.silent) {
106 return util.log(util.colors.green(this.text));
107 }
108 };
109
110 ConnectApp.prototype.logWarning = function(text) {
111 this.text = text;
112 if (!opt.silent) {
113 return util.log(util.colors.yellow(this.text));
114 }
115 };
116
117 ConnectApp.prototype.oldMethod = function(type) {
118 var text;
119 text = 'does not work in gulp-connect v 2.*. Please read "readme" https://github.com/AveVlad/gulp-connect';
120 switch (type) {
121 case "open":
122 return this.logWarning("Option open " + text);
123 }
124 };
125
126 return ConnectApp;
127
128})();
129
130module.exports = {
131 server: function(options) {
132 if (options == null) {
133 options = {};
134 }
135 return new ConnectApp(options);
136 },
137 reload: function() {
138 return es.map(function(file, callback) {
139 if (opt.livereload && typeof lr === "object") {
140 lr.changed({
141 body: {
142 files: file.path
143 }
144 });
145 }
146 return callback(null, file);
147 });
148 },
149 serverClose: function() {
150 return server.close();
151 }
152};