path-class 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/path-class/Path.d.ts +155 -0
- package/dist/lib/path-class/chunks/chunk-KSCIGSNU.js +317 -0
- package/dist/lib/path-class/chunks/chunk-KSCIGSNU.js.map +7 -0
- package/dist/lib/path-class/index.d.ts +1 -148
- package/dist/lib/path-class/index.js +5 -296
- package/dist/lib/path-class/index.js.map +3 -3
- package/dist/lib/path-class/sync/index.d.ts +57 -0
- package/dist/lib/path-class/sync/index.js +109 -0
- package/dist/lib/path-class/sync/index.js.map +7 -0
- package/dist/lib/path-class/sync/static.d.ts +6 -0
- package/package.json +5 -1
- package/src/{index.test.ts → Path.test.ts} +27 -5
- package/src/Path.ts +446 -0
- package/src/index.ts +1 -431
- package/src/sync/index.ts +235 -0
- package/src/sync/static.ts +14 -0
- package/src/sync/sync.test.ts +191 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Abortable } from "node:events";
|
|
2
|
+
import type { Dirent, ObjectEncodingOptions, OpenMode } from "node:fs";
|
|
3
|
+
import { cp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
declare function readDirType(options?: (ObjectEncodingOptions & {
|
|
5
|
+
withFileTypes?: false | undefined;
|
|
6
|
+
recursive?: boolean | undefined;
|
|
7
|
+
}) | BufferEncoding | null): Promise<string[]>;
|
|
8
|
+
declare function readDirType(options: {
|
|
9
|
+
encoding: "buffer";
|
|
10
|
+
withFileTypes?: false | undefined;
|
|
11
|
+
recursive?: boolean | undefined;
|
|
12
|
+
} | "buffer"): Promise<Buffer[]>;
|
|
13
|
+
declare function readDirType(options?: (ObjectEncodingOptions & {
|
|
14
|
+
withFileTypes?: false | undefined;
|
|
15
|
+
recursive?: boolean | undefined;
|
|
16
|
+
}) | BufferEncoding | null): Promise<string[] | Buffer[]>;
|
|
17
|
+
declare function readDirType(options: ObjectEncodingOptions & {
|
|
18
|
+
withFileTypes: true;
|
|
19
|
+
recursive?: boolean | undefined;
|
|
20
|
+
}): Promise<Dirent[]>;
|
|
21
|
+
declare function readDirType(options: {
|
|
22
|
+
encoding: "buffer";
|
|
23
|
+
withFileTypes: true;
|
|
24
|
+
recursive?: boolean | undefined;
|
|
25
|
+
}): Promise<Dirent<Buffer>[]>;
|
|
26
|
+
declare function readFileType(options?: ({
|
|
27
|
+
encoding?: null | undefined;
|
|
28
|
+
flag?: OpenMode | undefined;
|
|
29
|
+
} & Abortable) | null): Promise<Buffer>;
|
|
30
|
+
declare function readFileType(options: ({
|
|
31
|
+
encoding: BufferEncoding;
|
|
32
|
+
flag?: OpenMode | undefined;
|
|
33
|
+
} & Abortable) | BufferEncoding): Promise<string>;
|
|
34
|
+
declare function readFileType(options?: (ObjectEncodingOptions & Abortable & {
|
|
35
|
+
flag?: OpenMode | undefined;
|
|
36
|
+
}) | BufferEncoding | null): Promise<string | Buffer>;
|
|
37
|
+
export declare class Path {
|
|
38
|
+
#private;
|
|
39
|
+
/**
|
|
40
|
+
* If `path` is a string starting with `file:///`, it will be parsed as a file URL.
|
|
41
|
+
*/
|
|
42
|
+
constructor(path: string | URL | Path);
|
|
43
|
+
/**
|
|
44
|
+
* Similar to `new URL(path, base)`, but accepting and returning `Path` objects.
|
|
45
|
+
* Note that `base` must be one of:
|
|
46
|
+
*
|
|
47
|
+
* - a valid second argument to `new URL(…)`.
|
|
48
|
+
* - a `Path` representing an absolute path.
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
static resolve(path: string | URL | Path, base: string | URL | Path): Path;
|
|
52
|
+
isAbsolutePath(): boolean;
|
|
53
|
+
toFileURL(): URL;
|
|
54
|
+
/**
|
|
55
|
+
* The `Path` can have a trailing slash, indicating that it represents a
|
|
56
|
+
* directory. (If there is no trailing slash, it can represent either a file
|
|
57
|
+
* or a directory.)
|
|
58
|
+
*
|
|
59
|
+
* Some operations will refuse to treat a directory path as a file path. This
|
|
60
|
+
* function identifies such paths.
|
|
61
|
+
*/
|
|
62
|
+
hasTrailingSlash(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Same as `.toString()`, but more concise.
|
|
65
|
+
*/
|
|
66
|
+
get path(): string;
|
|
67
|
+
toString(): string;
|
|
68
|
+
/** Constructs a new path by appending the given path segments.
|
|
69
|
+
* This follows `node` semantics for absolute paths: leading slashes in the given descendant segments are ignored.
|
|
70
|
+
*/
|
|
71
|
+
join(...segments: (string | Path)[]): Path;
|
|
72
|
+
extendBasename(suffix: string): Path;
|
|
73
|
+
get parent(): Path;
|
|
74
|
+
/** @deprecated Alias for `.parent`. */
|
|
75
|
+
get dirname(): Path;
|
|
76
|
+
get basename(): Path;
|
|
77
|
+
get extension(): string;
|
|
78
|
+
/** @deprecated Alias for `.extension`. */
|
|
79
|
+
get extname(): string;
|
|
80
|
+
exists(constraints?: {
|
|
81
|
+
mustBe: "file" | "directory";
|
|
82
|
+
}): Promise<boolean>;
|
|
83
|
+
existsAsFile(): Promise<boolean>;
|
|
84
|
+
existsAsDir(): Promise<boolean>;
|
|
85
|
+
/** Defaults to `recursive: true`. */
|
|
86
|
+
mkdir(options?: Parameters<typeof mkdir>[1]): Promise<Path>;
|
|
87
|
+
/** Returns the destination path. */
|
|
88
|
+
cp(destination: string | URL | Path, options?: Parameters<typeof cp>[2]): Promise<Path>;
|
|
89
|
+
rename(destination: string | URL | Path): Promise<void>;
|
|
90
|
+
/** Create a temporary dir inside the global temp dir for the current user. */
|
|
91
|
+
static makeTempDir(prefix?: string): Promise<Path>;
|
|
92
|
+
rm(options?: Parameters<typeof rm>[1]): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Equivalent to:
|
|
95
|
+
*
|
|
96
|
+
* .rm({ recursive: true, force: true, ...(options ?? {}) })
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
rm_rf(options?: Parameters<typeof rm>[1]): Promise<void>;
|
|
100
|
+
read: typeof readFileType;
|
|
101
|
+
readText(): Promise<string>;
|
|
102
|
+
readJSON<T>(): Promise<T>;
|
|
103
|
+
/** Creates intermediate directories if they do not exist.
|
|
104
|
+
*
|
|
105
|
+
* Returns the original `Path` (for chaining).
|
|
106
|
+
*/
|
|
107
|
+
write(data: Parameters<typeof writeFile>[1], options?: Parameters<typeof writeFile>[2]): Promise<Path>;
|
|
108
|
+
/**
|
|
109
|
+
* If only `data` is provided, this is equivalent to:
|
|
110
|
+
*
|
|
111
|
+
* .write(JSON.stringify(data, null, " "));
|
|
112
|
+
*
|
|
113
|
+
* `replacer` and `space` can also be specified, making this equivalent to:
|
|
114
|
+
*
|
|
115
|
+
* .write(JSON.stringify(data, replacer, space));
|
|
116
|
+
*
|
|
117
|
+
* Returns the original `Path` (for chaining).
|
|
118
|
+
*/
|
|
119
|
+
writeJSON<T>(data: T, replacer?: Parameters<typeof JSON.stringify>[1], space?: Parameters<typeof JSON.stringify>[2]): Promise<Path>;
|
|
120
|
+
readDir: typeof readDirType;
|
|
121
|
+
static get homedir(): Path;
|
|
122
|
+
static xdg: {
|
|
123
|
+
cache: Path;
|
|
124
|
+
config: Path;
|
|
125
|
+
data: Path;
|
|
126
|
+
state: Path;
|
|
127
|
+
/**
|
|
128
|
+
* {@link Path.xdg.runtime} does not have a default value. Consider
|
|
129
|
+
* {@link Path.xdg.runtimeWithStateFallback} if you need a fallback but do not have a particular fallback in mind.
|
|
130
|
+
*/
|
|
131
|
+
runtime: Path | undefined;
|
|
132
|
+
runtimeWithStateFallback: Path;
|
|
133
|
+
};
|
|
134
|
+
/** Chainable function to print the path. Prints the same as:
|
|
135
|
+
*
|
|
136
|
+
* if (args.length > 0) {
|
|
137
|
+
* console.log(...args);
|
|
138
|
+
* }
|
|
139
|
+
* console.log(this.path);
|
|
140
|
+
*
|
|
141
|
+
*/
|
|
142
|
+
debugPrint(...args: any[]): Path;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* This function is useful to serialize any `Path`s in a structure to pass on to
|
|
146
|
+
* functions that do not know about the `Path` class, e.g.
|
|
147
|
+
*
|
|
148
|
+
* function process(args: (string | Path)[]) {
|
|
149
|
+
* const argsAsStrings = args.map(stringifyIfPath);
|
|
150
|
+
* }
|
|
151
|
+
*
|
|
152
|
+
*/
|
|
153
|
+
export declare function stringifyIfPath<T>(value: T | Path): T | string;
|
|
154
|
+
export declare function mustNotHaveTrailingSlash(path: Path): void;
|
|
155
|
+
export {};
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// src/Path.ts
|
|
2
|
+
import {
|
|
3
|
+
cp,
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readdir,
|
|
7
|
+
readFile,
|
|
8
|
+
rename,
|
|
9
|
+
rm,
|
|
10
|
+
stat,
|
|
11
|
+
writeFile
|
|
12
|
+
} from "node:fs/promises";
|
|
13
|
+
import { homedir, tmpdir } from "node:os";
|
|
14
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
15
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
16
|
+
import {
|
|
17
|
+
xdgCache,
|
|
18
|
+
xdgConfig,
|
|
19
|
+
xdgData,
|
|
20
|
+
xdgRuntime,
|
|
21
|
+
xdgState
|
|
22
|
+
} from "xdg-basedir";
|
|
23
|
+
var Path = class _Path {
|
|
24
|
+
// @ts-expect-error ts(2564): False positive. https://github.com/microsoft/TypeScript/issues/32194
|
|
25
|
+
#path;
|
|
26
|
+
/**
|
|
27
|
+
* If `path` is a string starting with `file:///`, it will be parsed as a file URL.
|
|
28
|
+
*/
|
|
29
|
+
constructor(path) {
|
|
30
|
+
this.#setNormalizedPath(_Path.#pathlikeToString(path));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Similar to `new URL(path, base)`, but accepting and returning `Path` objects.
|
|
34
|
+
* Note that `base` must be one of:
|
|
35
|
+
*
|
|
36
|
+
* - a valid second argument to `new URL(…)`.
|
|
37
|
+
* - a `Path` representing an absolute path.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
static resolve(path, base) {
|
|
41
|
+
const baseURL = (() => {
|
|
42
|
+
if (!(base instanceof _Path)) {
|
|
43
|
+
return base;
|
|
44
|
+
}
|
|
45
|
+
if (!base.isAbsolutePath()) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"The `base` arg to `Path.resolve(\u2026)` must be an absolute path."
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return pathToFileURL(base.#path);
|
|
51
|
+
})();
|
|
52
|
+
return new _Path(new URL(_Path.#pathlikeToString(path), baseURL));
|
|
53
|
+
}
|
|
54
|
+
static #pathlikeToString(path) {
|
|
55
|
+
if (path instanceof _Path) {
|
|
56
|
+
return path.#path;
|
|
57
|
+
}
|
|
58
|
+
if (path instanceof URL) {
|
|
59
|
+
return fileURLToPath(path);
|
|
60
|
+
}
|
|
61
|
+
if (typeof path === "string") {
|
|
62
|
+
if (path.startsWith("file:///")) {
|
|
63
|
+
return fileURLToPath(path);
|
|
64
|
+
}
|
|
65
|
+
return path;
|
|
66
|
+
}
|
|
67
|
+
throw new Error("Invalid path");
|
|
68
|
+
}
|
|
69
|
+
#setNormalizedPath(path) {
|
|
70
|
+
this.#path = join(path);
|
|
71
|
+
}
|
|
72
|
+
isAbsolutePath() {
|
|
73
|
+
return this.#path.startsWith("/");
|
|
74
|
+
}
|
|
75
|
+
toFileURL() {
|
|
76
|
+
if (!this.isAbsolutePath()) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
"Tried to convert to file URL when the path is not absolute."
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return pathToFileURL(this.#path);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The `Path` can have a trailing slash, indicating that it represents a
|
|
85
|
+
* directory. (If there is no trailing slash, it can represent either a file
|
|
86
|
+
* or a directory.)
|
|
87
|
+
*
|
|
88
|
+
* Some operations will refuse to treat a directory path as a file path. This
|
|
89
|
+
* function identifies such paths.
|
|
90
|
+
*/
|
|
91
|
+
hasTrailingSlash() {
|
|
92
|
+
return this.#path.endsWith("/");
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Same as `.toString()`, but more concise.
|
|
96
|
+
*/
|
|
97
|
+
get path() {
|
|
98
|
+
return this.#path;
|
|
99
|
+
}
|
|
100
|
+
toString() {
|
|
101
|
+
return this.#path;
|
|
102
|
+
}
|
|
103
|
+
/** Constructs a new path by appending the given path segments.
|
|
104
|
+
* This follows `node` semantics for absolute paths: leading slashes in the given descendant segments are ignored.
|
|
105
|
+
*/
|
|
106
|
+
join(...segments) {
|
|
107
|
+
const segmentStrings = segments.map(
|
|
108
|
+
(segment) => segment instanceof _Path ? segment.path : segment
|
|
109
|
+
);
|
|
110
|
+
return new _Path(join(this.#path, ...segmentStrings));
|
|
111
|
+
}
|
|
112
|
+
extendBasename(suffix) {
|
|
113
|
+
const joinedSuffix = join(suffix);
|
|
114
|
+
if (joinedSuffix !== basename(joinedSuffix)) {
|
|
115
|
+
throw new Error("Invalid suffix to extend file name.");
|
|
116
|
+
}
|
|
117
|
+
return new _Path(this.#path + joinedSuffix);
|
|
118
|
+
}
|
|
119
|
+
get parent() {
|
|
120
|
+
return new _Path(dirname(this.#path));
|
|
121
|
+
}
|
|
122
|
+
// Normally I'd stick with `node`'s name, but I think `.dirname` is a
|
|
123
|
+
// particularly poor name. So we support `.dirname` for discovery but mark it
|
|
124
|
+
// as deprecated, even if it will never be removed.
|
|
125
|
+
/** @deprecated Alias for `.parent`. */
|
|
126
|
+
get dirname() {
|
|
127
|
+
return this.parent;
|
|
128
|
+
}
|
|
129
|
+
get basename() {
|
|
130
|
+
return new _Path(basename(this.#path));
|
|
131
|
+
}
|
|
132
|
+
get extension() {
|
|
133
|
+
mustNotHaveTrailingSlash(this);
|
|
134
|
+
return extname(this.#path);
|
|
135
|
+
}
|
|
136
|
+
// Normally I'd stick with `node`'s name, but I think `.extname` is a
|
|
137
|
+
// particularly poor name. So we support `.extname` for discovery but mark it
|
|
138
|
+
// as deprecated, even if it will never be removed.
|
|
139
|
+
/** @deprecated Alias for `.extension`. */
|
|
140
|
+
get extname() {
|
|
141
|
+
return this.extension;
|
|
142
|
+
}
|
|
143
|
+
// TODO: find a neat way to dedup with the sync version?
|
|
144
|
+
async exists(constraints) {
|
|
145
|
+
let stats;
|
|
146
|
+
try {
|
|
147
|
+
stats = await stat(this.#path);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (e.code === "ENOENT") {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
if (!constraints?.mustBe) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
switch (constraints?.mustBe) {
|
|
158
|
+
case "file": {
|
|
159
|
+
mustNotHaveTrailingSlash(this);
|
|
160
|
+
if (stats.isFile()) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`Path exists but is not a file: ${this.#path}`);
|
|
164
|
+
}
|
|
165
|
+
case "directory": {
|
|
166
|
+
if (stats.isDirectory()) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
throw new Error(`Path exists but is not a directory: ${this.#path}`);
|
|
170
|
+
}
|
|
171
|
+
default: {
|
|
172
|
+
throw new Error("Invalid path type constraint");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async existsAsFile() {
|
|
177
|
+
return this.exists({ mustBe: "file" });
|
|
178
|
+
}
|
|
179
|
+
async existsAsDir() {
|
|
180
|
+
return this.exists({ mustBe: "directory" });
|
|
181
|
+
}
|
|
182
|
+
// I don't think `mkdir` is a great name, but it does match the
|
|
183
|
+
// well-established canonical commandline name. So in this case we keep the
|
|
184
|
+
// awkward abbreviation.
|
|
185
|
+
/** Defaults to `recursive: true`. */
|
|
186
|
+
async mkdir(options) {
|
|
187
|
+
const optionsObject = (() => {
|
|
188
|
+
if (typeof options === "string" || typeof options === "number") {
|
|
189
|
+
return { mode: options };
|
|
190
|
+
}
|
|
191
|
+
return options ?? {};
|
|
192
|
+
})();
|
|
193
|
+
await mkdir(this.#path, { recursive: true, ...optionsObject });
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
196
|
+
// TODO: check idempotency semantics when the destination exists and is a folder.
|
|
197
|
+
/** Returns the destination path. */
|
|
198
|
+
async cp(destination, options) {
|
|
199
|
+
await cp(this.#path, new _Path(destination).#path, options);
|
|
200
|
+
return new _Path(destination);
|
|
201
|
+
}
|
|
202
|
+
// TODO: check idempotency semantics when the destination exists and is a folder.
|
|
203
|
+
async rename(destination) {
|
|
204
|
+
await rename(this.#path, new _Path(destination).#path);
|
|
205
|
+
}
|
|
206
|
+
/** Create a temporary dir inside the global temp dir for the current user. */
|
|
207
|
+
static async makeTempDir(prefix) {
|
|
208
|
+
return new _Path(
|
|
209
|
+
await mkdtemp(new _Path(tmpdir()).join(prefix ?? "js-temp-").toString())
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
async rm(options) {
|
|
213
|
+
await rm(this.#path, options);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Equivalent to:
|
|
217
|
+
*
|
|
218
|
+
* .rm({ recursive: true, force: true, ...(options ?? {}) })
|
|
219
|
+
*
|
|
220
|
+
*/
|
|
221
|
+
async rm_rf(options) {
|
|
222
|
+
await this.rm({ recursive: true, force: true, ...options ?? {} });
|
|
223
|
+
}
|
|
224
|
+
read = (options) => (
|
|
225
|
+
// biome-ignore lint/suspicious/noExplicitAny: Needed to wrangle the types.
|
|
226
|
+
readFile(this.#path, options)
|
|
227
|
+
);
|
|
228
|
+
async readText() {
|
|
229
|
+
return readFile(this.#path, "utf-8");
|
|
230
|
+
}
|
|
231
|
+
async readJSON() {
|
|
232
|
+
return JSON.parse(await this.readText());
|
|
233
|
+
}
|
|
234
|
+
/** Creates intermediate directories if they do not exist.
|
|
235
|
+
*
|
|
236
|
+
* Returns the original `Path` (for chaining).
|
|
237
|
+
*/
|
|
238
|
+
async write(data, options) {
|
|
239
|
+
await this.parent.mkdir();
|
|
240
|
+
await writeFile(this.#path, data, options);
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* If only `data` is provided, this is equivalent to:
|
|
245
|
+
*
|
|
246
|
+
* .write(JSON.stringify(data, null, " "));
|
|
247
|
+
*
|
|
248
|
+
* `replacer` and `space` can also be specified, making this equivalent to:
|
|
249
|
+
*
|
|
250
|
+
* .write(JSON.stringify(data, replacer, space));
|
|
251
|
+
*
|
|
252
|
+
* Returns the original `Path` (for chaining).
|
|
253
|
+
*/
|
|
254
|
+
async writeJSON(data, replacer = null, space = " ") {
|
|
255
|
+
await this.write(JSON.stringify(data, replacer, space));
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
// Normally we'd add a `@deprecated` alias named `.readdir`, but that would
|
|
259
|
+
// differ only by capitalization of a single non-leading character. This can
|
|
260
|
+
// be a bit confusing, especially when autocompleting. So for this function in
|
|
261
|
+
// particular we don't include an alias.
|
|
262
|
+
readDir = (options) => (
|
|
263
|
+
// biome-ignore lint/suspicious/noExplicitAny: Needed to wrangle the types.
|
|
264
|
+
readdir(this.#path, options)
|
|
265
|
+
);
|
|
266
|
+
static get homedir() {
|
|
267
|
+
return new _Path(homedir());
|
|
268
|
+
}
|
|
269
|
+
static xdg = {
|
|
270
|
+
cache: new _Path(xdgCache ?? _Path.homedir.join(".cache")),
|
|
271
|
+
config: new _Path(xdgConfig ?? _Path.homedir.join(".config")),
|
|
272
|
+
data: new _Path(xdgData ?? _Path.homedir.join(".local/share")),
|
|
273
|
+
state: new _Path(xdgState ?? _Path.homedir.join(".local/state")),
|
|
274
|
+
/**
|
|
275
|
+
* {@link Path.xdg.runtime} does not have a default value. Consider
|
|
276
|
+
* {@link Path.xdg.runtimeWithStateFallback} if you need a fallback but do not have a particular fallback in mind.
|
|
277
|
+
*/
|
|
278
|
+
runtime: xdgRuntime ? new _Path(xdgRuntime) : void 0,
|
|
279
|
+
runtimeWithStateFallback: xdgRuntime ? new _Path(xdgRuntime) : new _Path(xdgState ?? _Path.homedir.join(".local/state"))
|
|
280
|
+
};
|
|
281
|
+
/** Chainable function to print the path. Prints the same as:
|
|
282
|
+
*
|
|
283
|
+
* if (args.length > 0) {
|
|
284
|
+
* console.log(...args);
|
|
285
|
+
* }
|
|
286
|
+
* console.log(this.path);
|
|
287
|
+
*
|
|
288
|
+
*/
|
|
289
|
+
// biome-ignore lint/suspicious/noExplicitAny: This is the correct type, based on `console.log(…)`.
|
|
290
|
+
debugPrint(...args) {
|
|
291
|
+
if (args.length > 0) {
|
|
292
|
+
console.log(...args);
|
|
293
|
+
}
|
|
294
|
+
console.log(this.#path);
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
function stringifyIfPath(value) {
|
|
299
|
+
if (value instanceof Path) {
|
|
300
|
+
return value.toString();
|
|
301
|
+
}
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
function mustNotHaveTrailingSlash(path) {
|
|
305
|
+
if (path.hasTrailingSlash()) {
|
|
306
|
+
throw new Error(
|
|
307
|
+
"Path ends with a slash, which cannot be treated as a file."
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export {
|
|
313
|
+
Path,
|
|
314
|
+
stringifyIfPath,
|
|
315
|
+
mustNotHaveTrailingSlash
|
|
316
|
+
};
|
|
317
|
+
//# sourceMappingURL=chunk-KSCIGSNU.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/Path.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Abortable } from \"node:events\";\nimport type { Dirent, ObjectEncodingOptions, OpenMode } from \"node:fs\";\nimport {\n cp,\n mkdir,\n mkdtemp,\n readdir,\n readFile,\n rename,\n rm,\n stat,\n writeFile,\n} from \"node:fs/promises\";\nimport { homedir, tmpdir } from \"node:os\";\nimport { basename, dirname, extname, join } from \"node:path\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport {\n xdgCache,\n xdgConfig,\n xdgData,\n xdgRuntime,\n xdgState,\n} from \"xdg-basedir\";\n\n// Modifying the type of `readdir(\u2026)` from `node:fs/promises` to remove the\n// first parameter is difficult, if not impossible. So we give up and duplicate\n// the types manually. This ensures ergonomic types, such as an inferred return\n// type of `string[]` when `options` is not passed.\n\ndeclare function readDirType(\n options?:\n | (ObjectEncodingOptions & {\n withFileTypes?: false | undefined;\n recursive?: boolean | undefined;\n })\n | BufferEncoding\n | null,\n): Promise<string[]>;\n\ndeclare function readDirType(\n options:\n | {\n encoding: \"buffer\";\n withFileTypes?: false | undefined;\n recursive?: boolean | undefined;\n }\n | \"buffer\",\n): Promise<Buffer[]>;\n\ndeclare function readDirType(\n options?:\n | (ObjectEncodingOptions & {\n withFileTypes?: false | undefined;\n recursive?: boolean | undefined;\n })\n | BufferEncoding\n | null,\n): Promise<string[] | Buffer[]>;\n\ndeclare function readDirType(\n options: ObjectEncodingOptions & {\n withFileTypes: true;\n recursive?: boolean | undefined;\n },\n): Promise<Dirent[]>;\n\ndeclare function readDirType(options: {\n encoding: \"buffer\";\n withFileTypes: true;\n recursive?: boolean | undefined;\n}): Promise<Dirent<Buffer>[]>;\n\ndeclare function readFileType(\n options?:\n | ({\n encoding?: null | undefined;\n flag?: OpenMode | undefined;\n } & Abortable)\n | null,\n): Promise<Buffer>;\ndeclare function readFileType(\n options:\n | ({\n encoding: BufferEncoding;\n flag?: OpenMode | undefined;\n } & Abortable)\n | BufferEncoding,\n): Promise<string>;\ndeclare function readFileType(\n options?:\n | (ObjectEncodingOptions &\n Abortable & {\n flag?: OpenMode | undefined;\n })\n | BufferEncoding\n | null,\n): Promise<string | Buffer>;\n\nexport class Path {\n // @ts-expect-error ts(2564): False positive. https://github.com/microsoft/TypeScript/issues/32194\n #path: string;\n /**\n * If `path` is a string starting with `file:///`, it will be parsed as a file URL.\n */\n constructor(path: string | URL | Path) {\n this.#setNormalizedPath(Path.#pathlikeToString(path));\n }\n\n /**\n * Similar to `new URL(path, base)`, but accepting and returning `Path` objects.\n * Note that `base` must be one of:\n *\n * - a valid second argument to `new URL(\u2026)`.\n * - a `Path` representing an absolute path.\n *\n */\n static resolve(path: string | URL | Path, base: string | URL | Path): Path {\n const baseURL = (() => {\n if (!(base instanceof Path)) {\n return base;\n }\n if (!base.isAbsolutePath()) {\n throw new Error(\n \"The `base` arg to `Path.resolve(\u2026)` must be an absolute path.\",\n );\n }\n return pathToFileURL(base.#path);\n })();\n return new Path(new URL(Path.#pathlikeToString(path), baseURL));\n }\n\n static #pathlikeToString(path: string | URL | Path): string {\n if (path instanceof Path) {\n return path.#path;\n }\n if (path instanceof URL) {\n return fileURLToPath(path);\n }\n if (typeof path === \"string\") {\n // TODO: allow turning off this heuristic?\n if (path.startsWith(\"file:///\")) {\n return fileURLToPath(path);\n }\n return path;\n }\n throw new Error(\"Invalid path\");\n }\n\n #setNormalizedPath(path: string): void {\n this.#path = join(path);\n }\n\n isAbsolutePath(): boolean {\n return this.#path.startsWith(\"/\");\n }\n\n toFileURL(): URL {\n if (!this.isAbsolutePath()) {\n throw new Error(\n \"Tried to convert to file URL when the path is not absolute.\",\n );\n }\n return pathToFileURL(this.#path);\n }\n\n /**\n * The `Path` can have a trailing slash, indicating that it represents a\n * directory. (If there is no trailing slash, it can represent either a file\n * or a directory.)\n *\n * Some operations will refuse to treat a directory path as a file path. This\n * function identifies such paths.\n */\n hasTrailingSlash(): boolean {\n // TODO: handle Windows semantically\n return this.#path.endsWith(\"/\");\n }\n\n /**\n * Same as `.toString()`, but more concise.\n */\n get path() {\n return this.#path;\n }\n\n toString(): string {\n return this.#path;\n }\n\n /** Constructs a new path by appending the given path segments.\n * This follows `node` semantics for absolute paths: leading slashes in the given descendant segments are ignored.\n */\n join(...segments: (string | Path)[]): Path {\n const segmentStrings = segments.map((segment) =>\n segment instanceof Path ? segment.path : segment,\n );\n return new Path(join(this.#path, ...segmentStrings));\n }\n\n extendBasename(suffix: string): Path {\n const joinedSuffix = join(suffix);\n if (joinedSuffix !== basename(joinedSuffix)) {\n throw new Error(\"Invalid suffix to extend file name.\");\n }\n // TODO: join basename and dirname instead?\n return new Path(this.#path + joinedSuffix);\n }\n\n get parent(): Path {\n return new Path(dirname(this.#path));\n }\n\n // Normally I'd stick with `node`'s name, but I think `.dirname` is a\n // particularly poor name. So we support `.dirname` for discovery but mark it\n // as deprecated, even if it will never be removed.\n /** @deprecated Alias for `.parent`. */\n get dirname(): Path {\n return this.parent;\n }\n\n get basename(): Path {\n return new Path(basename(this.#path));\n }\n\n get extension(): string {\n mustNotHaveTrailingSlash(this);\n return extname(this.#path);\n }\n\n // Normally I'd stick with `node`'s name, but I think `.extname` is a\n // particularly poor name. So we support `.extname` for discovery but mark it\n // as deprecated, even if it will never be removed.\n /** @deprecated Alias for `.extension`. */\n get extname(): string {\n return this.extension;\n }\n\n // TODO: find a neat way to dedup with the sync version?\n async exists(constraints?: {\n mustBe: \"file\" | \"directory\";\n }): Promise<boolean> {\n let stats: Awaited<ReturnType<typeof stat>>;\n try {\n stats = await stat(this.#path);\n // biome-ignore lint/suspicious/noExplicitAny: TypeScript limitation\n } catch (e: any) {\n if (e.code === \"ENOENT\") {\n return false;\n }\n throw e;\n }\n if (!constraints?.mustBe) {\n return true;\n }\n switch (constraints?.mustBe) {\n case \"file\": {\n mustNotHaveTrailingSlash(this);\n if (stats.isFile()) {\n return true;\n }\n throw new Error(`Path exists but is not a file: ${this.#path}`);\n }\n case \"directory\": {\n if (stats.isDirectory()) {\n return true;\n }\n throw new Error(`Path exists but is not a directory: ${this.#path}`);\n }\n default: {\n throw new Error(\"Invalid path type constraint\");\n }\n }\n }\n\n async existsAsFile(): Promise<boolean> {\n return this.exists({ mustBe: \"file\" });\n }\n\n async existsAsDir(): Promise<boolean> {\n return this.exists({ mustBe: \"directory\" });\n }\n\n // I don't think `mkdir` is a great name, but it does match the\n // well-established canonical commandline name. So in this case we keep the\n // awkward abbreviation.\n /** Defaults to `recursive: true`. */\n async mkdir(options?: Parameters<typeof mkdir>[1]): Promise<Path> {\n const optionsObject = (() => {\n if (typeof options === \"string\" || typeof options === \"number\") {\n return { mode: options };\n }\n return options ?? {};\n })();\n await mkdir(this.#path, { recursive: true, ...optionsObject });\n return this;\n }\n\n // TODO: check idempotency semantics when the destination exists and is a folder.\n /** Returns the destination path. */\n async cp(\n destination: string | URL | Path,\n options?: Parameters<typeof cp>[2],\n ): Promise<Path> {\n await cp(this.#path, new Path(destination).#path, options);\n return new Path(destination);\n }\n\n // TODO: check idempotency semantics when the destination exists and is a folder.\n async rename(destination: string | URL | Path): Promise<void> {\n await rename(this.#path, new Path(destination).#path);\n }\n\n /** Create a temporary dir inside the global temp dir for the current user. */\n static async makeTempDir(prefix?: string): Promise<Path> {\n return new Path(\n await mkdtemp(new Path(tmpdir()).join(prefix ?? \"js-temp-\").toString()),\n );\n }\n\n async rm(options?: Parameters<typeof rm>[1]): Promise<void> {\n await rm(this.#path, options);\n }\n\n /**\n * Equivalent to:\n *\n * .rm({ recursive: true, force: true, ...(options ?? {}) })\n *\n */\n async rm_rf(options?: Parameters<typeof rm>[1]): Promise<void> {\n await this.rm({ recursive: true, force: true, ...(options ?? {}) });\n }\n\n read: typeof readFileType = (options) =>\n // biome-ignore lint/suspicious/noExplicitAny: Needed to wrangle the types.\n readFile(this.#path, options as any) as any;\n\n async readText(): Promise<string> {\n return readFile(this.#path, \"utf-8\");\n }\n\n async readJSON<T>(): Promise<T> {\n return JSON.parse(await this.readText());\n }\n\n /** Creates intermediate directories if they do not exist.\n *\n * Returns the original `Path` (for chaining).\n */\n async write(\n data: Parameters<typeof writeFile>[1],\n options?: Parameters<typeof writeFile>[2],\n ): Promise<Path> {\n await this.parent.mkdir();\n await writeFile(this.#path, data, options);\n return this;\n }\n\n /**\n * If only `data` is provided, this is equivalent to:\n *\n * .write(JSON.stringify(data, null, \" \"));\n *\n * `replacer` and `space` can also be specified, making this equivalent to:\n *\n * .write(JSON.stringify(data, replacer, space));\n *\n * Returns the original `Path` (for chaining).\n */\n async writeJSON<T>(\n data: T,\n replacer: Parameters<typeof JSON.stringify>[1] = null,\n space: Parameters<typeof JSON.stringify>[2] = \" \",\n ): Promise<Path> {\n await this.write(JSON.stringify(data, replacer, space));\n return this;\n }\n\n // Normally we'd add a `@deprecated` alias named `.readdir`, but that would\n // differ only by capitalization of a single non-leading character. This can\n // be a bit confusing, especially when autocompleting. So for this function in\n // particular we don't include an alias.\n readDir: typeof readDirType = (options) =>\n // biome-ignore lint/suspicious/noExplicitAny: Needed to wrangle the types.\n readdir(this.#path, options as any) as any;\n\n static get homedir(): Path {\n return new Path(homedir());\n }\n\n static xdg = {\n cache: new Path(xdgCache ?? Path.homedir.join(\".cache\")),\n config: new Path(xdgConfig ?? Path.homedir.join(\".config\")),\n data: new Path(xdgData ?? Path.homedir.join(\".local/share\")),\n state: new Path(xdgState ?? Path.homedir.join(\".local/state\")),\n /**\n * {@link Path.xdg.runtime} does not have a default value. Consider\n * {@link Path.xdg.runtimeWithStateFallback} if you need a fallback but do not have a particular fallback in mind.\n */\n runtime: xdgRuntime ? new Path(xdgRuntime) : undefined,\n runtimeWithStateFallback: xdgRuntime\n ? new Path(xdgRuntime)\n : new Path(xdgState ?? Path.homedir.join(\".local/state\")),\n };\n\n /** Chainable function to print the path. Prints the same as:\n *\n * if (args.length > 0) {\n * console.log(...args);\n * }\n * console.log(this.path);\n *\n */\n // biome-ignore lint/suspicious/noExplicitAny: This is the correct type, based on `console.log(\u2026)`.\n debugPrint(...args: any[]): Path {\n if (args.length > 0) {\n console.log(...args);\n }\n console.log(this.#path);\n return this;\n }\n}\n\n/**\n * This function is useful to serialize any `Path`s in a structure to pass on to\n * functions that do not know about the `Path` class, e.g.\n *\n * function process(args: (string | Path)[]) {\n * const argsAsStrings = args.map(stringifyIfPath);\n * }\n *\n */\nexport function stringifyIfPath<T>(value: T | Path): T | string {\n if (value instanceof Path) {\n return value.toString();\n }\n return value;\n}\n\nexport function mustNotHaveTrailingSlash(path: Path): void {\n if (path.hasTrailingSlash()) {\n throw new Error(\n \"Path ends with a slash, which cannot be treated as a file.\",\n );\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,SAAS,cAAc;AAChC,SAAS,UAAU,SAAS,SAAS,YAAY;AACjD,SAAS,eAAe,qBAAqB;AAC7C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA4EA,IAAM,OAAN,MAAM,MAAK;AAAA;AAAA,EAEhB;AAAA;AAAA;AAAA;AAAA,EAIA,YAAY,MAA2B;AACrC,SAAK,mBAAmB,MAAK,kBAAkB,IAAI,CAAC;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAO,QAAQ,MAA2B,MAAiC;AACzE,UAAM,WAAW,MAAM;AACrB,UAAI,EAAE,gBAAgB,QAAO;AAC3B,eAAO;AAAA,MACT;AACA,UAAI,CAAC,KAAK,eAAe,GAAG;AAC1B,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AACA,aAAO,cAAc,KAAK,KAAK;AAAA,IACjC,GAAG;AACH,WAAO,IAAI,MAAK,IAAI,IAAI,MAAK,kBAAkB,IAAI,GAAG,OAAO,CAAC;AAAA,EAChE;AAAA,EAEA,OAAO,kBAAkB,MAAmC;AAC1D,QAAI,gBAAgB,OAAM;AACxB,aAAO,KAAK;AAAA,IACd;AACA,QAAI,gBAAgB,KAAK;AACvB,aAAO,cAAc,IAAI;AAAA,IAC3B;AACA,QAAI,OAAO,SAAS,UAAU;AAE5B,UAAI,KAAK,WAAW,UAAU,GAAG;AAC/B,eAAO,cAAc,IAAI;AAAA,MA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
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,148 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Dirent, ObjectEncodingOptions, OpenMode } from "node:fs";
|
|
3
|
-
import { cp, mkdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
declare function readDirType(options?: (ObjectEncodingOptions & {
|
|
5
|
-
withFileTypes?: false | undefined;
|
|
6
|
-
recursive?: boolean | undefined;
|
|
7
|
-
}) | BufferEncoding | null): Promise<string[]>;
|
|
8
|
-
declare function readDirType(options: {
|
|
9
|
-
encoding: "buffer";
|
|
10
|
-
withFileTypes?: false | undefined;
|
|
11
|
-
recursive?: boolean | undefined;
|
|
12
|
-
} | "buffer"): Promise<Buffer[]>;
|
|
13
|
-
declare function readDirType(options?: (ObjectEncodingOptions & {
|
|
14
|
-
withFileTypes?: false | undefined;
|
|
15
|
-
recursive?: boolean | undefined;
|
|
16
|
-
}) | BufferEncoding | null): Promise<string[] | Buffer[]>;
|
|
17
|
-
declare function readDirType(options: ObjectEncodingOptions & {
|
|
18
|
-
withFileTypes: true;
|
|
19
|
-
recursive?: boolean | undefined;
|
|
20
|
-
}): Promise<Dirent[]>;
|
|
21
|
-
declare function readDirType(options: {
|
|
22
|
-
encoding: "buffer";
|
|
23
|
-
withFileTypes: true;
|
|
24
|
-
recursive?: boolean | undefined;
|
|
25
|
-
}): Promise<Dirent<Buffer>[]>;
|
|
26
|
-
declare function readFileType(options?: ({
|
|
27
|
-
encoding?: null | undefined;
|
|
28
|
-
flag?: OpenMode | undefined;
|
|
29
|
-
} & Abortable) | null): Promise<Buffer>;
|
|
30
|
-
declare function readFileType(options: ({
|
|
31
|
-
encoding: BufferEncoding;
|
|
32
|
-
flag?: OpenMode | undefined;
|
|
33
|
-
} & Abortable) | BufferEncoding): Promise<string>;
|
|
34
|
-
declare function readFileType(options?: (ObjectEncodingOptions & Abortable & {
|
|
35
|
-
flag?: OpenMode | undefined;
|
|
36
|
-
}) | BufferEncoding | null): Promise<string | Buffer>;
|
|
37
|
-
export declare class Path {
|
|
38
|
-
#private;
|
|
39
|
-
/**
|
|
40
|
-
* If `path` is a string starting with `file:///`, it will be parsed as a file URL.
|
|
41
|
-
*/
|
|
42
|
-
constructor(path: string | URL | Path);
|
|
43
|
-
/**
|
|
44
|
-
* Similar to `new URL(path, base)`, but accepting and returning `Path` objects.
|
|
45
|
-
* Note that `base` must be one of:
|
|
46
|
-
*
|
|
47
|
-
* - a valid second argument to `new URL(…)`.
|
|
48
|
-
* - a `Path` representing an absolute path.
|
|
49
|
-
*
|
|
50
|
-
*/
|
|
51
|
-
static resolve(path: string | URL | Path, base: string | URL | Path): Path;
|
|
52
|
-
isAbsolutePath(): boolean;
|
|
53
|
-
toFileURL(): URL;
|
|
54
|
-
/**
|
|
55
|
-
* The `Path` can have a trailing slash, indicating that it represents a
|
|
56
|
-
* directory. (If there is no trailing slash, it can represent either a file
|
|
57
|
-
* or a directory.)
|
|
58
|
-
*
|
|
59
|
-
* Some operations will refuse to treat a directory path as a file path. This
|
|
60
|
-
* function identifies such paths.
|
|
61
|
-
*/
|
|
62
|
-
hasTrailingSlash(): boolean;
|
|
63
|
-
/**
|
|
64
|
-
* Same as `.toString()`, but more concise.
|
|
65
|
-
*/
|
|
66
|
-
get path(): string;
|
|
67
|
-
toString(): string;
|
|
68
|
-
/** Constructs a new path by appending the given path segments.
|
|
69
|
-
* This follows `node` semantics for absolute paths: leading slashes in the given descendant segments are ignored.
|
|
70
|
-
*/
|
|
71
|
-
join(...segments: (string | Path)[]): Path;
|
|
72
|
-
extendBasename(suffix: string): Path;
|
|
73
|
-
get parent(): Path;
|
|
74
|
-
/** @deprecated Alias for `.parent`. */
|
|
75
|
-
get dirname(): Path;
|
|
76
|
-
get basename(): Path;
|
|
77
|
-
get extension(): string;
|
|
78
|
-
/** @deprecated Alias for `.extension`. */
|
|
79
|
-
get extname(): string;
|
|
80
|
-
exists(constraints?: {
|
|
81
|
-
mustBe: "file" | "directory";
|
|
82
|
-
}): Promise<boolean>;
|
|
83
|
-
existsAsFile(): Promise<boolean>;
|
|
84
|
-
existsAsDir(): Promise<boolean>;
|
|
85
|
-
/** Defaults to `recursive: true`. */
|
|
86
|
-
mkdir(options?: Parameters<typeof mkdir>[1]): Promise<Path>;
|
|
87
|
-
/** Returns the destination path. */
|
|
88
|
-
cp(destination: string | URL | Path, options?: Parameters<typeof cp>[2]): Promise<Path>;
|
|
89
|
-
rename(destination: string | URL | Path): Promise<void>;
|
|
90
|
-
/** Create a temporary dir inside the global temp dir for the current user. */
|
|
91
|
-
static makeTempDir(prefix?: string): Promise<Path>;
|
|
92
|
-
rm(options?: Parameters<typeof rm>[1]): Promise<void>;
|
|
93
|
-
/**
|
|
94
|
-
* Equivalent to:
|
|
95
|
-
*
|
|
96
|
-
* .rm({ recursive: true, force: true, ...(options ?? {}) })
|
|
97
|
-
*
|
|
98
|
-
*/
|
|
99
|
-
rm_rf(options?: Parameters<typeof rm>[1]): Promise<void>;
|
|
100
|
-
read: typeof readFileType;
|
|
101
|
-
readText(): Promise<string>;
|
|
102
|
-
readJSON<T>(): Promise<T>;
|
|
103
|
-
/** Creates intermediate directories if they do not exist.
|
|
104
|
-
*
|
|
105
|
-
* Returns the original `Path` (for chaining).
|
|
106
|
-
*/
|
|
107
|
-
write(data: Parameters<typeof writeFile>[1], options?: Parameters<typeof writeFile>[2]): Promise<Path>;
|
|
108
|
-
/**
|
|
109
|
-
* If only `data` is provided, this is equivalent to:
|
|
110
|
-
*
|
|
111
|
-
* .write(JSON.stringify(data, null, " "));
|
|
112
|
-
*
|
|
113
|
-
* `replacer` and `space` can also be specified, making this equivalent to:
|
|
114
|
-
*
|
|
115
|
-
* .write(JSON.stringify(data, replacer, space));
|
|
116
|
-
*
|
|
117
|
-
* Returns the original `Path` (for chaining).
|
|
118
|
-
*/
|
|
119
|
-
writeJSON<T>(data: T, replacer?: Parameters<typeof JSON.stringify>[1], space?: Parameters<typeof JSON.stringify>[2]): Promise<Path>;
|
|
120
|
-
readDir: typeof readDirType;
|
|
121
|
-
static get homedir(): Path;
|
|
122
|
-
static xdg: {
|
|
123
|
-
cache: Path;
|
|
124
|
-
config: Path;
|
|
125
|
-
data: Path;
|
|
126
|
-
state: Path;
|
|
127
|
-
};
|
|
128
|
-
/** Chainable function to print the path. Prints the same as:
|
|
129
|
-
*
|
|
130
|
-
* if (args.length > 0) {
|
|
131
|
-
* console.log(...args);
|
|
132
|
-
* }
|
|
133
|
-
* console.log(this.path);
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
debugPrint(...args: any[]): Path;
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* This function is useful to serialize any `Path`s in a structure to pass on to
|
|
140
|
-
* functions that do not know about the `Path` class, e.g.
|
|
141
|
-
*
|
|
142
|
-
* function process(args: (string | Path)[]) {
|
|
143
|
-
* const argsAsStrings = args.map(stringifyIfPath);
|
|
144
|
-
* }
|
|
145
|
-
*
|
|
146
|
-
*/
|
|
147
|
-
export declare function stringifyIfPath<T>(value: T | Path): T | string;
|
|
148
|
-
export {};
|
|
1
|
+
export * from "./Path";
|