char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somgenerate path typedefs from typescript impl83ff6f3

main
2.8 KiB80 linesraw
1import { op_path_platform } from "ext:core/ops";
2import { posix as posixNamespace, win32 as win32Namespace } from "./api.js";
3
4export interface ParsedPath {
5  root: string;
6  dir: string;
7  base: string;
8  name: string;
9  ext: string;
10}
11
12export interface PathObject {
13  dir?: string;
14  root?: string;
15  base?: string;
16  name?: string;
17  ext?: string;
18}
19
20/** Lexical path operations with one fixed separator flavor, plus file URL conversion for that flavor. */
21export interface Path {
22  join(...paths: string[]): string;
23  resolve(...paths: string[]): string;
24  normalize(path: string): string;
25  relative(from: string, to: string): string;
26  dirname(path: string): string;
27  basename(path: string, suffix?: string): string;
28  extname(path: string): string;
29  isAbsolute(path: string): boolean;
30  parse(path: string): ParsedPath;
31  format(pathObject: PathObject): string;
32  toNamespacedPath(path: string): string;
33  readonly sep: string;
34  readonly delimiter: string;
35  fromFileUrl(url: URL | string): string;
36  toFileUrl(path: string): URL;
37}
38
39/** Explicit POSIX semantics on any host. */
40export const posix: Path = posixNamespace;
41
42/** Explicit Windows semantics on any host. */
43export const win32: Path = win32Namespace;
44
45const native = op_path_platform() === "win32" ? win32 : posix;
46
47/** Joins segments with the native separator, then normalizes; `""` segments are skipped. */
48export const join: (...paths: string[]) => string = native.join;
49
50/** Resolves segments right to left, consulting the working directory only for relative, root-relative, or drive-relative input; fully qualified inputs need no read grant. */
51export const resolve: (...paths: string[]) => string = native.resolve;
52
53export const normalize: (path: string) => string = native.normalize;
54
55/** Lexical path from `from` to `to`; fully qualified pairs need no read grant. */
56export const relative: (from: string, to: string) => string = native.relative;
57
58export const dirname: (path: string) => string = native.dirname;
59
60export const basename: (path: string, suffix?: string) => string = native.basename;
61
62export const extname: (path: string) => string = native.extname;
63
64export const isAbsolute: (path: string) => boolean = native.isAbsolute;
65
66export const parse: (path: string) => ParsedPath = native.parse;
67
68export const format: (pathObject: PathObject) => string = native.format;
69
70export const toNamespacedPath: (path: string) => string = native.toNamespacedPath;
71
72export const sep: string = native.sep;
73
74export const delimiter: string = native.delimiter;
75
76/** Converts an absolute `file:` URL to a path; rejects queries, fragments, and encoded separators. Needs no permissions. */
77export const fromFileUrl: (url: URL | string) => string = native.fromFileUrl;
78
79/** Converts an absolute path to a `file:` URL; never consults the working directory. Needs no permissions. */
80export const toFileUrl: (path: string) => URL = native.toFileUrl;