Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created August 12, 2015 03:58
Show Gist options
  • Save dtinth/ae649b7d1c814c31718a to your computer and use it in GitHub Desktop.
Save dtinth/ae649b7d1c814c31718a to your computer and use it in GitHub Desktop.
Vinylsmith
###
Vinylsmith: A Wintersmith plugin factory that pipes files through a
series of gulp plugins.
By Thai Pangsakulyanont <[email protected]>
Licensed WTFPL
###
fs = require 'vinyl-fs'
lazypipe = require 'lazypipe'
buffer = require 'vinyl-buffer'
through2 = require 'through2'
class Source
constructor: (@path, @base, @piper) ->
compile: (callback) ->
emitter = ->
emitted = false
through2.obj (vinyl, enc, cb) ->
if emitted
console.error "Oops! Your pipe should emit only one file!"
else
emitted = true
callback null, vinyl
cb()
, (cb) ->
unless emitted
callback new Error "No files received from your pipe!"
cb()
.on 'error', (err) ->
if emitted
console.error err
else
emitted = true
callback err
fs.src(@path, base: @base).pipe(@piper()).pipe(buffer()).pipe(emitter())
vinylsmith = (env) ->
class VinylPlugin extends env.ContentPlugin
constructor: (@_filename, @_source) ->
getFilename: ->
@_filename
getView: ->
return (env, locals, contents, templates, callback) ->
# re-compile the source every time user requests it to make sure
# it’s the latest version (for example, when the source depends on
# external files outside of contents/ directory)
@_source.compile (err, vinyl) ->
if err
callback err
else
callback null, vinyl.contents
createContentPlugin = (piper) ->
pipe: (args...) ->
createContentPlugin(piper.pipe(args...))
fromFile: (filepath, callback) ->
source = new Source filepath.full, env.contentsPath, piper
# compile the source once to get the file name
source.compile (err, vinyl) ->
if err
callback err
else
callback null, new VinylPlugin vinyl.relative, source
createContentPlugin lazypipe()
module.exports = vinylsmith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment