forked from MeoMix/StreamusChromeExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
283 lines (262 loc) · 11.4 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
/*jslint node: true*/
// Options:
// * grunt: Lint JavaScript, LESS, and _locales
// * grunt build: Build a test release
// * grunt build --newVersion="vx.xxx": Build a release
'use strict';
var _ = require('lodash');
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
// Setup environment variables before initializing config so that initConfig can use the variables.
var versionParameter = grunt.option('newVersion');
// Strip out v because need to pass a string to grunt or else trailing zeros get dropped (i.e. can't provide --newVersion=0.170, interpreted as 0.17)
var version = _.isUndefined(versionParameter) ? 'Debug' : versionParameter.replace('v', '');
var isDebug = !versionParameter;
var baseReleaseDirectory = 'release/Streamus v' + version;
var chromeReleaseDirectory = baseReleaseDirectory + '/chrome/';
var operaReleaseDirectory = baseReleaseDirectory + '/opera/';
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 this value dynamically to inform other packages where to write information.
releaseDirectory: ''
},
// 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: {
camelcase: true,
immed: true,
latedef: true,
newcap: true,
nonew: true,
quotmark: 'single',
jquery: true,
maxparams: 5,
maxdepth: 4,
maxstatements: 25,
maxcomplexity: 10,
maxlen: 200,
// Don't validate third-party libraries
ignores: ['src/js/thirdParty/**/*.js']
},
files: ['src/js/**/*.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: {
// Update the version in manifest.json and package.json with the provided version
updateVersion: {
src: ['src/manifest.json', 'package.json'],
overwrite: true,
replacements: [{
from: /"version": "\d{0,3}.\d{0,3}"/,
to: '"version": "' + version + '"'
}]
},
// Not all Chrome permissions supported on other browsers (Opera). Remove them from manifest.json when building a release.
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: [{
// Find the line that looks like: "localDebug: true" and set it to false. Local debugging is for development only.
from: 'localDebug: true',
to: 'localDebug: false'
}]
},
// Remove development key and comments from manifest for deployment
transformManifest: {
src: ['dist/manifest.json'],
overwrite: true,
replacements: [{
// Remove manifest key because it can't be uploaded to the web store, but it's helpful to have in debugging to keep the extension ID stable.
from: /"key".*/,
to: function(match) {
// Don't remove key when testing because server will throw CORS errors.
return isDebug ? match : '';
}
}, {
// Remove comments because they can't be uploaded to the web store.
from: /\/\/ .*/ig,
to: ''
}]
}
},
compress: {
// Zip up browser-specific folders which are ready for release. The .zip file is then uploaded to the appropriate store
release: {
options: {
archive: '<%= meta.releaseDirectory %>Streamus v' + version + '.zip'
},
files: [{
expand: true,
cwd: '<%= meta.releaseDirectory %>',
src: ['**']
}]
}
},
clean: {
// Remove all non-English translations from the _locales folder (in Opera) because translation requirements are stricter than what I'm willing to fulfill.
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 %>'
}
},
concat: {
// TODO: This isn't really a concat anymore. Just a copy operation.
// 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'],
'dist/js/contentScript/youTubeIFrame.js': ['src/js/contentScript/youTubeIFrame.js']
}
}
},
watch: {
less: {
options: {
nospawn: true
},
files: ['src/less/*'],
tasks: ['less']
}
}
});
grunt.registerTask('build', 'Build release and place .zip files in /release directory.', function() {
// Ensure tests pass before performing any sort of bundling.
grunt.task.run('test');
if (!isDebug) {
grunt.task.run('replace:updateVersion');
}
grunt.task.run('requirejs', 'replace:transformManifest', 'replace:localDebug', 'concat:contentScripts', 'less', 'imagemin', 'clean:dist');
// Build chrome release
grunt.task.run('compressRelease:' + chromeReleaseDirectory);
// Build opera release
grunt.task.run('compressRelease:' + operaReleaseDirectory + ':sanitize=true');
});
grunt.registerTask('diffLocales', 'ensure that all of the message.json files located under _locales are in-sync with the English version', 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- '));
}
});
});
grunt.registerTask('default', 'An alias task for running tests.', ['test']);
grunt.registerTask('test', 'Run tests and code-quality analysis', ['diffLocales', 'jshint', 'recess']);
grunt.registerTask('compressRelease', 'A synchronous wrapper around compress:release', 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');
});
};