Skip to content

Commit

Permalink
feat(index): add config callback
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Mar 11, 2017
1 parent ba94268 commit bdda211
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions index.js
Expand Up @@ -4,75 +4,87 @@

'use strict'

const assign = Object.assign
const path = require('path')

const dirname = require('path').dirname
const extname = require('path').extname

const Error = require('gulp-util').PluginError
const transform = require('through2').obj

const PLUGIN_NAME = 'gulp-posthtml'

const posthtml = require('posthtml')
const posthtmlrc = require('posthtml-load-config')

const PLUGIN_NAME = 'gulp-posthtml'
const Error = require('gulp-util').PluginError

function rc (cb) {
return function (plugins, options) {
if (Array.isArray(plugins)) {
return cb(() => Promise.resolve({ plugins: plugins, options: options }))
} else if (typeof plugins === 'function') {
return cb((file) => Promise.resolve(plugins(file)))
} else {
const ctx = plugins || {}

return cb((file) => {
const config = {}

if (ctx.config) {
config.path = path.resolve(ctx.config)
} else {
config.path = file.dirname
}

config.ctx = { file: file, options: ctx }

return posthtmlrc(config.ctx, config.path)
})
}
}
}
/**
* @author Ivan Voishev (@voishev) <voischev.ivan@ya.ru>
* @description PostHTML Plugin for Gulp
* @license MIT
*
* @module gulp-posthtml
* @version 2.0.0
* @version 3.0.0
*
* @requires gulp-util
* @requires through2
* @requires posthtml
* @requires posthtml-load-config
*
* @method gulp-posthtml
*
* @param {Array} plugins PostHTML Plugins
* @param {Object} options PostHTML Options
*
* @return {Function} Stream (Transform)
*/
module.exports = function (plugins, options) {
module.exports = rc((loadConfig) => {
return transform((file, enc, cb) => {
if (file.isNull()) {
return cb(null, file)
}

const defaults = { from: file.path, to: file.path }

if (!plugins || !Array.isArray(plugins)) {
const ctx = assign(
defaults, plugins, { ext: extname(file.path), dir: dirname(file.path) }
)

posthtmlrc(ctx, ctx.dir).then((config) => {
posthtml(config.plugins)
.process(file.contents.toString(enc), config.options)
.then((result) => {
file.contents = new Buffer(result.html)
cb(null, file)
})
.catch((err) => {
cb(new Error(PLUGIN_NAME, err))
})
})
if (file.isStream()) {
return new Error(`Streams are not supported by ${PLUGIN_NAME}`)
}

if (plugins && Array.isArray(plugins)) {
options = assign(defaults, options)
loadConfig(file).then((config) => {
config.options = Object.assign(
{ from: file.path, to: file.path }, config.options
)

posthtml(plugins)
.process(file.contents.toString(enc), assign(options, defaults))
return posthtml(config.plugins)
.process(file.contents.toString(enc), config.options)
.then((result) => {
// console.log(result.html)
file.contents = new Buffer(result.html)
cb(null, file)
})
.catch((err) => {
cb(new Error(PLUGIN_NAME, err))
})
}
})
})
}
})

0 comments on commit bdda211

Please sign in to comment.