forked from HDTheKiller/twitch-giveaways
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
170 lines (147 loc) · 4.44 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
/*jshint node:true */
var gulp = require('gulp');
// tasks config
var argv = require('minimist')(process.argv.slice(2), { alias: {
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 maxStackLines = 10;
errorOccurred = true;
if (err.name && err.stack) {
err = err.plugin + ': ' + err.name + ': ' + err.message + '\n'
+ err.stack.replace(err.message, '').split('\n').slice(0, maxStackLines).join('\n');
}
console.error(err);
if (this.emit) this.emit('end');
}
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()))
.on('error', handleError);
}
function iconsStream() {
var svgmin = require('gulp-svgmin');
var rename = require('gulp-rename');
var svgstore = require('gulp-svgstore');
return gulp.src('src/icon/*.svg')
.pipe(svgmin())
.pipe(rename({prefix: 'icon-'}))
.pipe(svgstore())
.pipe(rename({basename: 'icons'}));
}
function assetsStream() {
var streamqueue = require('streamqueue');
return streamqueue({ objectMode: true },
gulp.src(['./manifest.json', './src/img/**/*']),
gulp.src('./banner/*', {base: process.cwd()})
);
}
gulp.task('assets', function () {
return assetsStream().pipe(gulp.dest(destination));
});
gulp.task('icons', function () {
return iconsStream().pipe(gulp.dest(destination));
});
gulp.task('clean', function (cb) {
require('rimraf')(destination, cb);
});
gulp.task('scripts:content', function () {
return contentScriptsStream().pipe(gulp.dest(destination));
});
gulp.task('scripts', ['scripts:content']);
gulp.task('styles:content', function () {
return contentStylesStream().pipe(gulp.dest(destination));
});
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(),
iconsStream(),
scriptsStream(),
stylesStream()
).pipe(zip('tga-' + version + '.zip')).pipe(gulp.dest('.'));
});
gulp.task('release', ['bump'], function () {
gulp.start('package');
});
gulp.task('watch', function () {
gulp.watch(['manifest.json', 'src/img/**/*', 'banner/*'], ['assets']);
gulp.watch(['component.json', 'data/*.json', 'src/js/**/*.js'], ['scripts']);
gulp.watch('src/styl/**/*.styl', ['styles']);
gulp.watch(['src/icon/*.svg'], ['icons']);
});
gulp.task('default', ['build'], function () {
gulp.start('watch');
});