forked from MeoMix/StreamusChromeExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
338 lines (314 loc) · 10.5 KB
/
Gruntfile.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*jslint node: true*/
// Options:
// * grunt: Lint JavaScript, LESS, and run tests
// * grunt build: Build a debug release
// * grunt build:release: Build a release / increment version
'use strict';
var _ = require('lodash');
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
// Read project settings from package.json in order to be able to reference the properties with grunt.
pkg: grunt.file.readJSON('package.json'),
meta: {
// Set these values dynamically to inform packages where to write information.
releaseDirectory: '',
buildVersion: ''
},
// Compress image sizes and move to dist folder
imagemin: {
files: {
expand: true,
cwd: 'dist/img',
src: ['**/*.{png,jpg,gif}'],
dest: 'dist/img/'
}
},
// Improve code quality by applying a code-quality check with jshint
jshint: {
// A full list of options and their defaults here: https://github.com/jshint/jshint/blob/master/examples/.jshintrc
options: {
immed: true,
latedef: true,
newcap: true,
nonew: true,
quotmark: 'single',
jquery: true,
maxparams: 5,
maxdepth: 4,
maxstatements: 35,
maxcomplexity: 10,
// Don't validate libraries
ignores: ['src/js/lib/**/*.js']
},
files: ['src/js/**/*.js', 'Gruntfile.js']
},
// Compile LESS to CSS
less: {
options: {
ieCompat: false,
compress: true,
strictImports: true,
strictMath: true,
strictUnits: true
},
files: {
expand: true,
cwd: 'src/less/',
src: 'foreground.less',
dest: 'src/css/',
ext: '.css'
}
},
// Ensure LESS code-quality by comparing it against Twitter's ruleset.
// Using a slightly modified version which has support for modern browser properties
recess: {
foreground: {
src: 'src/less/foreground.less',
options: {
noUniversalSelectors: false,
strictPropertyOrder: false
}
}
},
requirejs: {
production: {
// All r.js options can be found here: https://github.com/jrburke/r.js/blob/master/build/example.build.js
options: {
appDir: 'src',
mainConfigFile: 'src/js/common/requireConfig.js',
dir: 'dist/',
// Skip optimizing because there's no load benefit for an extension and it makes error debugging hard.
optimize: 'none',
optimizeCss: 'none',
// Inlines the text for any text! dependencies, to avoid the separate
// async XMLHttpRequest calls to load those dependencies.
inlineText: true,
useStrict: true,
stubModules: ['text'],
findNestedDependencies: true,
// Don't leave a copy of the file if it has been concatenated into a larger one.
removeCombined: true,
// List the modules that will be optimized. All their immediate and deep
// dependencies will be included in the module's file when the build is done
modules: [{
name: 'background/main',
insertRequire: ['background/main']
}, {
name: 'foreground/main',
insertRequire: ['foreground/main']
}],
fileExclusionRegExp: /^\.|vsdoc.js$|\.example$|test|test.html|less$/
}
}
},
replace: {
// Remove Chrome permissions not supported on other browsers.
invalidPermissions: {
src: ['<%= meta.releaseDirectory %>/manifest.json'],
overwrite: true,
replacements: [{
from: '"background",',
to: ''
}, {
from: '"identity.email",',
to: ''
}]
},
// Ensure that the localDebug flag is not set to true when building a release.
localDebug: {
src: ['dist/js/background/background.js'],
overwrite: true,
replacements: [{
// Local debugging is for development only.
from: 'localDebug: true',
to: 'localDebug: false'
}]
//}, {
// from: 'referer = \'https://streamus.com\/',
// to: 'referer = \'https://streаmus.com\/'
//}]
},
// Remove development key and comments from manifest for deployment
manifest: {
src: ['dist/manifest.json'],
overwrite: true,
replacements: [{
// Remove manifest key because it can't be uploaded to the web store.
// The key is helpful for debugging because it keeps the extension ID stable.
from: /"key".*/,
to: ''
}, {
// Remove comments because they can't be uploaded to the web store.
from: /\/\/ .*/ig,
to: ''
}]
}
},
compress: {
// Zip browser-specific folders which are ready for release.
// Each zip file can then be uploaded to their respective web store.
release: {
options: {
archive: '<%= meta.releaseDirectory %>Streamus v<%= meta.buildVersion %>.zip'
},
files: [{
expand: true,
cwd: '<%= meta.releaseDirectory %>',
src: ['**']
}]
}
},
clean: {
// Remove all non-English translations from Opera because their translation requirements are too strict.
locales: {
files: [{
expand: true,
cwd: '<%= meta.releaseDirectory %>/_locales/',
src: ['*', '!en']
}]
},
// Cleanup the dist folder of files which don't need to be pushed to production.
dist: {
files: [{
expand: true,
cwd: 'dist/',
src: ['template', 'build.txt']
}]
}
},
copy: {
release: {
expand: true,
cwd: 'dist/',
src: '**/*',
dest: '<%= meta.releaseDirectory %>'
},
// Content scripts don't use RequireJS so they need to be concatenated and moved to dist with a separate task
contentScripts: {
files: {
'dist/js/contentScript/beatport.js': ['src/js/contentScript/beatport.js'],
'dist/js/contentScript/youTube.js': ['src/js/contentScript/youTube.js']
}
}
},
watch: {
less: {
options: {
nospawn: true
},
files: ['src/less/*'],
tasks: ['less']
}
},
jscs: {
src: ['src/js/**/*.js', '!src/js/lib/**/*.js', 'Gruntfile.js'],
options: {
config: '.jscsrc',
fix: true
}
},
mocha: {
tests: {
options: {
log: true,
// Don't automatically inject mocha.run because requireJS deps need to load first
run: false
},
src: ['src/test.html']
}
},
// Create the key file YouTube's API using the testing key (NOT the production key).
// Used mainly for Travis CI builds.
'file-creator': {
youTubeAPIKey: {
'src/js/background/key/youTubeAPIKey.js': function(fs, fd, done) {
fs.writeSync(fd, 'define(function() { return \'AIzaSyDBCJuq0aey3bL3K6C0l4mKzT_y8zy9Msw\'; });');
done();
}
}
},
version: {
project: {
options: {
prefix: '[^\\-minimum_chrome_version]version[\'"]?\\s*[:=]\\s*[\'"]'
},
src: ['package.json', 'src/manifest.json']
}
},
'webstore-upload': {
accounts: {
'default': {
publish: false
}
},
extensions: {
streamus: {
appID: 'jbnkffmindojffecdhbbmekbmkkfpmjd',
zip: 'release/Streamus v<%= meta.buildVersion %>/chrome/Streamus v<%= meta.buildVersion %>.zip'
}
}
}
});
// Build release and place .zip files in the release directory
grunt.registerTask('build', function(buildFlag) {
var isRelease = buildFlag === 'release';
// Ensure tests pass before performing any sort of bundling.
grunt.task.run('test');
if (isRelease) {
grunt.task.run('version:project:minor');
}
grunt.task.run('requirejs');
// Don't replace manifest key during debugging because server will throw CORS errors.
// No need to clean-up comments because debug version isn't uploaded
if (isRelease) {
grunt.task.run('replace:manifest');
}
grunt.task.run('replace:localDebug', 'copy:contentScripts', 'less', 'imagemin', 'clean:dist');
grunt.task.run('buildReleases:' + isRelease);
});
// Ensure that non-English translations are in-sync with the English translation
grunt.registerTask('diffLocales', function() {
var englishJson = grunt.file.readJSON('src/_locales/en/messages.json');
var englishKeys = _.keys(englishJson);
grunt.file.recurse('src/_locales/', function(abspath, rootdir, subdir) {
var json = grunt.file.readJSON(abspath);
var keys = _.keys(json);
var missingEnglishKeys = _.difference(englishKeys, keys);
var extraNonEnglishKeys = _.difference(keys, englishKeys);
if (missingEnglishKeys.length > 0) {
grunt.log.error('The translation for ' + subdir + ' is missing keys: \n- ' + missingEnglishKeys.join('\n- '));
}
if (extraNonEnglishKeys.length > 0) {
grunt.log.error('The translation for ' + subdir + ' has extra keys: \n- ' + extraNonEnglishKeys.join('\n- '));
}
});
});
// Run linters and enforce code-quality standards
grunt.registerTask('default', ['test']);
grunt.registerTask('test', ['jshint', 'recess', 'jscs', 'mocha']);
// Synchronous wrapper
grunt.registerTask('buildReleases', function(isRelease) {
var buildVersion = isRelease === 'true' ? grunt.file.readJSON('package.json').version : 'Debug';
grunt.config.set('meta.buildVersion', buildVersion);
var baseReleaseDirectory = 'release/Streamus v' + buildVersion;
var chromeReleaseDirectory = baseReleaseDirectory + '/chrome/';
var operaReleaseDirectory = baseReleaseDirectory + '/opera/';
// Build chrome release
grunt.task.run('compressRelease:' + chromeReleaseDirectory);
// Build opera release
grunt.task.run('compressRelease:' + operaReleaseDirectory + ':sanitize=true');
if (isRelease) {
grunt.task.run('webstore-upload');
}
});
// A synchronous wrapper for compress:release
grunt.registerTask('compressRelease', function(releaseDirectory, sanitize) {
grunt.config.set('meta.releaseDirectory', releaseDirectory);
grunt.task.run('copy:release');
if (sanitize) {
grunt.task.run('replace:invalidPermissions', 'clean:locales');
}
grunt.task.run('compress:release');
});
};