From 3d81e78d4c1532412400d0ed4828b8d3707a3c97 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sun, 1 Oct 2017 22:13:34 -0600 Subject: [PATCH] format JavaScript using Prettier w/ ESLint rules --- gulpfile.js | 18 +++------- tasks/build-preview.js | 76 ++++++++++++++++++++++++--------------- tasks/build.js | 12 +++---- tasks/format.js | 2 +- tasks/lib/gulp-prettier-eslint.js | 6 ++-- 5 files changed, 63 insertions(+), 51 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 51082d0..23e0b0d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,20 +20,14 @@ const destDir = path.join(previewSiteDestDir, '_') const jsFiles = [ 'gulpfile.js', 'tasks/**/*.js', - path.join(srcDir, '{helpers,js}/**/*.js') + path.join(srcDir, '{helpers,js}/**/*.js'), ] -gulp.task('lint', () => - lint(jsFiles) -) +gulp.task('lint', () => lint(jsFiles)) -gulp.task('format', () => - format(jsFiles) -) +gulp.task('format', () => format(jsFiles)) -gulp.task('build', () => - build(srcDir, destDir) -) +gulp.task('build', () => build(srcDir, destDir)) gulp.task('build:preview', ['build'], () => buildPreview(srcDir, destDir, previewSiteSrcDir, previewSiteDestDir) @@ -49,8 +43,6 @@ gulp.task('preview', ['build:preview'], () => }) ) -gulp.task('pack', ['build', 'lint'], () => - pack(destDir, buildDir, bundleName) -) +gulp.task('pack', ['build', 'lint'], () => pack(destDir, buildDir, bundleName)) gulp.task('default', ['build']) diff --git a/tasks/build-preview.js b/tasks/build-preview.js index 47dc1b8..a78375a 100644 --- a/tasks/build-preview.js +++ b/tasks/build-preview.js @@ -17,27 +17,36 @@ module.exports = async (src, dest, siteSrc, siteDest) => { const mockUIModel = loadSampleUIModel(siteSrc) - vfs.src('**/*.html', { base: siteSrc, cwd: siteSrc }) - .pipe(map((file, next) => { - const compiledLayout = layouts['default.hbs'] - const siteRootPath = path.relative(path.dirname(file.path), path.resolve(siteSrc)) - mockUIModel['siteRootPath'] = siteRootPath - mockUIModel['siteRootUrl'] = path.join(siteRootPath, 'index.html') - mockUIModel['uiRootPath'] = path.join(siteRootPath, '_') - mockUIModel['contents'] = file.contents.toString().trimRight() - file.contents = Buffer.from(compiledLayout(mockUIModel)) - next(null, file) - })) + vfs + .src('**/*.html', { base: siteSrc, cwd: siteSrc }) + .pipe( + map((file, next) => { + const compiledLayout = layouts['default.hbs'] + const siteRootPath = path.relative( + path.dirname(file.path), + path.resolve(siteSrc) + ) + mockUIModel['siteRootPath'] = siteRootPath + mockUIModel['siteRootUrl'] = path.join(siteRootPath, 'index.html') + mockUIModel['uiRootPath'] = path.join(siteRootPath, '_') + mockUIModel['contents'] = file.contents.toString().trimRight() + file.contents = Buffer.from(compiledLayout(mockUIModel)) + next(null, file) + }) + ) .pipe(vfs.dest(siteDest)) } function registerPartials (src) { return new Promise((resolve, reject) => { - vfs.src(['partials/*.hbs'], { base: src, cwd: src }) - .pipe(map((file, next) => { - handlebars.registerPartial(file.stem, file.contents.toString()) - next(null, file) - })) + vfs + .src(['partials/*.hbs'], { base: src, cwd: src }) + .pipe( + map((file, next) => { + handlebars.registerPartial(file.stem, file.contents.toString()) + next(null, file) + }) + ) .on('error', reject) .on('end', resolve) }) @@ -45,12 +54,15 @@ function registerPartials (src) { function registerHelpers (src) { return new Promise((resolve, reject) => { - vfs.src(['helpers/*.js'], { base: src, cwd: src }) - .pipe(map((file, next) => { - const helperFunction = requireFromString(file.contents.toString()) - handlebars.registerHelper(file.stem, helperFunction) - next(null, file) - })) + vfs + .src(['helpers/*.js'], { base: src, cwd: src }) + .pipe( + map((file, next) => { + const helperFunction = requireFromString(file.contents.toString()) + handlebars.registerHelper(file.stem, helperFunction) + next(null, file) + }) + ) .on('error', reject) .on('end', resolve) }) @@ -59,16 +71,24 @@ function registerHelpers (src) { function compileLayouts (src) { const layouts = {} return new Promise((resolve, reject) => { - vfs.src('layouts/*.hbs', { base: src, cwd: src }) - .pipe(map((file, next) => { - layouts[file.basename] = handlebars.compile(file.contents.toString(), { preventIndent: true }) - next(null, file) - })) + vfs + .src('layouts/*.hbs', { base: src, cwd: src }) + .pipe( + map((file, next) => { + layouts[file.basename] = handlebars.compile( + file.contents.toString(), + { preventIndent: true } + ) + next(null, file) + }) + ) .on('error', reject) .on('end', () => resolve(layouts)) }) } function loadSampleUIModel (siteSrc) { - return yaml.safeLoad(fs.readFileSync(path.join(siteSrc, 'ui-model.yml'), 'utf8')) + return yaml.safeLoad( + fs.readFileSync(path.join(siteSrc, 'ui-model.yml'), 'utf8') + ) } diff --git a/tasks/build.js b/tasks/build.js index f5047cc..9709543 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -24,10 +24,10 @@ module.exports = (src, dest) => { const opts = { base: src, cwd: src } return merge([ - vfs.src('img/**/*.{jpg,png,svg}', opts) - .pipe(imagemin()), + vfs.src('img/**/*.{jpg,png,svg}', opts).pipe(imagemin()), - vfs.src('js/+([0-9])-*.js', opts) + vfs + .src('js/+([0-9])-*.js', opts) .pipe(uglify()) .pipe(concat('js/site.js')), @@ -35,14 +35,12 @@ module.exports = (src, dest) => { vfs.src('fonts/*.woff*(2)', opts), - vfs.src('css/site.css', opts) - .pipe(postcss(postcssPlugins)), + vfs.src('css/site.css', opts).pipe(postcss(postcssPlugins)), vfs.src('helpers/*.js', opts), vfs.src('layouts/*.hbs', opts), vfs.src('partials/*.hbs', opts), - ]) - .pipe(vfs.dest(dest)) + ]).pipe(vfs.dest(dest)) } diff --git a/tasks/format.js b/tasks/format.js index f2506d0..628408d 100644 --- a/tasks/format.js +++ b/tasks/format.js @@ -7,4 +7,4 @@ module.exports = (files) => vfs .src(files) .pipe(prettier()) - .pipe(vfs.dest(file => file.base)) + .pipe(vfs.dest((file) => file.base)) diff --git a/tasks/lib/gulp-prettier-eslint.js b/tasks/lib/gulp-prettier-eslint.js index ad90572..ae43eb1 100644 --- a/tasks/lib/gulp-prettier-eslint.js +++ b/tasks/lib/gulp-prettier-eslint.js @@ -21,13 +21,15 @@ module.exports = () => { } }) - function format(file, encoding, callback) { + 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')) + return callback( + new PluginError('gulp-prettier-eslint', 'Streaming not supported') + ) } const input = file.contents.toString('utf8') -- 2.11.0