forked from honestbleeps/Reddit-Enhancement-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
307 lines (273 loc) · 7.81 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* eslint-env node */
import gulp from 'gulp';
import del from 'del';
import zip from 'gulp-zip';
import replace from 'gulp-replace';
import path from 'path';
import babel from 'gulp-babel';
import sass from 'gulp-sass';
import autoprefixer from 'gulp-autoprefixer';
import merge from 'merge-stream';
import cache from 'gulp-cached';
import plumber from 'gulp-plumber';
import sourcemaps from 'gulp-sourcemaps';
import eslint from 'gulp-eslint';
import scsslint from 'gulp-scss-lint';
import qunit from 'gulp-qunit';
import through from 'through-gulp';
const options = require('minimist')(process.argv.slice(2));
// Paths
const baseConf = {
sources: {
copy: { cwd: 'lib/**', src: ['*.json', '*.css', '*.html', 'vendor/**/*.js'] },
babel: { cwd: 'lib/**', src: ['*.js', '!vendor/**/*.js'] },
sass: { cwd: 'lib/**', src: ['*.scss'] }
},
dests: {
root: 'dist'
}
};
const browserConf = {
// The name used to refer to the browser from the command line, i.e. `gulp build -b chrome`
chrome: {
// The file for addFileToManifest to modify when adding new hosts/modules
manifest: 'Chrome/manifest.json',
manifestReplacements: [
{ key: 'title', needle: /("title": ")(?:.*?)(")/ },
{ key: 'description', needle: /("description": ")(?:.*?)(")/ },
{ key: 'version', needle: /("version": ")(?:[\d\.])+(")/ }
],
// Browser-specific files to be copied when building the extension (paths relative to project root)
sources: [
{ cwd: 'Chrome/**', src: ['images/*.png'] },
{ src: ['Chrome/*.js', 'Chrome/*.png', 'Chrome/*.html'] }
],
dests: {
// Subdirectory of baseConf.dests.root that the sources will be copied to
root: 'chrome',
// Subdirectory of the above line that baseConf.sources will be copied to
baseSources: '/'
}
},
safari: {
manifest: 'Safari/Info.plist',
manifestReplacements: [
{ key: 'version', needle: /(<key>CFBundleVersion<\/key>\s*<string>)(?:.+)(<\/string>)/ },
{ key: 'version', needle: /(<key>CFBundleShortVersionString<\/key>\s*<string>)(?:.+)(<\/string>)/ },
{ key: 'description', needle: /(<key>Description<\/key>\s*<string>)(?:.+)(<\/string>)/ },
{ key: 'title', needle: /(<key>CFBundleDisplayName<\/key>\s*<string>)(?:.+)(<\/string>)/ }
],
sources: [
{ src: ['Safari/*.js', 'Safari/*.png', 'Safari/*.html'] }
],
dests: {
root: 'RES.safariextension',
baseSources: '/'
}
},
firefox: {
filesList: 'Firefox/index.js',
manifest: 'Firefox/package.json',
manifestReplacements: [
{ key: 'title', needle: /("title": ")(?:.*)(")/ },
{ key: 'description', needle: /("description": ")(?:.*)(")/ },
{ key: 'version', needle: /("version": ")(?:[\d\.])+(")/ }
],
sources: [
{ cwd: 'Firefox/**', src: ['data/**/*'] },
{ src: ['*.png', 'Firefox/index.js'] }
],
dests: {
root: 'firefox',
baseSources: 'data'
}
},
node: {
filesList: 'node/files.json',
sources: [
{ src: ['node/**/*'] }
],
dests: {
root: 'node',
baseSources: 'lib'
}
},
qunit: {
sources: [
{ cwd: 'tests/qunit/**', src: '*.*' }
],
dests: {
root: 'qunit',
baseSources: '/'
}
}
};
// the `-b browser` argument(s) or all browsers, if unspecified
const browsers = options.b ? [].concat(options.b) : Object.keys(browserConf);
function src({ src, cwd }) {
return gulp.src(src, { cwd });
}
function dest(...paths) {
return gulp.dest(path.join(...paths));
}
function getBuildDir(browser) {
return path.join(baseConf.dests.root, browserConf[browser].dests.root);
}
// Accepts a stream, sourced from baseConf.sources
// and pipes it to each selected browsers' dests.baseSources directory.
function pipeToBrowsers(inStream) {
return browsers.reduce(
(stream, browser) => stream.pipe(dest(getBuildDir(browser), browserConf[browser].dests.baseSources)),
inStream
);
}
gulp.task('default', ['clean'], () => {
gulp.start('watch');
});
gulp.task('clean', () =>
del(browsers.map(browser => getBuildDir(browser)))
);
gulp.task('watch', ['build'], () => {
const sources = browsers.reduce(
(acc, browser) => acc.concat(browserConf[browser].sources.reduce((a, { src }) => a.concat(src), [])),
[baseConf.sources.copy.cwd]
);
gulp.watch(sources, ['build']);
});
gulp.task('build', ['babel', 'sass', 'copy', 'copy-browser', 'manifests']);
gulp.task('babel', () =>
pipeToBrowsers(
src(baseConf.sources.babel)
.pipe(cache('babel'))
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(sourcemaps.write('.'))
.pipe(plumber.stop())
)
);
gulp.task('sass', () =>
pipeToBrowsers(
src(baseConf.sources.sass)
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['Chrome >= 25', 'Firefox >= 30', 'Safari >= 6'],
cascade: false
}))
)
);
gulp.task('copy', () =>
pipeToBrowsers(
merge(
src(baseConf.sources.copy),
gulp.src(getPackageMetadata().path)
).pipe(cache('copy'))
)
);
gulp.task('copy-browser', () =>
merge(
browsers.map(browser =>
merge(browserConf[browser].sources.map(paths => src(paths)))
.pipe(cache(browser))
.pipe(dest(getBuildDir(browser)))
)
)
);
gulp.task('manifests', () =>
merge(
browsers
.filter(browser => browserConf[browser].manifest)
.map(browser =>
gulp.src(browserConf[browser].manifest)
.pipe(cache('manifests'))
.pipe(through.map(file => populateManifest(browser, file)))
.pipe(dest(getBuildDir(browser)))
)
)
);
function getPackageMetadata() {
const path = './' + (options.p || 'package.json');
return {
path,
contents: require(path)
};
}
function populateManifest(browser, file) {
const replaceConfig = browserConf[browser].manifestReplacements;
if (replaceConfig) {
const values = getPackageMetadata().contents;
const transformed = replaceConfig.reduce(
(haystack, { needle, key }) => haystack.replace(needle, (m, prefix, suffix) => prefix + values[key] + suffix),
file.contents.toString()
);
file.contents = new Buffer(transformed);
}
return file;
}
// Add new modules to browser manifests
gulp.task('add-module', () =>
merge(
browsers
.map(browser => browserConf[browser].filesList || browserConf[browser].manifest)
.filter(manifest => manifest)
.map(manifest => addModuleToManifest(manifest))
)
);
gulp.task('add-host', () =>
merge(
browsers
.map(browser => browserConf[browser].filesList || browserConf[browser].manifest)
.filter(manifest => manifest)
.map(manifest => addHostToManifest(manifest))
)
);
// "Add file to manifests" task support
function addFileToManifest(manifest, pattern) {
const addModulename = options['file'];
return gulp.src(manifest)
.pipe(replace(pattern, `$&\n$1${addModulename}$2`))
.pipe(dest(path.dirname(manifest)));
}
function addModuleToManifest(manifest) {
const pattern = new RegExp(`^(.*modules/)${getRefModule()}(.*)$`, 'm');
return addFileToManifest(manifest, pattern);
}
// "Add file to manifests" task support
function addHostToManifest(manifest) {
const pattern = new RegExp(`^(.*modules/hosts/)${getRefHost()}(.*)$`, 'm');
return addFileToManifest(manifest, pattern);
}
function getRefModule() {
return 'commandLine.js';
}
function getRefHost() {
return 'imgur.js';
}
gulp.task('zip', () => {
// --zipdir argument or <baseConf.dests.root>/zip/
const zipDir = options.zipdir || path.join(baseConf.dests.root, 'zip');
return merge(
browsers.map(browser =>
gulp.src(path.join(getBuildDir(browser), '**/*'))
.pipe(zip(browserConf[browser].dests.root + '.zip'))
.pipe(dest(zipDir))
)
);
});
gulp.task('travis', ['eslint', 'scsslint', 'qunit']);
gulp.task('eslint', () =>
src(baseConf.sources.babel)
.pipe(eslint())
.pipe(eslint.formatEach())
.pipe(eslint.failAfterError())
);
gulp.task('scsslint', () =>
src(baseConf.sources.sass)
.pipe(scsslint({ maxBuffer: 1024 * 1024 }))
.pipe(scsslint.failReporter())
);
// todo: when Gulp 4 is released, only build qunit
gulp.task('qunit', ['build'], () =>
gulp.src('dist/qunit/tests.html')
.pipe(qunit())
);