-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
219 lines (165 loc) · 3.93 KB
/
gulpfile.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
var fs = require('fs');
var path = require('path');
var join = path.join;
var extname = path.extname;
var child_process = require('child_process');
var Batch = require('batch');
var gulp = require('gulp');
var symlink = require('gulp-symlink');
var conflict = require('gulp-conflict');
var template = require('gulp-template');
var inquirer = require('inquirer');
var _ = require('lodash');
var colors = require('colors');
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var Duo = require('duo');
var Watcher = require('duo-watch');
var lr = require('tiny-lr');
var build = write();
var buildAndRestart = write(function(entries) {
setTimeout(function() {
console.log('reload');
reload(entries);
}, 500);
});
Error.stackTraceLimit = Infinity;
var outRelative = 'public';
var options = {
watch: false,
dev: process.env.NODE_ENV === 'production' ? false : true,
outRelative: outRelative,
out: join(__dirname, outRelative),
root: __dirname,
entries: ['index.js', 'index.css']
}
console.log('options dev', options.dev);
gulp.task('unlink-lib', unlink);
gulp.task('public', makePublic);
/**
* enable `lib/...` requires shortcut
*/
gulp.task('link-lib', ['unlink-lib'], link);
gulp.task('app', function(fn) {
options.watch = true;
buildAndStartApp(options.entries);
});
gulp.task('build', ['public', 'assets'], function() {
build(options.entries);
});
gulp.task('dev', ['public', 'assets', 'link-lib'], function() {
startApp();
startReloadServer();
options.watch = true;
buildAndRestart(options.entries);
});
gulp.task('assets', ['public'], function() {
return gulp.src(['assets/**'])
.pipe(gulp.dest('public'));
});
/////////
// Duo //
/////////
function create(entry, cb) {
return Duo(options.root)
.entry(entry)
.development(options.dev)
.copy(!options.dev)
.buildTo(options.outRelative)
}
function write(onWrite) {
function _write(entries) {
entries = [].concat(entries);
var batch = new Batch;
var push = batch.push.bind(batch);
var duos = entries
.map(multiple)
.map(push);
batch.end(function(err) {
if (err) return error(err);
options.watch && watch(write)
onWrite && onWrite(entries);
});
function multiple(entry) {
return function(done) {
create(entry).run(function(err, src) {
if (err) {
console.log('build error', err);
return done();
}
var out = join(options.out, 'build' + extname(entry));
fs.writeFileSync(out, src);
var len = Buffer.byteLength(src);
console.log('wrote %dkb to %s', len / 1024 | 0, path.basename(out));
done();
});
};
}
}
var watching = false;
function watch(action) {
if (watching) return;
watching = true;
Watcher(options.root, 'lib/boot/index.js').watch(_write)
}
return _write;
}
////////////
// Reload //
////////////
var server = null;
function startReloadServer() {
server = lr();
server.listen(35729);
}
function reload() {
server.changed({
body: {
files: ['/build.js', '/build.css']
}
});
}
//////////
// App //
//////////
var app = null;
function startApp(cb) {
if(app) {
app.kill();
setTimeout(function() {
reload('server');
}, 500);
}
app = child_process.fork('app.js');
app.on('message', function(msg) {
if(msg === 'listening')
cb && cb();
});
}
//////////////////
// Folder setup //
//////////////////
function makePublic() {
try {
fs.mkdirSync('public');
} catch(e) {
}
}
function link() {
return gulp.src('lib')
.pipe(symlink('node_modules'));
}
function unlink() {
try {
fs.unlinkSync('node_modules/lib');
} catch(e) {
if(e.code !== 'ENOENT')
throw e;
}
}
process.on('uncaughtException', function(err) {
app && app.kill();
// Don't print out jshint error stacks
if(err.stack.indexOf('JSHint') === -1)
console.error(err.stack);
process.exit(-1);
});