Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added sinatra-assetpack style glob expansion

  • Loading branch information...
commit 26445114909ad0509bd1e84c5c36f508136b1672 1 parent 67a3cee
Stephen Belanger authored
Showing with 69 additions and 28 deletions.
  1. +18 −0 lib/files.js
  2. +19 −0 lib/helper.js
  3. +31 −27 lib/index.js
  4. +1 −1  package.json
18 lib/files.js
View
@@ -1,4 +1,6 @@
var async = require('async')
+ , helper = require('./helper')
+ , glob = require('glob')
, path = require('path')
, fs = require('fs')
@@ -38,6 +40,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
*
* @param string
19 lib/helper.js
View
@@ -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 lib/index.js
View
@@ -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()
+ })
})
})
})
2  package.json
View
@@ -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"
},
Please sign in to comment.
Something went wrong with that request. Please try again.