UNPKG

grunt-hotbox-deploy

Version:

Grunt task for deploying front-end bundles to Hotbox

192 lines (156 loc) 5.22 kB
var gitInfo = require('./gitInfo'); var path = require('path'); module.exports = function(grunt, task, options) { grunt.log.subhead('Delete old build files'); grunt.file.delete(options.dest); var HTTP_PREFIX = task.data.httpPrefix || options.httpPrefix || ('https://' + options.bucket + '.' + options.endpoint + '/'); var TEMPLATES_PATH = path.join(task.data.forcedPath || gitInfo.GIT_BRANCH, 'templates'); var STATIC_PATH = path.join(task.data.forcedPath || gitInfo.GIT_BRANCH, 'static'); var VERSIONED_STATIC_PATH = path.join(STATIC_PATH, task.data.forcedPath || gitInfo.GIT_COMMIT); var staticFiles = []; var versionedStaticFiles = []; var templateFiles = []; if (task.data.versionedStatic && task.data.versionedStatic.files) { task.data.versionedStatic.files.forEach(function(files){ versionedStaticFiles.push({ expand: true, cwd: files.cwd, src: files.src, dest: path.join(options.dest, VERSIONED_STATIC_PATH) }); }); } if (task.data.static && task.data.static.files) { task.data.static.files.forEach(function(files){ staticFiles.push({ expand: true, cwd: files.cwd, src: files.src, dest: path.join(options.dest, STATIC_PATH) }); }); } if (task.data.templates && task.data.templates.files) { task.data.templates.files.forEach(function(files){ templateFiles.push({ expand: true, cwd: files.cwd, src: files.src, dest: path.join(options.dest, TEMPLATES_PATH) }); }); } grunt.log.subhead('Copy files to build dir'); var buildFiles = templateFiles.concat(staticFiles).concat(versionedStaticFiles); buildFiles.forEach(function(files) { var paths = grunt.file.expandMapping(files.src, files.dest, files); paths.forEach(function(file) { grunt.file.copy(file.src[0], file.dest); grunt.log.writeln(file.src[0] + ' -> ' + file.dest); }); }); grunt.log.subhead('Make replacements in js'); var filesToReplace; filesToReplace = grunt.file.expandMapping(['**/*.js', "**/*.css"], '/', { expand: true, cwd: path.join(options.dest) }); filesToReplace.forEach(function(file) { file = file.src[0]; var content = grunt.file.read(file); content = content.replace(options.jsRegexp, '$1' + HTTP_PREFIX + VERSIONED_STATIC_PATH + '/$3'); content = content.replace(options.stringRegexp, HTTP_PREFIX + VERSIONED_STATIC_PATH + '/'); grunt.file.write(file, content); grunt.log.writeln('Replaced paths in ' + file); }); grunt.log.subhead('Make replacements in templates'); filesToReplace = grunt.file.expandMapping(['**/*.*'], '/', { expand: true, cwd: path.join(options.dest, TEMPLATES_PATH) }); filesToReplace.forEach(function(file) { file = file.src[0]; var content = grunt.file.read(file); content = content.replace(options.templatesRegexp, '$1' + HTTP_PREFIX + VERSIONED_STATIC_PATH + '$2'); content = content.replace(options.contentTemplatesRegexp, '$1' + HTTP_PREFIX + STATIC_PATH + '$2'); grunt.file.write(file, content); grunt.log.writeln('Replaced paths in ' + file); }); grunt.log.subhead('Make replacements in css'); filesToReplace = grunt.file.expandMapping(['**/*.css'], '/', { expand: true, cwd: path.join(options.dest, STATIC_PATH) }); filesToReplace.forEach(function(file) { file = file.src[0]; var content = grunt.file.read(file); content = content.replace(options.stylesRegexp, '$1' + HTTP_PREFIX + VERSIONED_STATIC_PATH + '$2'); grunt.file.write(file, content); grunt.log.writeln('Replaced paths in ' + file); }); grunt.config.set('aws_s3.hotbox_deploy_static', { options: { accessKeyId: options.AccessKeyId, secretAccessKey: options.SecretKey, uploadConcurrency: 25, bucket: options.bucket, endpoint: options.endpoint, differential: true, debug: options.debug, params: { CacheControl: "max-age=0, no-cache, no-store, must-revalidate" } }, files: [{ action: 'upload', expand: true, cwd: '.build', src: [STATIC_PATH + '/**/**.*', '!' + VERSIONED_STATIC_PATH + '/**/**.*'], dest: '/' }] }); grunt.config.set('aws_s3.hotbox_deploy_static_versioned', { options: { accessKeyId: options.AccessKeyId, secretAccessKey: options.SecretKey, uploadConcurrency: 25, bucket: options.bucket, endpoint: options.endpoint, differential: true, debug: options.debug, params: { CacheControl: "max-age=315360000, must-revalidate" } }, files: [{ action: 'upload', expand: true, cwd: '.build', src: [VERSIONED_STATIC_PATH + '/**/**.*'], dest: '/' }] }); grunt.config.set('aws_s3.hotbox_deploy_templates', { options: { accessKeyId: options.AccessKeyId, secretAccessKey: options.SecretKey, uploadConcurrency: 25, bucket: options.bucket, endpoint: options.endpoint, differential: true, params: { CacheControl: "max-age=0, no-cache, no-store, must-revalidate" } }, files: [{ action: 'upload', expand: true, cwd: '.build', src: TEMPLATES_PATH + '/**/**.*', dest: '/' }] }); if (!task.data.onlyBuild) { grunt.task.run(['aws_s3:hotbox_deploy_static', 'aws_s3:hotbox_deploy_static_versioned', 'aws_s3:hotbox_deploy_templates']); } };