broccoli-glob-caching-writer
Advanced tools
Comparing version 0.1.2 to 0.2.0
46
index.js
var path = require('path') | ||
var fs = require('fs') | ||
var crypto = require('crypto') | ||
var quickTemp = require('quick-temp') | ||
@@ -9,10 +8,11 @@ var _ = require('underscore') | ||
var dirmatch = require('dirmatch') | ||
var quickTemp = require('quick-temp') | ||
/** | ||
* @param inputTrees {array.<Tree>|Tree} Input tree or list of input trees. | ||
* @param inputTree {Tree} | ||
* @param [options] {object} | ||
* @param [options.files] {array.<string>} List of glob patterns to specify cached files. | ||
*/ | ||
function CachingWriter(inputTrees, options) { | ||
this.inputTrees = _.isArray(inputTrees) ? inputTrees : [inputTrees] | ||
function CachingWriter(inputTree, options) { | ||
this.inputTree = inputTree | ||
if (!options) options = {} | ||
@@ -23,23 +23,14 @@ if (!options.files) options.files = ['**'] | ||
var promiseSeries = function(arr, fn) { | ||
var ready = Q() | ||
var result = [] | ||
_.each(arr, function(item) { | ||
ready = ready | ||
.then(function() { return fn(item) }) | ||
.then(function(res) { result.push(res) }) | ||
}) | ||
return ready.then(function() { return result }) | ||
} | ||
//Compares current files hash and cached hash, | ||
//and if they are different, calls updateCache. | ||
CachingWriter.prototype.read = function(readTree) { | ||
return promiseSeries(this.inputTrees, readTree).then(function(srcDirs) { | ||
return readTree(this.inputTree).then(function(srcDir) { | ||
var destDir = this.destDir | ||
var files = this.findFiles(srcDirs) | ||
var hash = this.hashFiles(files) | ||
var files = dirmatch(srcDir, this.options.files) | ||
var absFiles = _.map(files, function(file) { return path.join(srcDir, file) }) | ||
var hash = this.hashFiles(absFiles) | ||
if (hash !== this.cachedHash) { | ||
destDir = quickTemp.makeOrRemake(this, 'destDir') | ||
this.cachedHash = hash | ||
var srcArg = srcDirs.length ? srcDirs : srcDirs[0] | ||
this.cachedResult = this.updateCache(srcArg, destDir, files) | ||
this.cachedResult = this.updateCache(srcDir, destDir, files) | ||
} | ||
@@ -50,16 +41,3 @@ return Q.when(this.cachedResult).then(function() { return destDir }) | ||
//Finds all files mathing globs in srcDirs | ||
CachingWriter.prototype.findFiles = function(srcDirs) { | ||
return _.flatten(_.map(srcDirs, function(srcDir) { | ||
var files = dirmatch(srcDir, this.options.files, { | ||
nodir: true, | ||
nomatch: true | ||
}) | ||
return _.map(files, function(file) { | ||
return path.join(srcDir, file) | ||
}) | ||
}, this)) | ||
} | ||
//Calculates hash of all files. | ||
//Calculates hash of all files | ||
CachingWriter.prototype.hashFiles = function(files) { | ||
@@ -66,0 +44,0 @@ var keys = [] |
{ | ||
"name": "broccoli-glob-caching-writer", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Caching writer for broccoli that allows to specify files that need to be cached using glob patterns.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -17,3 +17,3 @@ #broccoli-glob-caching-writer | ||
var MyWriter = function(inputTrees, options) { | ||
var MyWriter = function(inputTree, options) { | ||
CachingWriter.apply(this, arguments) | ||
@@ -34,14 +34,11 @@ } | ||
###CachingWriter(inputTrees, [options]) | ||
###CachingWriter(inputTree, [options]) | ||
Constructor. | ||
####inputTrees | ||
####inputTree | ||
Type: `Tree|array.<Tree>` | ||
Type: `Tree` | ||
Single tree or an array of trees. | ||
<br> | ||
If an array of trees was specified, an array of source paths will be provided | ||
to `updateCache`. | ||
Input broccoli tree. | ||
@@ -64,3 +61,3 @@ ####options | ||
###CachingWriter.prototype.updateCache(srcDirs, destDir, cachedFiles) | ||
###CachingWriter.prototype.updateCache(srcDir, destDir, cachedFiles) | ||
@@ -72,7 +69,7 @@ This method creates build results, it must be implemented in inherited class. | ||
####srcDirs | ||
####srcDir | ||
Type: `string|array.<string>` | ||
Type: `string` | ||
Path or list of paths of source dirs. | ||
Path of the `inputTree`. | ||
@@ -90,3 +87,3 @@ ####destDir | ||
Array of cached files paths. | ||
Paths are absolute. | ||
Paths are relative to the `inputTree`. | ||
@@ -93,0 +90,0 @@ ##License |
var assert = require('assert') | ||
var fs = require('fs-extra') | ||
var path = require('path') | ||
var sinon = require('sinon') | ||
var fs = require('fs-extra') | ||
var broccoli = require('broccoli') | ||
@@ -15,4 +15,4 @@ | ||
var createWriter = function() { | ||
var Writer = function(inputTrees, options) { | ||
if (!(this instanceof Writer)) return new Writer(inputTrees, options) | ||
var Writer = function(inputTree, options) { | ||
if (!(this instanceof Writer)) return new Writer(inputTree, options) | ||
CachingWriter.apply(this, arguments) | ||
@@ -102,3 +102,3 @@ } | ||
describe('updateCache', function() { | ||
it('is called with single srcDir when single inputTree was provided', function() { | ||
it('is called with srcDir', function() { | ||
var Writer = createWriterWithSpy() | ||
@@ -110,18 +110,6 @@ var tree = Writer(DIR) | ||
var srcDir = tree.updateCache.getCall(0).args[0] | ||
assert.deepEqual(srcDir, [DIR]) | ||
assert.deepEqual(srcDir, DIR) | ||
}) | ||
}) | ||
it('is called with array of srcDirs when array of inputTrees was provided', | ||
function() { | ||
var Writer = createWriterWithSpy() | ||
var tree = Writer([DIR, DIR]) | ||
builder = new broccoli.Builder(tree) | ||
return builder.build() | ||
.then(function() { | ||
var srcDirs = tree.updateCache.getCall(0).args[0] | ||
assert.deepEqual(srcDirs, [DIR, DIR]) | ||
}) | ||
}) | ||
it('is called with destDir as second arg', function() { | ||
@@ -145,3 +133,3 @@ var Writer = createWriterWithSpy() | ||
var files = tree.updateCache.getCall(0).args[2] | ||
assert.deepEqual(files, [path.join(DIR, 'file.css'), path.join(DIR, 'file.js')]) | ||
assert.deepEqual(files, ['file.css', 'file.js']) | ||
}) | ||
@@ -148,0 +136,0 @@ }) |
11411
214
89