Skip to content

Commit

Permalink
New: Track path changes in a history property using a getter/setter (c…
Browse files Browse the repository at this point in the history
…loses #19)
  • Loading branch information
popomore authored and phated committed Sep 27, 2016
1 parent c0b99db commit 79903bf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ var cloneBuffer = require('./lib/cloneBuffer');
function File(file) {
if (!file) file = {};

// record path change
this.history = file.path ? [file.path] : [];

// TODO: should this be moved to vinyl-fs?
this.cwd = file.cwd || process.cwd();
this.base = file.base || this.cwd;

this.path = file.path || null;

// stat = fs stats object
// TODO: should this be moved to vinyl-fs?
this.stat = file.stat || null;
Expand Down Expand Up @@ -127,4 +128,18 @@ Object.defineProperty(File.prototype, 'relative', {
}
});

Object.defineProperty(File.prototype, 'path', {
get: function() {
return this.history[this.history.length - 1];
},
set: function(path) {
if (typeof path !== 'string') throw new Error('path should be string');

// record history only when path changed
if (path && path !== this.path) {
this.history.push(path);
}
}
});

module.exports = File;
65 changes: 62 additions & 3 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('File', function() {
done();
});
});

describe('isBuffer()', function() {
it('should return true when the contents are a Buffer', function(done) {
var val = new Buffer("test");
Expand Down Expand Up @@ -406,7 +406,7 @@ describe('File', function() {
process.nextTick(done);
});
});

describe('inspect()', function() {
it('should return correct format when no contents and no path', function(done) {
var file = new File();
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('File', function() {
done();
});
});

describe('contents get/set', function() {
it('should work with Buffer', function(done) {
var val = new Buffer("test");
Expand Down Expand Up @@ -561,4 +561,63 @@ describe('File', function() {
});
});

describe('path get/set', function() {

it('should record history when instantiation', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee']);
});

it('should record history when path change', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path = '/test/test.js';
file.path.should.eql('/test/test.js');
file.history.should.eql(['/test/test.coffee', '/test/test.js']);

file.path = '/test/test.coffee';
file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee', '/test/test.js', '/test/test.coffee']);
});

it('should not record history when set the same path', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path = '/test/test.coffee';
file.path = '/test/test.coffee';
file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee']);

// ignore when set empty string
file.path = '';
file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee']);
});

it('should throw when set path null', function() {
var file = new File({
cwd: '/',
path: null
});

should.not.exist(file.path)
file.history.should.eql([]);

(function() {
file.path = null;
}).should.throw('path should be string');
});
});

});

0 comments on commit 79903bf

Please sign in to comment.