diff --git a/index.js b/index.js index 58c3632..d7d5c21 100644 --- a/index.js +++ b/index.js @@ -11,12 +11,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; @@ -123,4 +124,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; diff --git a/test/File.js b/test/File.js index 1f2a83a..b25a189 100644 --- a/test/File.js +++ b/test/File.js @@ -80,7 +80,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"); @@ -382,7 +382,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(); @@ -445,7 +445,7 @@ describe('File', function() { done(); }); }); - + describe('contents get/set', function() { it('should work with Buffer', function(done) { var val = new Buffer("test"); @@ -537,4 +537,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'); + }); + }); + });