grunt-hotbox-deploy
Version:
Grunt task for deploying front-end bundles to Hotbox
156 lines (136 loc) • 4.64 kB
JavaScript
/*
* grunt-hotbox-deploy
*
*
* Copyright (c) 2017 Egor Dydykin
* Licensed under the MIT license.
*/
;
var deployBranch = require('../lib/deployBranch');
var deployToProd = require('../lib/deployToProd');
var inquirer = require('inquirer');
var DEFAULT_CONFIG = {
punctuation: '.',
separator: ', ',
dest: '.build',
prodBranch: 'omega-test',
jsRegexp: /(['"])(\/static\/)(.*?['"])/g,
stringRegexp: /__%%STATIC_ROOT%%__/g,
templatesRegexp: /((?:src|href|srcset|poster)=['"]|url\(['"]?|\s\dx,\s*)\/static(\/.+?['"]?)/g,
contentTemplatesRegexp: /((?:content)=['"]|url\(['"]?|\s\dx,\s)\/static(\/.+?['"]?)/g,
stylesRegexp: /((?:url)\(['"]?)\/static(\/\S+?['"]?\))/g,
endpoint: 'hb.bizmrg.com',
bucket: '',
AccessKeyId: '',
SecretKey: '',
httpPrefix: '',
prodDest: '___prod',
debug: false
};
module.exports = function(grunt) {
function deploy(config, localConfig = {}) {
delete config.name;
delete localConfig.name;
var mergedConfig = Object.assign({}, DEFAULT_CONFIG, config, localConfig);
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options(mergedConfig);
var PROD_OPTION = 'deploy-to-prod-branch';
var deployToProdBranch = grunt.option(PROD_OPTION);
if (!deployToProdBranch) {
grunt.log.subhead('Deploying to test');
deployBranch(grunt, this, options);
} else {
if (typeof deployToProdBranch === 'string') {
options.prodBranch = deployToProdBranch.toLowerCase();
}
grunt.log.subhead('Deploying to prod branch ' + options.prodBranch);
deployToProd(grunt, this, options);
}
}
function askConfigName(names) {
return inquirer
.prompt([
{
type: 'list',
name: 'configName',
message: 'What config to use?',
choices: names
}
])
.then(({ configName }) => configName);
}
function getConfigByName(configs, name) {
return configs.find(config => config.name === name) || {};
}
function getMergedConfigsNames(configs1, configs2) {
return Object.keys(
configs1.concat(configs2).reduce((namesObj, { name }) => {
namesObj[name] = true;
return namesObj;
}, {})
);
}
grunt.loadNpmTasks('grunt-aws-s3');
// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks
grunt.registerMultiTask(
'hotbox_deploy',
'Grunt task for deploying front-end bundles to Hotbox',
function() {
deploy = deploy.bind(this);
var CONFIG = grunt.file.readJSON('aws-config.json');
var configs = Array.isArray(CONFIG) ? CONFIG : [CONFIG];
var LOCAL_CONFIG_NAME = 'config';
var enteredLocalConfigName = grunt.option(LOCAL_CONFIG_NAME);
var CONFIG_LOCAL;
try {
CONFIG_LOCAL = grunt.file.readJSON(
enteredLocalConfigName || 'aws-config.local.json'
);
} catch (e) {
if (enteredLocalConfigName) {
grunt.fail.fatal(`File ${enteredLocalConfigName} not found`);
}
}
var localConfigs = CONFIG_LOCAL || {};
if (!Array.isArray(localConfigs)) {
localConfigs = [localConfigs];
}
if (configs.length > 1 || localConfigs.length > 1) {
configs = configs.filter(({ name }) => name);
localConfigs = localConfigs.filter(({ name }) => name);
}
var config, localConfig;
var SELECT_CONFIG_OPTION = 'project';
var selectedConfigName = grunt.option(SELECT_CONFIG_OPTION);
if (selectedConfigName) {
config = getConfigByName(configs, selectedConfigName);
localConfig = getConfigByName(localConfigs, selectedConfigName);
if (!config.name && !localConfig.name) {
grunt.fail.fatal(
`Config with name ${selectedConfigName} not found in aws-config.json or aws-config.local.json`
);
} else {
deploy(config, localConfig);
}
} else if (
configs.length > 1 ||
localConfigs.length > 1 || (
Object.keys(localConfigs[0]).length &&
configs[0].name !== localConfigs[0].name
)
) {
var done = this.async();
var allNames = getMergedConfigsNames(configs, localConfigs);
askConfigName(allNames).then(name => {
config = getConfigByName(configs, name);
localConfig = getConfigByName(localConfigs, name);
deploy(config, localConfig);
done();
});
} else {
deploy(configs[0], localConfigs[0]);
}
}
);
};