-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgulpfile.js
256 lines (228 loc) · 6.8 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
/*jshint node:true */
var gulp = require('gulp');
var livereload = require('gulp-livereload');
// tasks config
var argv = require('minimist')(process.argv.slice(2), { alias: {
port: 'p',
production: 'P',
type: 't'
}});
var destination = 'build';
// exit with a correct code
var errorOccurred = false;
process.once('exit', function (code) {
if (errorOccurred && code === 0) process.exit(1);
});
/**
* Error handler.
* @param {Error} err
*/
function handleError(err) {
var gutil = require('gulp-util');
var maxStackLines = 10;
errorOccurred = true;
if (err.name && err.stack)
err = gutil.colors.red(err.plugin + ': ' + err.name + ': ') +
gutil.colors.bold.red(err.message) +
'\n' + err.stack.replace(err.message, '').split('\n').map(shortenLine).slice(0, maxStackLines).join('\n');
gutil.log(err);
if (this.emit) this.emit('end');
}
function shortenLine(line) {
var max = 80;
return line.length > 80
? line.substr(0, max * 0.6 | 0) + '{...}' + line.substr(-max * 0.4 | 0)
: line;
}
/**
* Dynamic callback concatenation.
* @param {Function} callback
* @return {Function}
*/
function callbacks(callback) {
var count = 0;
var done = false;
return function () {
count++;
return function (err) {
if ((!--count || err) && !done) {
done = true;
callback(err);
}
};
};
}
function stylesStream() {
return contentStylesStream();
}
function contentStylesStream() {
var stylus = require('gulp-stylus');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var rename = require('gulp-rename');
var gulpif = require('gulp-if');
return gulp.src('src/styl/main.styl')
.pipe(stylus({ errors: false })).on('error', handleError)
.pipe(rename('content.css'))
.pipe(autoprefixer('last 2 Chrome versions')).on('error', handleError)
.pipe(gulpif(argv.production, minifyCSS({ noAdvanced: true })));
}
function scriptsStream() {
return contentScriptsStream();
}
function contentScriptsStream() {
var resolve = require('component-resolver');
var Builder = require('component-build');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var File = require('vinyl');
var stream = new require('stream').Readable({ objectMode: true });
var done = false;
stream._read = function () {
if (done) return stream.push(null);
resolve('.', { development: !argv.production }, build);
};
function build(err, tree) {
done = true;
if (err) return stream.emit('error', err);
new Builder(tree, {
autorequire: true,
development: !argv.production
}).scripts(pushFile);
}
function pushFile(err, string) {
if (err) return stream.emit('error', err);
stream.push(new File({
cwd: '/',
base: '/',
path: '/content.js',
contents: new Buffer(string)
}));
}
return stream.on('error', handleError)
.pipe(gulpif(argv.production, uglify()));
}
function iconFontStream() {
var svgfont = require('gulp-svgicons2svgfont');
var svg2ttf = require('gulp-svg2ttf');
var ttf2woff = require('gulp-ttf2woff');
return gulp.src('src/icon/*.svg')
.pipe(svgfont({
fontName: 'icons',
normalize: true,
// centerHorizontally: true
}))
.on('error', handleError)
.pipe(svg2ttf())
.on('error', handleError)
.pipe(ttf2woff())
.on('error', handleError);
}
function assetsStream() {
return gulp.src(['manifest.json', 'src/img/**/*']);
}
gulp.task('assets', function () {
return assetsStream().pipe(gulp.dest(destination)).pipe(livereload({ auto: false }));
});
gulp.task('icons', function () {
return iconFontStream().pipe(gulp.dest(destination)).pipe(livereload({ auto: false }));
});
gulp.task('icons:serialize', function (cb) {
var fs = require('fs');
var path = require('path');
var consolidate = require('gulp-consolidate');
var iconsDir = 'src/icon';
var iconsTemplate = 'template/icons.styl';
var stylModules = 'src/styl/module';
var cbs = callbacks(cb);
fs.readdir(iconsDir, processFiles);
function processFiles(err, files) {
if (err) return handleError(err);
var icons = files.filter(onlysvg).map(describe).sort(byName).map(addCodepoints);
icons.forEach(renameFile);
generateStyl(icons);
}
function onlysvg(filename) { return filename.substr(-4) === '.svg'; }
function byName(a, b) { return a.name < b.name ? -1 : 1; }
function describe(filename) {
return {
filename: filename,
name: filename.replace(/(^ue[a-f\d]{3}-)|(\.svg$)/gi, '')
};
}
function addCodepoints(file, i) {
file.codepoint = 57345 + i;
file.character = file.codepoint.toString(16);
return file;
}
function renameFile(icon) {
var filename = 'u' + icon.character + '-' + icon.name + '.svg';
if (filename === icon.filename) return;
else fs.rename(path.join(iconsDir, icon.filename), path.join(iconsDir, filename), cbs());
}
function generateStyl(icons) {
gulp.src(iconsTemplate)
.pipe(consolidate('swig', {
name: 'icons',
glyphs: icons
}))
.on('error', handleError)
.pipe(gulp.dest(stylModules))
.on('end', cbs());
}
});
gulp.task('clean', function (cb) {
require('rimraf')(destination, cb);
});
gulp.task('scripts:content', function () {
return contentScriptsStream().pipe(gulp.dest(destination)).pipe(livereload({ auto: false }));
});
gulp.task('scripts', ['scripts:content']);
gulp.task('styles:content', function () {
return contentStylesStream().pipe(gulp.dest(destination)).pipe(livereload({ auto: false }));
});
gulp.task('styles', ['styles:content']);
gulp.task('build', ['clean'], function () {
gulp.start('assets', 'icons', 'scripts', 'styles');
});
gulp.task('bump', function () {
var bump = require('gulp-bump');
var type = argv.type ? argv.type : 'patch';
var isType = ~['major', 'minor', 'patch'].indexOf(type);
return gulp.src('manifest.json')
.pipe(bump(isType ? { type: type } : { version: type }))
.pipe(gulp.dest('.'));
});
gulp.task('package', function () {
var version = require('./manifest.json').version;
var streamqueue = require('streamqueue');
var zip = require('gulp-zip');
argv.production = true;
return streamqueue({ objectMode: true },
assetsStream(),
iconFontStream(),
scriptsStream(),
stylesStream()
).pipe(zip('tga-' + version + '.zip')).pipe(gulp.dest('.'));
});
gulp.task('release', ['bump'], function () {
gulp.start('package');
});
gulp.task('serve', function () {
var gutil = require('gulp-util');
var http = require('http');
var ecstatic = require('ecstatic');
var port = argv.port || 8080;
http.createServer(ecstatic({ root: __dirname })).listen(port);
gutil.log('Serving:', gutil.colors.magenta('http://localhost:' + port + '/test/chat'));
gulp.start('watch');
});
gulp.task('watch', function () {
livereload.listen();
gulp.watch(['manifest.json', 'src/img/**/*'], ['assets']);
gulp.watch(['component.json', 'data/*.json', 'src/js/**/*.js'], ['scripts']);
gulp.watch('src/styl/**/*.styl', ['styles']);
});
gulp.task('default', ['build'], function () {
gulp.start('serve');
});