Skip to content

Commit

Permalink
allow a function to be used as value for the overwrite option - closes
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 28, 2017
1 parent 11d7cd3 commit 0d08e73
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/prepareWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('graceful-fs');

function booleanOrFunc(v, file) {
if (typeof v !== 'boolean' && typeof v !== 'function') {
return null;
}

return typeof v === 'boolean' ? v : v(file);
}

function stringOrFunc(v, file) {
if (typeof v !== 'string' && typeof v !== 'function') {
return null;
Expand All @@ -20,7 +28,8 @@ function prepareWrite(outFolder, file, opt, cb) {
dirMode: null,
overwrite: true
}, opt);
options.flag = (options.overwrite ? 'w' : 'wx');
var overwrite = booleanOrFunc(options.overwrite, file);
options.flag = (overwrite ? 'w' : 'wx');

var cwd = path.resolve(options.cwd);
var outFolderPath = stringOrFunc(outFolder, file);
Expand Down
70 changes: 70 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,76 @@ describe('dest stream', function() {
stream.end();
});

it('should not overwrite files with overwrite option set to a function that returns false', function(done) {
var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var inputContents = fs.readFileSync(inputPath);

var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedBase = path.join(__dirname, './out-fixtures');
var existingContents = 'Lorem Ipsum';

var inputFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: inputContents
});

var onEnd = function(){
buffered.length.should.equal(1);
bufEqual(fs.readFileSync(expectedPath), new Buffer(existingContents)).should.equal(true);
done();
};

// Write expected file which should not be overwritten
fs.mkdirSync(expectedBase);
fs.writeFileSync(expectedPath, existingContents);

var stream = vfs.dest('./out-fixtures/', {cwd: __dirname, overwrite: function() { return false; }});

var buffered = [];
bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);
stream.pipe(bufferStream);
stream.write(inputFile);
stream.end();
});

it('should overwrite files with overwrite option set to a function that returns true', function(done) {
var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var inputContents = fs.readFileSync(inputPath);

var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedBase = path.join(__dirname, './out-fixtures');
var existingContents = 'Lorem Ipsum';

var inputFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: inputContents
});

var onEnd = function(){
buffered.length.should.equal(1);
bufEqual(fs.readFileSync(expectedPath), new Buffer(inputContents)).should.equal(true);
done();
};

// This should be overwritten
fs.mkdirSync(expectedBase);
fs.writeFileSync(expectedPath, existingContents);

var stream = vfs.dest('./out-fixtures/', {cwd: __dirname, overwrite: function() { return true; }});

var buffered = [];
bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);
stream.pipe(bufferStream);
stream.write(inputFile);
stream.end();
});

it('should create symlinks when the `symlink` attribute is set on the file', function (done) {
var inputPath = path.join(__dirname, './fixtures/test-create-dir-symlink');
var inputBase = path.join(__dirname, './fixtures/');
Expand Down

0 comments on commit 0d08e73

Please sign in to comment.