unbag
Version:
一个专门用来开发npm工具的包
135 lines (134 loc) • 3.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.usePath = exports.default = exports.createPathUtils = exports.RelativePath = exports.BasePath = exports.AbsolutePath = void 0;
var nodePath = _interopRequireWildcard(require("node:path"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const createPathUtils = path => {
const {
extname,
resolve
} = path;
const trimExtname = (path2, extnames) => {
let willTrim = true;
const _extname = extname(path2);
if (extnames) {
willTrim = extnames.includes(_extname);
}
if (willTrim && _extname) {
return path2.slice(0, path2.length - _extname.length);
} else {
return path2;
}
};
const replaceExtname = (path2, extname2) => {
let newPath = trimExtname(path2);
const _extname = extname2.startsWith(".") ? extname2 : `.${extname2}`;
return `${newPath}${_extname}`;
};
const rootName = () => {
return resolve();
};
return {
trimExtname,
replaceExtname,
rootName,
...path
};
};
exports.createPathUtils = createPathUtils;
const pathUtils = createPathUtils(nodePath);
const usePath = () => {
const pathUtils2 = createPathUtils(nodePath);
return pathUtils2;
};
exports.usePath = usePath;
var stdin_default = exports.default = pathUtils;
class BasePath {
#content;
constructor(params) {
const {
content
} = params;
this.#content = content;
}
get content() {
return this.#content;
}
get extname() {
return nodePath.extname(this.content);
}
replaceExtname(extname) {
const path = usePath();
const content = path.replaceExtname(this.content, extname);
return new BasePath({
content
});
}
}
exports.BasePath = BasePath;
class RelativePath extends BasePath {
constructor(params) {
const {
content
} = params;
if (nodePath.isAbsolute(content)) {
throw new Error("RelativePath content is absolute path");
}
super(params);
}
toAbsolutePath(params) {
const {
rel
} = params;
const absolutePath = nodePath.resolve(rel.content, this.content);
return new AbsolutePath({
content: absolutePath
});
}
replaceExtname(extname) {
const basePath = super.replaceExtname(extname);
return new RelativePath({
content: basePath.content
});
}
}
exports.RelativePath = RelativePath;
class AbsolutePath extends BasePath {
constructor(params) {
const {
content
} = params;
if (!nodePath.isAbsolute(content)) {
throw new Error("AbsolutePath content must is absolute path");
}
super(params);
}
toRelativePath(params) {
const {
rel
} = params;
const content = nodePath.relative(rel.content, this.content);
return new RelativePath({
content
});
}
replaceExtname(extname) {
const basePath = super.replaceExtname(extname);
return new AbsolutePath({
content: basePath.content
});
}
resolve(params) {
const {
next
} = params;
const content = nodePath.resolve(this.content, next);
return new AbsolutePath({
content
});
}
}
exports.AbsolutePath = AbsolutePath;