Skip to content

Commit

Permalink
path get/set for recording path change #19
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and phated committed Sep 27, 2016
1 parent 7107326 commit 2abd707
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var cloneBuffer = require('./lib/cloneBuffer');
function File(file) {
if (!file) file = {};

// record path change
this.history = [];

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

Object.defineProperty(File.prototype, 'path', {
get: function() {
return this.history[this.history.length - 1];
},
set: function(path) {
// record history only when path changed
if (path && path !== this.path) {
this.history.push(path);
}

this._path = path;
}
});

module.exports = File;
59 changes: 56 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,57 @@ 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']);
});

it('should not record history when path is null', function() {
var file = new File({
cwd: '/',
path: null
});

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

file.path = '/test/test.coffee';
file.path.should.eql('/test/test.coffee');
});
});

});

0 comments on commit 2abd707

Please sign in to comment.