Skip to content

Commit

Permalink
Breaking: Replace flag option with append option
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent d6a56d0 commit e739f6c
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 41 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ Type: `Boolean`

Default: `true` (always overwrite existing files)

##### `options.append`

Whether or not new data should be appended after existing file contents (if any).

Type: `Boolean`

Default: `false` (always replace existing contents, if any)

##### `options.sourcemaps`

Enables sourcemap support on files passed through the stream. Will write inline soucemaps if specified as `true`.
Expand Down
9 changes: 3 additions & 6 deletions lib/dest/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ var config = {
type: 'boolean',
default: true,
},
flag: {
type: 'string',
default: function(file) {
var overwrite = this.resolve('overwrite', file);
return (overwrite ? 'w': 'wx');
},
append: {
type: 'boolean',
default: false,
},
sourcemaps: {
type: ['string', 'boolean'],
Expand Down
7 changes: 5 additions & 2 deletions lib/dest/write-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ function writeContents(optResolver) {
// This is invoked by the various writeXxx modules when they've finished
// writing the contents.
function onWritten(writeErr) {
var flag = optResolver.resolve('flag', file);
if (fo.isFatalOverwriteError(writeErr, flag)) {
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: optResolver.resolve('append', file),
});
if (fo.isFatalOverwriteError(writeErr, flags)) {
return callback(writeErr);
}

Expand Down
6 changes: 5 additions & 1 deletion lib/dest/write-contents/write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
var fo = require('../../file-operations');

function writeBuffer(file, optResolver, onWritten) {
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: optResolver.resolve('append', file),
});
var opt = {
mode: file.stat.mode,
flag: optResolver.resolve('flag', file),
flags: flags,
};

fo.writeFile(file.path, file.contents, opt, onWriteFile);
Expand Down
8 changes: 6 additions & 2 deletions lib/dest/write-contents/write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ var fo = require('../../file-operations');
var readStream = require('../../src/read-contents/read-stream');

function writeStream(file, optResolver, onWritten) {
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: optResolver.resolve('append', file),
});
var opt = {
mode: file.stat.mode,
// TODO: need to test this (node calls this `flags` property)
flag: optResolver.resolve('flag', file),
// TODO: need to test this
flags: flags,
};

// TODO: is this the best API?
Expand Down
7 changes: 5 additions & 2 deletions lib/dest/write-contents/write-symbolic-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function writeSymbolicLink(file, optResolver, onWritten) {
}

var isRelative = optResolver.resolve('relativeSymlinks', file);
var flag = optResolver.resolve('flag', file);
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: optResolver.resolve('append', file),
});

