Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var isStream = require('./lib/isStream');
var isNull = require('./lib/isNull');
var inspectStream = require('./lib/inspectStream');
var cloneBuffer = require('./lib/cloneBuffer');
var Stream = require('stream');

function File(file) {
if (!file) file = {};
Expand Down Expand Up @@ -43,8 +44,14 @@ File.prototype.isDirectory = function() {
};

File.prototype.clone = function() {
var clonedContents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
var clonedStat = this.stat ? cloneStats(this.stat) : null;
var clonedContents = null;
if(this.isBuffer()) {
clonedContents = cloneBuffer(this.contents);
} else if(this.isStream()) {
clonedContents = this.contents.pipe(new Stream.PassThrough());
this.contents = this.contents.pipe(new Stream.PassThrough());
}

return new File({
cwd: this.cwd,
Expand Down
49 changes: 47 additions & 2 deletions test/File.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var File = require('../');
var Stream = require('stream');
var fs = require('fs');
var es = require('event-stream');

var should = require('should');
require('mocha');
Expand Down Expand Up @@ -196,20 +197,64 @@ describe('File', function() {
});

it('should copy all attributes over with Stream', function(done) {
var contents = new Stream.PassThrough();
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: new Stream()
contents: contents
};
var file = new File(options);
var file2 = file.clone();

contents.write(Buffer('wa'));

process.nextTick(function() {
contents.write(Buffer('dup'));
contents.end();
});

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file.contents.pipe(es.wait(function(err, data) {
file2.contents.pipe(es.wait(function(err, data2) {
data2.should.equal(data, 'stream contents should be the same');
}));
}));
file2.contents.should.not.equal(file.contents, 'stream ref should not be the same');
done();
});

it('should copy all attributes over with Stream', function(done) {
var contents = new Stream.PassThrough();
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: contents
};
var file = new File(options);
var file2 = file.clone();

contents.write(Buffer('wa'));

process.nextTick(function() {
contents.write(Buffer('dup'));
contents.end();
});

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.contents.should.equal(file.contents, 'stream ref should be the same');
file2.contents.pipe(es.wait(function(err, data) {
file.contents.pipe(es.wait(function(err, data2) {
data2.should.equal(data, 'stream contents should be the same');
}));
}));
file2.contents.should.not.equal(file.contents, 'stream ref should not be the same');
done();
});

Expand Down