fix(tools): windows path

This commit is contained in:
forehalo
2025-01-13 22:53:34 +08:00
parent 80f8944188
commit 4ec02dbb39

View File

@@ -1,10 +1,8 @@
import { existsSync, statSync } from 'node:fs';
import { join, relative } from 'node:path/posix';
import { join, relative, sep } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
export class Path {
private readonly path: string;
static dir(url: string) {
return new Path(fileURLToPath(url)).join('..');
}
@@ -17,9 +15,7 @@ export class Path {
return './' + this.path.slice(ProjectRoot.path.length).replace(/\\/g, '/');
}
constructor(path: string) {
this.path = path.replaceAll('\\', '/');
}
constructor(private readonly path: string) {}
join(...paths: string[]) {
return new Path(join(this.path, ...paths));
@@ -29,6 +25,14 @@ export class Path {
return this.join('..');
}
toPosixString() {
if (sep === '\\') {
return this.path.replaceAll('\\', '/');
}
return this.path;
}
toString() {
return this.path;
}
@@ -54,7 +58,12 @@ export class Path {
}
relative(to: string) {
return relative(this.value, to);
const re = relative(this.value, to);
if (sep === '\\') {
return re.replaceAll('\\', '/');
}
return re;
}
}