1 var path = require('path');
2 var clone = require('clone');
3 var cloneStats = require('clone-stats');
4 var cloneBuffer = require('./lib/cloneBuffer');
5 var isBuffer = require('./lib/isBuffer');
6 var isStream = require('./lib/isStream');
7 var isNull = require('./lib/isNull');
8 var inspectStream = require('./lib/inspectStream');
9 var Stream = require('stream');
10 var replaceExt = require('replace-ext');
16 var history = file.path ? [file.path] : file.history;
17 this.history = history || [];
19 this.cwd = file.cwd || process.cwd();
20 this.base = file.base || this.cwd;
22 // stat = files stats object
23 this.stat = file.stat || null;
25 // contents = stream, buffer, or null if not read
26 this.contents = file.contents || null;
31 File.prototype.isBuffer = function() {
32 return isBuffer(this.contents);
35 File.prototype.isStream = function() {
36 return isStream(this.contents);
39 File.prototype.isNull = function() {
40 return isNull(this.contents);
43 // TODO: should this be moved to vinyl-fs?
44 File.prototype.isDirectory = function() {
45 return this.isNull() && this.stat && this.stat.isDirectory();
48 File.prototype.clone = function(opt) {
49 if (typeof opt === 'boolean') {
60 opt.deep = opt.deep === true;
61 opt.contents = opt.contents !== false;
64 // clone our file contents
66 if (this.isStream()) {
67 contents = this.contents.pipe(new Stream.PassThrough());
68 this.contents = this.contents.pipe(new Stream.PassThrough());
69 } else if (this.isBuffer()) {
70 contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
76 stat: (this.stat ? cloneStats(this.stat) : null),
77 history: this.history.slice(),
81 // clone our custom properties
82 Object.keys(this).forEach(function(key) {
83 // ignore built-in fields
84 if (key === '_contents' || key === 'stat' ||
85 key === 'history' || key === 'path' ||
86 key === 'base' || key === 'cwd') {
89 file[key] = opt.deep ? clone(this[key], true) : this[key];
94 File.prototype.pipe = function(stream, opt) {
96 if (typeof opt.end === 'undefined') opt.end = true;
98 if (this.isStream()) {
99 return this.contents.pipe(stream, opt);
101 if (this.isBuffer()) {
103 stream.end(this.contents);
105 stream.write(this.contents);
111 if (opt.end) stream.end();
115 File.prototype.inspect = function() {
118 // use relative path if possible
119 var filePath = (this.base && this.path) ? this.relative : this.path;
122 inspect.push('"'+filePath+'"');
125 if (this.isBuffer()) {
126 inspect.push(this.contents.inspect());
129 if (this.isStream()) {
130 inspect.push(inspectStream(this.contents));
133 return '<File '+inspect.join(' ')+'>';
136 File.isVinyl = function(file) {
137 return file && file._isVinyl === true;
140 // virtual attributes
141 // or stuff with extra logic
142 Object.defineProperty(File.prototype, 'contents', {
144 return this._contents;
147 if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
148 throw new Error('File.contents can only be a Buffer, a Stream, or null.');
150 this._contents = val;
154 // TODO: should this be moved to vinyl-fs?
155 Object.defineProperty(File.prototype, 'relative', {
157 if (!this.base) throw new Error('No base specified! Can not get relative.');
158 if (!this.path) throw new Error('No path specified! Can not get relative.');
159 return path.relative(this.base, this.path);
162 throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
166 Object.defineProperty(File.prototype, 'dirname', {
168 if (!this.path) throw new Error('No path specified! Can not get dirname.');
169 return path.dirname(this.path);
171 set: function(dirname) {
172 if (!this.path) throw new Error('No path specified! Can not set dirname.');
173 this.path = path.join(dirname, path.basename(this.path));
177 Object.defineProperty(File.prototype, 'basename', {
179 if (!this.path) throw new Error('No path specified! Can not get basename.');
180 return path.basename(this.path);
182 set: function(basename) {
183 if (!this.path) throw new Error('No path specified! Can not set basename.');
184 this.path = path.join(path.dirname(this.path), basename);
188 Object.defineProperty(File.prototype, 'extname', {
190 if (!this.path) throw new Error('No path specified! Can not get extname.');
191 return path.extname(this.path);
193 set: function(extname) {
194 if (!this.path) throw new Error('No path specified! Can not set extname.');
195 this.path = replaceExt(this.path, extname);
199 Object.defineProperty(File.prototype, 'path', {
201 return this.history[this.history.length - 1];
203 set: function(path) {
204 if (typeof path !== 'string') throw new Error('path should be string');
206 // record history only when path changed
207 if (path && path !== this.path) {
208 this.history.push(path);
213 module.exports = File;