From 7cd7e533007081c3df3b2d72f11be532773bda76 Mon Sep 17 00:00:00 2001 From: Dan Mindru Date: Tue, 3 Sep 2019 23:32:30 +0200 Subject: [PATCH] Refactor build to split up reading and gettings configs into utils --- tasks/build.js | 129 +++++++++---------------------- tasks/util/check-for-missing..js | 0 tasks/util/check-for-unused.js | 0 tasks/util/util.js | 69 +++++++++++++++++ 4 files changed, 106 insertions(+), 92 deletions(-) delete mode 100644 tasks/util/check-for-missing..js delete mode 100644 tasks/util/check-for-unused.js create mode 100644 tasks/util/util.js diff --git a/tasks/build.js b/tasks/build.js index 3d27646..ea92b30 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -4,15 +4,12 @@ const minifyHTML = require('gulp-minify-html'); const minifyInline = require('gulp-minify-inline'); const preprocess = require('gulp-preprocess'); const rename = require('gulp-rename'); -const klaw = require('klaw'); -const fs = require('fs'); const del = require('del'); const inlineimg = require('gulp-inline-images-no-http'); const path = require('path'); +const { getConfigsForDir, getFilePathsForDir } = require('./util/util'); function buildTask(options) { - const { configurationFile } = options; - // Requires: 'dupe', 'less', 'sass', 'postcss', 'lint'. gulp.task('build', function build(done) { /** @@ -23,68 +20,45 @@ function buildTask(options) { * @param {Array} confItems A list of configurations objects (usually persons) to make templates from. */ function makeTemplates(dir, confItems) { - return confItems.map(conf => { - const cwd = options.workingDir + '/' + dir; - const files = []; + return confItems.map(async conf => { + const cwd = `${options.workingDir}/${dir}`; /** * Find stylesheets relative to the CWD & generate tags. * This way we can automagically inject them into . */ - return new Promise(resolve => { - klaw(cwd) - .on('readable', function walkTemplateDir() { - let file; - - while ((file = this.read())) { - const relativePath = - __dirname.substring(0, __dirname.lastIndexOf('/')) + - '/tmp/' + - dir; - files.push(file.path.replace(relativePath, '')); - } - }) - .on('end', function finishedTemplateDirWalk() { - /** - * Creates a context of stylesheets to inject them into HTML files later. - * Generates a list of tags with the found stylesheets. - */ - const context = Object.assign(conf, { - stylesheets: files - .filter(file => !!file.match(/.*\.css/)) // Read only CSS files. - .reduce((acc, cur) => { - const cssPath = path.win32.basename(cur); - return (acc += - ''); - }, '') - }); - - options - .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) - .pipe(preprocess({ context })) - .pipe(inlineimg()) - .pipe( - inlineCss({ - applyTableAttributes: true, - applyWidthAttributes: true, - preserveMediaQueries: true, - removeStyleTags: false - }) - ) - .pipe(minifyHTML({ quotes: true })) - .pipe(minifyInline()) - .pipe( - rename(function rename(path) { - path.dirname = dir; - path.basename += '-' + conf.id; - return path; - }) - ) - .pipe(gulp.dest(options.dist)); - - resolve(); - }); + const files = await getFilePathsForDir(cwd); + const context = Object.assign(conf, { + stylesheets: files + .filter(file => !!file.match(/.*\.css/)) // Read only CSS files. + .reduce((acc, cur) => { + const cssPath = path.win32.basename(cur); + return (acc += ''); + }, '') }); + + return options + .src([cwd + '/**/*.html', '!' + cwd + '/**/*.inc.html']) + .pipe(preprocess({ context })) + .pipe(inlineimg()) + .pipe( + inlineCss({ + applyTableAttributes: true, + applyWidthAttributes: true, + preserveMediaQueries: true, + removeStyleTags: false + }) + ) + .pipe(minifyHTML({ quotes: true })) + .pipe(minifyInline()) + .pipe( + rename(function rename(path) { + path.dirname = dir; + path.basename += '-' + conf.id; + return path; + }) + ) + .pipe(gulp.dest(options.dist)); }); } @@ -93,42 +67,13 @@ function buildTask(options) { * For each found config, a template group will be generated through `makeTemplates`. */ return del(options.dist) - .then(function buildStart() { + .then(() => { /** * Loop through dirs and load their conf files. * Promisify all 'makeTemplate' calls and when resolved, let gulp know we're done. */ - const promises = []; - - fs.readdirSync(options.workingDir).forEach(function readConfigurations( - dir - ) { - const confPath = `../tmp/${dir}/${configurationFile}`; - - /** Exit with warn if no configuration file found. */ - if (!fs.existsSync(path.resolve(options.workingDir, confPath))) { - return console.warn( - `Missing configuration in "${dir}". Did you remember to create "${dir}/${configurationFile}"?` - ); - } - - let current = null; - let confItems; - - delete require.cache[require.resolve(confPath)]; // NB: For 'watch' to properly work, the cache needs to be deleted before each require. - current = require(confPath); - - // Handle single objects or arrays of configs. - if (current && current.length) { - confItems = [...current]; - } else { - confItems = [current]; - } - - promises.push(makeTemplates(dir, confItems)); - }); - - Promise.all(promises); + const configs = getConfigsForDir(options.workingDir, options.configurationFile); + return Promise.all(configs.map(({ dir, confItems }) => makeTemplates(dir, confItems))); }) .then(() => done()) .catch(err => console.log(err)); diff --git a/tasks/util/check-for-missing..js b/tasks/util/check-for-missing..js deleted file mode 100644 index e69de29..0000000 diff --git a/tasks/util/check-for-unused.js b/tasks/util/check-for-unused.js deleted file mode 100644 index e69de29..0000000 diff --git a/tasks/util/util.js b/tasks/util/util.js new file mode 100644 index 0000000..57a3294 --- /dev/null +++ b/tasks/util/util.js @@ -0,0 +1,69 @@ +const fs = require('fs'); +const path = require('path'); +const klaw = require('klaw'); + +/** + * Given a directory, scans all directories in it (not deep) and returns found config items. + * + * @param { string } rootDir Dir to look into. + * @param { string } configFileName Files to look for in each dir, i.e. conf.json + */ +const getConfigsForDir = (rootDir, configFileName) => { + return fs + .readdirSync(rootDir) + .map(dir => { + const confPath = `${dir}/${configFileName}`; + + /** Exit with warn if no configuration file found. */ + if (!fs.existsSync(path.resolve(rootDir, confPath))) { + console.warn(`Missing configuration in "${dir}". Did you remember to create "${dir}/${configFileName}"?`); + return false; + } + + let current = null; + let confItems; + + delete require.cache[require.resolve(rootDir, confPath)]; // NB: For 'watch' to properly work, the cache needs to be deleted before each require. + current = require(path.resolve(rootDir, confPath)); + + // Handle single objects or arrays of configs. + if (current && current.length) { + confItems = [...current]; + } else { + confItems = [current]; + } + + return { + dir, + confItems + }; + }) + .filter(config => config); +}; + +/** + * Given a directory, gets all file paths in it. + */ +const getFilePathsForDir = dir => { + const files = []; + + return new Promise(resolve => { + klaw(dir) + .on('readable', function walkTemplateDir() { + let file; + + while ((file = this.read())) { + const relativePath = `${__dirname.substring(0, __dirname.lastIndexOf('/'))}/${dir}`; + files.push(file.path.replace(relativePath, '')); + } + }) + .on('end', function finishedTemplateDirWalk() { + resolve(files); + }); + }); +}; + +module.exports = { + getConfigsForDir, + getFilePathsForDir +};