/
gulpfile.babel.js
271 lines (246 loc) · 5.82 KB
/
gulpfile.babel.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
import _ from 'lodash';
import args from 'yargs';
import browserify from 'browserify';
import buffer from 'vinyl-buffer';
import cache from 'gulp-cached';
import debug from 'gulp-debug';
import del from 'del';
import es from 'gulp-babel';
import fs from 'fs';
import globArray from 'glob-array';
import gulp from 'gulp';
import gutil from 'gulp-util';
import path from 'path';
import ptime from 'pretty-hrtime';
import runSequence from 'run-sequence';
import source from 'vinyl-source-stream';
import sourcemap from 'gulp-sourcemaps';
import ts from 'gulp-typescript';
import tslint from 'gulp-tslint';
import txtjs from 'gulp-txtjs';
import typedoc from 'gulp-typedoc';
import watchify from 'watchify';
import exec from 'gulp-exec';
let buildAllFinished = () => {};
gulp.task('default', ['build']);
gulp.task('build-all', ['build', 'build-test']);
/**
* build gr.js
*/
gulp.task('build', () => {
runSequence(['ts-es6'], ['copy-json', 'txt-es5', 'es6-es5'], 'bundle');
});
/**
* build test
*/
gulp.task('build-test', ['test-txt-es5', 'test-es6-es5']);
/**
* watch
*/
gulp.task('watch', () => {
enableWatch();
runSequence('build');
});
gulp.task('test', () => {
if (watching) {
gulp.watch("./src/**/*.ts", ['ts-es6', 'test']);
}
gulp.src('./').pipe(exec("ava ./test-es5/**/*Test.js"));
});
let watching = false;
function enableWatch() {
watching = true;
gutil.log(gutil.colors.green('Watch Mode Enabled'));
}
if (args.argv.watch || args.argv.w) {
enableWatch();
}
/**
* Transpile ts to es6
*/
const tsProject = ts.createProject('tsconfig.json');
gulp.task('ts-es6', () => {
const entry = './src/**/*.ts';
const dest = './lib';
if (watching) {
gulp.watch(entry, ['ts-es6']);
}
return gulp
.src(entry)
.pipe(sourcemap.init())
.pipe(ts(tsProject))
.js
.pipe(cache('ts'))
.pipe(sourcemap.write())
.pipe(debug({
title: 'Compiling ts'
}))
.pipe(gulp.dest(dest));
});
/**
* Expose text file to js modules
*/
gulp.task('txt-es5', () => {
const entryExtensions = ['.html', '.css', '.glsl', '.xmml', '.rsml', '.xml'];
const entry = entryExtensions.map((ext) => `./src/**/*${ext}`);
const dest = './lib-es5';
if (watching) {
gulp.watch(entry, ['txt-es5']);
}
return txtToEs5(entry, dest);
});
gulp.task('test-txt-es5', () => {
const entryExtensions = ['.html', '.css', '.glsl', '.xmml', '.rsml', '.xml', '.goml'];
const entry = entryExtensions.map((ext) => `./test/**/*${ext}`);
const dest = './test-es5';
if (watching) {
gulp.watch(entry, ['test-txt-es5']);
}
return txtToEs5(entry, dest);
});
function txtToEs5(entry, dest) {
return gulp
.src(entry)
.pipe(cache('txt'))
.pipe(txtjs())
.pipe(debug({
title: 'Compiling txt'
}))
.pipe(gulp.dest(dest));
}
/**
* Copy
*/
gulp.task('copy-json', () => {
const entry = './src/**/*.json';
const dest = './lib-es5';
if (watching) {
gulp.watch(entry, ['copy-json']);
}
return copy(entry, dest);
});
function copy(entry, dest) {
return gulp
.src(entry)
.pipe(debug({
title: 'Copying'
}))
.pipe(gulp.dest(dest));
}
/**
* Transpile es6 to es5
*/
gulp.task('es6-es5', () => {
const entry = './lib/**/*.js';
const dest = './lib-es5';
if (watching) {
gulp.watch(entry, ['es6-es5']);
}
return es6to5(entry, dest);
});
gulp.task('test-es6-es5', () => {
const entry = './test/**/*.js';
const dest = './test-es5';
if (watching) {
gulp.watch(entry, ['test-es6-es5']);
}
return es6to5(entry, dest);
});
function es6to5(entry, dest) {
return gulp
.src(entry)
.pipe(cache('es'))
.pipe(sourcemap.init({
loadMaps: true
}))
.pipe(es())
.pipe(sourcemap.write())
.pipe(debug({
title: 'Compiling es'
}))
.pipe(gulp.dest(dest));
}
/**
* bundle with browserify
*/
gulp.task('bundle', () => {
const entry = './lib-es5/Grimoire.js';
const dest = './product';
const opt = {
entries: entry,
debug: true,
cache: {},
packageCache: {},
};
const b = browserify(opt);
if (watching) {
b.plugin(watchify, {
delay: 100,
ignoreWatch: ['**/node_modules/**'],
});
}
function bundle() {
const time = process.hrtime();
gutil.log('Bundling...');
return b
.bundle()
.pipe(source('gr.js'))
.pipe(buffer())
.pipe(sourcemap.init({
loadMaps: true
}))
.pipe(sourcemap.write('./'))
.pipe(gulp.dest(dest))
.on('end', () => {
gutil.log('Finished bundling ' + gutil.colors.magenta(ptime(process.hrtime(time))));
});
}
if (watching) {
b.on('update', bundle);
}
return bundle();
});
/**
* doc
*/
gulp.task('doc', () => {
const entry = './src/**/*.ts';
const dest = 'ci/docs';
const branch = args.argv.branch;
const outPath = path.join(dest, branch || 'unknown');
const jsonPath = path.join(dest, `${branch}.json`);
return gulp.src('./')
.pipe(exec(`typedoc --out ${outPath} --json ${jsonPath} --name Grimoire --target es6 ${entry}`));
});
/**
* lint
*/
gulp.task('lint-ts', () => {
const entry = ['!./src/typings/**/*.ts', './src/**/*.ts', '!./src/refs/**/*.ts', '!./src/bundle-notdoc.ts'];
gulp
.src(entry)
.pipe(tslint({
configuration: './tslint.json',
rulesDirectory: './lint/rules/',
}))
.pipe(tslint.report('verbose'));
});
/**
* tsconfig
*/
gulp.task('tscfg', () => {
const entry = './tsconfig.json';
const json = JSON.parse(fs.readFileSync(entry));
const files = _(globArray.sync(json.filesGlob)).uniq(true);
json.files = files;
fs.writeFileSync(entry, JSON.stringify(json, null, 2));
});
/**
* clean
*/
gulp.task('clean', () => {
const temp = ['./product/gr.js', './product/gr.js.map', './src/**/*.js', './lib', './lib-es5', './test-es5', './coverage', './ci'];
del(temp).then((paths) => {
paths.forEach((p) => gutil.log(`deleted: \"${p}\"`));
});
});