From 13ba026692e97ca1fe74f57de1919c0cd6b3497e Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sat, 10 Feb 2018 04:49:36 -0700 Subject: [PATCH] use map-stream only - use map-stream to apply a transform to a stream - read UI model data asynchronously - output message if no files are formatted --- package.json | 1 - tasks/build-preview.js | 22 +++++++++++++--------- tasks/build.js | 5 +++-- tasks/lib/gulp-prettier-eslint.js | 21 +++++++++------------ yarn.lock | 6 ------ 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 3fe3bd5..534cb2a 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "gulp-imagemin": "^4.0.0", "gulp-postcss": "^7.0.0", "gulp-stylelint": "^5.0.0", - "gulp-tap": "^1.0.1", "gulp-uglify": "^3.0.0", "gulp-vinyl-zip": "^2.1.0", "handlebars": "^4.0.10", diff --git a/tasks/build-preview.js b/tasks/build-preview.js index dd24112..262664d 100644 --- a/tasks/build-preview.js +++ b/tasks/build-preview.js @@ -4,14 +4,18 @@ const fs = require('fs') const handlebars = require('handlebars') const map = require('map-stream') const path = require('path') +const { promisify } = require('util') const requireFromString = require('require-from-string') const vfs = require('vinyl-fs') const yaml = require('js-yaml') module.exports = async (src, dest, siteSrc, siteDest) => { - const [layouts] = await Promise.all([compileLayouts(src), registerPartials(src), registerHelpers(src)]) - - const uiModel = loadSampleUiModel(siteSrc) + const [uiModel, layouts] = await Promise.all([ + loadSampleUiModel(siteSrc), + compileLayouts(src), + registerPartials(src), + registerHelpers(src), + ]) vfs .src('**/*.html', { base: siteSrc, cwd: siteSrc }) @@ -30,10 +34,14 @@ module.exports = async (src, dest, siteSrc, siteDest) => { .pipe(vfs.dest(siteDest)) } +function loadSampleUiModel (siteSrc) { + return promisify(fs.readFile)(path.join(siteSrc, 'ui-model.yml'), 'utf8').then((contents) => yaml.safeLoad(contents)) +} + function registerPartials (src) { return new Promise((resolve, reject) => { vfs - .src(['partials/*.hbs'], { base: src, cwd: src }) + .src('partials/*.hbs', { base: src, cwd: src }) .pipe( map((file, next) => { handlebars.registerPartial(file.stem, file.contents.toString()) @@ -48,7 +56,7 @@ function registerPartials (src) { function registerHelpers (src) { return new Promise((resolve, reject) => { vfs - .src(['helpers/*.js'], { base: src, cwd: src }) + .src('helpers/*.js', { base: src, cwd: src }) .pipe( map((file, next) => { const helperFunction = requireFromString(file.contents.toString()) @@ -76,7 +84,3 @@ function compileLayouts (src) { .on('end', () => resolve(layouts)) }) } - -function loadSampleUiModel (siteSrc) { - return yaml.safeLoad(fs.readFileSync(path.join(siteSrc, 'ui-model.yml'), 'utf8')) -} diff --git a/tasks/build.js b/tasks/build.js index 780a54b..a17e3a6 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -7,6 +7,7 @@ const concat = require('gulp-concat') const cssnano = require('cssnano') const fs = require('fs') const imagemin = require('gulp-imagemin') +const map = require('map-stream') const merge = require('merge-stream') const mkdirp = require('mkdirp') const path = require('path') @@ -15,7 +16,6 @@ const postcssCalc = require('postcss-calc') const postcssImport = require('postcss-import') const postcssUrl = require('postcss-url') const postcssVar = require('postcss-custom-properties') -const tap = require('gulp-tap') const uglify = require('gulp-uglify') const vfs = require('vinyl-fs') @@ -58,8 +58,9 @@ module.exports = (src, dest) => { .src('js/vendor/*.js', Object.assign({ read: false }, opts)) .pipe( // see https://gulpjs.org/recipes/browserify-multiple-destination.html - tap((file) => { + map((file, next) => { file.contents = browserify(file.relative, { basedir: src, detectGlobals: false }).bundle() + next(null, file) }) ) .pipe(buffer()) diff --git a/tasks/lib/gulp-prettier-eslint.js b/tasks/lib/gulp-prettier-eslint.js index a19c2fc..ed79458 100644 --- a/tasks/lib/gulp-prettier-eslint.js +++ b/tasks/lib/gulp-prettier-eslint.js @@ -2,11 +2,11 @@ const { PluginError } = require('gulp-util') const prettierEslint = require('prettier-eslint') -const through = require('through2') +const map = require('map-stream') module.exports = () => { const report = { changed: 0, unchanged: 0 } - return through.obj(format).on('end', () => { + return map(format).on('end', () => { if (report.changed > 0) { const changed = 'formatted ' .concat(report.changed) @@ -18,17 +18,14 @@ module.exports = () => { .concat(report.unchanged === 1 ? '' : 's') .concat(' unchanged') console.log(`prettier-eslint: ${changed}; ${unchanged}`) + } else { + console.log(`prettier-eslint: left ${report.unchanged} file${report.unchanged === 1 ? '' : 's'} unchanged`) } }) - function format (file, encoding, callback) { - if (file.isNull()) { - return callback(null, file) - } - - if (file.isStream()) { - return callback(new PluginError('gulp-prettier-eslint', 'Streaming not supported')) - } + function format (file, next) { + if (file.isNull()) return next() + if (file.isStream()) return next(new PluginError('gulp-prettier-eslint', 'Streaming not supported')) const input = file.contents.toString() const output = prettierEslint({ text: input }) @@ -37,9 +34,9 @@ module.exports = () => { report.unchanged += 1 } else { report.changed += 1 - file.contents = Buffer.from(output, encoding) + file.contents = Buffer.from(output) } - return callback(null, file) + next(null, file) } } diff --git a/yarn.lock b/yarn.lock index 05572c9..bd91194 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2546,12 +2546,6 @@ gulp-stylelint@^5.0.0: stylelint "^8.0.0" through2 "^2.0.3" -gulp-tap@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/gulp-tap/-/gulp-tap-1.0.1.tgz#e671124e1259b4cea219ed1ca97b7f585c334690" - dependencies: - through2 "^2.0.3" - gulp-uglify@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/gulp-uglify/-/gulp-uglify-3.0.0.tgz#0df0331d72a0d302e3e37e109485dddf33c6d1ca" -- 2.11.0