Skip to content

Commit

Permalink
Merge pull request #59 from derekslife/master
Browse files Browse the repository at this point in the history
Implemented `overwrite` option in `dest()`
  • Loading branch information
Eric Schoffstall committed Feb 23, 2015
2 parents 2dfe290 + c4750b2 commit 6d79529
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion lib/dest/writeContents/index.js
Expand Up @@ -31,7 +31,8 @@ function writeContents(writePath, file, cb) {
}

function written(err) {
if (err) {

if (isErrorFatal(err)) {
return complete(err);
}

Expand All @@ -51,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;
3 changes: 2 additions & 1 deletion lib/dest/writeContents/writeBuffer.js
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/dest/writeContents/writeStream.js
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/prepareWrite.js
Expand Up @@ -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;
Expand All @@ -43,4 +44,4 @@ function prepareWrite(outFolder, file, opt, cb) {
});
}

module.exports = prepareWrite;
module.exports = prepareWrite;
72 changes: 71 additions & 1 deletion test/dest.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 6d79529

Please sign in to comment.