forked from BrowserSync/browser-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·372 lines (335 loc) · 10 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#! /usr/bin/env node
"use strict";
/**
* @module BrowserSync
*/
var pjson = require("./package.json");
var BrowserSync = require("./lib/browser-sync");
var publicUtils = require("./lib/public/public-utils");
var events = require("events");
var PassThrough = require("stream").PassThrough;
var logger = require("eazy-logger").Logger({
useLevelPrefixes: true
});
var singleton = false;
var singletonPlugins = [];
var instances = [];
/**
* @type {boolean|EventEmitter}
*/
var singletonEmitter = false;
module.exports = initSingleton;
/**
* Create a Browsersync instance
* @method create
* @param {String} name an identifier that can used for retrieval later
*/
module.exports.create = create;
/**
* Get a single instance by name. This is useful if you have your
* build scripts in separate files
* @method get
* @param {String} name
* @returns {Object|Boolean}
*/
module.exports.get = function (name) {
var instance = getSingle(name);
if (instance) {
return instance;
}
throw new Error("An instance with the name `%s` was not found.".replace("%s", name));
};
/**
* Check if an instance has been created.
* @method has
* @param {String} name
* @returns {Boolean}
*/
module.exports.has = function (name) {
var instance = getSingle(name);
if (instance) {
return true;
}
return false;
};
/**
* Start the Browsersync service. This will launch a server, proxy or start the snippet
* mode depending on your use-case.
* @method init
* @param {Object} [config] This is the main configuration for your Browsersync instance and can contain any of the [available options]({{site.links.options}})
* If you do not pass a config an argument for configuration, Browsersync will still run; but it will be in the `snippet` mode
* @param {Function} [cb] If you pass a callback function, it will be called when Browsersync has completed all setup tasks and is ready to use. This
* is useful when you need to wait for information (for example: urls, port etc) or perform other tasks synchronously.
* @returns {BrowserSync}
*/
module.exports.init = initSingleton;
/**
* Register a plugin. Must implement at least a 'plugin' method that returns a
* callable function.
*
* @method use
* @param {String} name The name of the plugin
* @param {Object} module The object to be `required`.
* @param {Function} [cb] A callback function that will return any errors.
*/
module.exports.use = function () {
var args = Array.prototype.slice.call(arguments);
singletonPlugins.push({
args: args
});
};
/**
* The `reload` method will inform all browsers about changed files and will either cause the browser to refresh, or inject the files where possible.
*
* @method reload
* @param {String|Array|Object} [arg] The file or files to be reloaded.
* @returns {*}
*/
module.exports.reload = noop("reload");
/**
* The `stream` method returns a transform stream and can act once or on many files.
*
* @method stream
* @param {Object} [opts] Configuration for the stream method
* @since 2.6.0
* @returns {*}
*/
module.exports.stream = noop("stream");
/**
* Helper method for browser notifications
*
* @method notify
* @param {String|HTML} msg Can be a simple message such as 'Connected' or HTML
* @param {Number} [timeout] How long the message will remain in the browser. @since 1.3.0
*/
module.exports.notify = noop("notify");
/**
* This method will close any running server, stop file watching & exit the current process.
*
* @method exit
*/
module.exports.exit = noop("exit");
/**
* Stand alone file-watcher. Use this along with Browsersync to create your own, minimal build system
* @method watch
* @param {string} patterns Glob patterns for files to watch
* @param {object} [opts] Options to be passed to Chokidar - check what's available in [their docs](https://github.com/paulmillr/chokidar#getting-started)
* @param {function} [fn] Callback function for each event.
* @since 2.6.0
*/
module.exports.watch = noop("watch");
/**
* Method to pause file change events
*
* @method pause
*/
module.exports.pause = noop("pause");
/**
* Method to resume paused watchers
*
* @method resume
*/
module.exports.resume = noop("resume");
/**
* Add properties fo
*/
Object.defineProperties(module.exports, {
/**
* The internal Event Emitter used by the running Browsersync instance (if there is one).
* You can use this to emit your own events, such as changed files, logging etc.
*
* @property emitter
*/
"emitter": {
get: function () {
if (!singletonEmitter) {
singletonEmitter = newEmitter();
return singletonEmitter;
}
return singletonEmitter;
}
},
/**
* A simple true/false flag that you can use to determine if there's a currently-running Browsersync instance.
*
* @property active
*/
"active": {
get: getSingletonValue.bind(null, "active")
},
/**
* A simple true/false flag to determine if the current instance is paused
*
* @property paused
*/
"paused": {
get: getSingletonValue.bind(null, "paused")
}
});
/**
* Event emitter factory
* @returns {EventEmitter}
*/
function newEmitter() {
var emitter = new events.EventEmitter();
emitter.setMaxListeners(20);
return emitter;
}
/**
* Get the singleton's emitter, or a new one.
* @returns {EventEmitter}
*/
function getSingletonEmitter() {
if (singletonEmitter) {
return singletonEmitter;
}
singletonEmitter = newEmitter();
return singletonEmitter;
}
/**
* Helper to allow methods to be called on the module export
* before there's a running instance
* @param {String} name
* @returns {Function}
*/
function noop(name) {
return function () {
var args = Array.prototype.slice.call(arguments);
if (singleton) {
return singleton[name].apply(singleton, args);
} else {
if (publicUtils.isStreamArg(name, args)) {
return new PassThrough({objectMode: true});
}
}
};
}
/**
* Create a single instance when module export is used directly via browserSync({});
* This is mostly for back-compatibility, for also for the nicer api.
* This will never be removed to ensure we never break user-land, but
* we should discourage it's use.
* @returns {*}
*/
function initSingleton() {
var instance;
if (instances.length) {
instance = instances.filter(function (item) {
return item.name === "singleton";
});
if (instance.length) {
logger.error("{yellow:You tried to start Browsersync twice!} To create multiple instances, use {cyan:browserSync.create().init()");
return instance;
}
}
var args = Array.prototype.slice.call(arguments);
singleton = create("singleton", getSingletonEmitter());
if (singletonPlugins.length) {
singletonPlugins.forEach(function (obj) {
singleton.instance.registerPlugin.apply(singleton.instance, obj.args);
});
}
singleton.init.apply(null, args);
return singleton;
}
/**
* @param {String} prop
* @returns {Object|Boolean}
*/
function getSingletonValue(prop) {
var single = getSingle("singleton");
if (single) {
return single[prop];
}
return false;
}
/**
* Get a single instance by name
* @param {String} name
* @returns {Object|Boolean}
*/
function getSingle(name) {
if (instances.length) {
var match = instances.filter(function (item) {
return item.name === name;
});
if (match.length) {
return match[0];
}
}
return false;
}
/**
* Create an instance of Browsersync
* @param {String} [name]
* @param {EventEmitter} [emitter]
* @returns {{init: *, exit: (exit|exports), notify: *, reload: *, cleanup: *, emitter: (Browsersync.events|*), use: *}}
*/
function create(name, emitter) {
name = name || new Date().getTime();
emitter = emitter || newEmitter();
var browserSync = new BrowserSync(emitter);
var instance = {
name: name,
instance: browserSync,
exit: require("./lib/public/exit")(browserSync),
notify: require("./lib/public/notify")(browserSync),
pause: require("./lib/public/pause")(browserSync),
resume: require("./lib/public/resume")(browserSync),
reload: require("./lib/public/reload")(emitter),
stream: require("./lib/public/stream")(emitter),
cleanup: browserSync.cleanup.bind(browserSync),
use: browserSync.registerPlugin.bind(browserSync),
getOption: browserSync.getOption.bind(browserSync),
emitter: browserSync.events,
watch: require("./lib/file-watcher").watch
};
browserSync.publicInstance = instance;
instance.init = require("./lib/public/init")(browserSync, name, pjson);
Object.defineProperty(instance, "active", {
get: function () {
return browserSync.active;
}
});
Object.defineProperty(instance, "paused", {
get: function () {
return browserSync.paused;
}
});
/**
* Access to client-side socket for emitting events
*
* @property sockets
*/
Object.defineProperty(instance, "sockets", {
get: function () {
if (!browserSync.active) {
return {
emit: function () {},
on: function () {}
};
} else {
return browserSync.io.sockets;
}
}
});
instances.push(instance);
return instance;
}
/**
* Reset the state of the module.
* (should only be needed for test environments)
*/
module.exports.reset = function () {
instances.forEach(function (item) {
item.cleanup();
});
instances = [];
singletonPlugins = [];
singletonEmitter = false;
singleton = false;
};
/**
* @type {Array}
*/
module.exports.instances = instances;