forked from fin-hypergrid/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
262 lines (233 loc) · 7.7 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
'use strict';
var gulp = require('gulp'),
$$ = require('gulp-load-plugins')(),
runSequence = require('run-sequence'),
browserSync = require('browser-sync').create(),
exec = require('child_process').exec,
path = require('path'),
pipe = require('multipipe');
var name = 'fin-hypergrid',
srcDir = './src/',
testDir = './test/',
jsFiles = '**/*.js',
addOnsDir = './add-ons/',
demoDir = './demo/',
buildDir = demoDir + 'build/';
var replacementFinHypergrid = 'window.fin.Hypergrid.$2$1$2';
// // // // // // // // // // // //
function gulpTaskAddOn(name, exportName) {
gulp.task('browserify-add-on-' + name, browserify.bind(null,
name,
addOnsDir + name + '/',
buildDir + addOnsDir,
exportName
));
}
gulp.task('lint', lint);
gulp.task('test', test);
gulp.task('doc', doc);
gulp.task('beautify', beautify);
gulp.task('images', swallowImages);
gulp.task('browserify', browserify.bind(null,
name,
srcDir,
buildDir
));
gulp.task('browserify-demo', browserify.bind(null,
'index',
'./demo/js/demo/',
'./demo/js/demo/build/'
));
gulp.task('reloadBrowsers', reloadBrowsers);
gulp.task('serve', browserSyncLaunchServer);
gulp.task('add-ons', addOns);
gulp.task('css-templates', function() {
return templates('./css/*.css', 'css');
});
gulp.task('build', function(callback) {
clearBashScreen();
runSequence(
'lint',
'images',
'css-templates',
'test',
'add-ons',
//'beautify',
'browserify',
'browserify-demo',
//'doc',
callback
);
});
gulp.task('watch', function () {
gulp.watch([
addOnsDir + jsFiles,
srcDir + '**', '!' + srcDir + 'jsdoc/**',
'./css/*.css',
'./html/*.html',
demoDir + 'js/' + jsFiles, '!' + demoDir + 'js/demo/build/' + jsFiles,
testDir + '**'
], [
'build'
]);
gulp.watch([
demoDir + '*.html',
demoDir + 'css/demo.css',
buildDir + '*'
], [
'reloadBrowsers'
]);
});
gulp.task('default', ['build', 'watch'], browserSyncLaunchServer);
// // // // // // // // // // // //
function lint() {
return gulp.src([
'index.js',
addOnsDir + jsFiles,
srcDir + jsFiles, '!' + srcDir + '**/old/**/',
demoDir + 'js/' + jsFiles, '!' + demoDir + 'js/demo/build/' + jsFiles,
testDir + jsFiles,
])
.pipe($$.excludeGitignore())
.pipe($$.eslint())
.pipe($$.eslint.format())
.pipe($$.eslint.failAfterError());
}
function test(cb) {
return gulp.src(testDir + jsFiles)
.pipe($$.mocha({reporter: 'spec'}));
}
function beautify() {
return gulp.src([srcDir + jsFiles, testDir + jsFiles])
.pipe($$.beautify()) //apparent bug: presence of a .jsbeautifyrc file seems to force all options to their defaults (except space_after_anon_function which is forced to true) so I deleted the file. Any needed options can be included here.
.pipe(gulp.dest(srcDir));
}
function browserify(name, srcDir, buildDir, exportName) {
var exportsRegExp = exportName && new RegExp('module\\.exports(\\s*=\\s*)(' + exportName + ')');
return gulp.src(srcDir + 'index.js')
// .pipe($$.replace(
// /require\('fin-hypergrid\/src\/(.*?)'\)/g,
// function(match, p1) { console.log('hi');return 'window.fin.Hypergrid.' + p1.replace(/\//g, '.'); }
// ))
.pipe(
$$.mirror(
pipe(
$$.rename(name + '.js'),
$$.browserify({ debug: true })
.on('error', $$.util.log),
$$.replace(exportsRegExp, replacementFinHypergrid)
),
pipe(
$$.rename(name + '.min.js'),
$$.browserify(),
$$.uglify()
.on('error', $$.util.log),
$$.replace(exportsRegExp, replacementFinHypergrid)
)
)
)
.pipe(gulp.dest(buildDir));
}
function doc(cb) {
exec(path.resolve('jsdoc.sh'), function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
}
function browserSyncLaunchServer() {
browserSync.init({
server: {
// Serve up our build folder
baseDir: demoDir
},
port: 9000
});
}
function reloadBrowsers() {
browserSync.reload();
}
function clearBashScreen() {
var ESC = '\x1B';
console.log(ESC + 'c'); // (VT-100 escape sequence)
}
function swallowImages() {
var config = {
src: {
globs: [ 'images/*.png', 'images/*.gif','images/*.jpeg', 'images/*.jpg' ],
options: {}
},
transform: {
options: {},
header: '',
footer: ''
},
dest: {
path: 'images',
filename: 'images.js',
header: 'module.exports = {\n',
footer: '\n};\n',
options: {}
}
};
return gulp.src(config.src.globs, config.src.options)
.pipe($$.imagine64(config.transform.options))
.pipe($$.header(config.transform.header))
.pipe($$.footer(config.transform.footer))
.pipe($$.concat(config.dest.filename))
.pipe($$.header(config.dest.header))
.pipe($$.footer(config.dest.footer))
.pipe(gulp.dest(config.dest.path, config.dest.options));
}
function templates(src, type) {
return gulp.src(src)
.pipe($$.each(function(content, file, callback) {
var filename = path.basename(file.path, "." + type),
member = /[^\w]/.test(filename) ? "['" + filename + "']" : "." + filename;
// convert (groups of) 4 space chars at start of lines to tab(s)
do {
var len = content.length;
content = content.replace(/\n(( )*) (.*)/, "\n$1\t$3");
} while (content.length < len);
// quote each line and join them into a single string
content = 'exports' + member + " = [\n'" + content
.replace(/\\/g, "\\\\") // escape all backslashes
.replace(/'/g, "\\'") // escape all single-quotes
.replace(/\n/g, "',\n'") + "'\n].join('\\n');\n";
// remove possible blank line at end of each
content = content.replace(/,\n''\n]/g, "\n]");
callback(null, content); // the first argument is an error, if you encounter one
}))
.pipe($$.concat("index.js"))
.pipe($$.header("'use strict';\n\n"))
.pipe(gulp.dest(function(file) { return file.base; }));
}
function addOns() {
return gulp.src(addOnsDir + '*.js')
// Insert an IIFE around the code...
.pipe($$.replace( // ...starting immediately following 'use strict' and...
"'use strict';\n",
"'use strict';\n(function() {"
))
.pipe($$.replace( // ...ending after modules.exports.
/\w+\.exports(\s*=\s*)(\w+);/,
'window.fin.Hypergrid.$2$1$2;\n})();'
))
.pipe(
$$.mirror(
pipe(
$$.rename(function (path) {
path.basename = addOnsDir + path.basename;
})
),
pipe(
$$.rename(function (path) {
path.basename = addOnsDir + path.basename + '.min';
}),
$$.uglify() // minimize
.on('error', $$.util.log)
)
)
)
.pipe(gulp.dest(buildDir));
}