src/lib/AtviseFile.js
/**
* @external {File} https://github.com/gulpjs/vinyl
*/
import { File } from 'gulp-util';
import NodeId, { NodeIdType } from './model/NodeId';
import NodeAddress from './NodeAddress';
/**
* A {@link File} subclass used for transform streams. Currently only extends vinyl by a `rc`
* property holding the runtime config.
*/
export default class AtviseFile extends File {
/**
* Creates a new AtviseFile based on some options.
* @param {Object} [options] Options passed directly to {@link File}.
* @param {Object} [options.rc] The runtime configuration to attach to the file.
*/
constructor(options) {
super(options);
/**
* The file's runtime configuration
* @type {Object}
*/
this.rc = options.rc || {};
}
/**
* Create an AtviseFile from a regular vinyl {@link File}.
* @param {File} file The source file
* @param {Object} [runtimeConfig={}] The runtime configuration to use.
* @return {AtviseFile} An AtviseFile based on `file`.
*/
static from(file, runtimeConfig = {}) {
return new AtviseFile({
cwd: file.cwd,
base: file.base,
path: file.path,
contents: file.contents,
stat: file.stat,
rc: runtimeConfig,
});
}
/**
* A required method for the {@link File} subclass to work properly.
* @see https://github.com/gulpjs/vinyl#extending-vinyl
* @param {String} name Property name
* @return {Boolean} Whether the property `name` is custom or not.
*/
static isCustomProp(name) {
return super.isCustomProp(name) && name !== 'rc';
}
/**
* Returns a new AtviseFile with all attributes cloned.
* @see https://github.com/gulpjs/vinyl#filecloneoptions
* @param {Object} [options] The options passed to {@link File#clone}
* @return {AtviseFile}
*/
clone(options) {
return AtviseFile.from(super.clone(options), this.rc);
}
/**
* The file's node id.
* @type {NodeId}
*/
get nodeId() {
return new NodeId(NodeAddress.getNodePath(this), NodeIdType.String, this.rc.namespace);
}
}