2018-12-03 21:31:43 +00:00
|
|
|
/**
|
|
|
|
* Patch process to add process.cwd(), always giving the root dir.
|
|
|
|
* NOTE: this line needs to happen *before* we require in `path`.
|
|
|
|
*/
|
|
|
|
process.cwd = () => '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* https://github.com/browserify/path-browserify via Parcel.
|
|
|
|
* We use is as a base for our own Filer.Path, and patch/add
|
|
|
|
* a few things we need for the browser environment.
|
|
|
|
*/
|
|
|
|
const nodePath = require('path');
|
2021-04-18 14:40:34 +00:00
|
|
|
const filerPath = Object.assign({}, nodePath);
|
2018-12-03 21:31:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Patch path.basename() to return / vs. ''
|
|
|
|
*/
|
|
|
|
filerPath.basename = (path, ext) => {
|
|
|
|
const basename = nodePath.basename(path, ext);
|
|
|
|
return basename === '' ? '/' : basename;
|
2014-05-23 18:14:06 +00:00
|
|
|
};
|
|
|
|
|
2018-12-03 21:31:43 +00:00
|
|
|
/**
|
|
|
|
* Patch path.normalize() to not add a trailing /
|
|
|
|
*/
|
|
|
|
filerPath.normalize = (path) => {
|
|
|
|
path = nodePath.normalize(path);
|
|
|
|
return path === '/' ? path : filerPath.removeTrailing(path);
|
|
|
|
};
|
2014-02-21 17:39:33 +00:00
|
|
|
|
2018-12-03 21:31:43 +00:00
|
|
|
/**
|
|
|
|
* Add new utility method isNull() to path: check for null paths.
|
|
|
|
*/
|
|
|
|
filerPath.isNull = path => ('' + path).indexOf('\u0000') !== -1;
|
2014-05-23 18:14:06 +00:00
|
|
|
|
2018-12-03 21:31:43 +00:00
|
|
|
/**
|
|
|
|
* Add new utility method addTrailing() to add trailing / without doubling to //.
|
|
|
|
*/
|
|
|
|
filerPath.addTrailing = path => path.replace(/\/*$/, '/');
|
2014-11-28 17:48:10 +00:00
|
|
|
|
2018-12-03 21:31:43 +00:00
|
|
|
/**
|
|
|
|
* Add new utility method removeTrailing() to remove trailing /, dealing with multiple
|
|
|
|
*/
|
|
|
|
filerPath.removeTrailing = path => {
|
2014-11-28 17:48:10 +00:00
|
|
|
path = path.replace(/\/*$/, '');
|
|
|
|
return path === '' ? '/' : path;
|
2014-05-23 18:14:06 +00:00
|
|
|
};
|
2018-12-03 21:31:43 +00:00
|
|
|
|
|
|
|
module.exports = filerPath;
|