Skip to content

Commit

Permalink
chore: Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored and actions-user committed Sep 11, 2022
1 parent 9302802 commit e16aa35
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 322 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var jsFile = new Vinyl({
cwd: '/',
base: '/test/',
path: '/test/file.js',
contents: Buffer.from('var x = 123')
contents: Buffer.from('var x = 123'),
});
```

Expand Down Expand Up @@ -87,7 +87,7 @@ Default: `undefined`

##### `options.contents`

The contents of the file. If `options.contents` is a [`ReadableStream`][readable-stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.
The contents of the file. If `options.contents` is a [`ReadableStream`][readable-stream], it is wrapped in a [`cloneable-readable`][cloneable-readable] stream.

Type: [`ReadableStream`][readable-stream], [`Buffer`][buffer], or `null`

Expand Down Expand Up @@ -148,7 +148,7 @@ When constructing a Vinyl object, pass in a valid [`fs.Stats`][fs-stats] object

Returns a new Vinyl object with all attributes cloned.

__By default custom attributes are cloned deeply.__
**By default custom attributes are cloned deeply.**

If `options` or `options.deep` is `false`, custom attributes will not be cloned deeply.

Expand Down Expand Up @@ -215,7 +215,7 @@ Example:
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
path: '/test/file.js',
});

console.log(file.relative); // file.js
Expand All @@ -235,7 +235,7 @@ Example:
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
path: '/test/file.js',
});

console.log(file.dirname); // /test
Expand All @@ -260,7 +260,7 @@ Example:
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
path: '/test/file.js',
});

console.log(file.basename); // file.js
Expand All @@ -285,7 +285,7 @@ Example:
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
path: '/test/file.js',
});

console.log(file.stem); // file
Expand All @@ -310,7 +310,7 @@ Example:
var file = new File({
cwd: '/',
base: '/test/',
path: '/test/file.js'
path: '/test/file.js',
});

console.log(file.extname); // .js
Expand All @@ -335,7 +335,7 @@ Static method used for checking if an object is a Vinyl file. Use this method in

Takes an object and returns `true` if it is a Vinyl file, otherwise returns `false`.

__Note: This method uses an internal flag that some older versions of Vinyl didn't expose.__
**Note: This method uses an internal flag that some older versions of Vinyl didn't expose.**

Example:

Expand Down Expand Up @@ -410,18 +410,17 @@ class SuperFile extends Vinyl {
}

// `foo` won't be assigned to the object below
new SuperFile({ foo: "something" });
new SuperFile({ foo: 'something' });
```

This makes properties `foo` and `_foo` skipped when passed in options to `constructor(options)` so they don't get assigned to the new object and override your custom implementation. They also won't be copied when cloning. __Note:__ The `_foo` and `foo` properties will still exist on the created/cloned object because you are assigning `_foo` in the constructor and `foo` is defined on the prototype.
This makes properties `foo` and `_foo` skipped when passed in options to `constructor(options)` so they don't get assigned to the new object and override your custom implementation. They also won't be copied when cloning. **Note:** The `_foo` and `foo` properties will still exist on the created/cloned object because you are assigning `_foo` in the constructor and `foo` is defined on the prototype.

Same goes for `clone()`. If you have your own internal stuff that needs special handling during cloning, you should extend it to do so.

## License

MIT


<!-- prettier-ignore-start -->
[downloads-image]: https://img.shields.io/npm/dm/vinyl.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/vinyl
Expand All @@ -446,4 +445,3 @@ MIT
[vinyl-fs]: https://github.com/gulpjs/vinyl-fs
[cloneable-readable]: https://github.com/mcollina/cloneable-readable
<!-- prettier-ignore-end -->

86 changes: 48 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ var normalize = require('./lib/normalize');
var inspectStream = require('./lib/inspect-stream');

var builtInFields = [
'_contents', '_symlink', 'contents', 'stat', 'history', 'path',
'_base', 'base', '_cwd', 'cwd',
'_contents',
'_symlink',
'contents',
'stat',
'history',
'path',
'_base',
'base',
'_cwd',
'cwd',
];

