From: Dan Allen Date: Wed, 29 May 2019 08:24:46 +0000 (-0600) Subject: reorganize build X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=07780e020e738c576cde27c89544838a1b3516bb;p=working%2Feg-antora.git reorganize build - move gulpfile.js folder to gulp.d - move gulpfile.js/index.js back to gulpfile.js - rename task to createTask - import tasks under task namespace --- diff --git a/gulp.d/lib/create-task.js b/gulp.d/lib/create-task.js new file mode 100644 index 0000000..8c90062 --- /dev/null +++ b/gulp.d/lib/create-task.js @@ -0,0 +1,24 @@ +'use strict' + +const metadata = require('undertaker/lib/helpers/metadata') +const { watch } = require('gulp') + +module.exports = ({ name, desc, opts, call: fn, loop }) => { + if (name) { + const displayName = fn.displayName + if (displayName === '' || displayName === '') { + metadata.get(fn).tree.label = `${displayName} ${name}` + } + fn.displayName = name + } + if (loop) { + const delegate = fn + name = delegate.displayName + delegate.displayName = `${name}:loop` + fn = () => watch(loop, { ignoreInitial: false }, delegate) + fn.displayName = name + } + if (desc) fn.description = desc + if (opts) fn.flags = opts + return fn +} diff --git a/gulp.d/lib/export-tasks.js b/gulp.d/lib/export-tasks.js new file mode 100644 index 0000000..7c9de48 --- /dev/null +++ b/gulp.d/lib/export-tasks.js @@ -0,0 +1,14 @@ +'use strict' + +module.exports = (...tasks) => { + const seed = {} + if (tasks.length) { + if (tasks.lastIndexOf(tasks[0]) > 0) { + const task1 = tasks.shift() + seed.default = Object.assign(task1.bind(null), { description: `=> ${task1.displayName}`, displayName: 'default' }) + } + return tasks.reduce((acc, it) => (acc[it.displayName || it.name] = it) && acc, seed) + } else { + return seed + } +} diff --git a/gulp.d/lib/gulp-prettier-eslint.js b/gulp.d/lib/gulp-prettier-eslint.js new file mode 100644 index 0000000..dca0a74 --- /dev/null +++ b/gulp.d/lib/gulp-prettier-eslint.js @@ -0,0 +1,42 @@ +'use strict' + +const { obj: map } = require('through2') +const PluginError = require('plugin-error') +const prettierEslint = require('prettier-eslint') + +module.exports = () => { + const report = { changed: 0, unchanged: 0 } + return map(format).on('finish', () => { + if (report.changed > 0) { + const changed = 'formatted ' + .concat(report.changed) + .concat(' file') + .concat(report.changed === 1 ? '' : 's') + const unchanged = 'left ' + .concat(report.unchanged) + .concat(' file') + .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, enc, 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, filePath: file.path }) + + if (input === output) { + report.unchanged += 1 + } else { + report.changed += 1 + file.contents = Buffer.from(output) + } + + next(null, file) + } +} diff --git a/gulp.d/tasks/build-preview-pages.js b/gulp.d/tasks/build-preview-pages.js new file mode 100644 index 0000000..12f3219 --- /dev/null +++ b/gulp.d/tasks/build-preview-pages.js @@ -0,0 +1,105 @@ +'use strict' + +const asciidoctor = require('asciidoctor.js')() +const fs = require('fs-extra') +const handlebars = require('handlebars') +const { obj: map } = require('through2') +const merge = require('merge-stream') +const ospath = require('path') +const path = ospath.posix +const requireFromString = require('require-from-string') +const vfs = require('vinyl-fs') +const yaml = require('js-yaml') + +const ASCIIDOC_ATTRIBUTES = { + experimental: '', + icons: 'font', + sectanchors: '', + 'source-highlighter': 'highlight.js', +} + +module.exports = (src, previewSrc, previewDest, sink = () => map(), layouts = {}) => () => + Promise.all([ + loadSampleUiModel(previewSrc), + toPromise( + merge( + compileLayouts(src, layouts), + registerPartials(src), + registerHelpers(src), + copyImages(previewSrc, previewDest) + ) + ), + ]) + .then(([baseUiModel]) => Object.assign(baseUiModel, { env: process.env })) + .then((baseUiModel) => + vfs + .src('**/*.adoc', { base: previewSrc, cwd: previewSrc }) + .pipe( + map((file, enc, next) => { + const siteRootPath = path.relative(ospath.dirname(file.path), ospath.resolve(previewSrc)) + const uiModel = Object.assign({}, baseUiModel) + uiModel.page = Object.assign({}, uiModel.page) + uiModel.siteRootPath = siteRootPath + uiModel.siteRootUrl = path.join(siteRootPath, 'index.html') + uiModel.uiRootPath = path.join(siteRootPath, '_') + if (file.stem === '404') { + uiModel.page = { layout: '404', title: 'Page Not Found' } + } else { + const doc = asciidoctor.load(file.contents, { safe: 'safe', attributes: ASCIIDOC_ATTRIBUTES }) + uiModel.page.attributes = Object.entries(doc.getAttributes()) + .filter(([name, val]) => name.startsWith('page-')) + .reduce((accum, [name, val]) => { + accum[name.substr(5)] = val + return accum + }, {}) + uiModel.page.layout = doc.getAttribute('page-layout', 'default') + uiModel.page.title = doc.getDocumentTitle() + uiModel.page.contents = Buffer.from(doc.convert()) + } + file.extname = '.html' + file.contents = Buffer.from(layouts[uiModel.page.layout](uiModel)) + next(null, file) + }) + ) + .pipe(vfs.dest(previewDest)) + .pipe(sink()) + ) + +function loadSampleUiModel (src) { + return fs.readFile(ospath.join(src, 'ui-model.yml'), 'utf8').then((contents) => yaml.safeLoad(contents)) +} + +function registerPartials (src) { + return vfs.src('partials/*.hbs', { base: src, cwd: src }).pipe( + map((file, enc, next) => { + handlebars.registerPartial(file.stem, file.contents.toString()) + next() + }) + ) +} + +function registerHelpers (src) { + return vfs.src('helpers/*.js', { base: src, cwd: src }).pipe( + map((file, enc, next) => { + handlebars.registerHelper(file.stem, requireFromString(file.contents.toString())) + next() + }) + ) +} + +function compileLayouts (src, layouts) { + return vfs.src('layouts/*.hbs', { base: src, cwd: src }).pipe( + map((file, enc, next) => { + layouts[file.stem] = handlebars.compile(file.contents.toString(), { preventIndent: true }) + next() + }) + ) +} + +function copyImages (src, dest) { + return vfs.src('**/*.{png,svg}', { base: src, cwd: src }).pipe(vfs.dest(dest)) +} + +function toPromise (stream) { + return new Promise((resolve, reject) => stream.on('error', reject).on('finish', resolve)) +} diff --git a/gulp.d/tasks/build.js b/gulp.d/tasks/build.js new file mode 100644 index 0000000..509c275 --- /dev/null +++ b/gulp.d/tasks/build.js @@ -0,0 +1,77 @@ +'use strict' + +const autoprefixer = require('autoprefixer') +const browserify = require('browserify') +const buffer = require('vinyl-buffer') +const concat = require('gulp-concat') +const cssnano = require('cssnano') +const fs = require('fs-extra') +const imagemin = require('gulp-imagemin') +const { obj: map } = require('through2') +const merge = require('merge-stream') +const ospath = require('path') +const path = ospath.posix +const postcss = require('gulp-postcss') +const postcssCalc = require('postcss-calc') +const postcssImport = require('postcss-import') +const postcssUrl = require('postcss-url') +const postcssVar = require('postcss-custom-properties') +const uglify = require('gulp-uglify') +const vfs = require('vinyl-fs') + +module.exports = (src, dest, preview) => () => { + const opts = { base: src, cwd: src } + const postcssPlugins = [ + postcssImport(), + postcssUrl([ + { + filter: '**/~typeface-*/files/*', + url: (asset) => { + const relpath = asset.pathname.substr(1) + const abspath = ospath.resolve('node_modules', relpath) + const basename = ospath.basename(abspath) + const destpath = ospath.join(dest, 'font', basename) + if (!fs.pathExistsSync(destpath)) fs.copySync(abspath, destpath) + return path.join('..', 'font', basename) + }, + }, + ]), + postcssVar({ preserve: preview ? 'preserve-computed' : false }), + postcssCalc(), + autoprefixer({ browsers: ['last 2 versions'] }), + preview ? () => {} : cssnano({ preset: 'default' }), + ] + + return merge( + vfs + .src('js/+([0-9])-*.js', opts) + .pipe(uglify()) + .pipe(concat('js/site.js')), + vfs + .src('js/vendor/*.js', Object.assign({ read: false }, opts)) + .pipe( + // see https://gulpjs.org/recipes/browserify-multiple-destination.html + map((file, enc, next) => { + file.contents = browserify(file.relative, { basedir: src, detectGlobals: false }).bundle() + next(null, file) + }) + ) + .pipe(buffer()) + .pipe(uglify()), + vfs.src('css/site.css', opts).pipe(postcss(postcssPlugins)), + vfs.src('font/*.woff*(2)', opts), + vfs + .src('img/**/*.{jpg,ico,png,svg}', opts) + .pipe( + imagemin([ + imagemin.gifsicle(), + imagemin.jpegtran(), + imagemin.optipng(), + imagemin.svgo({ plugins: [{ removeViewBox: false }] }), + ]) + ), + vfs.src('helpers/*.js', opts), + vfs.src('layouts/*.hbs', opts), + vfs.src('partials/*.hbs', opts) + ).pipe(vfs.dest(dest)) +} diff --git a/gulp.d/tasks/format.js b/gulp.d/tasks/format.js new file mode 100644 index 0000000..2d50496 --- /dev/null +++ b/gulp.d/tasks/format.js @@ -0,0 +1,10 @@ +'use strict' + +const prettier = require('../lib/gulp-prettier-eslint') +const vfs = require('vinyl-fs') + +module.exports = (files) => () => + vfs + .src(files) + .pipe(prettier()) + .pipe(vfs.dest((file) => file.base)) diff --git a/gulp.d/tasks/index.js b/gulp.d/tasks/index.js new file mode 100644 index 0000000..a5795fc --- /dev/null +++ b/gulp.d/tasks/index.js @@ -0,0 +1,5 @@ +'use strict' + +const camelCase = (name) => name.replace(/[-]./g, (m) => m.substr(1).toUpperCase()) + +module.exports = require('require-directory')(module, __dirname, { recurse: false, rename: camelCase }) diff --git a/gulp.d/tasks/lint-css.js b/gulp.d/tasks/lint-css.js new file mode 100644 index 0000000..d684014 --- /dev/null +++ b/gulp.d/tasks/lint-css.js @@ -0,0 +1,10 @@ +'use strict' + +const stylelint = require('gulp-stylelint') +const vfs = require('vinyl-fs') + +module.exports = (files) => (done) => + vfs + .src(files) + .pipe(stylelint({ reporters: [{ formatter: 'string', console: true }], failAfterError: true })) + .on('error', done) diff --git a/gulp.d/tasks/lint-js.js b/gulp.d/tasks/lint-js.js new file mode 100644 index 0000000..ef4f3c9 --- /dev/null +++ b/gulp.d/tasks/lint-js.js @@ -0,0 +1,12 @@ +'use strict' + +const eslint = require('gulp-eslint') +const vfs = require('vinyl-fs') + +module.exports = (files) => (done) => + vfs + .src(files) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()) + .on('error', done) diff --git a/gulp.d/tasks/pack.js b/gulp.d/tasks/pack.js new file mode 100644 index 0000000..7c9d3ae --- /dev/null +++ b/gulp.d/tasks/pack.js @@ -0,0 +1,10 @@ +'use strict' + +const vfs = require('vinyl-fs') +const zip = require('gulp-vinyl-zip') + +module.exports = (src, dest, bundleName) => () => + vfs + .src('**/*', { base: src, cwd: src }) + .pipe(zip.zip(`${bundleName}-bundle.zip`)) + .pipe(vfs.dest(dest)) diff --git a/gulp.d/tasks/remove.js b/gulp.d/tasks/remove.js new file mode 100644 index 0000000..3f71eec --- /dev/null +++ b/gulp.d/tasks/remove.js @@ -0,0 +1,8 @@ +'use strict' + +const fs = require('fs-extra') +const { obj: map } = require('through2') +const vfs = require('vinyl-fs') + +module.exports = (files) => () => + vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => fs.remove(file.path, next))) diff --git a/gulp.d/tasks/serve.js b/gulp.d/tasks/serve.js new file mode 100644 index 0000000..82ee933 --- /dev/null +++ b/gulp.d/tasks/serve.js @@ -0,0 +1,10 @@ +'use strict' + +const connect = require('gulp-connect') + +module.exports = (root, opts = {}, watch = undefined) => (done) => { + connect.server({ ...opts, root }, function () { + this.server.on('close', done) + if (watch) watch() + }) +} diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..561a796 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,107 @@ +'use strict' + +const { parallel, series, tree, watch } = require('gulp') +const createTask = require('./gulp.d/lib/create-task') +const exportTasks = require('./gulp.d/lib/export-tasks') + +const bundleName = 'ui' +const buildDir = 'build' +const previewSrcDir = 'preview-src' +const previewDestDir = 'public' +const srcDir = 'src' +const destDir = `${previewDestDir}/_` +const { reload: livereload } = process.env.LIVERELOAD === 'true' ? require('gulp-connect') : {} + +const task = require('./gulp.d/tasks') +const glob = { + all: [srcDir, previewSrcDir], + css: `${srcDir}/css/**/*.css`, + js: ['gulpfile.js', 'gulp.d/**/*.js', `${srcDir}/{helpers,js}/**/*.js`], +} + +const cleanTask = createTask({ + name: 'clean', + desc: 'Clean files and folders generated by build', + call: task.remove(['build', 'public']), +}) + +const lintCssTask = createTask({ + name: 'lint:css', + desc: 'Lint the CSS source files using stylelint (standard config)', + call: task.lintCss(glob.css), +}) + +const lintJsTask = createTask({ + name: 'lint:js', + desc: 'Lint the JavaScript source files using eslint (JavaScript Standard Style)', + call: task.lintJs(glob.js), +}) + +const lintTask = createTask({ + name: 'lint', + desc: 'Lint the CSS and JavaScript source files', + call: parallel(lintCssTask, lintJsTask), +}) + +const formatTask = createTask({ + name: 'format', + desc: 'Format the JavaScript source files using prettify (JavaScript Standard Style)', + call: task.format(glob.js), +}) + +const buildTask = createTask({ + name: 'build', + desc: 'Build and stage the UI assets for bundling', + call: task.build(srcDir, destDir, tree().nodes.some((name) => ~name.indexOf('preview'))), +}) + +const bundleBuildTask = createTask({ + name: 'bundle:build', + call: series(cleanTask, lintTask, buildTask), +}) + +const bundlePackTask = createTask({ + name: 'bundle:pack', + desc: 'Create a bundle of the staged UI assets for publishing', + call: task.pack(destDir, buildDir, bundleName), +}) + +const bundleTask = createTask({ + name: 'bundle', + desc: 'Clean, lint, build, and bundle the UI for publishing', + call: series(bundleBuildTask, bundlePackTask), +}) + +const buildPreviewPagesTask = createTask({ + name: 'preview:build-pages', + call: task.buildPreviewPages(srcDir, previewSrcDir, previewDestDir, livereload), +}) + +const previewBuildTask = createTask({ + name: 'preview:build', + desc: 'Process and stage the UI assets and generate pages for the preview', + call: parallel(buildTask, buildPreviewPagesTask), +}) + +const previewServeTask = createTask({ + name: 'preview:serve', + call: task.serve(previewDestDir, { port: 5252, livereload }, () => watch(glob.all, previewBuildTask)), +}) + +const previewTask = createTask({ + name: 'preview', + desc: 'Generate a preview site and launch a server to view it', + call: series(previewBuildTask, previewServeTask), +}) + +module.exports = exportTasks( + bundleTask, + cleanTask, + lintTask, + formatTask, + buildTask, + bundleTask, + bundlePackTask, + previewTask, + previewBuildTask +) diff --git a/gulpfile.js/index.js b/gulpfile.js/index.js deleted file mode 100644 index 55a29a8..0000000 --- a/gulpfile.js/index.js +++ /dev/null @@ -1,106 +0,0 @@ -'use strict' - -const { parallel, series, tree, watch } = require('gulp') -const task = require('./lib/task') - -const bundleName = 'ui' -const buildDir = 'build' -const previewSrcDir = 'preview-src' -const previewDestDir = 'public' -const srcDir = 'src' -const destDir = `${previewDestDir}/_` -const { reload: livereload } = process.env.LIVERELOAD === 'true' ? require('gulp-connect') : {} - -const { remove, lintCss, lintJs, format, build, pack, previewPages, serve } = require('./tasks') -const glob = { - all: [srcDir, previewSrcDir], - css: `${srcDir}/css/**/*.css`, - js: ['gulpfile.js/**/*.js', `${srcDir}/{helpers,js}/**/*.js`], -} - -const cleanTask = task({ - name: 'clean', - desc: 'Clean files and folders generated by build', - call: remove(['build', 'public']), -}) - -const lintCssTask = task({ - name: 'lint:css', - desc: 'Lint the CSS source files using stylelint (standard config)', - call: lintCss(glob.css), -}) - -const lintJsTask = task({ - name: 'lint:js', - desc: 'Lint the JavaScript source files using eslint (JavaScript Standard Style)', - call: lintJs(glob.js), -}) - -const lintTask = task({ - name: 'lint', - desc: 'Lint the CSS and JavaScript source files', - call: parallel(lintCssTask, lintJsTask), -}) - -const formatTask = task({ - name: 'format', - desc: 'Format the JavaScript source files using prettify (JavaScript Standard Style)', - call: format(glob.js), -}) - -const buildTask = task({ - name: 'build', - desc: 'Build and stage the UI assets for bundling', - call: build(srcDir, destDir, tree().nodes.some((name) => ~name.indexOf('preview'))), -}) - -const bundleBuildTask = task({ - name: 'bundle:build', - call: series(cleanTask, lintTask, buildTask), -}) - -const bundlePackTask = task({ - name: 'bundle:pack', - desc: 'Create a bundle of the staged UI assets for publishing', - call: pack(destDir, buildDir, bundleName), -}) - -const bundleTask = task({ - name: 'bundle', - desc: 'Clean, lint, build, and bundle the UI for publishing', - call: series(bundleBuildTask, bundlePackTask), -}) - -const previewPagesTask = task({ - name: 'preview:pages', - call: previewPages(srcDir, previewSrcDir, previewDestDir, livereload), -}) - -const previewBuildTask = task({ - name: 'preview:build', - desc: 'Process and stage the UI assets and generate pages for the preview', - call: parallel(buildTask, previewPagesTask), -}) - -const previewServeTask = task({ - name: 'preview:serve', - call: serve(previewDestDir, { port: 5252, livereload }, () => watch(glob.all, previewBuildTask)), -}) - -const previewTask = task({ - name: 'preview', - desc: 'Generate a preview site and launch a server to view it', - call: series(previewBuildTask, previewServeTask), -}) - -module.exports = require('./lib/export-tasks')( - bundleTask, - cleanTask, - lintTask, - formatTask, - buildTask, - bundleTask, - bundlePackTask, - previewTask, - previewBuildTask -) diff --git a/gulpfile.js/lib/export-tasks.js b/gulpfile.js/lib/export-tasks.js deleted file mode 100644 index 7c9de48..0000000 --- a/gulpfile.js/lib/export-tasks.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict' - -module.exports = (...tasks) => { - const seed = {} - if (tasks.length) { - if (tasks.lastIndexOf(tasks[0]) > 0) { - const task1 = tasks.shift() - seed.default = Object.assign(task1.bind(null), { description: `=> ${task1.displayName}`, displayName: 'default' }) - } - return tasks.reduce((acc, it) => (acc[it.displayName || it.name] = it) && acc, seed) - } else { - return seed - } -} diff --git a/gulpfile.js/lib/gulp-prettier-eslint.js b/gulpfile.js/lib/gulp-prettier-eslint.js deleted file mode 100644 index dca0a74..0000000 --- a/gulpfile.js/lib/gulp-prettier-eslint.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' - -const { obj: map } = require('through2') -const PluginError = require('plugin-error') -const prettierEslint = require('prettier-eslint') - -module.exports = () => { - const report = { changed: 0, unchanged: 0 } - return map(format).on('finish', () => { - if (report.changed > 0) { - const changed = 'formatted ' - .concat(report.changed) - .concat(' file') - .concat(report.changed === 1 ? '' : 's') - const unchanged = 'left ' - .concat(report.unchanged) - .concat(' file') - .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, enc, 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, filePath: file.path }) - - if (input === output) { - report.unchanged += 1 - } else { - report.changed += 1 - file.contents = Buffer.from(output) - } - - next(null, file) - } -} diff --git a/gulpfile.js/lib/task.js b/gulpfile.js/lib/task.js deleted file mode 100644 index 8c90062..0000000 --- a/gulpfile.js/lib/task.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict' - -const metadata = require('undertaker/lib/helpers/metadata') -const { watch } = require('gulp') - -module.exports = ({ name, desc, opts, call: fn, loop }) => { - if (name) { - const displayName = fn.displayName - if (displayName === '' || displayName === '') { - metadata.get(fn).tree.label = `${displayName} ${name}` - } - fn.displayName = name - } - if (loop) { - const delegate = fn - name = delegate.displayName - delegate.displayName = `${name}:loop` - fn = () => watch(loop, { ignoreInitial: false }, delegate) - fn.displayName = name - } - if (desc) fn.description = desc - if (opts) fn.flags = opts - return fn -} diff --git a/gulpfile.js/tasks/build.js b/gulpfile.js/tasks/build.js deleted file mode 100644 index 509c275..0000000 --- a/gulpfile.js/tasks/build.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict' - -const autoprefixer = require('autoprefixer') -const browserify = require('browserify') -const buffer = require('vinyl-buffer') -const concat = require('gulp-concat') -const cssnano = require('cssnano') -const fs = require('fs-extra') -const imagemin = require('gulp-imagemin') -const { obj: map } = require('through2') -const merge = require('merge-stream') -const ospath = require('path') -const path = ospath.posix -const postcss = require('gulp-postcss') -const postcssCalc = require('postcss-calc') -const postcssImport = require('postcss-import') -const postcssUrl = require('postcss-url') -const postcssVar = require('postcss-custom-properties') -const uglify = require('gulp-uglify') -const vfs = require('vinyl-fs') - -module.exports = (src, dest, preview) => () => { - const opts = { base: src, cwd: src } - const postcssPlugins = [ - postcssImport(), - postcssUrl([ - { - filter: '**/~typeface-*/files/*', - url: (asset) => { - const relpath = asset.pathname.substr(1) - const abspath = ospath.resolve('node_modules', relpath) - const basename = ospath.basename(abspath) - const destpath = ospath.join(dest, 'font', basename) - if (!fs.pathExistsSync(destpath)) fs.copySync(abspath, destpath) - return path.join('..', 'font', basename) - }, - }, - ]), - postcssVar({ preserve: preview ? 'preserve-computed' : false }), - postcssCalc(), - autoprefixer({ browsers: ['last 2 versions'] }), - preview ? () => {} : cssnano({ preset: 'default' }), - ] - - return merge( - vfs - .src('js/+([0-9])-*.js', opts) - .pipe(uglify()) - .pipe(concat('js/site.js')), - vfs - .src('js/vendor/*.js', Object.assign({ read: false }, opts)) - .pipe( - // see https://gulpjs.org/recipes/browserify-multiple-destination.html - map((file, enc, next) => { - file.contents = browserify(file.relative, { basedir: src, detectGlobals: false }).bundle() - next(null, file) - }) - ) - .pipe(buffer()) - .pipe(uglify()), - vfs.src('css/site.css', opts).pipe(postcss(postcssPlugins)), - vfs.src('font/*.woff*(2)', opts), - vfs - .src('img/**/*.{jpg,ico,png,svg}', opts) - .pipe( - imagemin([ - imagemin.gifsicle(), - imagemin.jpegtran(), - imagemin.optipng(), - imagemin.svgo({ plugins: [{ removeViewBox: false }] }), - ]) - ), - vfs.src('helpers/*.js', opts), - vfs.src('layouts/*.hbs', opts), - vfs.src('partials/*.hbs', opts) - ).pipe(vfs.dest(dest)) -} diff --git a/gulpfile.js/tasks/format.js b/gulpfile.js/tasks/format.js deleted file mode 100644 index 2d50496..0000000 --- a/gulpfile.js/tasks/format.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const prettier = require('../lib/gulp-prettier-eslint') -const vfs = require('vinyl-fs') - -module.exports = (files) => () => - vfs - .src(files) - .pipe(prettier()) - .pipe(vfs.dest((file) => file.base)) diff --git a/gulpfile.js/tasks/index.js b/gulpfile.js/tasks/index.js deleted file mode 100644 index a5795fc..0000000 --- a/gulpfile.js/tasks/index.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict' - -const camelCase = (name) => name.replace(/[-]./g, (m) => m.substr(1).toUpperCase()) - -module.exports = require('require-directory')(module, __dirname, { recurse: false, rename: camelCase }) diff --git a/gulpfile.js/tasks/lint-css.js b/gulpfile.js/tasks/lint-css.js deleted file mode 100644 index d684014..0000000 --- a/gulpfile.js/tasks/lint-css.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const stylelint = require('gulp-stylelint') -const vfs = require('vinyl-fs') - -module.exports = (files) => (done) => - vfs - .src(files) - .pipe(stylelint({ reporters: [{ formatter: 'string', console: true }], failAfterError: true })) - .on('error', done) diff --git a/gulpfile.js/tasks/lint-js.js b/gulpfile.js/tasks/lint-js.js deleted file mode 100644 index ef4f3c9..0000000 --- a/gulpfile.js/tasks/lint-js.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -const eslint = require('gulp-eslint') -const vfs = require('vinyl-fs') - -module.exports = (files) => (done) => - vfs - .src(files) - .pipe(eslint()) - .pipe(eslint.format()) - .pipe(eslint.failAfterError()) - .on('error', done) diff --git a/gulpfile.js/tasks/pack.js b/gulpfile.js/tasks/pack.js deleted file mode 100644 index 7c9d3ae..0000000 --- a/gulpfile.js/tasks/pack.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const vfs = require('vinyl-fs') -const zip = require('gulp-vinyl-zip') - -module.exports = (src, dest, bundleName) => () => - vfs - .src('**/*', { base: src, cwd: src }) - .pipe(zip.zip(`${bundleName}-bundle.zip`)) - .pipe(vfs.dest(dest)) diff --git a/gulpfile.js/tasks/preview-pages.js b/gulpfile.js/tasks/preview-pages.js deleted file mode 100644 index 12f3219..0000000 --- a/gulpfile.js/tasks/preview-pages.js +++ /dev/null @@ -1,105 +0,0 @@ -'use strict' - -const asciidoctor = require('asciidoctor.js')() -const fs = require('fs-extra') -const handlebars = require('handlebars') -const { obj: map } = require('through2') -const merge = require('merge-stream') -const ospath = require('path') -const path = ospath.posix -const requireFromString = require('require-from-string') -const vfs = require('vinyl-fs') -const yaml = require('js-yaml') - -const ASCIIDOC_ATTRIBUTES = { - experimental: '', - icons: 'font', - sectanchors: '', - 'source-highlighter': 'highlight.js', -} - -module.exports = (src, previewSrc, previewDest, sink = () => map(), layouts = {}) => () => - Promise.all([ - loadSampleUiModel(previewSrc), - toPromise( - merge( - compileLayouts(src, layouts), - registerPartials(src), - registerHelpers(src), - copyImages(previewSrc, previewDest) - ) - ), - ]) - .then(([baseUiModel]) => Object.assign(baseUiModel, { env: process.env })) - .then((baseUiModel) => - vfs - .src('**/*.adoc', { base: previewSrc, cwd: previewSrc }) - .pipe( - map((file, enc, next) => { - const siteRootPath = path.relative(ospath.dirname(file.path), ospath.resolve(previewSrc)) - const uiModel = Object.assign({}, baseUiModel) - uiModel.page = Object.assign({}, uiModel.page) - uiModel.siteRootPath = siteRootPath - uiModel.siteRootUrl = path.join(siteRootPath, 'index.html') - uiModel.uiRootPath = path.join(siteRootPath, '_') - if (file.stem === '404') { - uiModel.page = { layout: '404', title: 'Page Not Found' } - } else { - const doc = asciidoctor.load(file.contents, { safe: 'safe', attributes: ASCIIDOC_ATTRIBUTES }) - uiModel.page.attributes = Object.entries(doc.getAttributes()) - .filter(([name, val]) => name.startsWith('page-')) - .reduce((accum, [name, val]) => { - accum[name.substr(5)] = val - return accum - }, {}) - uiModel.page.layout = doc.getAttribute('page-layout', 'default') - uiModel.page.title = doc.getDocumentTitle() - uiModel.page.contents = Buffer.from(doc.convert()) - } - file.extname = '.html' - file.contents = Buffer.from(layouts[uiModel.page.layout](uiModel)) - next(null, file) - }) - ) - .pipe(vfs.dest(previewDest)) - .pipe(sink()) - ) - -function loadSampleUiModel (src) { - return fs.readFile(ospath.join(src, 'ui-model.yml'), 'utf8').then((contents) => yaml.safeLoad(contents)) -} - -function registerPartials (src) { - return vfs.src('partials/*.hbs', { base: src, cwd: src }).pipe( - map((file, enc, next) => { - handlebars.registerPartial(file.stem, file.contents.toString()) - next() - }) - ) -} - -function registerHelpers (src) { - return vfs.src('helpers/*.js', { base: src, cwd: src }).pipe( - map((file, enc, next) => { - handlebars.registerHelper(file.stem, requireFromString(file.contents.toString())) - next() - }) - ) -} - -function compileLayouts (src, layouts) { - return vfs.src('layouts/*.hbs', { base: src, cwd: src }).pipe( - map((file, enc, next) => { - layouts[file.stem] = handlebars.compile(file.contents.toString(), { preventIndent: true }) - next() - }) - ) -} - -function copyImages (src, dest) { - return vfs.src('**/*.{png,svg}', { base: src, cwd: src }).pipe(vfs.dest(dest)) -} - -function toPromise (stream) { - return new Promise((resolve, reject) => stream.on('error', reject).on('finish', resolve)) -} diff --git a/gulpfile.js/tasks/remove.js b/gulpfile.js/tasks/remove.js deleted file mode 100644 index 3f71eec..0000000 --- a/gulpfile.js/tasks/remove.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' - -const fs = require('fs-extra') -const { obj: map } = require('through2') -const vfs = require('vinyl-fs') - -module.exports = (files) => () => - vfs.src(files, { allowEmpty: true }).pipe(map((file, enc, next) => fs.remove(file.path, next))) diff --git a/gulpfile.js/tasks/serve.js b/gulpfile.js/tasks/serve.js deleted file mode 100644 index 82ee933..0000000 --- a/gulpfile.js/tasks/serve.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const connect = require('gulp-connect') - -module.exports = (root, opts = {}, watch = undefined) => (done) => { - connect.server({ ...opts, root }, function () { - this.server.on('close', done) - if (watch) watch() - }) -}