Make PolyVinyl's functional

This commit is contained in:
Berkeley Martinez
2016-05-10 23:38:11 -07:00
parent 5d95a98b64
commit 6e30044ba7

View File

@@ -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
}; };
isEmpty() {
return !this._contents;
} }
toJSON() { // isPoly(poly: Any) => Boolean
return Object export function isPoly(poly) {
.keys(this) return poly &&
.reduce((obj, key) => (obj[key] = this[key], obj), {}); typeof poly.contents === 'string' &&
typeof poly.path === 'string' &&
Array.isArray(poly.history);
} }
get contents() { // checkPoly(poly: Any) => Void, throws
return this._contents; export function checkPoly(poly) {
invariant(
isPoly(poly),
'function should receive a PolyVinyl, but got %s',
poly
);
} }
set contents(val) { // isEmpty(poly: PolyVinyl) => Boolean, throws
if (typeof val !== 'string') { export function isEmpty(poly) {
throw new TypeError('File.contents can only a String'); checkPoly(poly);
} return !!poly.contents;
this._contents = val;
} }
get basename() { // updateContents(contents: String, poly: PolyVinyl) => PolyVinyl
if (!this.path) { export function updateContents(contents, poly) {
throw new Error('No path specified! Can not get basename.'); checkPoly(poly);
} return {
return path.basename(this.path); ...poly,
contents
};
} }
set basename(basename) { export function getExt(poly) {
if (!this.path) { checkPoly(poly);
throw new Error('No path specified! Can not set basename.'); invariant(
} !!poly.path,
this.path = path.join(path.dirname(this.path), basename); 'No path specified! Can not get extname'
);
return path.extname(poly.path);
} }
get extname() { export function setExt(extname, poly) {
if (!this.path) { invariant(
throw new Error('No path specified! Can not get extname.'); poly.path,
} 'No path specified! Can not set extname',
return path.extname(this.path); );
const newPoly = {
...poly,
path: replaceExt(this.path, extname)
};
newPoly.history = [ ...poly.history, newPoly.path ];
return newPoly;
} }
set extname(extname) { export function setError(error, poly) {
if (!this.path) { invariant(
throw new Error('No path specified! Can not set extname.'); typeof error === 'object',
} 'error must be an object or null, but got %',
this.path = replaceExt(this.path, extname); error
} );
checkPoly(poly);
get path() { return {
return this.history[this.history.length - 1]; ...poly,
} error
};
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;
}
} }