Make PolyVinyl's functional
This commit is contained in:
@ -1,98 +1,104 @@
|
|||||||
// originally base off of https://github.com/gulpjs/vinyl
|
// originally base off of https://github.com/gulpjs/vinyl
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import replaceExt from 'replace-ext';
|
import replaceExt from 'replace-ext';
|
||||||
|
import invariant from 'invariant';
|
||||||
|
|
||||||
export default class File {
|
// interface PolyVinyl {
|
||||||
constructor({
|
// contents: String,
|
||||||
path,
|
// path: String,
|
||||||
history = [],
|
// history: [...String],
|
||||||
base,
|
// error: Null|Object
|
||||||
contents = ''
|
// }
|
||||||
} = {}) {
|
//
|
||||||
// Record path change
|
// createPoly({
|
||||||
this.history = path ? [path] : history;
|
// path: String,
|
||||||
this.base = base;
|
// contents: String,
|
||||||
this.contents = contents;
|
// history?: [...String],
|
||||||
this._isPolyVinyl = true;
|
// }) => PolyVinyl, throws
|
||||||
this.error = null;
|
export function createPoly({ path, contents, history } = {}) {
|
||||||
}
|
invariant(
|
||||||
|
typeof path === 'string',
|
||||||
|
'path must be a string but got %s',
|
||||||
|
path
|
||||||
|
);
|
||||||
|
|
||||||
static isPolyVinyl = function(file) {
|
invariant(
|
||||||
return file && file._isPolyVinyl === true || false;
|
typeof contents === 'string',
|
||||||
|
'contents must be a string but got %s',
|
||||||
|
contents
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
history: Array.isArray(history) ? history : [ path ],
|
||||||
|
path: path,
|
||||||
|
contents: contents,
|
||||||
|
error: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// isPoly(poly: Any) => Boolean
|
||||||
|
export function isPoly(poly) {
|
||||||
|
return poly &&
|
||||||
|
typeof poly.contents === 'string' &&
|
||||||
|
typeof poly.path === 'string' &&
|
||||||
|
Array.isArray(poly.history);
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPoly(poly: Any) => Void, throws
|
||||||
|
export function checkPoly(poly) {
|
||||||
|
invariant(
|
||||||
|
isPoly(poly),
|
||||||
|
'function should receive a PolyVinyl, but got %s',
|
||||||
|
poly
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// isEmpty(poly: PolyVinyl) => Boolean, throws
|
||||||
|
export function isEmpty(poly) {
|
||||||
|
checkPoly(poly);
|
||||||
|
return !!poly.contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateContents(contents: String, poly: PolyVinyl) => PolyVinyl
|
||||||
|
export function updateContents(contents, poly) {
|
||||||
|
checkPoly(poly);
|
||||||
|
return {
|
||||||
|
...poly,
|
||||||
|
contents
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getExt(poly) {
|
||||||
|
checkPoly(poly);
|
||||||
|
invariant(
|
||||||
|
!!poly.path,
|
||||||
|
'No path specified! Can not get extname'
|
||||||
|
);
|
||||||
|
return path.extname(poly.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setExt(extname, poly) {
|
||||||
|
invariant(
|
||||||
|
poly.path,
|
||||||
|
'No path specified! Can not set extname',
|
||||||
|
);
|
||||||
|
const newPoly = {
|
||||||
|
...poly,
|
||||||
|
path: replaceExt(this.path, extname)
|
||||||
|
};
|
||||||
|
newPoly.history = [ ...poly.history, newPoly.path ];
|
||||||
|
return newPoly;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setError(error, poly) {
|
||||||
|
invariant(
|
||||||
|
typeof error === 'object',
|
||||||
|
'error must be an object or null, but got %',
|
||||||
|
error
|
||||||
|
);
|
||||||
|
checkPoly(poly);
|
||||||
|
return {
|
||||||
|
...poly,
|
||||||
|
error
|
||||||
};
|
};
|
||||||
|
|
||||||
isEmpty() {
|
|
||||||
return !this._contents;
|
|
||||||
}
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return Object
|
|
||||||
.keys(this)
|
|
||||||
.reduce((obj, key) => (obj[key] = this[key], obj), {});
|
|
||||||
}
|
|
||||||
|
|
||||||
get contents() {
|
|
||||||
return this._contents;
|
|
||||||
}
|
|
||||||
|
|
||||||
set contents(val) {
|
|
||||||
if (typeof val !== 'string') {
|
|
||||||
throw new TypeError('File.contents can only a String');
|
|
||||||
}
|
|
||||||
this._contents = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
get basename() {
|
|
||||||
if (!this.path) {
|
|
||||||
throw new Error('No path specified! Can not get basename.');
|
|
||||||
}
|
|
||||||
return path.basename(this.path);
|
|
||||||
}
|
|
||||||
|
|
||||||
set basename(basename) {
|
|
||||||
if (!this.path) {
|
|
||||||
throw new Error('No path specified! Can not set basename.');
|
|
||||||
}
|
|
||||||
this.path = path.join(path.dirname(this.path), basename);
|
|
||||||
}
|
|
||||||
|
|
||||||
get extname() {
|
|
||||||
if (!this.path) {
|
|
||||||
throw new Error('No path specified! Can not get extname.');
|
|
||||||
}
|
|
||||||
return path.extname(this.path);
|
|
||||||
}
|
|
||||||
|
|
||||||
set extname(extname) {
|
|
||||||
if (!this.path) {
|
|
||||||
throw new Error('No path specified! Can not set extname.');
|
|
||||||
}
|
|
||||||
this.path = replaceExt(this.path, extname);
|
|
||||||
}
|
|
||||||
|
|
||||||
get path() {
|
|
||||||
return this.history[this.history.length - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
set path(path) {
|
|
||||||
if (typeof path !== 'string') {
|
|
||||||
throw new TypeError('path should be string');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Record history only when path changed
|
|
||||||
if (path && path !== this.path) {
|
|
||||||
this.history.push(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get error() {
|
|
||||||
return this._error;
|
|
||||||
}
|
|
||||||
|
|
||||||
set error(err) {
|
|
||||||
if (typeof err !== 'object') {
|
|
||||||
throw new TypeError('error must be an object or null');
|
|
||||||
}
|
|
||||||
this.error = err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user