adgile
Version:
An easy-to-use automated front-end setup.
76 lines (63 loc) • 2.49 kB
JavaScript
/**
* A module for running the `adg build` task
* @module task.build
*/
let settings = require('../settings.default');
const chalk = require('chalk'),
_ = require('lodash'),
fs = require('fs-extra'),
ProgressBar = require('progress'),
gulp = require('gulp');
/**
* Run the build task in response to Gulp
* Load the config.json file and parse it as JSON
* Allow customization of gulp-htmlmin and folder names through the config file
* Create a progressbar to inform the user of this process (when not in verbose mode)
* Finally, run all gulp-related tasks in sequence, ending with serving the files if a --serve flag was given
* @param {*} cb
*/
function runtask(cb) {
console.log(chalk.grey('\nLoading config.json...'));
fs.readFile('config.json', 'utf8', (err, data) => {
if (err) {
console.log(chalk.red('✘ Cannot find config.json. Have you initiated Adgile through `adg init`?'));
process.exit(0);
}
try {
config = JSON.parse(data);
} catch (err) {
console.log(chalk.red('✘ The config.json file is not valid json. Aborting.'));
process.exit(0);
}
if (!_.isNull(config.htmlminOptions) && !_.isUndefined(config.htmlminOptions)) {
settings.htmlminOptions = _.assign(settings.htmlminOptions, config.htmlminOptions);
}
// If no assetsFolder is set in config.json, use the fallback from settings.
settings.assetsFolder = config.assetsFolder || settings.assetsFolder;
if (!settings.isVerbose) {
settings.bar = new ProgressBar(chalk.grey('Building ' + (settings.isProduction ? 'production' : 'development') + ' version [:bar] :percent :step.'), {
complete: '=',
incomplete: ' ',
total: 7, // Equal to number of gulp tasks in sequence below
width: 21,
clear: true,
callback: () => { console.log(chalk.grey('Building ' + (settings.isProduction ? 'production' : 'development') + ' version [=====================] 100% Done.\n')); }
});
} else {
console.log(chalk.grey('Building ' + (settings.isProduction ? 'production' : 'development') + ' version...'));
}
let nextTasks = gulp.series(['clean:export', 'sass', 'scripts', 'images', 'other', 'templates', 'uncss'], () => {
console.log(chalk.green('✔ Build complete'));
if(settings.isServe) {
gulp.series('server')();
}
settings.builtFlag = true;
cb(null);
});
nextTasks();
});
}
module.exports = {
runtask
}