index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. function File(file) {
  11. if (!file) file = {};
  12. // record path change
  13. var history = file.path ? [file.path] : file.history;
  14. this.history = history || [];
  15. // TODO: should this be moved to vinyl-fs?
  16. this.cwd = file.cwd || process.cwd();
  17. this.base = file.base || this.cwd;
  18. // stat = fs stats object
  19. // TODO: should this be moved to vinyl-fs?
  20. this.stat = file.stat || null;
  21. // contents = stream, buffer, or null if not read
  22. this.contents = file.contents || null;
  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: false,
  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. // virtual attributes
  113. // or stuff with extra logic
  114. Object.defineProperty(File.prototype, 'contents', {
  115. get: function() {
  116. return this._contents;
  117. },
  118. set: function(val) {
  119. if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
  120. throw new Error('File.contents can only be a Buffer, a Stream, or null.');
  121. }
  122. this._contents = val;
  123. }
  124. });
  125. // TODO: should this be moved to vinyl-fs?
  126. Object.defineProperty(File.prototype, 'relative', {
  127. get: function() {
  128. if (!this.base) throw new Error('No base specified! Can not get relative.');
  129. if (!this.path) throw new Error('No path specified! Can not get relative.');
  130. return path.relative(this.base, this.path);
  131. },
  132. set: function() {
  133. throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
  134. }
  135. });
  136. Object.defineProperty(File.prototype, 'path', {
  137. get: function() {
  138. return this.history[this.history.length - 1];
  139. },
  140. set: function(path) {
  141. if (typeof path !== 'string') throw new Error('path should be string');
  142. // record history only when path changed
  143. if (path && path !== this.path) {
  144. this.history.push(path);
  145. }
  146. }
  147. });
  148. module.exports = File;