UNPKG

adgile

Version:

An easy-to-use automated front-end setup.

159 lines (132 loc) 3.79 kB
'use strict' /** * A module containing helper functions * @module helpers */ let settings = require('../settings.default'); const chalk = require('chalk'), _ = require('lodash'), stripAnsi = require('strip-ansi'), open = require('open'), fs = require('fs-extra'), globule = require('globule'); /** * Log message to console in verbose mode * @param {string} msg - The message */ function verbose (msg) { if(settings.isVerbose) console.log(msg); } /** * Check format of the console message before writing it out * @param {string} msg - The message */ function validForWrite (msg) { let cleanMsg = stripAnsi(msg); let allowFlag = false; let cleanMsgComponents = cleanMsg.split(' '); // Style changed file output if (cleanMsg.indexOf('was changed') > -1) { msg = cleanMsg; msg = chalk.green(msg); } if(cleanMsgComponents[0] === 'directory') { // Unnecessary log return false; } if(cleanMsg.indexOf('No files matched') > -1) { msg = chalk.keyword('orange').inverse('Warning: ' + msg); } // Detect output of gulp-imagemin if(cleanMsg.indexOf('gulp-imagemin: Minified') > -1) { msg = cleanMsg.split('gulp-imagemin:').pop().trim(); msg = chalk.green('✄ ' + msg) + '\n'; } // Suppress Gulp Size output if (cleanMsgComponents[0] === 'all' && cleanMsgComponents[1] === 'files') { return false; } // Detect gulp-util "[XX:XX:XX] ..." logs, if (/^\[[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\]/.test(cleanMsg)) { // Allow gulp-plumber errors, // but format them a bit if (!allowFlag && cleanMsg.indexOf('Plumber found') > - 1) { msg = cleanMsg.split('Plumber found unhandled error:').pop().trim(); msg = chalk.red.inverse('ERROR') + ' ' + msg + '\n'; allowFlag = true; } // Allow W3C validation errors, // but format them a bit if (!allowFlag && cleanMsg.indexOf('HTML Error:') > -1) { msg = cleanMsg.split('HTML Error:').pop().trim(); msg = chalk.red.inverse('HTML ERROR') + ' ' + msg + '\n'; allowFlag = true; } // Block all the others if (!allowFlag) { return false; } } return msg; } /** * Open cwd in editor designated in config.json */ function openEditor() { console.log( chalk.cyan('☞ Editing in'), chalk.magenta(config.editor) ); open(settings.cwd, { app: config.editor }); } /** * Handle change events for Gulp watch instances * @param {string} path */ function watchHandler (path) { // console.log(chalk.grey('"' + e.path.split('/').pop() + '" was ' + e.type)); console.log(chalk.grey('"' + path.split('/').pop() + '" was changed.')); } /** * Update the loading bar in terminal if one is set */ function updateBar (msg) { if (!settings.isVerbose && settings.bar !== null) { settings.bar.tick({ 'step': msg }); } } /** * Get all subdirectories for a variable number of given source folders. * @param {...any} sources */ // function getAllDirectories (...sources) { // let globArray = []; // for (let src of sources) { // globArray = globArray.concat(globule.find('./templates/' + src + '/**', '!./templates/' + src + '/**/*.*')); // } // return globArray; // } /** * Get all subdirectories for a variable number of given source folders. * @param {...any} sources */ function getAllPartials (...sources) { let globArray = []; let partials; for (let src of sources) { globArray = globArray.concat(globule.find('./templates/' + src + '/**/*.html')); } if (globArray.length > 0) { partials = Object.assign(...globArray.map(key => ({ [key.replace('./templates/', '').replace('.html', '')]: fs.readFileSync(key, 'utf8') }))); } return partials; } module.exports = { verbose, validForWrite, openEditor, watchHandler, updateBar, // getAllDirectories, getAllPartials }