@tobiastengler/create-relay-app
Version:
Easy configuration of Relay for existing projects
38 lines (37 loc) • 944 B
JavaScript
import path from "path";
export class RelativePath {
constructor(root, rel) {
this.root = root;
if (rel) {
if (path.isAbsolute(rel)) {
this.abs = rel;
}
else {
this.abs = path.join(this.root, rel);
}
}
else {
this.abs = root;
}
this.rel = prettifyPath(path.relative(this.root, this.abs));
}
get parentDirectory() {
return path.dirname(this.abs);
}
get name() {
return path.basename(this.abs);
}
toString() {
return this.rel;
}
}
function prettifyPath(input) {
let normalizedPath = normalizePath(input);
if (!normalizedPath.startsWith("..") && !normalizedPath.startsWith("./")) {
normalizedPath = "./" + normalizedPath;
}
return normalizedPath;
}
function normalizePath(input) {
return input.split(path.sep).join("/");
}