Comparing version 0.1.1 to 0.1.2
var fs = require('fs'), | ||
crypto = require('crypto'), | ||
path = require('path'), | ||
mkdirp = require('mkdirp'); | ||
mkdirp = require('mkdirp'), | ||
assert = require('assert'); | ||
@@ -13,5 +14,14 @@ function Cache(opts) { | ||
if(opts.path) { | ||
this.metaPath = opts.path + '/meta.json'; | ||
this.data = (fs.existsSync(this.metaPath) ? require(this.metaPath) : {}); | ||
this.metaPath = path.normalize(opts.path + '/meta.json'); | ||
try { | ||
this.data = (fs.existsSync(this.metaPath) ? require(this.metaPath) : {}); | ||
} catch(e) { | ||
console.error(''); | ||
console.error('ERROR: The cache index file "' + this.metaPath + '" cannot be parsed as JSON.'); | ||
console.error('To fix this issue, you should delete the folder "' + | ||
path.dirname(this.metaPath) + '" to clear the cache.'); | ||
throw e; | ||
} | ||
// need to do this early on, since if the path is missing, | ||
@@ -27,4 +37,48 @@ // writes to the cache dir will fail | ||
} | ||
this.validate(); | ||
} | ||
Cache.prototype.validate = function() { | ||
// { | ||
// inputFilePath: { | ||
// stat: (expected stat meta) | ||
// md5: (expected hash meta) | ||
// | ||
// taskResults: { | ||
// taskHash: { | ||
// path: (path in cache for this task) | ||
// } | ||
// } | ||
// } | ||
// } | ||
try { | ||
var data = this.data, | ||
topKeys = Object.keys(data); | ||
assert.ok(Array.isArray(topKeys)); | ||
topKeys.forEach(function(key) { | ||
var file = data[key]; | ||
assert.ok(file.taskResults); | ||
assert.ok(Array.isArray(Object.keys(file.taskResults))); | ||
Object.keys(file.taskResults).forEach(function(key) { | ||
assert.equal(typeof file.taskResults[key]['path'], 'string'); | ||
}); | ||
assert.ok(file.stat); | ||
assert.ok(Array.isArray(Object.keys(file.stat))); | ||
[ | ||
'size', 'mtime' | ||
].forEach(function(key) { | ||
assert.ok(!!file.stat[key]); | ||
}); | ||
}); | ||
} catch(e) { | ||
console.error(''); | ||
console.error('ERROR: The cache index file "' + this.metaPath + '" is in an unexpected or outdated format.'); | ||
console.error('To fix this issue, you should delete the folder "' + | ||
path.dirname(this.metaPath) + '" to clear the cache.'); | ||
throw e; | ||
} | ||
}; | ||
Cache.prototype.save = function() { | ||
@@ -112,18 +166,2 @@ // just in case | ||
cacheMeta = this.data[inputFilePath]; | ||
// { | ||
// inputFilePath: { | ||
// stat: (expected stat meta) | ||
// md5: (expected hash meta) | ||
// | ||
// taskResults: { | ||
// taskHash: { | ||
// path: (path in cache for this task) | ||
// } | ||
// } | ||
// } | ||
// } | ||
// if: | ||
@@ -130,0 +168,0 @@ // 1) the input file file path or |
@@ -9,6 +9,14 @@ var fs = require('fs'), | ||
List.prototype.add = function(filepath){ | ||
if(!filepath) return this; | ||
List.prototype.exclude = function(arr) { | ||
if(!arr) { | ||
this.excludeFn = null; | ||
} else { | ||
this.excludeFn = (Array.isArray(arr) ? arr : [ arr ]); | ||
} | ||
}; | ||
List.prototype.add = function(filepath) { | ||
if (!filepath) return this; | ||
var self = this, | ||
paths = (Array.isArray(filepath) ? filepath : [ filepath ]); | ||
paths = (Array.isArray(filepath) ? filepath : [filepath]); | ||
@@ -18,8 +26,9 @@ paths.forEach(function(p) { | ||
p = path.normalize(p); // for windows | ||
try { | ||
stat = fs.statSync(p); | ||
} catch(e) { | ||
} catch (e) { | ||
// ENOENT can occur when stat'ing a symlink to a nonexistent location | ||
// we want to traverse symlinks in general but ignore these issues | ||
if(e.code != 'ENOENT') { | ||
if (e.code != 'ENOENT') { | ||
throw e; | ||
@@ -33,4 +42,13 @@ } else { | ||
if (stat.isDirectory()) { | ||
p += (p[p.length-1] !== path.sep ? path.sep : ''); | ||
return fs.readdirSync(p).forEach(function (f) { | ||
// exclusions | ||
var isExcluded = self.excludeFn && self.excludeFn.some(function(callback) { | ||
return callback(p); | ||
}); | ||
if (isExcluded) { | ||
log.info('Excluded path from traversal: ', p); | ||
return; | ||
} | ||
p += (p[p.length - 1] !== path.sep ? path.sep : ''); | ||
return fs.readdirSync(p).forEach(function(f) { | ||
self.add(p + f); | ||
@@ -41,7 +59,9 @@ }); | ||
}); | ||
// sort on each add | ||
self.files.sort(function(a, b) { return a.name.localeCompare(b.name); }); | ||
return this; | ||
}; | ||
List.prototype.sort = function() { | ||
this.files.sort(function(a, b) { return a.name.localeCompare(b.name); }); | ||
}; | ||
module.exports = List; |
{ | ||
"name": "minitask", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A standard/convention for running tasks over a list of files based around Node core streams2", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
57237
1262