forked from liabru/matter-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
313 lines (269 loc) · 9 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
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
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var header = require('gulp-header');
var eslint = require('gulp-eslint');
var bump = require('gulp-bump');
var changelog = require('gulp-conventional-changelog');
var tag = require('gulp-tag-version');
var release = require('gulp-github-release');
var sequence = require('run-sequence');
var gutil = require('gulp-util');
var replace = require('gulp-replace');
var webserver = require('gulp-webserver');
var concat = require('gulp-concat');
var preprocess = require('gulp-preprocess');
var browserify = require('browserify');
var derequire = require('gulp-derequire');
var transform = require('vinyl-transform');
var through2 = require('through2');
var pkg = require('./package.json');
var clone = require('gulp-clone');
var livereload = require('connect-livereload');
var es = require('event-stream');
var path = require('path');
var fs = require('fs');
var watchify = require('watchify');
var extend = require('util')._extend;
var exec = require('child_process').exec;
var buildDirectory = 'build';
var server;
gulp.task('default', ['build:dev']);
gulp.task('dev', function(callback) {
sequence('watch', 'serve', callback);
});
gulp.task('release', function(callback) {
shell('git status --porcelain', function(err, stdout) {
if (stdout && stdout.trim()) {
throw new gutil.PluginError({
plugin: 'release',
message: 'cannot build release as there are uncomitted changes'
});
} else {
sequence('test', 'bump', 'reload', 'build:release', 'doc', 'changelog', callback);
}
});
});
gulp.task('release:push', function(callback) {
shell('git status --porcelain', function(err, stdout) {
if (stdout && stdout.trim()) {
throw new gutil.PluginError({
plugin: 'release',
message: 'cannot push release as it has not yet been committed'
});
} else {
sequence('tag', 'release:push:git', 'release:push:npm', callback);
}
});
});
gulp.task('release:push:github', function(callback) {
return gulp.src([
'CHANGELOG.md',
'LICENSE',
buildDirectory + '/matter.min.js',
buildDirectory + '/matter.js'
]).pipe(release({
owner: 'liabru',
repo: pkg.name,
tag: pkg.version,
name: 'Matter.js ' + pkg.version
}));
});
gulp.task('release:push:git', function(callback) {
shell('git push', callback);
});
gulp.task('release:push:npm', function(callback) {
shell('npm publish', callback);
});
gulp.task('build:dev', function() {
return build(extend(extend({}, pkg), { version: pkg.version + '-dev' }));
});
gulp.task('build:edge', function() {
return build(extend(extend({}, pkg), { version: pkg.version + '-alpha' }));
});
gulp.task('build:release', function() {
return build(extend(extend({}, pkg), { version: pkg.version }));
});
gulp.task('build:examples', function() {
var options = extend(extend({}, pkg), { version: pkg.version + '-dev' });
options.name = options.name + '-examples';
options.date = options.date || new Date().toISOString().slice(0, 10);
options.author = '@liabru';
return gulp.src('examples/**/*.js')
.pipe(concat('examples.js'))
.pipe(header(banner, { context: options }))
.pipe(gulp.dest('examples/build'));
});
gulp.task('watch', function() {
var b = browserify({
entries: ['src/module/main.js'],
standalone: 'Matter',
plugin: [watchify]
});
var bundle = function() {
gutil.log('Updated bundle build/matter-dev.js');
b.bundle()
.on('error', function(err) {
gutil.log('ERROR', err.message);
this.emit('end');
})
.pipe(through2({ objectMode: true }, function(chunk, encoding, callback) {
return callback(
null,
chunk.toString().replace(/@@VERSION@@/g, pkg.version + '-dev')
);
}))
.pipe(fs.createWriteStream('build/matter-dev.js'));
};
b.on('update', bundle);
bundle();
});
gulp.task('bump', function() {
return gulp.src(['package.json', 'bower.json'])
.pipe(bump({ type: process.argv[4] || 'minor' }))
.pipe(gulp.dest('.'));
});
gulp.task('reload', function(callback) {
delete require.cache[require.resolve('./package.json')];
pkg = require('./package.json');
callback();
});
gulp.task('tag', function() {
return gulp.src('package.json')
.pipe(tag({ prefix: '' }));
});
gulp.task('changelog', function () {
return gulp.src('CHANGELOG.md')
.pipe(changelog())
.pipe(gulp.dest('.'));
});
gulp.task('serve', function() {
serve(false);
});
gulp.task('serve:test', function() {
serve(true);
});
gulp.task('serve:stop', function() {
if (server) {
try {
server.emit('kill');
} catch (e) {} // eslint-disable-line no-empty
gutil.log('Web server stopped');
}
});
gulp.task('test', function(callback) {
// TODO: fix tests by switching to nightmare instead of phantom
// sequence('serve:test', 'lint', 'build:dev', 'test:browser', 'test:node', 'serve:stop', callback);
sequence('lint', callback);
});
gulp.task('test:browser', function(callback) {
shell('phantomjs test/browser/TestDemo.js', callback);
});
gulp.task('test:node', function(callback) {
shell('node test/node/TestDemo.js', callback);
});
gulp.task('lint', function() {
return gulp.src([
'src/**/*.js',
'demo/js/*.js',
'examples/*.js',
'test/browser/TestDemo.js',
'test/node/TestDemo.js',
'Gulpfile.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('doc', function(callback) {
var options = {
paths: ['src'],
themedir: 'matter-doc-theme',
outdir: 'doc/build',
linkNatives: true,
project: {
name: pkg.name + ' ' + pkg.version + ' Physics Engine API Docs',
description: pkg.description,
version: pkg.version,
url: pkg.homepage
}
};
var Y = require('yuidocjs');
var json = new Y.YUIDoc(options).run();
json.project = options.project;
var builder = new Y.DocBuilder(options, json);
builder.compile(callback);
});
var serve = function(isTest) {
process.on('uncaughtException', function(err) {
if (err.errno === 'EADDRINUSE') {
gutil.log('Server already running (or port is otherwise in use)');
}
});
server = gulp.src('.')
.pipe(webserver({
host: '0.0.0.0',
livereload: {
enable: !isTest,
filter: function(filename) {
return filename.match(/build|demo/);
}
},
middleware: livereload(),
open: isTest ? false : 'http://localhost:8000/demo/index.html',
directoryListing: true
}));
};
var build = function(options) {
var isDev = options.version.indexOf('-dev') !== -1,
filename = buildDirectory + (isDev ? '/matter-dev' : '/matter'),
dest = filename + '.js',
destMin = filename + '.min.js';
options.date = options.date || new Date().toISOString().slice(0, 10);
options.author = '@liabru';
gutil.log('Building', filename, options.date);
var compiled = gulp.src(['src/module/main.js'])
.pipe(through2.obj(function(file, enc, next){
browserify(file.path, { standalone: 'Matter' })
.bundle(function(err, res){
file.contents = res;
next(null, file);
});
}))
.pipe(derequire())
.pipe(replace('@@VERSION@@', options.version));
if (!isDev) {
compiled.pipe(preprocess({ context: { DEBUG: false } }));
}
var build = compiled.pipe(clone())
.pipe(header(banner + '\n' + license + '\n\n', { context: options }))
.pipe(rename(dest))
.pipe(gulp.dest('.'));
var buildMin = compiled.pipe(clone())
.pipe(uglify({ output: { max_line_len: 1000 } }))
.pipe(header(banner, { context: options }))
.pipe(rename(destMin))
.pipe(gulp.dest('.'));
return es.merge(build, buildMin);
};
var shell = function(command, callback) {
var args = process.argv.slice(3).join(' '),
proc = exec(command + ' ' + args, function(err, stdout, stderr) {
callback(err, stdout, stderr, proc);
});
proc.stdout.on('data', function(data) {
process.stdout.write(data);
});
proc.stderr.on('data', function(data) {
process.stderr.write(data);
});
};
var license = fs.readFileSync('src/module/license.js');
var banner = [
'/**',
'* <%= context.name %> <%= context.version %> by <%= context.author %> <%= context.date %>',
'* <%= context.homepage %>',
'* License <%= context.license %>',
'*/',
''
].join('\n');