Skip to content

Commit

Permalink
Added sinatra-assetpack style glob expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed May 10, 2012
1 parent 67a3cee commit 2644511
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
18 changes: 18 additions & 0 deletions lib/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var async = require('async')
, helper = require('./helper')
, glob = require('glob')
, path = require('path')
, fs = require('fs')

Expand Down Expand Up @@ -37,6 +39,22 @@ Files.prototype.load = function (files, fn) {
})
}

/**
* Expand glob path
*
* @param array
* Ordered list of file paths
*/
Files.prototype.expand = function (files, fn) {
async.parallel(files.map(function (path) {
return function (cb) {
glob(path, cb)
}
}), function (err, res) {
fn(err, helper.unique(helper.flatten(res)))
})
}

/**
* Safely write data to a file
*
Expand Down
19 changes: 19 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ var helper = {
md5.update(str)
return md5.digest('hex')
}
, flatten: function (arr) {
return (function recurse (a, b) {
a.forEach(function (v) {
Array.isArray(v) ? recurse(v, b) : b.push(v)
})
return b
})(arr, [])
}
, unique: function (array) {
var results = []
array.reduce(function (memo, value, index) {
if ( ! ~memo.indexOf(value)) {
memo.push(value)
results.push(array[index])
}
return memo
}, [])
return results
}
}

module.exports = helper
58 changes: 31 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,34 +321,38 @@ Crsh.prototype.compile = function (cb) {

// Generate name only when a compile occurs
this.name = 'crsh-' + helper.md5(this.list.join(',')) + '.' + this.format

// Load all the files into a hash
Files.load(list, function (err, files) {
if (err) cb(err)

// Run through matching input filters
self.applyInputFilters(files, function (err, filteredFiles) {
if (err) { return cb(err) }

// Merge all the files
var data = list.map(function (file) {
return filteredFiles[file]
}).join('')

// Only uglify in production
if (isProd && self.format === 'js') {
var ast = jsp.parse(data)
ast = pro.ast_mangle(ast)
ast = pro.ast_squeeze(ast)
data = pro.gen_code(ast)
}

// Write merged and uglified data to file
var p = path.join(self.path, self.name)
Files.write(p, data, function (err) {
if (err) return cb(err)
self.lastChange = Date.now()
cb()
Files.expand(list, function (err, list) {
self.list = list

// Load all the files into a hash
Files.load(list, function (err, files) {
if (err) cb(err)

// Run through matching input filters
self.applyInputFilters(files, function (err, filteredFiles) {
if (err) { return cb(err) }

// Merge all the files
var data = list.map(function (file) {
return filteredFiles[file]
}).join('')

// Only uglify in production
if (isProd && self.format === 'js') {
var ast = jsp.parse(data)
ast = pro.ast_mangle(ast)
ast = pro.ast_squeeze(ast)
data = pro.gen_code(ast)
}

// Write merged and uglified data to file
var p = path.join(self.path, self.name)
Files.write(p, data, function (err) {
if (err) return cb(err)
self.lastChange = Date.now()
cb()
})
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Stephen Belanger <admin@stephenbelanger.com> (http://stephenbelanger.com)",
"name": "crsh",
"description": "crsh your javascript and css into tiny blocks.",
"version": "0.2.3",
"version": "0.3.0",
"repository": {
"url": "git://github.com/Qard/crsh.git"
},
Expand Down

0 comments on commit 2644511

Please sign in to comment.