forked from honestbleeps/Reddit-Enhancement-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
154 lines (140 loc) · 4.67 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
/* global require */
'use strict';
var gulp = require('gulp'),
del = require('del'),
zip = require('gulp-zip'),
replace = require('gulp-replace-async'),
path = require('path');
var options = require('minimist')(process.argv.slice(2));
// What happens when you do gulp without any arguments
gulp.task('default', ['clean'], function() {
gulp.start('build');
});
// Paths
var rootBuildDir = 'dist',
commonFiles = ['lib/**/*.js', 'lib/**/*.json', 'lib/**/*.css', 'lib/**/*.html'],
config = {
// The name used to refer to the browser from the command line, i.e. `gulp build -b chrome`
chrome: {
// Subfolder of rootBuildDir that the buildFiles will be copied to
buildFolder: 'chrome',
// The file for addFileToManifest to modify when adding new hosts/modules
manifest: 'Chrome/manifest.json',
// Files to be copied when building the extension
buildFiles: [
// dest is relative to the browser's buildFolder, src is relative to the project root
{ dest: 'images', src: ['Chrome/images/*.png'] },
{ dest: '/', src: ['Chrome/*.js', 'Chrome/*.png', 'Chrome/*.html', 'package.json', 'Chrome/manifest.json'] },
{ dest: '/', src: commonFiles }
]
},
safari: {
buildFolder: 'RES.safariextension',
manifest: 'RES.safariextension/Info.plist',
buildFiles: [
{ dest: '/', src: ['RES.safariextension/*.js', 'RES.safariextension/*.png', 'RES.safariextension/*.html', 'package.json', 'RES.safariextension/info.plist'] },
{ dest: '/', src: commonFiles }
]
},
firefox: {
buildFolder: 'XPI',
manifest: 'XPI/index.js',
buildFiles: [
{ dest: 'data', src: ['XPI/data/**/*'] },
{ dest: 'data', src: commonFiles },
{ dest: '/', src: ['*.png', 'XPI/package.json', 'XPI/index.js'] }
]
},
oblink: {
buildFolder: 'oblink',
manifest: 'OperaBlink/manifest.json',
buildFiles: [
{ dest: 'images', src: ['OperaBlink/images/*.png'] },
{ dest: '/', src: ['OperaBlink/*.js', 'Chrome/browsersupport-chrome.js', 'OperaBlink/*.png', 'OperaBlink/*.json', 'package.json'] },
{ dest: '/', src: commonFiles }
]
},
opera: {
buildFolder: 'opera',
manifest: 'Opera/includes/loader.js',
buildFiles: [
{ dest: 'includes', src: ['Opera/includes/*.js'] },
{ dest: '/', src: ['Opera/*.js', 'OperaBlink/*.gif', 'Opera/*.html', 'Opera/*.xml', 'package.json'] },
{ dest: '/', src: commonFiles }
]
}
},
// the `-b browser` argument(s) or all browsers, if unspecified
selectedBrowsers = options.b ? [].concat(options.b) : Object.keys(config);
function getBuildDir(browser) {
return path.join(rootBuildDir, config[browser].buildFolder);
}
gulp.task('build', function(cb) {
selectedBrowsers.forEach(function(browser) {
config[browser].buildFiles.forEach(function(paths) {
gulp.src(paths.src)
.pipe(gulp.dest(path.join(getBuildDir(browser), paths.dest)));
});
});
cb();
});
gulp.task('zip', function(cb) {
// --zipdir argument or <rootBuildDir>/zip/
var zipDir = options.zipdir || path.join(rootBuildDir, 'zip');
selectedBrowsers.forEach(function(browser) {
gulp.src(path.join(getBuildDir(browser), '**/*'))
.pipe(zip(config[browser].buildFolder + '.zip'))
.pipe(gulp.dest(zipDir));
});
cb();
});
gulp.task('watch', function() {
var sources = selectedBrowsers.reduce(function(previous, browser) {
return previous.concat(config[browser].buildFiles.reduce(function(pre, cur) {
return pre.concat(cur.src);
}, []));
}, []);
gulp.watch(sources, [ 'build' ]);
});
// Add new modules to browser manifests
gulp.task('add-module', function(cb) {
selectedBrowsers.forEach(function(browser) {
addModuleToManifest(config[browser].manifest);
});
cb();
});
gulp.task('add-host', function(cb) {
selectedBrowsers.forEach(function(browser) {
addHostToManifest(config[browser].manifest);
});
cb();
});
// "Add file to manifests" task support
function addFileToManifest(manifest, pattern) {
var addModulename = options['file'];
return gulp.src(manifest)
.pipe(replace(pattern, function(match, callback) {
var withNewFile = match[0] + '\n' +
match[1] + addModulename + match[2];
callback(null, withNewFile);
}))
.pipe(gulp.dest(path.dirname(manifest)));
}
function addModuleToManifest(manifest) {
var pattern = new RegExp('^(.*modules/)' + getRefModule() + '(.*)$', 'm');
return addFileToManifest(manifest, pattern);
}
// "Add file to manifests" task support
function addHostToManifest(manifest) {
var pattern = new RegExp('^(.*modules/hosts/)' + getRefHost() + '(.*)$', 'm');
return addFileToManifest(manifest, pattern);
}
function getRefModule() {
return 'commandLine.js';
}
function getRefHost() {
return 'imgur.js';
}
gulp.task('clean', function(cb) {
del(['dist/*'], cb);
});