Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ Filter.prototype.processAndCacheFile =
then(function asyncProcessFile() {
return self.processFile(srcDir, destDir, relativePath);
}).
then(copyToCache,
then(function checkIfCacheNecessary(outputPath) {
if (outputPath) {
return copyToCache();
}
},
// TODO(@caitp): error wrapper is for API compat, but is not particularly
// useful.
// istanbul ignore next
Expand Down Expand Up @@ -164,6 +168,9 @@ Filter.prototype.processFile =

return Promise.resolve(this.processString(contents, relativePath)).
then(function asyncOutputFilteredFile(outputString) {
if (!outputString) {
return;
}
var outputPath = self.getDestFilePath(relativePath);
if (outputPath == null) {
throw new Error('canProcessFile("' + relativePath + '") is true, but getDestFilePath("' + relativePath + '") is null');
Expand All @@ -173,6 +180,7 @@ Filter.prototype.processFile =
fs.writeFileSync(outputPath, outputString, {
encoding: outputEncoding
});
return outputPath;
});
};

Expand Down
28 changes: 27 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@ describe('Filter', function() {
});
});

it('should remove file when processString returns a falsy value',
function() {
function RemovingFilter(inputTree, options) {
if (!this) return new RemovingFilter(inputTree, options);
Filter.call(this, inputTree, options);
}

inherits(RemovingFilter, Filter);

RemovingFilter.prototype.processString = function(content) {
return "Dogs... who needs dogs?" !== content;
}

var builder = makeBuilder(RemovingFilter, fixturePath, function(awk) {
return awk;
});

return builder('dir').then(function(results) {
expect(existsSync(results.directory + '/a/README.md')).to.be.true;
expect(existsSync(results.directory + '/a/foo.js')).to.be.true;
expect(existsSync(results.directory + '/a/bar/bar.js')).to.be.false;
});
}
);

it('should complain if canProcessFile is true but getDestFilePath is null',
function() {
var builder = makeBuilder(ReplaceFilter, fixturePath, function(awk) {
Expand Down Expand Up @@ -321,4 +346,5 @@ describe('Filter', function() {
});
});
});
});

});