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