server.js
2.94 KB
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
import Promise from 'bluebird';
import browserify from 'browserify';
import budo from 'budo';
import fs from 'fs';
import glob from 'glob';
import mkdirp from 'mkdirp';
import path from 'path';
/* eslint no-console: 0 */
const pkg = require(path.join(__dirname, '../package.json'));
// Replace "%s" tokens with the plugin name in a string.
const nameify = (str) =>
str.replace(/%s/g, pkg.name.split('/').reverse()[0]);
const srces = {
js: 'src/plugin.js',
tests: glob.sync('test/**/*.test.js')
};
const dests = {
js: nameify('dist/%s.js'),
tests: 'test/dist/bundle.js'
};
const bundlers = {
js: browserify({
debug: true,
entries: [srces.js],
standalone: nameify('%s'),
transform: [
'babelify'
]
}),
tests: browserify({
debug: true,
entries: srces.tests,
transform: [
'babelify',
'browserify-shim'
]
})
};
const bundle = (name) => {
return new Promise((resolve, reject) => {
bundlers[name]
.bundle()
.pipe(fs.createWriteStream(dests[name]))
.on('finish', resolve)
.on('error', reject);
});
};
mkdirp.sync('dist');
// Start the server _after_ the initial bundling is done.
Promise.all([bundle('js'), bundle('tests')]).then(() => {
const server = budo({
port: 9999,
stream: process.stdout
}).on('reload', (f) => console.log('reloading %s', f || 'everything'));
/**
* A collection of functions which are mapped to strings that are used to
* generate RegExp objects. If a filepath matches the RegExp, the function
* will be used to handle that watched file.
*
* @type {Object}
*/
const handlers = {
/**
* Handler for JavaScript source.
*
* @param {String} event
* @param {String} file
*/
'^src/.+\.js$'(event, file) {
console.log('re-bundling javascript and tests');
Promise.all([bundle('js'), bundle('tests')]).then(() => server.reload());
},
/**
* Handler for JavaScript tests.
*
* @param {String} event
* @param {String} file
*/
'^test/.+\.test\.js$'(event, file) {
console.log('re-bundling tests');
bundle('tests').then(() => server.reload());
}
};
/**
* Finds the first handler function for the file that matches a RegExp
* derived from the keys.
*
* @param {String} file
* @return {Function|Undefined}
*/
const findHandler = (file) => {
const keys = Object.keys(handlers);
for (let i = 0; i < keys.length; i++) {
let regex = new RegExp(keys[i]);
if (regex.test(file)) {
return handlers[keys[i]];
}
}
};
server
.live()
.watch([
'index.html',
'src/**/*.js',
'test/**/*.test.js',
'test/index.html'
])
.on('watch', (event, file) => {
const handler = findHandler(file);
console.log(`detected a "${event}" event in "${file}"`);
if (handler) {
handler(event, file);
} else {
server.reload();
}
});
});