index.js 7.7 KB

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