From 5572793954b63e1ec00983bad716af01e02476ef Mon Sep 17 00:00:00 2001 From: derekslife Date: Mon, 23 Feb 2015 11:05:13 -0800 Subject: [PATCH 1/3] Implemented `overwrite` option in `dest()` --- lib/dest/writeContents/index.js | 4 +- lib/dest/writeContents/writeBuffer.js | 3 +- lib/dest/writeContents/writeStream.js | 3 +- lib/prepareWrite.js | 3 +- test/dest.js | 72 ++++++++++++++++++++++++++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/lib/dest/writeContents/index.js b/lib/dest/writeContents/index.js index 42890097..c934e624 100644 --- a/lib/dest/writeContents/index.js +++ b/lib/dest/writeContents/index.js @@ -31,7 +31,9 @@ function writeContents(writePath, file, cb) { } function written(err) { - if (err) { + var ignorableError = (err && err.code === 'EEXIST' && file.flag === 'wx'); + + if (err && !ignorableError) { return complete(err); } diff --git a/lib/dest/writeContents/writeBuffer.js b/lib/dest/writeContents/writeBuffer.js index fe4be8f2..fd21ca5e 100644 --- a/lib/dest/writeContents/writeBuffer.js +++ b/lib/dest/writeContents/writeBuffer.js @@ -4,7 +4,8 @@ var fs = require('graceful-fs'); function writeBuffer(writePath, file, cb) { var opt = { - mode: file.stat.mode + mode: file.stat.mode, + flag: file.flag }; fs.writeFile(writePath, file.contents, opt, cb); diff --git a/lib/dest/writeContents/writeStream.js b/lib/dest/writeContents/writeStream.js index 5bd1a5b0..b55bb0c2 100644 --- a/lib/dest/writeContents/writeStream.js +++ b/lib/dest/writeContents/writeStream.js @@ -5,7 +5,8 @@ var fs = require('graceful-fs'); function writeStream(writePath, file, cb) { var opt = { - mode: file.stat.mode + mode: file.stat.mode, + flag: file.flag }; var outStream = fs.createWriteStream(writePath, opt); diff --git a/lib/prepareWrite.js b/lib/prepareWrite.js index ac8a6e7a..ec6e62dc 100644 --- a/lib/prepareWrite.js +++ b/lib/prepareWrite.js @@ -30,6 +30,7 @@ function prepareWrite(outFolder, file, opt, cb) { // wire up new properties file.stat = (file.stat || new fs.Stats()); file.stat.mode = options.mode; + file.flag = options.flag; file.cwd = cwd; file.base = basePath; file.path = writePath; @@ -43,4 +44,4 @@ function prepareWrite(outFolder, file, opt, cb) { }); } -module.exports = prepareWrite; \ No newline at end of file +module.exports = prepareWrite; diff --git a/test/dest.js b/test/dest.js index 3a983262..c678d5f5 100644 --- a/test/dest.js +++ b/test/dest.js @@ -526,7 +526,7 @@ describe('dest stream', function() { stream.write(expectedFile); stream.end(); }); - + it('should use different modes for files and directories', function(done) { var inputBase = path.join(__dirname, './fixtures'); var inputPath = path.join(__dirname, './fixtures/wow/suchempty'); @@ -713,6 +713,76 @@ describe('dest stream', function() { stream.end(); }); + it('should not overwrite files with overwrite option set to 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: 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 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: true}); + + var buffered = []; + bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); + stream.pipe(bufferStream); + stream.write(inputFile); + stream.end(); + }); + ['end', 'finish'].forEach(function(eventName) { it('should emit ' + eventName + ' event', function(done) { var srcPath = path.join(__dirname, './fixtures/test.coffee'); From 32fc5c805ec6209c3d2145036ff2eabff79845be Mon Sep 17 00:00:00 2001 From: derekslife Date: Mon, 23 Feb 2015 11:20:46 -0800 Subject: [PATCH 2/3] Updated docs for `overwrite` option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9b259824..001bd197 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ This is just [glob-watcher] - cwd - Specify the working directory the folder is relative to. Default is `process.cwd()` - mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode) or the process mode if the input file has no mode property. - dirMode - Specify the mode the directory should be created with. Default is the process mode. + - overwrite - Specify if existing files with the same path should be overwritten or not. Default is `true`, to always overwrite existing files - Returns a Readable/Writable stream. - On write the stream will save the [vinyl] File to disk at the folder/cwd specified. - After writing the file to disk, it will be emitted from the stream so you can keep piping these around. From c4750b2efccd840e4ae840dc55b01cb7051f2f48 Mon Sep 17 00:00:00 2001 From: derekslife Date: Mon, 23 Feb 2015 14:27:37 -0800 Subject: [PATCH 3/3] Encapsulated error filtering logic into `isErrorFatal` func --- lib/dest/writeContents/index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/dest/writeContents/index.js b/lib/dest/writeContents/index.js index c934e624..be759675 100644 --- a/lib/dest/writeContents/index.js +++ b/lib/dest/writeContents/index.js @@ -31,9 +31,8 @@ function writeContents(writePath, file, cb) { } function written(err) { - var ignorableError = (err && err.code === 'EEXIST' && file.flag === 'wx'); - if (err && !ignorableError) { + if (isErrorFatal(err)) { return complete(err); } @@ -53,6 +52,20 @@ function writeContents(writePath, file, cb) { fs.chmod(writePath, file.stat.mode, complete); }); } + + function isErrorFatal(err) { + if (!err) { + return false; + } + + // Handle scenario for file overwrite failures. + else if (err.code === 'EEXIST' && file.flag === 'wx') { + return false; // "These aren't the droids you're looking for" + } + + // Otherwise, this is a fatal error + return true; + } } module.exports = writeContents;