index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. var builtInFields = [
  12. '_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd',
  13. ];
  14. function File(file) {
  15. var self = this;
  16. if (!file) {
  17. file = {};
  18. }
  19. // Record path change
  20. var history = file.path ? [file.path] : file.history;
  21. this.history = history || [];
  22. this.cwd = file.cwd || process.cwd();
  23. this.base = file.base || this.cwd;
  24. // Stat = files stats object
  25. this.stat = file.stat || null;
  26. // Contents = stream, buffer, or null if not read
  27. this.contents = file.contents || null;
  28. this._isVinyl = true;
  29. // Set custom properties
  30. Object.keys(file).forEach(function(key) {
  31. if (self.constructor.isCustomProp(key)) {
  32. self[key] = file[key];
  33. }
  34. });
  35. }
  36. File.prototype.isBuffer = function() {
  37. return isBuffer(this.contents);
  38. };
  39. File.prototype.isStream = function() {
  40. return isStream(this.contents);
  41. };
  42. File.prototype.isNull = function() {
  43. return isNull(this.contents);
  44. };
  45. // TODO: Should this be moved to vinyl-fs?
  46. File.prototype.isDirectory = function() {
  47. return this.isNull() && this.stat && this.stat.isDirectory();
  48. };
  49. File.prototype.clone = function(opt) {
  50. var self = this;
  51. if (typeof opt === 'boolean') {
  52. opt = {
  53. deep: opt,
  54. contents: true,
  55. };
  56. } else if (!opt) {
  57. opt = {
  58. deep: true,
  59. contents: true,
  60. };
  61. } else {
  62. opt.deep = opt.deep === true;
  63. opt.contents = opt.contents !== false;
  64. }
  65. // Clone our file contents
  66. var contents;
  67. if (this.isStream()) {
  68. contents = this.contents.pipe(new Stream.PassThrough());
  69. this.contents = this.contents.pipe(new Stream.PassThrough());
  70. } else if (this.isBuffer()) {
  71. contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
  72. }
  73. var file = new this.constructor({
  74. cwd: this.cwd,
  75. base: this.base,
  76. stat: (this.stat ? cloneStats(this.stat) : null),
  77. history: this.history.slice(),
  78. contents: contents,
  79. });
  80. // Clone our custom properties
  81. Object.keys(this).forEach(function(key) {
  82. if (self.constructor.isCustomProp(key)) {
  83. file[key] = opt.deep ? clone(self[key], true) : self[key];
  84. }
  85. });
  86. return file;
  87. };
  88. File.prototype.pipe = function(stream, opt) {
  89. if (!opt) {
  90. opt = {};
  91. }
  92. if (typeof opt.end === 'undefined') {
  93. opt.end = true;
  94. }
  95. if (this.isStream()) {
  96. return this.contents.pipe(stream, opt);
  97. }
  98. if (this.isBuffer()) {
  99. if (opt.end) {
  100. stream.end(this.contents);
  101. } else {
  102. stream.write(this.contents);
  103. }
  104. return stream;
  105. }
  106. // Check if isNull
  107. if (opt.end) {
  108. stream.end();
  109. }
  110. return stream;
  111. };
  112. File.prototype.inspect = function() {
  113. var inspect = [];
  114. // Use relative path if possible
  115. var filePath = (this.base && this.path) ? this.relative : this.path;
  116. if (filePath) {
  117. inspect.push('"' + filePath + '"');
  118. }
  119. if (this.isBuffer()) {
  120. inspect.push(this.contents.inspect());
  121. }
  122. if (this.isStream()) {
  123. inspect.push(inspectStream(this.contents));
  124. }
  125. return '<File ' + inspect.join(' ') + '>';
  126. };
  127. File.isCustomProp = function(key) {
  128. return builtInFields.indexOf(key) === -1;
  129. };
  130. File.isVinyl = function(file) {
  131. return (file && file._isVinyl === true) || false;
  132. };
  133. // Virtual attributes
  134. // Or stuff with extra logic
  135. Object.defineProperty(File.prototype, 'contents', {
  136. get: function() {
  137. return this._contents;
  138. },
  139. set: function(val) {
  140. if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
  141. throw new Error('File.contents can only be a Buffer, a Stream, or null.');
  142. }
  143. this._contents = val;
  144. },
  145. });
  146. // TODO: Should this be moved to vinyl-fs?
  147. Object.defineProperty(File.prototype, 'relative', {
  148. get: function() {
  149. if (!this.base) {
  150. throw new Error('No base specified! Can not get relative.');
  151. }
  152. if (!this.path) {
  153. throw new Error('No path specified! Can not get relative.');
  154. }
  155. return path.relative(this.base, this.path);
  156. },
  157. set: function() {
  158. throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
  159. },
  160. });
  161. Object.defineProperty(File.prototype, 'dirname', {
  162. get: function() {
  163. if (!this.path) {
  164. throw new Error('No path specified! Can not get dirname.');
  165. }
  166. return path.dirname(this.path);
  167. },
  168. set: function(dirname) {
  169. if (!this.path) {
  170. throw new Error('No path specified! Can not set dirname.');
  171. }
  172. this.path = path.join(dirname, path.basename(this.path));
  173. },
  174. });
  175. Object.defineProperty(File.prototype, 'basename', {
  176. get: function() {
  177. if (!this.path) {
  178. throw new Error('No path specified! Can not get basename.');
  179. }
  180. return path.basename(this.path);
  181. },
  182. set: function(basename) {
  183. if (!this.path) {
  184. throw new Error('No path specified! Can not set basename.');
  185. }
  186. this.path = path.join(path.dirname(this.path), basename);
  187. },
  188. });
  189. // Property for getting/setting stem of the filename.
  190. Object.defineProperty(File.prototype, 'stem', {
  191. get: function() {
  192. if (!this.path) {
  193. throw new Error('No path specified! Can not get stem.');
  194. }
  195. return path.basename(this.path, this.extname);
  196. },
  197. set: function(stem) {
  198. if (!this.path) {
  199. throw new Error('No path specified! Can not set stem.');
  200. }
  201. this.path = path.join(path.dirname(this.path), stem + this.extname);
  202. },
  203. });
  204. Object.defineProperty(File.prototype, 'extname', {
  205. get: function() {
  206. if (!this.path) {
  207. throw new Error('No path specified! Can not get extname.');
  208. }
  209. return path.extname(this.path);
  210. },
  211. set: function(extname) {
  212. if (!this.path) {
  213. throw new Error('No path specified! Can not set extname.');
  214. }
  215. this.path = replaceExt(this.path, extname);
  216. },
  217. });
  218. Object.defineProperty(File.prototype, 'path', {
  219. get: function() {
  220. return this.history[this.history.length - 1];
  221. },
  222. set: function(path) {
  223. if (typeof path !== 'string') {
  224. throw new Error('path should be string');
  225. }
  226. // Record history only when path changed
  227. if (path && path !== this.path) {
  228. this.history.push(path);
  229. }
  230. },
  231. });
  232. module.exports = File;