UNPKG

13.2 kBJavaScriptView Raw
1'use strict';
2
3/* global process:false */
4
5var _ = require('underscore');
6
7module.exports = function(grunt) {
8 require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
9 grunt.registerTask('runlog', function() {
10 grunt.log.write('http://localhost:8282/example/demo.html');
11 });
12
13 grunt.registerTask('awsconfig', function() {
14 var awsConfig;
15 try {
16 awsConfig = grunt.file.readJSON('grunt-aws.json');
17 }
18 catch (e) {
19 awsConfig = {};
20 }
21
22 awsConfig.key = (process.env.AWS_ACCESS_KEY_ID || awsConfig.key);
23 awsConfig.secret = (process.env.AWS_SECRET_ACCESS_KEY || awsConfig.secret);
24 awsConfig.bucket = (process.env.SK_JS_S3_BUCKET || awsConfig.bucket);
25
26 grunt.config.set('aws', awsConfig);
27 });
28
29 grunt.registerTask('maxcdnconfig', function() {
30 var maxCDN;
31 try {
32 maxCDN = grunt.file.readJSON('grunt-maxcdn.json');
33 }
34 catch (e) {
35 maxCDN = {};
36 }
37
38 maxCDN.companyAlias = (process.env.MAXCDN_COMPANY_ALIAS || maxCDN.companyAlias);
39 maxCDN.consumerKey = (process.env.MAXCDN_CONSUMER_KEY || maxCDN.consumerKey);
40 maxCDN.consumerSecret = (process.env.MAXCDN_CONSUMER_SECRET || maxCDN.consumerSecret);
41 maxCDN.zoneId = (process.env.MAXCDN_ZONE_ID || maxCDN.zoneId);
42
43 grunt.config.set('maxcdn.options', maxCDN);
44 });
45
46 // Project configuration
47 grunt.initConfig({
48 // Metadata
49 pkg: grunt.file.readJSON('package.json'),
50 license: grunt.file.read('LICENSE'),
51 globalVersion: '<%= pkg.version %>',
52 banner: '/*! \n\t<%= pkg.name %> <%= globalVersion %> \n\t<%= license %> \n*/\n',
53 // Task configuration
54 clean: ['dist/*'],
55 karma: {
56 unit: {
57 configFile: 'karma.conf.js',
58 singleRun: false,
59 browsers: ['PhantomJS', 'Chrome'],
60 reporters: ['progress']
61 },
62 ci: {
63 configFile: 'karma.conf.js',
64 singleRun: true,
65 browsers: ['PhantomJS']
66 }
67 },
68
69 watch: {
70 dev: {
71 files: ['src/js/**/*.js', '*.html', 'src/templates/*.tpl', 'src/stylesheets/*.less', 'config/config.json', 'example/template/*'],
72 tasks: ['devbuild'],
73 options: {
74 spawn: false,
75 }
76 },
77 min: {
78 files: ['src/js/**/*.js', '*.html', 'src/templates/*.tpl', 'src/stylesheets/*.less', 'config/config.json', 'example/template/*'],
79 tasks: ['devbuild:min'],
80 options: {
81 spawn: false,
82 },
83 }
84 },
85
86 uglify: {
87 options: {
88 banner: '<%= banner %>'
89 },
90 dist: {
91 src: 'dist/supportkit.js',
92 dest: 'dist/supportkit.min.js'
93 }
94 },
95
96 replace: {
97 dist: {
98 src: 'dist/supportkit.js',
99 dest: 'dist/supportkit.js',
100 replacements: [{
101 from: /var ROOT_URL = '.*';/,
102 to: 'var ROOT_URL = "<%= config.ROOT_URL %>";'
103 }]
104 },
105 demo: {
106 src: 'example/template/demo.html',
107 dest: 'example/demo.html',
108 replacements: [{
109 from: /APP_TOKEN/,
110 to: '<%= config.APP_TOKEN %>'
111 }, {
112 from: /GIVEN_NAME/,
113 to: '<%= config.GIVEN_NAME %>'
114 }, {
115 from: /JWT/,
116 to: '<%= config.JWT %>'
117 }, {
118 from: /USER_ID/,
119 to: '<%= config.USER_ID %>'
120 }, {
121 from: /SURNAME/,
122 to: '<%= config.SURNAME %>'
123 }, {
124 from: /EMAIL/,
125 to: '<%= config.EMAIL %>'
126 }, {
127 from: /WIDGET_CODE/,
128 to: '<%= config.WIDGET_CODE %>'
129 }]
130 }
131 },
132
133 'http-server': {
134 'dev': {
135 root: '.',
136 port: 8282,
137 host: '127.0.0.1',
138 showDir: true,
139 autoIndex: true,
140 ext: 'html',
141 runInBackground: false
142 }
143 },
144
145 browserify: {
146 dist: {
147 files: {
148 'dist/supportkit.js': ['src/js/main.js'],
149 }
150 },
151 options: {
152 browserifyOptions: {
153 debug: true,
154 'transform': [
155 'browserify-shim'
156 ],
157 standalone: 'SupportKit'
158 }
159 }
160 },
161
162 s3: {
163 options: {
164 key: '<%= aws.key %>',
165 secret: '<%= aws.secret %>',
166 bucket: '<%= aws.bucket %>',
167 access: 'public-read'
168 },
169 js: {
170 // Files to be uploaded.
171 upload: [{
172 src: 'dist/supportkit.min.js',
173 dest: 'supportkit.min.js'
174 }]
175 },
176 images: {
177 upload: [
178 {
179 src: 'src/images/**',
180 dest: 'images/',
181 options: {
182 gzip: true
183 }
184 }
185 ]
186 }
187 },
188
189 concurrent: {
190 dev: ['http-server', 'watch:dev'],
191 min: ['http-server', 'watch:min'],
192 options: {
193 logConcurrentOutput: true
194 }
195 },
196
197 maxcdn: {
198 purgeCache: {
199 options: {
200 companyAlias: '<%= maxcdn.options.companyAlias %>',
201 consumerKey: '<%= maxcdn.options.consumerKey %>',
202 consumerSecret: '<%= maxcdn.options.consumerSecret %>',
203 zone_id: '<%= maxcdn.options.zoneId %>',
204 method: 'delete'
205 },
206 files: [
207 { dest: '/supportkit.min.js' }
208 ],
209 },
210 },
211
212 release: {
213 options: {
214 npm: true,
215 bump: false,
216 commit: true,
217 push: false,
218 remote: 'https://github.com/supportkit/supportkit-js.git',
219 github: {
220 repo: 'supportkit/supportkit-js', //put your user/repo here
221 accessTokenVar: 'GITHUB_ACCESS_TOKEN',
222 releaseNotes: 'release_notes'
223 }
224 }
225 },
226
227 exec: {
228 createRelease: {
229 cmd: function() {
230 return [
231 'git checkout -b r/' + this.option('globalVersion')
232 ].join(' && ');
233 }
234 },
235 cleanRelease: {
236 cmd: function() {
237 return [
238 'git checkout master',
239 'git branch -D r/' + this.option('globalVersion'),
240 'git tag -d ' + this.option('globalVersion')
241 ].join(' && ');
242 }
243 },
244 commitFiles: {
245 cmd: function() {
246 return [
247 'git commit -am "Release v' + this.option('globalVersion') + ' [ci skip]"'
248 ].join(' && ');
249 }
250 },
251 push: {
252 cmd: function() {
253 return [
254 'git push origin master',
255 'git checkout integration',
256 'git merge master --no-ff',
257 'git push origin integration'
258 ].join(' && ');
259 }
260 },
261 addDist: {
262 cmd: function() {
263 return [
264 'git add --force dist/supportkit.js',
265 'git add --force dist/supportkit.min.js'
266 ].join(' && ');
267 }
268 }
269 },
270
271 gitinfo: {
272 commands: {
273 'status.porcelain': ['status', '--porcelain']
274 }
275 }
276 });
277
278 grunt.registerTask('checkBranchStatus', 'A task that ensures the correct branch is checked out and there are no working changes.', function() {
279 var gitInfo = grunt.config.get('gitinfo');
280
281 if (gitInfo.status.porcelain || gitInfo.local.branch.current.name !== 'master') {
282 grunt.log.error('Error. Please make sure you have master checked out and there are no working changes.');
283 grunt.log.error('Git Status:', '\n' + gitInfo.status.porcelain);
284 grunt.log.error('Git Branch: ', '\n ' + gitInfo.local.branch.current.name);
285 return false;
286 }
287
288 if (!process.env.GITHUB_ACCESS_TOKEN) {
289 grunt.log.error('Please set your github access token as env variables (GITHUB_ACCESS_TOKEN)');
290 return false;
291 }
292 });
293
294 grunt.registerTask('versionBump', function() {
295 var semver = require('semver');
296 var VERSION_REGEXP = /(\bversion[\'\"]?\s*[:=]\s*[\'\"])([\da-z\.-]+)([\'\"])/i;
297 var files = ['package.json', 'bower.json', 'src/js/main.js'];
298 var fullVersion = grunt.option('version');
299 var versionType = grunt.option('versionType');
300 var globalVersion;
301
302 files.forEach(function(file) {
303 var version = null;
304 var content = grunt.file.read(file).replace(VERSION_REGEXP, function(match, prefix, parsedVersion, suffix) {
305 version = fullVersion || semver.inc(parsedVersion, versionType);
306 return prefix + version + suffix;
307 });
308
309 if (!globalVersion) {
310 globalVersion = version;
311 } else if (globalVersion !== version) {
312 grunt.warn('Bumping multiple files with different versions!');
313 }
314
315 grunt.file.write(file, content);
316 grunt.log.ok('Version bumped to ' + version + (files.length > 1 ? ' (in ' + file + ')' : ''));
317 });
318
319 grunt.option('globalVersion', globalVersion);
320 grunt.config.set('globalVersion', globalVersion);
321
322 try {
323 grunt.file.read('release_notes/v' + globalVersion + '.md');
324 }
325 catch (err) {
326 grunt.log.error('Release notes not found.');
327 grunt.log.error('Please ensure release notes exist in the release_notes folder. (v' + globalVersion + '.md)');
328 return false;
329 }
330 });
331
332 grunt.registerTask('publish', 'Publishes a build to github and NPM, accepting a version as argument', function(version) {
333 if (!version || ['major', 'minor', 'patch'].indexOf(version) > -1) {
334 grunt.option('versionType', version || 'patch');
335 } else {
336 grunt.option('version', version);
337 }
338
339 grunt.task.run('branchCheck', 'publish:prepare', 'publish:release', 'publish:cleanup');
340 });
341
342 grunt.registerTask('loadConfig', 'Loads config from config folder (uses default if none present', function() {
343 var defaultConfig = grunt.file.readJSON('config/default/config.json');
344 var config = {};
345
346 try {
347 config = grunt.file.readJSON('config/config.json');
348 }
349 catch (err) {
350 grunt.log.warn('You might want to create a config with your app token at config/config.json');
351 }
352
353 var merged = _.extend(defaultConfig, config);
354 grunt.config.set('config', merged);
355 });
356
357 grunt.registerTask('setMinMode', function() {
358 grunt.config.set('config.WIDGET_CODE', 'supportkit.min.js');
359 });
360
361 grunt.registerTask('build', ['clean', 'browserify', 'uglify']);
362 grunt.registerTask('devbuild', ['clean', 'browserify', 'loadConfig', 'replace']);
363 grunt.registerTask('devbuild:min', ['clean', 'browserify', 'loadConfig', 'setMinMode', 'replace', 'uglify']);
364 grunt.registerTask('deploy', ['build', 'awsconfig', 'maxcdnconfig','s3:js', 'maxcdn']);
365 grunt.registerTask('run', ['runlog', 'devbuild', 'concurrent:dev']);
366 grunt.registerTask('run:min', ['runlog', 'devbuild:min', 'concurrent:min']);
367 grunt.registerTask('test', ['karma:unit']);
368 grunt.registerTask('test:ci', ['karma:ci']);
369 grunt.registerTask('default', ['run']);
370
371 grunt.registerTask('publish:prepare', ['versionBump', 'exec:commitFiles', 'exec:createRelease', 'build', 'exec:addDist']);
372 grunt.registerTask('publish:release', ['release']);
373 grunt.registerTask('publish:cleanup', ['exec:cleanRelease', 'exec:push']);
374
375 grunt.registerTask('branchCheck', 'Checks that you are publishing from Master branch with no working changes', ['gitinfo', 'checkBranchStatus']);
376
377 grunt.registerTask('cdnify', ['awsconfig', 's3:images']);
378};