From 0d08e73f0a5a3ef110f00bb57bca8f40e962c697 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 27 Sep 2015 14:38:55 -0700 Subject: [PATCH] allow a function to be used as value for the overwrite option - closes #68 --- lib/prepareWrite.js | 11 ++++++- test/dest.js | 70 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/lib/prepareWrite.js b/lib/prepareWrite.js index 9779c701..621d9b54 100644 --- a/lib/prepareWrite.js +++ b/lib/prepareWrite.js @@ -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; @@ -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); diff --git a/test/dest.js b/test/dest.js index 7b41146c..edd4e3d0 100644 --- a/test/dest.js +++ b/test/dest.js @@ -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/');