function File(file) {
Expand All @@ -38,7 +46,7 @@ function File(file) {
history.push(file.path);
}
this.history = [];
history.forEach(function(path) {
history.forEach(function (path) {
self.path = path;
});

Expand All @@ -50,26 +58,26 @@ function File(file) {
this._symlink = null;

// Set custom properties
Object.keys(file).forEach(function(key) {
Object.keys(file).forEach(function (key) {
if (self.constructor.isCustomProp(key)) {
self[key] = file[key];
}
});
}

File.prototype.isBuffer = function() {
File.prototype.isBuffer = function () {
return Buffer.isBuffer(this.contents);
};

File.prototype.isStream = function() {
File.prototype.isStream = function () {
return isStream(this.contents);
};

File.prototype.isNull = function() {
return (this.contents === null);
File.prototype.isNull = function () {
return this.contents === null;
};

File.prototype.isDirectory = function() {
File.prototype.isDirectory = function () {
if (!this.isNull()) {
return false;
}
Expand All @@ -81,7 +89,7 @@ File.prototype.isDirectory = function() {
return false;
};

File.prototype.isSymbolic = function() {
File.prototype.isSymbolic = function () {
if (!this.isNull()) {
return false;
}
Expand All @@ -93,7 +101,7 @@ File.prototype.isSymbolic = function() {
return false;
};

File.prototype.clone = function(opt) {
File.prototype.clone = function (opt) {
var self = this;

if (typeof opt === 'boolean') {
Expand Down Expand Up @@ -122,7 +130,7 @@ File.prototype.clone = function(opt) {
var file = new this.constructor({
cwd: this.cwd,
base: this.base,
stat: (this.stat ? cloneStats(this.stat) : null),
stat: this.stat ? cloneStats(this.stat) : null,
history: this.history.slice(),
contents: contents,
});
Expand All @@ -132,7 +140,7 @@ File.prototype.clone = function(opt) {
}

// Clone our custom properties
Object.keys(this).forEach(function(key) {
Object.keys(this).forEach(function (key) {
if (self.constructor.isCustomProp(key)) {
file[key] = opt.deep ? clone(self[key], true) : self[key];
}
Expand All @@ -141,7 +149,7 @@ File.prototype.clone = function(opt) {
};

// Node.js v6.6.0+ use this symbol for custom inspection.
File.prototype[util.inspect.custom] = function() {
File.prototype[util.inspect.custom] = function () {
var inspect = [];

// Use relative path if possible
Expand All @@ -162,22 +170,22 @@ File.prototype[util.inspect.custom] = function() {
return '<File ' + inspect.join(' ') + '>';
};

File.isCustomProp = function(key) {
File.isCustomProp = function (key) {
return builtInFields.indexOf(key) === -1;
};

File.isVinyl = function(file) {
File.isVinyl = function (file) {
return (file && file._isVinyl === true) || false;
};

// Virtual attributes
// Or stuff with extra logic
Object.defineProperty(File.prototype, 'contents', {
get: function() {
get: function () {
return this._contents;
},
set: function(val) {
if (!Buffer.isBuffer(val) && !isStream(val) && (val !== null)) {
set: function (val) {
if (!Buffer.isBuffer(val) && !isStream(val) && val !== null) {
throw new Error('File.contents can only be a Buffer, a Stream, or null.');
}

Expand All @@ -193,10 +201,10 @@ Object.defineProperty(File.prototype, 'contents', {
});

Object.defineProperty(File.prototype, 'cwd', {
get: function() {
get: function () {
return this._cwd;
},
set: function(cwd) {
set: function (cwd) {
if (!cwd || typeof cwd !== 'string') {
throw new Error('cwd must be a non-empty string.');
}
Expand All @@ -205,10 +213,10 @@ Object.defineProperty(File.prototype, 'cwd', {
});

Object.defineProperty(File.prototype, 'base', {
get: function() {
get: function () {
return this._base || this._cwd;
},
set: function(base) {
set: function (base) {
if (base == null) {
delete this._base;
return;
Expand All @@ -227,25 +235,27 @@ Object.defineProperty(File.prototype, 'base', {

// TODO: Should this be moved to vinyl-fs?
Object.defineProperty(File.prototype, 'relative', {
get: function() {
get: function () {
if (!this.path) {
throw new Error('No path specified! Can not get relative.');
}
return path.relative(this.base, this.path);
},
set: function() {
throw new Error('File.relative is generated from the base and path attributes. Do not modify it.');
set: function () {
throw new Error(
'File.relative is generated from the base and path attributes. Do not modify it.'
);
},
});

Object.defineProperty(File.prototype, 'dirname', {
get: function() {
get: function () {
if (!this.path) {
throw new Error('No path specified! Can not get dirname.');
}
return path.dirname(this.path);
},
set: function(dirname) {
set: function (dirname) {
if (!this.path) {
throw new Error('No path specified! Can not set dirname.');
}
Expand All @@ -254,13 +264,13 @@ Object.defineProperty(File.prototype, 'dirname', {
});

Object.defineProperty(File.prototype, 'basename', {
get: function() {
get: function () {
if (!this.path) {
throw new Error('No path specified! Can not get basename.');
}
return path.basename(this.path);
},
set: function(basename) {
set: function (basename) {
if (!this.path) {
throw new Error('No path specified! Can not set basename.');
}
Expand All @@ -270,13 +280,13 @@ Object.defineProperty(File.prototype, 'basename', {

// Property for getting/setting stem of the filename.
Object.defineProperty(File.prototype, 'stem', {
get: function() {
get: function () {
if (!this.path) {
throw new Error('No path specified! Can not get stem.');
}
return path.basename(this.path, this.extname);
},
set: function(stem) {
set: function (stem) {
if (!this.path) {
throw new Error('No path specified! Can not set stem.');
}
Expand All @@ -285,13 +295,13 @@ Object.defineProperty(File.prototype, 'stem', {
});

Object.defineProperty(File.prototype, 'extname', {
get: function() {
get: function () {
if (!this.path) {
throw new Error('No path specified! Can not get extname.');
}
return path.extname(this.path);
},
set: function(extname) {
set: function (extname) {
if (!this.path) {
throw new Error('No path specified! Can not set extname.');
}
Expand All @@ -300,15 +310,15 @@ Object.defineProperty(File.prototype, 'extname', {
});

Object.defineProperty(File.prototype, 'path', {
get: function() {
get: function () {
var path = this.history[this.history.length - 1];
if (path) {
return path;
} else {
return null;
}
},
set: function(path) {
set: function (path) {
if (typeof path !== 'string') {
throw new Error('path should be a string.');
}
Expand All @@ -322,10 +332,10 @@ Object.defineProperty(File.prototype, 'path', {
});

Object.defineProperty(File.prototype, 'symlink', {
get: function() {
get: function () {
return this._symlink;
},
set: function(symlink) {
set: function (symlink) {
// TODO: should this set the mode to symbolic if set?
if (typeof symlink !== 'string') {
throw new Error('symlink should be a string');
Expand Down
Loading

0 comments on commit e16aa35

Please sign in to comment.