-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
184 lines (155 loc) · 5.01 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
'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'),
escapeStr = require('js-string-escape'),
CleanCss = require("clean-css"),
pipe = require('multipipe');
var name = 'filter-tree',
main = 'FilterTree',
testDir = './test/',
buildDir = './build/';
// // // // // // // // // // // //
gulp.task('lint', lint);
gulp.task('test', test);
gulp.task('doc', doc);
gulp.task('injection', injectCSS);
gulp.task('browserify', browserify);
gulp.task('serve', browserSyncLaunchServer);
gulp.task('html-templates', function() {
templates('html');
});
gulp.task('build', function(callback) {
clearBashScreen();
runSequence(
'lint',
'html-templates',
//'test',
//'doc',
'injection',
'browserify',
callback
);
});
gulp.task('reload', function() {
browserSync.reload();
});
gulp.task('watch', function () {
gulp.watch([
'./css/*.css',
'./html/*.html',
'./js/**',
'!./js/stylesheet.js', // generated file
testDir + '**',
buildDir + '*'
], [
'build'
]);
gulp.watch([
buildDir + 'lib/' + name + '.js'
], [
'reload'
]);
});
gulp.task('default', ['build', 'watch'], browserSyncLaunchServer);
// // // // // // // // // // // //
function cssToJsFn(filePath, file) {
var STYLE_HEADER = 'css = \'',
STYLE_FOOTER = '\';';
var css = new CleanCss({})
.minify(file.contents.toString())
.styles;
file.contents = new Buffer(STYLE_HEADER + escapeStr(css) + STYLE_FOOTER);
return file.contents.toString('utf8');
}
function lint() {
return gulp.src(['./js/**/*.js', buildDir + '*.js'])
.pipe($$.excludeGitignore())
.pipe($$.eslint())
.pipe($$.eslint.format())
.pipe($$.eslint.failAfterError());
}
function test() {
return gulp.src(testDir + 'index.js')
.pipe($$.mocha({reporter: 'spec'}));
}
function browserSyncLaunchServer() {
browserSync.init({
server: {
// Serve up our build folder
baseDir: buildDir,
index: 'demo.html'
},
port: 9013
});
}
function injectCSS() {
// inject the css from css/filter-tree.css via css/css.js into new file js/stylesheet.js
return gulp.src('./css/css.js')
.pipe($$.inject(gulp.src('./css/' + name + '.css'), {
transform: cssToJsFn,
starttag: '/* {{name}}:{{ext}} */',
endtag: '/* endinject */'
}))
.pipe($$.rename('stylesheet.js'))
.pipe(gulp.dest('./js/'));
}
function browserify() {
// browserify the root file src/index.js into build/filter-tree.js and filter-tree.min.js
return gulp.src('./index.js')
.pipe($$.replace(
'module.exports =',
'window.' + main + ' ='
))
.pipe($$.mirror(
pipe(
$$.rename(name + '.js'),
$$.browserify({ debug: true })
.on('error', $$.util.log)
),
pipe(
$$.rename(name + '.min.js'),
$$.browserify(),
$$.uglify()
.on('error', $$.util.log)
)
))
.pipe(gulp.dest(buildDir + 'lib/'));
}
function doc(callback) {
exec(path.resolve('jsdoc.sh'), function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
callback(err);
});
}
function clearBashScreen() {
var ESC = '\x1B';
console.log(ESC + 'c'); // (VT-100 escape sequence)
}
function templates(folder) {
return gulp.src('./' + folder + '/*.' + folder)
.pipe($$.each(function(content, file, callback) {
var filename = path.basename(file.path, "." + folder),
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; }));
}