|
| 1 | +import pathe from 'pathe' |
| 2 | +import * as win32 from '@jsr/std__path/windows' |
| 3 | + |
| 4 | +// Node-`path`-shaped module. The sandboxed renderer and its preload cannot load |
| 5 | +// node's `path`, and a POSIX-only shim (`pathe`) makes the renderer build `/` |
| 6 | +// paths while the watcher, the main process and the OS shell use `\` on |
| 7 | +// Windows. Picking the real win32 implementation keeps a single path |
| 8 | +// representation (#5683). |
| 9 | +export interface PlatformPath { |
| 10 | + basename(path: string, ext?: string): string |
| 11 | + dirname(path: string): string |
| 12 | + extname(path: string): string |
| 13 | + join(...paths: string[]): string |
| 14 | + resolve(...paths: string[]): string |
| 15 | + relative(from: string, to: string): string |
| 16 | + isAbsolute(path: string): boolean |
| 17 | + normalize(path: string): string |
| 18 | + parse(path: string): { root: string, dir: string, base: string, ext: string, name: string } |
| 19 | + format(pathObject: { |
| 20 | + root?: string |
| 21 | + dir?: string |
| 22 | + base?: string |
| 23 | + ext?: string |
| 24 | + name?: string |
| 25 | + }): string |
| 26 | + sep: string |
| 27 | + delimiter: string |
| 28 | +} |
| 29 | + |
| 30 | +const posixPath: PlatformPath = { |
| 31 | + basename: (path, ext) => pathe.basename(path, ext), |
| 32 | + dirname: (path) => pathe.dirname(path), |
| 33 | + extname: (path) => pathe.extname(path), |
| 34 | + join: (...paths) => pathe.join(...paths), |
| 35 | + resolve: (...paths) => pathe.resolve(...paths), |
| 36 | + relative: (from, to) => pathe.relative(from, to), |
| 37 | + isAbsolute: (path) => pathe.isAbsolute(path), |
| 38 | + normalize: (path) => pathe.normalize(path), |
| 39 | + parse: (path) => pathe.parse(path), |
| 40 | + format: (pathObject) => pathe.format(pathObject), |
| 41 | + sep: pathe.sep, |
| 42 | + delimiter: pathe.delimiter |
| 43 | +} |
| 44 | + |
| 45 | +const win32Path: PlatformPath = { |
| 46 | + basename: (path, ext) => win32.basename(path, ext), |
| 47 | + dirname: (path) => win32.dirname(path), |
| 48 | + extname: (path) => win32.extname(path), |
| 49 | + join: (...paths) => win32.join(...paths), |
| 50 | + resolve: (...paths) => win32.resolve(...paths), |
| 51 | + relative: (from, to) => win32.relative(from, to), |
| 52 | + isAbsolute: (path) => win32.isAbsolute(path), |
| 53 | + normalize: (path) => win32.normalize(path), |
| 54 | + parse: (path) => win32.parse(path), |
| 55 | + format: (pathObject) => win32.format(pathObject), |
| 56 | + sep: win32.SEPARATOR, |
| 57 | + delimiter: win32.DELIMITER |
| 58 | +} |
| 59 | + |
| 60 | +export const createPlatformPath = (isWindows: boolean): PlatformPath => |
| 61 | + isWindows ? win32Path : posixPath |
0 commit comments