-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
56 lines (47 loc) · 1.48 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
'use strict';
var extend = require('extend-shallow');
var fs = require('fs');
var gutil = require('gulp-util');
var jsonServer = require('json-server');
var through = require('through2');
module.exports = function(options) {
options = extend({
hostname: 'localhost',
port: 3000,
watch: false
}, (options || {}));
var stream = through.obj(function(file, enc, callback) {
var server = jsonServer.create();
var router = jsonServer.router(file.path);
if (options.watch) {
fs.watch(file.path, function (event, changedFile) {
if (event === 'change') {
gutil.log(gutil.colors.magenta(file.path), 'has changed, reloading json-server database');
try {
router.db.object = JSON.parse(fs.readFileSync(file.path));
} catch (err) {
gutil.log('Unable to parse', gutil.colors.magenta(file.path));
gutil.log(err);
}
}
});
gutil.log('Watching', gutil.colors.magenta(file.path));
}
server.use(jsonServer.defaults)
.use(router)
.listen(options.port, options.hostname)
.on('listening', function() {
gutil.log('JSON server started at', gutil.colors.cyan('http://' + options.hostname + ':' + options.port));
})
.on('error', function(err) {
if (err.code === 'EADDRINUSE') {
throw new gutil.PluginError('gulp-json-server', 'Port ' + options.port + ' is already in use by another process.');
} else {
throw new gutil.PluginError('gulp-json-server', err);
}
});
this.push(file);
callback();
});
return stream;
};