UNPKG

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