unbag
Version:
一个专门用来开发npm工具的包
123 lines • 2.82 kB
JavaScript
import * as nodePath from "node:path";
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
};
};
const pathUtils = createPathUtils(nodePath);
const usePath = () => {
const pathUtils2 = createPathUtils(nodePath);
return pathUtils2;
};
var stdin_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
});
}
}
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
});
}
}
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
});
}
}
export { AbsolutePath, BasePath, RelativePath, createPathUtils, stdin_default as default, usePath };