Skip to content

Instantly share code, notes, and snippets.

@grncdr
Last active January 2, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grncdr/612586b5d808ba826ea4 to your computer and use it in GitHub Desktop.
Save grncdr/612586b5d808ba826ea4 to your computer and use it in GitHub Desktop.
sketch of how to do the useful part of gulp as a module
/**
* This is an example of what a build script would look like
*/
var globStream = require('glob-stream').create
var globWatch = require('glob-watcher')
// I have no idea if this exists, probably not
var createSassTransform = require('sass-transform')
// these two are defined below, haven't published them yet
var st = require('spit-take')
var replacer = require('replacer')
var unsassify = st(
replacer([/\.sass$/, '.css'], [/\/sassy\//, 'css']),
createSassTransform
)
var globs = "./sassy/*.sass"
globStream(globs).pipe(unsassify)
globWatch(globs, unsassify.write.bind(unsassify))
// Might publish this if I can't find something like it.
module.exports = function replacer () {
var replaceArgs = [].slice.call(arguments)
return function (string) {
replaceArgs.reduce(function (string, args) {
return string.replace.apply(string, args)
}, string)
}
}
var fs = require('fs')
var once = require('once')
var through = require('through2')
module.exports = function spitTake (transformPath, makeTransformStream) {
var inProgress = {}
return through2({objectMode: true}, transformFile)
function transformFileObject (file, _, cb) {
var self = this
var srcPath = file.path
var destPath = transformPath(srcPath)
if (!inProgress[destPath]) {
run(srcPath, destPath, makeTransformStream(), onEnd)
} else {
inProgress[destPath].on('end', function () {
run(srcPath, destPath, makeTransformStream(), onEnd)
})
}
function onEnd (err) {
delete inProgress[destPath]
this.push(destPath)
cb(err)
}
}
}
function run (src, dest, transform, callback) {
callback = once(callback)
var input = fs.createReadStream(src)
var output = fs.createWriteStream(dest)
var transform = makeTransformStream()
input.on('error', callback)
output.on('error', callback)
input.pipe(transform).pipe(output).on('end', callback.bind(null, null))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment