You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // Clone our custom properties
  105. Object.keys(this).forEach(function(key) {
  106. if (self.constructor.isCustomProp(key)) {
  107. file[key] = opt.deep ? clone(self[key], true) : self[key];
  108. }
  109. });
  110. return file;
  111. };
  112. File.prototype.inspect = function() {
  113. var inspect = [];
  114. // Use relative path if possible
  115. var filePath = this.path ? this.relative : null;
  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. // Newer Node.js versions use this symbol for custom inspection.
  128. if (util.inspect.custom) {
  129. File.prototype[util.inspect.custom] = File.prototype.inspect;
  130. }
  131. File.isCustomProp = function(key) {
  132. return builtInFields.indexOf(key) === -1;
  133. };
  134. File.isVinyl = function(file) {
  135. return (file && file._isVinyl === true) || false;
  136. };
  137. // Virtual attributes
  138. // Or stuff with extra logic
  139. Object.defineProperty(File.prototype, 'contents', {
  140. get: function() {
  141. return this._contents;
  142. },
  143. set: function(val) {
  144. if (!isBuffer(val) && !isStream(val) && (val !== null)) {
  145. throw new Error('File.contents can only be a Buffer, a Stream, or null.');
  146. }
  147. // Ask cloneable if the stream is a already a cloneable
  148. // this avoid piping into many streams
  149. // reducing the overhead of cloning
  150. if (isStream(val) && !cloneable.isCloneable(val)) {
  151. val = cloneable(val);
  152. }
  153. this._contents = val;
  154. },
  155. });
  156. Object.defineProperty(File.prototype, 'cwd', {
  157. get: function() {
  158. return this._cwd;
  159. },
  160. set: function(cwd) {
  161. if (!cwd || typeof cwd !== 'string') {
  162. throw new Error('cwd must be a non-empty string.');
  163. }
  164. this._cwd = removeTrailingSep(normalize(cwd));
  165. },
  166. });
  167. Object.defineProperty(File.prototype, 'base', {
  168. get: function() {
  169. return this._base || this._cwd;
  170. },
  171. set: function(base) {
  172. if (base == null) {
  173. delete this._base;
  174. return;
  175. }
  176. if (typeof base !== 'string' || !base) {
  177. throw new Error('base must be a non-empty string, or null/undefined.');
  178. }
  179. base = removeTrailingSep(normalize(base));
  180. if (base !== this._cwd) {
  181. this._base = base;
  182. } else {
  183. delete this._base;
  184. }
  185. },
  186. });
  187. // TODO: Should this be moved to vinyl-fs?
  188. Object.defineProperty(File.prototype, 'relative', {
  189. get: function() {
  190. if (!this.path) {
  191. throw new Error('No path specified! Can not get relative.');
  192. }
  193. return path.relative(this.base, this.path);
  194. },
  195. set: function() {
  196. throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
  197. },
  198. });
  199. Object.defineProperty(File.prototype, 'dirname', {
  200. get: function() {
  201. if (!this.path) {
  202. throw new Error('No path specified! Can not get dirname.');
  203. }
  204. return path.dirname(this.path);
  205. },
  206. set: function(dirname) {
  207. if (!this.path) {
  208. throw new Error('No path specified! Can not set dirname.');
  209. }
  210. this.path = path.join(dirname, this.basename);
  211. },
  212. });
  213. Object.defineProperty(File.prototype, 'basename', {
  214. get: function() {
  215. if (!this.path) {
  216. throw new Error('No path specified! Can not get basename.');
  217. }
  218. return path.basename(this.path);
  219. },
  220. set: function(basename) {
  221. if (!this.path) {
  222. throw new Error('No path specified! Can not set basename.');
  223. }
  224. this.path = path.join(this.dirname, basename);
  225. },
  226. });
  227. // Property for getting/setting stem of the filename.
  228. Object.defineProperty(File.prototype, 'stem', {
  229. get: function() {
  230. if (!this.path) {
  231. throw new Error('No path specified! Can not get stem.');
  232. }
  233. return path.basename(this.path, this.extname);
  234. },
  235. set: function(stem) {
  236. if (!this.path) {
  237. throw new Error('No path specified! Can not set stem.');
  238. }
  239. this.path = path.join(this.dirname, stem + this.extname);
  240. },
  241. });
  242. Object.defineProperty(File.prototype, 'extname', {
  243. get: function() {
  244. if (!this.path) {
  245. throw new Error('No path specified! Can not get extname.');
  246. }
  247. return path.extname(this.path);
  248. },
  249. set: function(extname) {
  250. if (!this.path) {
  251. throw new Error('No path specified! Can not set extname.');
  252. }
  253. this.path = replaceExt(this.path, extname);
  254. },
  255. });
  256. Object.defineProperty(File.prototype, 'path', {
  257. get: function() {
  258. return this.history[this.history.length - 1];
  259. },
  260. set: function(path) {
  261. if (typeof path !== 'string') {
  262. throw new Error('path should be a string.');
  263. }
  264. path = removeTrailingSep(normalize(path));
  265. // Record history only when path changed
  266. if (path && path !== this.path) {
  267. this.history.push(path);
  268. }
  269. },
  270. });
  271. Object.defineProperty(File.prototype, 'symlink', {
  272. get: function() {
  273. return this._symlink;
  274. },
  275. set: function(symlink) {
  276. // TODO: should this set the mode to symbolic if set?
  277. if (typeof symlink !== 'string') {
  278. throw new Error('symlink should be a string');
  279. }
  280. this._symlink = removeTrailingSep(normalize(symlink));
  281. },
  282. });
  283. module.exports = File;