use map-stream only
authorDan Allen <dan@opendevise.com>
Sat, 10 Feb 2018 11:49:36 +0000 (04:49 -0700)
committerDan Allen <dan@opendevise.com>
Sun, 11 Feb 2018 11:05:35 +0000 (04:05 -0700)
- use map-stream to apply a transform to a stream
- read UI model data asynchronously
- output message if no files are formatted

package.json
tasks/build-preview.js
tasks/build.js
tasks/lib/gulp-prettier-eslint.js
yarn.lock

index 3fe3bd5..534cb2a 100644 (file)
@@ -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",
index dd24112..262664d 100644 (file)
@@ -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'))
-}
index 780a54b..a17e3a6 100644 (file)
@@ -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())
index a19c2fc..ed79458 100644 (file)
@@ -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)
   }
 }
index 05572c9..bd91194 100644 (file)
--- 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"