-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathindex.js
144 lines (124 loc) · 3.79 KB
/
index.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
'use strict';
var stream = require('readable-stream');
var log = require('fancy-log');
var tinyLr = require('tiny-lr');
var relative = require('path').relative;
var _assign = require('lodash.assign');
var debug = require('debug')('gulp:livereload');
var magenta = require('chalk').magenta;
/**
* Global Options
* port Server Port
* host Server Host
* basePath base directory the path will be resolved to
* start start the server automatically
* quiet Enable/Disable debug logging
* reloadPage The page to reload upon issuing a full page reload
*/
var options = {
quiet: false,
reloadPage: 'index.html'
};
/**
* Create a stream for telling
* the livereload server about changes
*
* If opts isn't present then return
*
* @param {object|number} [opts]
* @param [opts.port] livereload server port
* @param [opts.host] livereload server host
* @param [opts.basePath] base directory the path will be resolved to
* @param [opts.start] automatically start the server
* @param [opts.quiet=false]
*/
module.exports = exports = function(opts) {
options = _assign(options, opts);
var glr = new stream.PassThrough({
objectMode: true,
});
glr.on('data', function(file) {
var filePath = file.path;
exports.changed(filePath);
});
if (options.start) exports.listen(options);
return glr;
};
// Note: This is a good way to directly set the settings once throughout
// the program, a reference to the global options
exports.options = options;
// A way to grab or change the underlying server instance
exports.server = undefined;
/**
* Express middleware
*
* A direct reference to the underlying servers middleware reference
*/
exports.middleware = tinyLr.middleware;
/**
* Start the livereload server
*
* If opts isn't present the global is used, if opts is a number its used as
* the port, otherwise as the config object
*
* @param {object|number} [opts]
* @param [opts.port] livereload server port
* @param [opts.host] livereload server host
* @param [opts.basePath] base directory the path will be resolved to
* @param [opts.start] automatically start the server
* @param [opts.quiet=false]
* @param {function} [cb] callback
*/
exports.listen = function(opts, cb) {
if (exports.server) return;
if (typeof opts === 'number') {
opts = { port: opts };
} else if (typeof opts === 'function') {
cb = opts;
opts = {};
}
options = _assign(options, opts);
exports.server = new tinyLr.Server(options);
exports.server.listen(options.port, options.host, function() {
debug('now listening on port %d', options.port);
if(typeof cb === 'function') cb.apply(exports.server, arguments);
});
};
/**
* Instruct the server that a file has changed
* and should be re-downloaded.
*
* The server must be running or this method will exit on error.
* If an object is given the "path" property is used, otherwise
* its a path string
*
* @param {string|object} filePath
*/
exports.changed = function (filePath) {
if (!exports.server) {
debug('no server listening, nothing notified.');
return;
}
if (typeof filePath === 'object') {
filePath = filePath.path;
}
if (options.basePath) {
filePath = '/' + relative(options.basePath, filePath);
}
exports.server.changed({ body: { files: [ filePath ] } });
if (!options.quiet) {
log(magenta(filePath) + ' reloaded.');
}
};
/**
* Invoke a full page reload, including all assets
*
* Path to the page in use must be given, If filePath isn't provided then the filePath will be used
* from the global config which is by default "index.html". The basePath will automatically be
* applied to this
*
* * @param {string} [filePath]
*/
exports.reload = function(filePath) {
exports.changed(filePath || options.reloadPage);
};