if (!isWindows) {
// On non-Windows, just use 'file'
Expand Down Expand Up @@ -56,7 +59,7 @@ function writeSymbolicLink(file, optResolver, onWritten) {
}

var opts = {
flag: flag,
flags: flags,
type: type,
};
fo.symlink(file.symlink, file.path, opts, onSymlink);
Expand Down
27 changes: 18 additions & 9 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ function isValidUnixId(id) {
return true;
}

function isFatalOverwriteError(err, flag) {
function getFlags(options) {
var flags = !options.append ? 'w' : 'a';
if (!options.overwrite) {
flags += 'x';
}
return flags;
}

function isFatalOverwriteError(err, flags) {
if (!err) {
return false;
}

if (err.code === 'EEXIST' && flag === 'wx') {
if (err.code === 'EEXIST' && flags[1] === 'x') {
// Handle scenario for file overwrite failures.
return false;
}
Expand Down Expand Up @@ -274,7 +282,7 @@ function updateMetadata(fd, file, callback) {
function symlink(srcPath, destPath, opts, callback) {
// Because fs.symlink does not allow atomic overwrite option with flags, we
// delete and recreate if the link already exists and overwrite is true.
if (opts.flag === 'w') {
if (opts.flags === 'w') {
// TODO What happens when we call unlink with windows junctions?
fs.unlink(destPath, onUnlink);
} else {
Expand All @@ -289,7 +297,7 @@ function symlink(srcPath, destPath, opts, callback) {
}

function onSymlink(symlinkErr) {
if (isFatalOverwriteError(symlinkErr, opts.flag)) {
if (isFatalOverwriteError(symlinkErr, opts.flags)) {
return callback(symlinkErr);
}
callback();
Expand Down Expand Up @@ -317,10 +325,10 @@ function writeFile(filepath, data, options, callback) {

// Default the same as node
var mode = options.mode || constants.DEFAULT_FILE_MODE;
var flag = options.flag || 'w';
var position = APPEND_MODE_REGEXP.test(flag) ? null : 0;
var flags = options.flags || 'w';
var position = APPEND_MODE_REGEXP.test(flags) ? null : 0;

fs.open(filepath, flag, mode, onOpen);
fs.open(filepath, flags, mode, onOpen);

function onOpen(openErr, fd) {
if (openErr) {
Expand Down Expand Up @@ -357,7 +365,7 @@ function WriteStream(path, options, flush) {
this.path = path;

this.mode = options.mode || constants.DEFAULT_FILE_MODE;
this.flag = options.flag || 'w';
this.flags = options.flags || 'w';

// Used by node's `fs.WriteStream`
this.fd = null;
Expand All @@ -374,7 +382,7 @@ util.inherits(WriteStream, FlushWriteStream);
WriteStream.prototype.open = function() {
var self = this;

fs.open(this.path, this.flag, this.mode, onOpen);
fs.open(this.path, this.flags, this.mode, onOpen);

function onOpen(openErr, fd) {
if (openErr) {
Expand Down Expand Up @@ -435,6 +443,7 @@ function cleanup(callback) {
module.exports = {
closeFd: closeFd,
isValidUnixId: isValidUnixId,
getFlags: getFlags,
isFatalOverwriteError: isFatalOverwriteError,
isFatalUnlinkError: isFatalUnlinkError,
getModeDiff: getModeDiff,
Expand Down
7 changes: 5 additions & 2 deletions lib/symlink/link-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function linkStream(optResolver) {

function linkFile(file, enc, callback) {
var isRelative = optResolver.resolve('relativeSymlinks', file);
var flag = optResolver.resolve('flag', file);
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: false,
});

if (!isWindows) {
// On non-Windows, just use 'file'
Expand Down Expand Up @@ -57,7 +60,7 @@ function linkStream(optResolver) {
}

var opts = {
flag: flag,
flags: flags,
type: type,
};
fo.symlink(file.symlink, file.path, opts, onSymlink);
Expand Down
7 changes: 0 additions & 7 deletions lib/symlink/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ var config = {
type: 'boolean',
default: true,
},
flag: {
type: 'string',
default: function(file) {
var overwrite = this.resolve('overwrite', file);
return (overwrite ? 'w': 'wx');
},
},
};

module.exports = config;
59 changes: 59 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,65 @@ describe('.dest()', function() {
], done);
});

it('appends content with append option set to true', function(done) {
var existingContents = 'Lorem Ipsum';

var file = new File({
base: inputBase,
path: inputPath,
contents: new Buffer(contents),
});

function assert(files) {
var outputContents = fs.readFileSync(outputPath, 'utf8');

expect(files.length).toEqual(1);
expect(outputContents).toEqual(existingContents + contents);
}

// This should be overwritten
fs.mkdirSync(outputBase);
fs.writeFileSync(outputPath, existingContents);

pipe([
from.obj([file]),
vfs.dest(outputBase, { append: true }),
concat(assert),
], done);
});

it('appends content with append option set to a function that returns true', function(done) {
var existingContents = 'Lorem Ipsum';

var file = new File({
base: inputBase,
path: inputPath,
contents: new Buffer(contents),
});

function append(f) {
expect(f).toEqual(file);
return true;
}

function assert(files) {
var outputContents = fs.readFileSync(outputPath, 'utf8');

expect(files.length).toEqual(1);
expect(outputContents).toEqual(existingContents + contents);
}

// This should be overwritten
fs.mkdirSync(outputBase);
fs.writeFileSync(outputPath, existingContents);

pipe([
from.obj([file]),
vfs.dest(outputBase, { append: append }),
concat(assert),
], done);
});

it('emits a finish event', function(done) {
var destStream = vfs.dest(outputBase);

Expand Down
Loading

0 comments on commit e739f6c

Please sign in to comment.