index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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');
  11. function File(file) {
  12. if (!file) file = {};
  13. // record path change
  14. var history = file.path ? [file.path] : file.history;
  15. this.history = history || [];
  16. this.cwd = file.cwd || process.cwd();
  17. this.base = file.base || this.cwd;
  18. // stat = files stats object
  19. this.stat = file.stat || null;
  20. // contents = stream, buffer, or null if not read
  21. this.contents = file.contents || null;
  22. this._isVinyl = true;
  23. }
  24. File.prototype.isBuffer = function() {
  25. return isBuffer(this.contents);
  26. };
  27. File.prototype.isStream = function() {
  28. return isStream(this.contents);
  29. };
  30. File.prototype.isNull = function() {
  31. return isNull(this.contents);
  32. };
  33. // TODO: should this be moved to vinyl-fs?
  34. File.prototype.isDirectory = function() {
  35. return this.isNull() && this.stat && this.stat.isDirectory();
  36. };
  37. File.prototype.clone = function(opt) {
  38. if (typeof opt === 'boolean') {
  39. opt = {
  40. deep: opt,
  41. contents: true
  42. };
  43. } else if (!opt) {
  44. opt = {
  45. deep: true,
  46. contents: true
  47. };
  48. } else {
  49. opt.deep = opt.deep === true;
  50. opt.contents = opt.contents !== false;
  51. }
  52. // clone our file contents
  53. var contents;
  54. if (this.isStream()) {
  55. contents = this.contents.pipe(new Stream.PassThrough());
  56. this.contents = this.contents.pipe(new Stream.PassThrough());
  57. } else if (this.isBuffer()) {
  58. contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
  59. }
  60. var file = new File({
  61. cwd: this.cwd,
  62. base: this.base,
  63. stat: (this.stat ? cloneStats(this.stat) : null),
  64. history: this.history.slice(),
  65. contents: contents
  66. });
  67. // clone our custom properties
  68. Object.keys(this).forEach(function(key) {
  69. // ignore built-in fields
  70. if (key === '_contents' || key === 'stat' ||
  71. key === 'history' || key === 'path' ||
  72. key === 'base' || key === 'cwd') {
  73. return;
  74. }
  75. file[key] = opt.deep ? clone(this[key], true) : this[key];
  76. }, this);
  77. return file;
  78. };
  79. File.prototype.pipe = function(stream, opt) {
  80. if (!opt) opt = {};
  81. if (typeof opt.end === 'undefined') opt.end = true;
  82. if (this.isStream()) {
  83. return this.contents.pipe(stream, opt);
  84. }
  85. if (this.isBuffer()) {
  86. if (opt.end) {
  87. stream.end(this.contents);
  88. } else {
  89. stream.write(this.contents);
  90. }
  91. return stream;
  92. }
  93. // isNull
  94. if (opt.end) stream.end();
  95. return stream;
  96. };
  97. File.prototype.inspect = function() {
  98. var inspect = [];
  99. // use relative path if possible
  100. var filePath = (this.base && this.path) ? this.relative : this.path;
  101. if (filePath) {
  102. inspect.push('"'+filePath+'"');
  103. }
  104. if (this.isBuffer()) {
  105. inspect.push(this.contents.inspect());
  106. }
  107. if (this.isStream()) {
  108. inspect.push(inspectStream(this.contents));
  109. }
  110. return '<File '+inspect.join(' ')+'>';
  111. };
  112. File.isVinyl = function(file) {
  113. return file && file._isVinyl === true;
  114. };
  115. // virtual attributes
  116. // or stuff with extra logic
  117. Object.defineProperty(File.prototype, 'contents', {
  118. get: function() {
  119. return this._contents;
  120. },
  121. set: function(val) {
  122. if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
  123. throw new Error('File.contents can only be a Buffer, a Stream, or null.');
  124. }
  125. this._contents = val;
  126. }
  127. });
  128. // TODO: should this be moved to vinyl-fs?
  129. Object.defineProperty(File.prototype, 'relative', {
  130. get: function() {
  131. if (!this.base) throw new Error('No base specified! Can not get relative.');
  132. if (!this.path) throw new Error('No path specified! Can not get relative.');
  133. return path.relative(this.base, this.path);
  134. },
  135. set: function() {
  136. throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
  137. }
  138. });
  139. Object.defineProperty(File.prototype, 'dirname', {
  140. get: function() {
  141. if (!this.path) throw new Error('No path specified! Can not get dirname.');
  142. return path.dirname(this.path);
  143. },
  144. set: function(dirname) {
  145. if (!this.path) throw new Error('No path specified! Can not set dirname.');
  146. this.path = path.join(dirname, path.basename(this.path));
  147. }
  148. });
  149. Object.defineProperty(File.prototype, 'basename', {
  150. get: function() {
  151. if (!this.path) throw new Error('No path specified! Can not get basename.');
  152. return path.basename(this.path);
  153. },
  154. set: function(basename) {
  155. if (!this.path) throw new Error('No path specified! Can not set basename.');
  156. this.path = path.join(path.dirname(this.path), basename);
  157. }
  158. });
  159. Object.defineProperty(File.prototype, 'extname', {
  160. get: function() {
  161. if (!this.path) throw new Error('No path specified! Can not get extname.');
  162. return path.extname(this.path);
  163. },
  164. set: function(extname) {
  165. if (!this.path) throw new Error('No path specified! Can not set extname.');
  166. this.path = replaceExt(this.path, extname);
  167. }
  168. });
  169. Object.defineProperty(File.prototype, 'path', {
  170. get: function() {
  171. return this.history[this.history.length - 1];
  172. },
  173. set: function(path) {
  174. if (typeof path !== 'string') throw new Error('path should be string');
  175. // record history only when path changed
  176. if (path && path !== this.path) {
  177. this.history.push(path);
  178. }
  179. }
  180. });
  181. module.exports = File;