index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. 'use strict';
  2. var path = require('path');
  3. var util = require('util');
  4. var isBuffer = require('buffer').Buffer.isBuffer;
  5. var clone = require('clone');
  6. var cloneable = require('cloneable-readable');
  7. var replaceExt = require('replace-ext');
  8. var cloneStats = require('clone-stats');
  9. var cloneBuffer = require('clone-buffer');
  10. var removeTrailingSep = require('remove-trailing-separator');
  11. var isStream = require('./lib/is-stream');
  12. var normalize = require('./lib/normalize');
  13. var inspectStream = require('./lib/inspect-stream');
  14. var builtInFields = [
  15. '_contents', '_symlink', 'contents', 'stat', 'history', 'path',
  16. '_base', 'base', '_cwd', 'cwd',
  17. ];
  18. function File(file) {
  19. var self = this;
  20. if (!file) {
  21. file = {};
  22. }
  23. // Stat = files stats object
  24. this.stat = file.stat || null;
  25. // Contents = stream, buffer, or null if not read
  26. this.contents = file.contents || null;
  27. // Replay path history to ensure proper normalization and trailing sep
  28. var history = Array.prototype.slice.call(file.history || []);
  29. if (file.path) {
  30. history.push(file.path);
  31. }
  32. this.history = [];
  33. history.forEach(function(path) {
  34. self.path = path;
  35. });
  36. this.cwd = file.cwd || process.cwd();
  37. this.base = file.base;
  38. this._isVinyl = true;
  39. this._symlink = null;
  40. // Set custom properties
  41. Object.keys(file).forEach(function(key) {
  42. if (self.constructor.isCustomProp(key)) {
  43. self[key] = file[key];
  44. }
  45. });
  46. }
  47. File.prototype.isBuffer = function() {
  48. return isBuffer(this.contents);
  49. };
  50. File.prototype.isStream = function() {
  51. return isStream(this.contents);
  52. };
  53. File.prototype.isNull = function() {
  54. return (this.contents === null);
  55. };
  56. File.prototype.isDirectory = function() {
  57. if (!this.isNull()) {
  58. return false;
  59. }
  60. if (this.stat && typeof this.stat.isDirectory === 'function') {
  61. return this.stat.isDirectory();
  62. }
  63. return false;
  64. };
  65. File.prototype.isSymbolic = function() {
  66. if (!this.isNull()) {
  67. return false;
  68. }
  69. if (this.stat && typeof this.stat.isSymbolicLink === 'function') {
  70. return this.stat.isSymbolicLink();
  71. }
  72. return false;
  73. };
  74. File.prototype.clone = function(opt) {
  75. var self = this;
  76. if (typeof opt === 'boolean') {
  77. opt = {
  78. deep: opt,
  79. contents: true,
  80. };
  81. } else if (!opt) {
  82. opt = {
  83. deep: true,
  84. contents: true,
  85. };
  86. } else {
  87. opt.deep = opt.deep === true;
  88. opt.contents = opt.contents !== false;
  89. }
  90. // Clone our file contents
  91. var contents;
  92. if (this.isStream()) {
  93. contents = this.contents.clone();
  94. } else if (this.isBuffer()) {
  95. contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
  96. }
  97. var file = new this.constructor({
  98. cwd: this.cwd,
  99. base: this.base,
  100. stat: (this.stat ? cloneStats(this.stat) : null),
  101. history: this.history.slice(),
  102. contents: contents,
  103. });
  104. if (this.isSymbolic()) {
  105. file.symlink = this.symlink;
  106. }
  107. // Clone our custom properties
  108. Object.keys(this).forEach(function(key) {
  109. if (self.constructor.isCustomProp(key)) {
  110. file[key] = opt.deep ? clone(self[key], true) : self[key];
  111. }
  112. });
  113. return file;
  114. };
  115. File.prototype.inspect = function() {
  116. var inspect = [];
  117. // Use relative path if possible
  118. var filePath = this.path ? this.relative : null;
  119. if (filePath) {
  120. inspect.push('"' + filePath + '"');
  121. }
  122. if (this.isBuffer()) {
  123. inspect.push(this.contents.inspect());
  124. }
  125. if (this.isStream()) {
  126. inspect.push(inspectStream(this.contents));
  127. }
  128. return '<File ' + inspect.join(' ') + '>';
  129. };
  130. // Newer Node.js versions use this symbol for custom inspection.
  131. if (util.inspect.custom) {
  132. File.prototype[util.inspect.custom] = File.prototype.inspect;
  133. }
  134. File.isCustomProp = function(key) {
  135. return builtInFields.indexOf(key) === -1;
  136. };
  137. File.isVinyl = function(file) {
  138. return (file && file._isVinyl === true) || false;
  139. };
  140. // Virtual attributes
  141. // Or stuff with extra logic
  142. Object.defineProperty(File.prototype, 'contents', {
  143. get: function() {
  144. return this._contents;
  145. },
  146. set: function(val) {
  147. if (!isBuffer(val) && !isStream(val) && (val !== null)) {
  148. throw new Error('File.contents can only be a Buffer, a Stream, or null.');
  149. }
  150. // Ask cloneable if the stream is a already a cloneable
  151. // this avoid piping into many streams
  152. // reducing the overhead of cloning
  153. if (isStream(val) && !cloneable.isCloneable(val)) {
  154. val = cloneable(val);
  155. }
  156. this._contents = val;
  157. },
  158. });
  159. Object.defineProperty(File.prototype, 'cwd', {
  160. get: function() {
  161. return this._cwd;
  162. },
  163. set: function(cwd) {
  164. if (!cwd || typeof cwd !== 'string') {
  165. throw new Error('cwd must be a non-empty string.');
  166. }
  167. this._cwd = removeTrailingSep(normalize(cwd));
  168. },
  169. });
  170. Object.defineProperty(File.prototype, 'base', {
  171. get: function() {
  172. return this._base || this._cwd;
  173. },
  174. set: function(base) {
  175. if (base == null) {
  176. delete this._base;
  177. return;
  178. }
  179. if (typeof base !== 'string' || !base) {
  180. throw new Error('base must be a non-empty string, or null/undefined.');
  181. }
  182. base = removeTrailingSep(normalize(base));
  183. if (base !== this._cwd) {
  184. this._base = base;
  185. } else {
  186. delete this._base;
  187. }
  188. },
  189. });
  190. // TODO: Should this be moved to vinyl-fs?
  191. Object.defineProperty(File.prototype, 'relative', {
  192. get: function() {
  193. if (!this.path) {
  194. throw new Error('No path specified! Can not get relative.');
  195. }
  196. return path.relative(this.base, this.path);
  197. },
  198. set: function() {
  199. throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
  200. },
  201. });
  202. Object.defineProperty(File.prototype, 'dirname', {
  203. get: function() {
  204. if (!this.path) {
  205. throw new Error('No path specified! Can not get dirname.');
  206. }
  207. return path.dirname(this.path);
  208. },
  209. set: function(dirname) {
  210. if (!this.path) {
  211. throw new Error('No path specified! Can not set dirname.');
  212. }
  213. this.path = path.join(dirname, this.basename);
  214. },
  215. });
  216. Object.defineProperty(File.prototype, 'basename', {
  217. get: function() {
  218. if (!this.path) {
  219. throw new Error('No path specified! Can not get basename.');
  220. }
  221. return path.basename(this.path);
  222. },
  223. set: function(basename) {
  224. if (!this.path) {
  225. throw new Error('No path specified! Can not set basename.');
  226. }
  227. this.path = path.join(this.dirname, basename);
  228. },
  229. });
  230. // Property for getting/setting stem of the filename.
  231. Object.defineProperty(File.prototype, 'stem', {
  232. get: function() {
  233. if (!this.path) {
  234. throw new Error('No path specified! Can not get stem.');
  235. }
  236. return path.basename(this.path, this.extname);
  237. },
  238. set: function(stem) {
  239. if (!this.path) {
  240. throw new Error('No path specified! Can not set stem.');
  241. }
  242. this.path = path.join(this.dirname, stem + this.extname);
  243. },
  244. });
  245. Object.defineProperty(File.prototype, 'extname', {
  246. get: function() {
  247. if (!this.path) {
  248. throw new Error('No path specified! Can not get extname.');
  249. }
  250. return path.extname(this.path);
  251. },
  252. set: function(extname) {
  253. if (!this.path) {
  254. throw new Error('No path specified! Can not set extname.');
  255. }
  256. this.path = replaceExt(this.path, extname);
  257. },
  258. });
  259. Object.defineProperty(File.prototype, 'path', {
  260. get: function() {
  261. return this.history[this.history.length - 1];
  262. },
  263. set: function(path) {
  264. if (typeof path !== 'string') {
  265. throw new Error('path should be a string.');
  266. }
  267. path = removeTrailingSep(normalize(path));
  268. // Record history only when path changed
  269. if (path && path !== this.path) {
  270. this.history.push(path);
  271. }
  272. },
  273. });
  274. Object.defineProperty(File.prototype, 'symlink', {
  275. get: function() {
  276. return this._symlink;
  277. },
  278. set: function(symlink) {
  279. // TODO: should this set the mode to symbolic if set?
  280. if (typeof symlink !== 'string') {
  281. throw new Error('symlink should be a string');
  282. }
  283. this._symlink = removeTrailingSep(normalize(symlink));
  284. },
  285. });
  286. module.exports = File;