@intellectual-frontiers/system 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/compile.d.ts +94 -0
- package/dist/compile.js +130 -0
- package/dist/dirs.d.ts +19 -0
- package/dist/dirs.js +64 -0
- package/dist/discover.d.ts +43 -0
- package/dist/discover.js +118 -0
- package/dist/errors.d.ts +130 -0
- package/dist/errors.js +189 -0
- package/dist/fs.d.ts +263 -0
- package/dist/fs.js +826 -0
- package/dist/git.d.ts +63 -0
- package/dist/git.js +144 -0
- package/dist/job-plan.d.ts +207 -0
- package/dist/job-plan.js +563 -0
- package/dist/lock.d.ts +41 -0
- package/dist/lock.js +130 -0
- package/dist/mod.d.ts +28 -0
- package/dist/mod.js +29 -0
- package/dist/path.d.ts +47 -0
- package/dist/path.js +55 -0
- package/dist/proc.d.ts +257 -0
- package/dist/proc.js +589 -0
- package/dist/schedule.d.ts +99 -0
- package/dist/schedule.js +131 -0
- package/dist/stdio.d.ts +31 -0
- package/dist/stdio.js +170 -0
- package/dist/tty.d.ts +34 -0
- package/dist/tty.js +71 -0
- package/dist/watch.d.ts +42 -0
- package/dist/watch.js +342 -0
- package/package.json +113 -4
- package/src/compile.ts +178 -0
- package/src/dirs.ts +87 -0
- package/src/discover.ts +144 -0
- package/src/errors.ts +176 -0
- package/src/fs.ts +967 -0
- package/src/git.ts +163 -0
- package/src/job-plan.ts +660 -0
- package/src/lock.ts +149 -0
- package/src/mod.ts +29 -0
- package/src/path.ts +59 -0
- package/src/proc.ts +786 -0
- package/src/schedule.ts +173 -0
- package/src/stdio.ts +204 -0
- package/src/tty.ts +94 -0
- package/src/watch.ts +338 -0
package/dist/fs.js
ADDED
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
// Spec: 0001-system FR-002
|
|
2
|
+
/**
|
|
3
|
+
* Files and folders (commons 0005-typescript-runtime FR-020), on `node:fs` alone, so the same code runs on Bun and Node. The functions
|
|
4
|
+
* have short, plain names and shapes (`readTextFile`, `writeTextFile`, `remove`, `mkdir`, `stat`, `readDir`,
|
|
5
|
+
* `makeTempDir`, ...); every failure is thrown as an error of `./errors.ts` (`NotFound`, `AlreadyExists`, ...).
|
|
6
|
+
* The helpers Stolos wrote over them (`exists`, `readTextOr`, `ensureDir`, `writeIfChanged`, `listDirs`, `listFiles`, `walkFiles`) and the
|
|
7
|
+
* setup's `sha256File` are here too.
|
|
8
|
+
*
|
|
9
|
+
* `readDir` is an async iterable, with `chown` and `symlinkSync` (FR-048); `open` and `openSync` give an `FsFile`
|
|
10
|
+
* with no lock, which is `./lock.ts`'s (FR-049); `copy` and `walk` are `@std/fs`'s (FR-050), and `expandGlob` gives the
|
|
11
|
+
* entries a glob of `../kernel/glob.ts` matches, in byte order (FR-058).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { makeTempDir, readTextFile, remove, writeTextFile } from "./fs.ts";
|
|
16
|
+
* const dir = await makeTempDir({ prefix: "example-" });
|
|
17
|
+
* await writeTextFile(`${dir}/a.txt`, "hello");
|
|
18
|
+
* await readTextFile(`${dir}/a.txt`); // "hello"
|
|
19
|
+
* await remove(dir, { recursive: true });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { Buffer } from "node:buffer";
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import * as nfs from "node:fs";
|
|
27
|
+
import * as fsp from "node:fs/promises";
|
|
28
|
+
import { tmpdir } from "node:os";
|
|
29
|
+
import { basename, dirname, isAbsolute, join, parse, relative, resolve, sep } from "node:path";
|
|
30
|
+
import process from "node:process";
|
|
31
|
+
import { fileURLToPath } from "node:url";
|
|
32
|
+
import { absurd } from "@intellectual-frontiers/core";
|
|
33
|
+
import { formatGlobError, globRoot, globToRegExp } from "@intellectual-frontiers/formats/glob";
|
|
34
|
+
import { AlreadyExists, hostCall, hostCallSync, InvalidData, NotADirectory, NotFound } from "./errors.js";
|
|
35
|
+
const p = (path) => (typeof path === "string" ? path : fileURLToPath(path));
|
|
36
|
+
const time = (ms) => (Number.isFinite(ms) && ms > 0 ? new Date(ms) : null);
|
|
37
|
+
function info(s) {
|
|
38
|
+
return {
|
|
39
|
+
isFile: s.isFile(),
|
|
40
|
+
isDirectory: s.isDirectory(),
|
|
41
|
+
isSymlink: s.isSymbolicLink(),
|
|
42
|
+
size: s.size,
|
|
43
|
+
mtime: time(s.mtimeMs),
|
|
44
|
+
atime: time(s.atimeMs),
|
|
45
|
+
birthtime: time(s.birthtimeMs),
|
|
46
|
+
ctime: time(s.ctimeMs),
|
|
47
|
+
mode: process.platform === "win32" ? null : s.mode,
|
|
48
|
+
uid: process.platform === "win32" ? null : s.uid,
|
|
49
|
+
gid: process.platform === "win32" ? null : s.gid,
|
|
50
|
+
dev: s.dev,
|
|
51
|
+
ino: process.platform === "win32" ? null : s.ino,
|
|
52
|
+
nlink: process.platform === "win32" ? null : s.nlink,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const entry = (d) => ({
|
|
56
|
+
name: d.name,
|
|
57
|
+
isFile: d.isFile(),
|
|
58
|
+
isDirectory: d.isDirectory(),
|
|
59
|
+
isSymlink: d.isSymbolicLink(),
|
|
60
|
+
});
|
|
61
|
+
function flag(o) {
|
|
62
|
+
if (o.createNew === true)
|
|
63
|
+
return o.append === true ? "ax" : "wx";
|
|
64
|
+
return o.append === true ? "a" : "w";
|
|
65
|
+
}
|
|
66
|
+
const modeOf = (o) => (o.mode === undefined ? {} : { mode: o.mode });
|
|
67
|
+
async function write(path, data, o) {
|
|
68
|
+
const file = p(path);
|
|
69
|
+
await hostCall(file, async () => {
|
|
70
|
+
if (o.create === false)
|
|
71
|
+
await fsp.access(file);
|
|
72
|
+
await fsp.writeFile(file, data, { flag: flag(o), ...modeOf(o) });
|
|
73
|
+
if (o.mode !== undefined && process.platform !== "win32")
|
|
74
|
+
await fsp.chmod(file, o.mode);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function writeSync(path, data, o) {
|
|
78
|
+
const file = p(path);
|
|
79
|
+
hostCallSync(file, () => {
|
|
80
|
+
if (o.create === false)
|
|
81
|
+
nfs.accessSync(file);
|
|
82
|
+
nfs.writeFileSync(file, data, { flag: flag(o), ...modeOf(o) });
|
|
83
|
+
if (o.mode !== undefined && process.platform !== "win32")
|
|
84
|
+
nfs.chmodSync(file, o.mode);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** A file's text, read as UTF-8. */
|
|
88
|
+
export function readTextFile(path) {
|
|
89
|
+
return hostCall(p(path), () => fsp.readFile(p(path), "utf8"));
|
|
90
|
+
}
|
|
91
|
+
/** A file's text, read as UTF-8, without waiting. */
|
|
92
|
+
export function readTextFileSync(path) {
|
|
93
|
+
return hostCallSync(p(path), () => nfs.readFileSync(p(path), "utf8"));
|
|
94
|
+
}
|
|
95
|
+
/** A file's bytes. */
|
|
96
|
+
export async function readFile(path) {
|
|
97
|
+
const b = await hostCall(p(path), () => fsp.readFile(p(path)));
|
|
98
|
+
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
99
|
+
}
|
|
100
|
+
/** A file's bytes, without waiting. */
|
|
101
|
+
export function readFileSync(path) {
|
|
102
|
+
const b = hostCallSync(p(path), () => nfs.readFileSync(p(path)));
|
|
103
|
+
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
104
|
+
}
|
|
105
|
+
/** Text written to a file as UTF-8, replacing it unless `append`. */
|
|
106
|
+
export function writeTextFile(path, data, options = {}) {
|
|
107
|
+
return write(path, data, options);
|
|
108
|
+
}
|
|
109
|
+
/** Text written to a file as UTF-8, without waiting. */
|
|
110
|
+
export function writeTextFileSync(path, data, options = {}) {
|
|
111
|
+
writeSync(path, data, options);
|
|
112
|
+
}
|
|
113
|
+
/** Bytes written to a file, replacing it unless `append`. */
|
|
114
|
+
export function writeFile(path, data, options = {}) {
|
|
115
|
+
return write(path, data, options);
|
|
116
|
+
}
|
|
117
|
+
/** Bytes written to a file, without waiting. */
|
|
118
|
+
export function writeFileSync(path, data, options = {}) {
|
|
119
|
+
writeSync(path, data, options);
|
|
120
|
+
}
|
|
121
|
+
/** A file, a link or an empty folder removed; with `recursive`, a folder and everything in it. `NotFound` where nothing is there; the error is of `./errors.ts`. */
|
|
122
|
+
export async function remove(path, options = {}) {
|
|
123
|
+
const file = p(path);
|
|
124
|
+
await hostCall(file, async () => {
|
|
125
|
+
const s = await fsp.lstat(file);
|
|
126
|
+
if (s.isDirectory())
|
|
127
|
+
await (options.recursive === true ? fsp.rm(file, { recursive: true }) : fsp.rmdir(file));
|
|
128
|
+
else
|
|
129
|
+
await fsp.unlink(file);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** The same, without waiting. */
|
|
133
|
+
export function removeSync(path, options = {}) {
|
|
134
|
+
const file = p(path);
|
|
135
|
+
hostCallSync(file, () => {
|
|
136
|
+
const s = nfs.lstatSync(file);
|
|
137
|
+
if (s.isDirectory()) {
|
|
138
|
+
if (options.recursive === true)
|
|
139
|
+
nfs.rmSync(file, { recursive: true });
|
|
140
|
+
else
|
|
141
|
+
nfs.rmdirSync(file);
|
|
142
|
+
}
|
|
143
|
+
else
|
|
144
|
+
nfs.unlinkSync(file);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/** A folder made. */
|
|
148
|
+
export async function mkdir(path, options = {}) {
|
|
149
|
+
const file = p(path);
|
|
150
|
+
await hostCall(file, () => fsp.mkdir(file, { recursive: options.recursive === true, ...(options.mode === undefined ? {} : { mode: options.mode }) }));
|
|
151
|
+
}
|
|
152
|
+
/** A folder made, without waiting. */
|
|
153
|
+
export function mkdirSync(path, options = {}) {
|
|
154
|
+
const file = p(path);
|
|
155
|
+
hostCallSync(file, () => nfs.mkdirSync(file, { recursive: options.recursive === true, ...(options.mode === undefined ? {} : { mode: options.mode }) }));
|
|
156
|
+
}
|
|
157
|
+
/** What is at a path, following links. */
|
|
158
|
+
export async function stat(path) {
|
|
159
|
+
return info(await hostCall(p(path), () => fsp.stat(p(path))));
|
|
160
|
+
}
|
|
161
|
+
/** What is at a path, following links, without waiting. */
|
|
162
|
+
export function statSync(path) {
|
|
163
|
+
return info(hostCallSync(p(path), () => nfs.statSync(p(path))));
|
|
164
|
+
}
|
|
165
|
+
/** What is at a path; a link is described, not followed. */
|
|
166
|
+
export async function lstat(path) {
|
|
167
|
+
return info(await hostCall(p(path), () => fsp.lstat(p(path))));
|
|
168
|
+
}
|
|
169
|
+
/** What is at a path; a link is described, not followed; without waiting. */
|
|
170
|
+
export function lstatSync(path) {
|
|
171
|
+
return info(hostCallSync(p(path), () => nfs.lstatSync(p(path))));
|
|
172
|
+
}
|
|
173
|
+
/** The entries of a folder, in the order the file system gives them (FR-048); `NotFound` at the first step where it is not there. */
|
|
174
|
+
export async function* readDir(path) {
|
|
175
|
+
const dir = p(path);
|
|
176
|
+
const entries = await hostCall(dir, () => fsp.readdir(dir, { withFileTypes: true }));
|
|
177
|
+
for (const d of entries)
|
|
178
|
+
yield entry(d);
|
|
179
|
+
}
|
|
180
|
+
/** The entries of a folder, without waiting. */
|
|
181
|
+
export function readDirSync(path) {
|
|
182
|
+
const dir = p(path);
|
|
183
|
+
return hostCallSync(dir, () => nfs.readdirSync(dir, { withFileTypes: true })).map(entry);
|
|
184
|
+
}
|
|
185
|
+
/** What `mkdtemp` is given: the folder with a separator, then the prefix, so that the random part is added inside the folder and not to its name. */
|
|
186
|
+
const tempBase = (options) => {
|
|
187
|
+
const dir = options.dir ?? tmpdir();
|
|
188
|
+
return `${dir.endsWith(sep) ? dir : `${dir}${sep}`}${options.prefix ?? ""}`;
|
|
189
|
+
};
|
|
190
|
+
/** A new, empty, private folder, and its path. */
|
|
191
|
+
export async function makeTempDir(options = {}) {
|
|
192
|
+
const base = tempBase(options);
|
|
193
|
+
const made = await hostCall(base, () => fsp.mkdtemp(base));
|
|
194
|
+
if (options.suffix === undefined || options.suffix === "")
|
|
195
|
+
return made;
|
|
196
|
+
await hostCall(made, () => fsp.rename(made, `${made}${options.suffix}`));
|
|
197
|
+
return `${made}${options.suffix}`;
|
|
198
|
+
}
|
|
199
|
+
/** A new, empty, private folder, without waiting. */
|
|
200
|
+
export function makeTempDirSync(options = {}) {
|
|
201
|
+
const base = tempBase(options);
|
|
202
|
+
return hostCallSync(base, () => nfs.mkdtempSync(base));
|
|
203
|
+
}
|
|
204
|
+
/** A new, empty, private file, and its path. */
|
|
205
|
+
export async function makeTempFile(options = {}) {
|
|
206
|
+
const dir = options.dir ?? tmpdir();
|
|
207
|
+
for (let i = 0; i < 100; i++) {
|
|
208
|
+
const name = join(dir, `${options.prefix ?? ""}${crypto.randomUUID().slice(0, 12)}${options.suffix ?? ""}`);
|
|
209
|
+
try {
|
|
210
|
+
await hostCall(name, () => fsp.writeFile(name, "", { flag: "wx", mode: 0o600 }));
|
|
211
|
+
return name;
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
if (!(e instanceof Error && "code" in e && e.code === "EEXIST"))
|
|
215
|
+
throw e;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
throw new Error(`no free temporary file name in ${dir}`);
|
|
219
|
+
}
|
|
220
|
+
/** A file or folder moved to a new path, replacing a file there. */
|
|
221
|
+
export function rename(from, to) {
|
|
222
|
+
return hostCall(p(from), () => fsp.rename(p(from), p(to)));
|
|
223
|
+
}
|
|
224
|
+
/** The same, without waiting. */
|
|
225
|
+
export function renameSync(from, to) {
|
|
226
|
+
hostCallSync(p(from), () => nfs.renameSync(p(from), p(to)));
|
|
227
|
+
}
|
|
228
|
+
/** A file's bytes copied to a new path, replacing a file there. */
|
|
229
|
+
export function copyFile(from, to) {
|
|
230
|
+
return hostCall(p(from), () => fsp.copyFile(p(from), p(to)));
|
|
231
|
+
}
|
|
232
|
+
/** A file's mode changed (ignored on Windows, which has none). */
|
|
233
|
+
export async function chmod(path, mode) {
|
|
234
|
+
if (process.platform === "win32")
|
|
235
|
+
return;
|
|
236
|
+
await hostCall(p(path), () => fsp.chmod(p(path), mode));
|
|
237
|
+
}
|
|
238
|
+
/** A second name, `to`, for the file at `from` (a hard link), refused with `AlreadyExists` where `to` is taken: a file put in place whole, or not at all. */
|
|
239
|
+
export function link(from, to) {
|
|
240
|
+
return hostCall(p(to), () => fsp.link(p(from), p(to)));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* A link at `path` that points at `target`; `type` matters on Windows only. `junction` (a folder, FR-073) is the Windows link any account
|
|
244
|
+
* may make (a symbolic link needs administrator rights or Developer Mode there): its target is made absolute, as Windows requires; on
|
|
245
|
+
* Linux and macOS it is an ordinary symbolic link.
|
|
246
|
+
*/
|
|
247
|
+
export function symlink(target, path, options = {}) {
|
|
248
|
+
return hostCall(p(path), () => fsp.symlink(p(target), p(path), options.type));
|
|
249
|
+
}
|
|
250
|
+
/** Where a link points, as it was written. */
|
|
251
|
+
export function readLink(path) {
|
|
252
|
+
return hostCall(p(path), () => fsp.readlink(p(path)));
|
|
253
|
+
}
|
|
254
|
+
/** The absolute path with every link followed. */
|
|
255
|
+
export function realPath(path) {
|
|
256
|
+
return hostCall(p(path), () => fsp.realpath(p(path)));
|
|
257
|
+
}
|
|
258
|
+
/** A file cut (or extended with zeros) to `length` bytes, 0 by default. */
|
|
259
|
+
export function truncate(path, length = 0) {
|
|
260
|
+
return hostCall(p(path), () => fsp.truncate(p(path), length));
|
|
261
|
+
}
|
|
262
|
+
/** A file's access and modification times set. */
|
|
263
|
+
export function utime(path, atime, mtime) {
|
|
264
|
+
return hostCall(p(path), () => fsp.utimes(p(path), atime, mtime));
|
|
265
|
+
}
|
|
266
|
+
/** Whether something is at `path` (a file, folder or link; a broken link counts). An error other than `NotFound` is thrown. */
|
|
267
|
+
export async function exists(path) {
|
|
268
|
+
try {
|
|
269
|
+
await lstat(path);
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
catch (e) {
|
|
273
|
+
if (e instanceof NotFound)
|
|
274
|
+
return false;
|
|
275
|
+
throw e;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/** The same, without waiting. */
|
|
279
|
+
export function existsSync(path) {
|
|
280
|
+
try {
|
|
281
|
+
lstatSync(path);
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
catch (e) {
|
|
285
|
+
if (e instanceof NotFound)
|
|
286
|
+
return false;
|
|
287
|
+
throw e;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** A text file's content, or `fallback` (undefined unless given) where nothing is there. Other errors are thrown. */
|
|
291
|
+
export async function readTextOr(path, fallback) {
|
|
292
|
+
try {
|
|
293
|
+
return await readTextFile(path);
|
|
294
|
+
}
|
|
295
|
+
catch (e) {
|
|
296
|
+
if (e instanceof NotFound)
|
|
297
|
+
return fallback;
|
|
298
|
+
throw e;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/** A folder and its parents made, where missing. */
|
|
302
|
+
export function ensureDir(path) {
|
|
303
|
+
return mkdir(path, { recursive: true });
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* A text file written only where its content changed, in one step (a file beside it renamed over it, so a reader never sees half of it),
|
|
307
|
+
* its folder made where missing. Whether it was written.
|
|
308
|
+
*/
|
|
309
|
+
export async function writeIfChanged(path, content) {
|
|
310
|
+
const file = p(path);
|
|
311
|
+
if ((await readTextOr(file)) === content)
|
|
312
|
+
return false;
|
|
313
|
+
await ensureDir(dirname(file));
|
|
314
|
+
const tmp = `${file}.${crypto.randomUUID().slice(0, 8)}.tmp`;
|
|
315
|
+
await writeTextFile(tmp, content);
|
|
316
|
+
try {
|
|
317
|
+
await rename(tmp, file);
|
|
318
|
+
}
|
|
319
|
+
catch (e) {
|
|
320
|
+
await remove(tmp).catch(() => { });
|
|
321
|
+
throw e;
|
|
322
|
+
}
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
async function namesIn(path, keep) {
|
|
326
|
+
const out = [];
|
|
327
|
+
try {
|
|
328
|
+
for await (const e of readDir(path))
|
|
329
|
+
if (keep(e))
|
|
330
|
+
out.push(e.name);
|
|
331
|
+
}
|
|
332
|
+
catch (e) {
|
|
333
|
+
if (!(e instanceof NotFound))
|
|
334
|
+
throw e;
|
|
335
|
+
}
|
|
336
|
+
return out.sort();
|
|
337
|
+
}
|
|
338
|
+
/** The names of the folders directly in `path`, sorted; none where it does not exist. */
|
|
339
|
+
export function listDirs(path) {
|
|
340
|
+
return namesIn(path, (e) => e.isDirectory);
|
|
341
|
+
}
|
|
342
|
+
/** The names of the files directly in `path` that end in `suffix`, sorted; none where it does not exist. */
|
|
343
|
+
export function listFiles(path, suffix = "") {
|
|
344
|
+
return namesIn(path, (e) => e.isFile && e.name.endsWith(suffix));
|
|
345
|
+
}
|
|
346
|
+
/** Every file under a folder (links included, not followed), by its path relative to it with `/` between parts, sorted; none where it does not exist. */
|
|
347
|
+
export async function walkFiles(root, rel = "") {
|
|
348
|
+
const out = [];
|
|
349
|
+
const base = p(root);
|
|
350
|
+
try {
|
|
351
|
+
for await (const e of readDir(rel === "" ? base : join(base, rel))) {
|
|
352
|
+
const r = rel === "" ? e.name : `${rel}/${e.name}`;
|
|
353
|
+
if (e.isDirectory)
|
|
354
|
+
out.push(...(await walkFiles(base, r)));
|
|
355
|
+
else
|
|
356
|
+
out.push(r);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
catch (e) {
|
|
360
|
+
if (!(e instanceof NotFound))
|
|
361
|
+
throw e;
|
|
362
|
+
}
|
|
363
|
+
return out.sort();
|
|
364
|
+
}
|
|
365
|
+
/** The SHA-256 of a file, read as a stream so a large file is not held in memory. */
|
|
366
|
+
export async function sha256File(path) {
|
|
367
|
+
const file = p(path);
|
|
368
|
+
return await hostCall(file, () => new Promise((resolve, reject) => {
|
|
369
|
+
const hash = createHash("sha256");
|
|
370
|
+
nfs
|
|
371
|
+
.createReadStream(file)
|
|
372
|
+
.on("error", reject)
|
|
373
|
+
.on("data", (chunk) => hash.update(chunk))
|
|
374
|
+
.on("end", () => resolve(hash.digest("hex")));
|
|
375
|
+
}));
|
|
376
|
+
}
|
|
377
|
+
/** A temporary folder for the length of `f`, removed with everything in it afterwards, whatever `f` does. */
|
|
378
|
+
export async function withTempDir(f, options = {}) {
|
|
379
|
+
const dir = await makeTempDir(options);
|
|
380
|
+
try {
|
|
381
|
+
return await f(dir);
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
await remove(dir, { recursive: true }).catch(() => { });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
// FR-048: the rest of the folder calls.
|
|
388
|
+
/** A file's owner and group changed; `null` keeps one as it is. Ignored on Windows, which has no POSIX owners (FR-018). */
|
|
389
|
+
export async function chown(path, uid, gid) {
|
|
390
|
+
if (process.platform === "win32")
|
|
391
|
+
return;
|
|
392
|
+
await hostCall(p(path), () => fsp.chown(p(path), uid ?? -1, gid ?? -1));
|
|
393
|
+
}
|
|
394
|
+
/** A link at `path` that points at `target`, without waiting; `type` matters on Windows only (`junction`: see `symlink`). */
|
|
395
|
+
export function symlinkSync(target, path, options = {}) {
|
|
396
|
+
hostCallSync(p(path), () => nfs.symlinkSync(p(target), p(path), options.type));
|
|
397
|
+
}
|
|
398
|
+
function openFlags(path, o) {
|
|
399
|
+
const c = nfs.constants;
|
|
400
|
+
const write = o.write === true || o.append === true;
|
|
401
|
+
if (o.read !== true && !write)
|
|
402
|
+
throw new InvalidData(`open ${path}: none of read, write and append was asked for`, "EINVAL", path);
|
|
403
|
+
if ((o.create === true || o.createNew === true) && !write)
|
|
404
|
+
throw new InvalidData(`open ${path}: create needs write or append`, "EINVAL", path);
|
|
405
|
+
if (o.truncate === true && o.write !== true)
|
|
406
|
+
throw new InvalidData(`open ${path}: truncate needs write`, "EINVAL", path);
|
|
407
|
+
let f = o.read === true && write ? c.O_RDWR : write ? c.O_WRONLY : c.O_RDONLY;
|
|
408
|
+
if (o.append === true)
|
|
409
|
+
f |= c.O_APPEND;
|
|
410
|
+
if (o.create === true || o.createNew === true)
|
|
411
|
+
f |= c.O_CREAT;
|
|
412
|
+
if (o.createNew === true)
|
|
413
|
+
f |= c.O_EXCL;
|
|
414
|
+
// No O_TRUNC: Bun on Windows refuses O_TRUNC without O_CREAT with EINVAL, so `truncate` cuts the file once it is open (see `cutIfAsked`),
|
|
415
|
+
// the same on every runtime and system.
|
|
416
|
+
return f;
|
|
417
|
+
}
|
|
418
|
+
/** The file just opened cut to nothing where `truncate` was asked: what O_TRUNC does, done after the open (see {@link openFlags}). */
|
|
419
|
+
async function cutIfAsked(fd, path, o) {
|
|
420
|
+
if (o.truncate !== true)
|
|
421
|
+
return;
|
|
422
|
+
try {
|
|
423
|
+
await hostCall(path, () => new Promise((res, rej) => nfs.ftruncate(fd, 0, (e) => (e === null ? res() : rej(e)))));
|
|
424
|
+
}
|
|
425
|
+
catch (e) {
|
|
426
|
+
nfs.closeSync(fd);
|
|
427
|
+
throw e;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** The same, without waiting. */
|
|
431
|
+
function cutIfAskedSync(fd, path, o) {
|
|
432
|
+
if (o.truncate !== true)
|
|
433
|
+
return;
|
|
434
|
+
try {
|
|
435
|
+
hostCallSync(path, () => nfs.ftruncateSync(fd, 0));
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
nfs.closeSync(fd);
|
|
439
|
+
throw e;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const CHUNK = 64 * 1024;
|
|
443
|
+
/**
|
|
444
|
+
* An open file: it reads and writes at its position (a handle opened with `append` always writes at the end), `seek`s,
|
|
445
|
+
* `truncate`s, `stat`s and `sync`s, gives its bytes as a web `ReadableStream` and takes them as a `WritableStream` (each closes the file
|
|
446
|
+
* when it ends), and is closed by `close`, `using` or `await using`. It has no lock: one run at a time is `./lock.ts` (FR-039, FR-049).
|
|
447
|
+
* Made by {@link open} and {@link openSync}.
|
|
448
|
+
*/
|
|
449
|
+
export class FsFile {
|
|
450
|
+
#fd;
|
|
451
|
+
#path;
|
|
452
|
+
#append;
|
|
453
|
+
#pos = 0;
|
|
454
|
+
#closed = false;
|
|
455
|
+
#readable;
|
|
456
|
+
#writable;
|
|
457
|
+
/** A handle over an open descriptor; use {@link open} or {@link openSync}. */
|
|
458
|
+
constructor(fd, path, append) {
|
|
459
|
+
this.#fd = fd;
|
|
460
|
+
this.#path = path;
|
|
461
|
+
this.#append = append;
|
|
462
|
+
}
|
|
463
|
+
#fdOpen() {
|
|
464
|
+
if (this.#closed)
|
|
465
|
+
throw new InvalidData(`${this.#path}: the file is closed`, "EBADF", this.#path);
|
|
466
|
+
return this.#fd;
|
|
467
|
+
}
|
|
468
|
+
/** Bytes read into `buffer` from the position, which moves past them: how many, or `null` at the end of the file. */
|
|
469
|
+
async read(buffer) {
|
|
470
|
+
const fd = this.#fdOpen();
|
|
471
|
+
if (buffer.byteLength === 0)
|
|
472
|
+
return 0;
|
|
473
|
+
const at = this.#pos;
|
|
474
|
+
const n = await hostCall(this.#path, () => new Promise((res, rej) => nfs.read(fd, buffer, 0, buffer.byteLength, at, (e, got) => (e === null ? res(got) : rej(e)))));
|
|
475
|
+
if (n === 0)
|
|
476
|
+
return null;
|
|
477
|
+
this.#pos = at + n;
|
|
478
|
+
return n;
|
|
479
|
+
}
|
|
480
|
+
/** The same, without waiting. */
|
|
481
|
+
readSync(buffer) {
|
|
482
|
+
const fd = this.#fdOpen();
|
|
483
|
+
if (buffer.byteLength === 0)
|
|
484
|
+
return 0;
|
|
485
|
+
const n = hostCallSync(this.#path, () => nfs.readSync(fd, buffer, 0, buffer.byteLength, this.#pos));
|
|
486
|
+
if (n === 0)
|
|
487
|
+
return null;
|
|
488
|
+
this.#pos += n;
|
|
489
|
+
return n;
|
|
490
|
+
}
|
|
491
|
+
/** Bytes written at the position (at the end with `append`), which moves past them: how many, which may be fewer than given. */
|
|
492
|
+
async write(data) {
|
|
493
|
+
const fd = this.#fdOpen();
|
|
494
|
+
// With `append`, no position is given: Linux ignores one on an append handle, but Windows would write there (FR-018).
|
|
495
|
+
const at = this.#append ? null : this.#pos;
|
|
496
|
+
const n = await hostCall(this.#path, () => new Promise((res, rej) => nfs.write(fd, data, 0, data.byteLength, at, (e, put) => (e === null ? res(put) : rej(e)))));
|
|
497
|
+
this.#pos =
|
|
498
|
+
at === null
|
|
499
|
+
? (await hostCall(this.#path, () => new Promise((res, rej) => nfs.fstat(fd, (e, s) => (e === null ? res(s) : rej(e))))))
|
|
500
|
+
.size
|
|
501
|
+
: at + n;
|
|
502
|
+
return n;
|
|
503
|
+
}
|
|
504
|
+
/** The same, without waiting. */
|
|
505
|
+
writeSync(data) {
|
|
506
|
+
const fd = this.#fdOpen();
|
|
507
|
+
const at = this.#append ? null : this.#pos;
|
|
508
|
+
const n = hostCallSync(this.#path, () => nfs.writeSync(fd, data, 0, data.byteLength, at));
|
|
509
|
+
this.#pos = at === null ? hostCallSync(this.#path, () => nfs.fstatSync(fd)).size : at + n;
|
|
510
|
+
return n;
|
|
511
|
+
}
|
|
512
|
+
#seekTo(offset, base) {
|
|
513
|
+
const to = base + offset;
|
|
514
|
+
if (!Number.isSafeInteger(to) || to < 0)
|
|
515
|
+
throw new InvalidData(`${this.#path}: cannot seek to ${String(to)}`, "EINVAL", this.#path);
|
|
516
|
+
this.#pos = to;
|
|
517
|
+
return to;
|
|
518
|
+
}
|
|
519
|
+
/** The position moved by `offset` from the start, the position or the end; the new position, from the start. */
|
|
520
|
+
async seek(offset, whence = "start") {
|
|
521
|
+
const fd = this.#fdOpen();
|
|
522
|
+
if (whence !== "end")
|
|
523
|
+
return this.seekSync(offset, whence);
|
|
524
|
+
const s = await hostCall(this.#path, () => new Promise((res, rej) => nfs.fstat(fd, (e, st) => (e === null ? res(st) : rej(e)))));
|
|
525
|
+
return this.#seekTo(offset, s.size);
|
|
526
|
+
}
|
|
527
|
+
/** The same, without waiting. */
|
|
528
|
+
seekSync(offset, whence = "start") {
|
|
529
|
+
const fd = this.#fdOpen();
|
|
530
|
+
switch (whence) {
|
|
531
|
+
case "start":
|
|
532
|
+
return this.#seekTo(offset, 0);
|
|
533
|
+
case "current":
|
|
534
|
+
return this.#seekTo(offset, this.#pos);
|
|
535
|
+
case "end":
|
|
536
|
+
return this.#seekTo(offset, hostCallSync(this.#path, () => nfs.fstatSync(fd)).size);
|
|
537
|
+
default:
|
|
538
|
+
return absurd(whence);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
/** The file cut (or extended with zeros) to `length` bytes, 0 by default; the position is kept. */
|
|
542
|
+
async truncate(length = 0) {
|
|
543
|
+
const fd = this.#fdOpen();
|
|
544
|
+
await hostCall(this.#path, () => new Promise((res, rej) => nfs.ftruncate(fd, length, (e) => (e === null ? res() : rej(e)))));
|
|
545
|
+
}
|
|
546
|
+
/** The same, without waiting. */
|
|
547
|
+
truncateSync(length = 0) {
|
|
548
|
+
const fd = this.#fdOpen();
|
|
549
|
+
hostCallSync(this.#path, () => nfs.ftruncateSync(fd, length));
|
|
550
|
+
}
|
|
551
|
+
/** What the open file is. */
|
|
552
|
+
async stat() {
|
|
553
|
+
const fd = this.#fdOpen();
|
|
554
|
+
return info(await hostCall(this.#path, () => new Promise((res, rej) => nfs.fstat(fd, (e, s) => (e === null ? res(s) : rej(e))))));
|
|
555
|
+
}
|
|
556
|
+
/** The same, without waiting. */
|
|
557
|
+
statSync() {
|
|
558
|
+
const fd = this.#fdOpen();
|
|
559
|
+
return info(hostCallSync(this.#path, () => nfs.fstatSync(fd)));
|
|
560
|
+
}
|
|
561
|
+
/** The file's data and metadata written through to the disk. */
|
|
562
|
+
async sync() {
|
|
563
|
+
const fd = this.#fdOpen();
|
|
564
|
+
await hostCall(this.#path, () => new Promise((res, rej) => nfs.fsync(fd, (e) => (e === null ? res() : rej(e)))));
|
|
565
|
+
}
|
|
566
|
+
/** The same, without waiting. */
|
|
567
|
+
syncSync() {
|
|
568
|
+
const fd = this.#fdOpen();
|
|
569
|
+
hostCallSync(this.#path, () => nfs.fsyncSync(fd));
|
|
570
|
+
}
|
|
571
|
+
/** The file's bytes from the position to the end, as a stream that closes the file when it ends or is cancelled. */
|
|
572
|
+
get readable() {
|
|
573
|
+
this.#readable ??= new ReadableStream({
|
|
574
|
+
pull: async (controller) => {
|
|
575
|
+
const buffer = new Uint8Array(CHUNK);
|
|
576
|
+
const n = await this.read(buffer);
|
|
577
|
+
if (n === null) {
|
|
578
|
+
this.close();
|
|
579
|
+
controller.close();
|
|
580
|
+
}
|
|
581
|
+
else
|
|
582
|
+
controller.enqueue(buffer.subarray(0, n));
|
|
583
|
+
},
|
|
584
|
+
cancel: () => this.close(),
|
|
585
|
+
});
|
|
586
|
+
return this.#readable;
|
|
587
|
+
}
|
|
588
|
+
/** A stream whose chunks are written whole at the position, closing the file when it is closed or aborted. */
|
|
589
|
+
get writable() {
|
|
590
|
+
this.#writable ??= new WritableStream({
|
|
591
|
+
write: async (chunk) => {
|
|
592
|
+
for (let done = 0; done < chunk.byteLength;)
|
|
593
|
+
done += await this.write(chunk.subarray(done));
|
|
594
|
+
},
|
|
595
|
+
close: () => this.close(),
|
|
596
|
+
abort: () => this.close(),
|
|
597
|
+
});
|
|
598
|
+
return this.#writable;
|
|
599
|
+
}
|
|
600
|
+
/** The file closed; closing it again does nothing. */
|
|
601
|
+
close() {
|
|
602
|
+
if (this.#closed)
|
|
603
|
+
return;
|
|
604
|
+
this.#closed = true;
|
|
605
|
+
hostCallSync(this.#path, () => nfs.closeSync(this.#fd));
|
|
606
|
+
}
|
|
607
|
+
/** Closed at the end of a `using` block. */
|
|
608
|
+
[Symbol.dispose]() {
|
|
609
|
+
this.close();
|
|
610
|
+
}
|
|
611
|
+
/** Closed at the end of an `await using` block. */
|
|
612
|
+
[Symbol.asyncDispose]() {
|
|
613
|
+
this.close();
|
|
614
|
+
return Promise.resolve();
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const modeArg = (o) => (o.mode === undefined ? 0o666 : o.mode);
|
|
618
|
+
/** A file opened for reading where no options are given. `AlreadyExists` with `createNew`, `NotFound` without `create`. */
|
|
619
|
+
export async function open(path, options = { read: true }) {
|
|
620
|
+
const file = p(path);
|
|
621
|
+
const flags = openFlags(file, options);
|
|
622
|
+
const fd = await hostCall(file, () => new Promise((res, rej) => nfs.open(file, flags, modeArg(options), (e, d) => (e === null ? res(d) : rej(e)))));
|
|
623
|
+
await cutIfAsked(fd, file, options);
|
|
624
|
+
return new FsFile(fd, file, options.append === true);
|
|
625
|
+
}
|
|
626
|
+
/** The same, without waiting. */
|
|
627
|
+
export function openSync(path, options = { read: true }) {
|
|
628
|
+
const file = p(path);
|
|
629
|
+
const flags = openFlags(file, options);
|
|
630
|
+
const fd = hostCallSync(file, () => nfs.openSync(file, flags, modeArg(options)));
|
|
631
|
+
cutIfAskedSync(fd, file, options);
|
|
632
|
+
return new FsFile(fd, file, options.append === true);
|
|
633
|
+
}
|
|
634
|
+
// FR-050: copy and walk.
|
|
635
|
+
/** Names in the byte order of their UTF-8, so that an order is the same on every platform and runtime. */
|
|
636
|
+
const byBytes = (a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b));
|
|
637
|
+
const lstatOr = async (path) => {
|
|
638
|
+
try {
|
|
639
|
+
return await hostCall(path, () => fsp.lstat(path));
|
|
640
|
+
}
|
|
641
|
+
catch (e) {
|
|
642
|
+
if (e instanceof NotFound)
|
|
643
|
+
return undefined;
|
|
644
|
+
throw e;
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
async function copyAny(from, to, s, o) {
|
|
648
|
+
const there = await lstatOr(to);
|
|
649
|
+
if (there !== undefined && o.overwrite !== true)
|
|
650
|
+
throw new AlreadyExists(`copy: ${to} already exists`, "EEXIST", to);
|
|
651
|
+
if (s.isDirectory()) {
|
|
652
|
+
if (there !== undefined && !there.isDirectory())
|
|
653
|
+
throw new NotADirectory(`copy: ${to} is not a folder, and ${from} is`, "ENOTDIR", to);
|
|
654
|
+
if (there === undefined)
|
|
655
|
+
await hostCall(to, () => fsp.mkdir(to));
|
|
656
|
+
const names = (await hostCall(from, () => fsp.readdir(from))).sort(byBytes);
|
|
657
|
+
for (const name of names) {
|
|
658
|
+
const child = join(from, name);
|
|
659
|
+
await copyAny(child, join(to, name), await hostCall(child, () => fsp.lstat(child)), o);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
else if (s.isSymbolicLink()) {
|
|
663
|
+
if (there !== undefined)
|
|
664
|
+
await hostCall(to, () => fsp.rm(to, { recursive: true }));
|
|
665
|
+
const target = await hostCall(from, () => fsp.readlink(from));
|
|
666
|
+
// Windows makes a link to a folder and to a file differently; elsewhere the type is ignored (FR-018).
|
|
667
|
+
const pointsAtDir = await fsp.stat(from).then((t) => t.isDirectory(), () => false);
|
|
668
|
+
await hostCall(to, () => fsp.symlink(target, to, pointsAtDir ? "dir" : "file"));
|
|
669
|
+
if (o.preserveTimestamps === true)
|
|
670
|
+
await hostCall(to, () => fsp.lutimes(to, s.atime, s.mtime));
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
if (there?.isSymbolicLink() === true)
|
|
675
|
+
await hostCall(to, () => fsp.unlink(to));
|
|
676
|
+
await hostCall(from, () => fsp.copyFile(from, to));
|
|
677
|
+
}
|
|
678
|
+
if (process.platform !== "win32")
|
|
679
|
+
await hostCall(to, () => fsp.chmod(to, s.mode & 0o7777));
|
|
680
|
+
if (o.preserveTimestamps === true)
|
|
681
|
+
await hostCall(to, () => fsp.utimes(to, s.atime, s.mtime));
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* A file, a link or a folder with everything in it copied, as `@std/fs`'s `copy` does: a link is copied as a link, modes are kept (not on
|
|
685
|
+
* Windows, FR-018), and what is at the destination is `AlreadyExists` unless `overwrite`. A folder cannot be copied into itself.
|
|
686
|
+
*/
|
|
687
|
+
export async function copy(from, to, options = {}) {
|
|
688
|
+
const src = resolve(p(from));
|
|
689
|
+
const dest = resolve(p(to));
|
|
690
|
+
if (src === dest)
|
|
691
|
+
throw new InvalidData(`copy: ${src} is both the source and the destination`, "EINVAL", src);
|
|
692
|
+
const s = await hostCall(src, () => fsp.lstat(src));
|
|
693
|
+
const rel = relative(src, dest);
|
|
694
|
+
if (s.isDirectory() && rel !== "" && !rel.startsWith("..") && !isAbsolute(rel)) {
|
|
695
|
+
throw new InvalidData(`copy: ${dest} is inside ${src}`, "EINVAL", dest);
|
|
696
|
+
}
|
|
697
|
+
await copyAny(src, dest, s, options);
|
|
698
|
+
}
|
|
699
|
+
const slashed = (path) => (sep === "/" ? path : path.split(sep).join("/"));
|
|
700
|
+
function included(path, exts, match, skip) {
|
|
701
|
+
const s = slashed(path);
|
|
702
|
+
if (exts !== undefined && !exts.some((e) => s.endsWith(e)))
|
|
703
|
+
return false;
|
|
704
|
+
if (match !== undefined && !match.some((r) => s.match(r) !== null))
|
|
705
|
+
return false;
|
|
706
|
+
return !(skip !== undefined && skip.some((r) => s.match(r) !== null));
|
|
707
|
+
}
|
|
708
|
+
async function* walkIn(root, depth, o, exts, seen) {
|
|
709
|
+
const maxDepth = o.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
710
|
+
if (depth > maxDepth)
|
|
711
|
+
return;
|
|
712
|
+
if (o.includeDirs !== false && included(root, exts, o.match, o.skip)) {
|
|
713
|
+
const s = await hostCall(root, () => fsp.stat(root));
|
|
714
|
+
yield { path: root, name: basename(root), isFile: s.isFile(), isDirectory: s.isDirectory(), isSymlink: false };
|
|
715
|
+
}
|
|
716
|
+
if (depth >= maxDepth || !included(root, undefined, undefined, o.skip))
|
|
717
|
+
return;
|
|
718
|
+
const entries = (await hostCall(root, () => fsp.readdir(root, { withFileTypes: true }))).sort((a, b) => byBytes(a.name, b.name));
|
|
719
|
+
for (const d of entries) {
|
|
720
|
+
let path = join(root, d.name);
|
|
721
|
+
let isDirectory = d.isDirectory();
|
|
722
|
+
let isFile = d.isFile();
|
|
723
|
+
let inner = seen;
|
|
724
|
+
if (d.isSymbolicLink()) {
|
|
725
|
+
if (o.followSymlinks !== true) {
|
|
726
|
+
if (o.includeSymlinks !== false && included(path, exts, o.match, o.skip))
|
|
727
|
+
yield { path, ...entry(d) };
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
const real = await hostCall(path, () => fsp.realpath(path));
|
|
731
|
+
// A link back to a folder the walk is inside of would never end; it is given, not entered.
|
|
732
|
+
if (seen.has(real)) {
|
|
733
|
+
if (o.includeSymlinks !== false && included(path, exts, o.match, o.skip))
|
|
734
|
+
yield { path, ...entry(d) };
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
if (o.canonicalize !== false)
|
|
738
|
+
path = real;
|
|
739
|
+
const t = await hostCall(real, () => fsp.stat(real));
|
|
740
|
+
isDirectory = t.isDirectory();
|
|
741
|
+
isFile = t.isFile();
|
|
742
|
+
if (isDirectory)
|
|
743
|
+
inner = new Set([...seen, real]);
|
|
744
|
+
}
|
|
745
|
+
else if (isDirectory && o.followSymlinks === true)
|
|
746
|
+
inner = new Set([...seen, await hostCall(path, () => fsp.realpath(path))]);
|
|
747
|
+
if (isDirectory)
|
|
748
|
+
yield* walkIn(path, depth + 1, o, exts, inner);
|
|
749
|
+
else if (o.includeFiles !== false && included(path, exts, o.match, o.skip)) {
|
|
750
|
+
yield { path, name: d.name, isFile, isDirectory, isSymlink: d.isSymbolicLink() };
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Every entry under a folder, as `@std/fs`'s `walk` gives them (see {@link WalkOptions}), depth first and in the byte order of the names at
|
|
756
|
+
* each level, so a walk is the same every time; the root first, where folders are included; a file as the root is the walk's one entry.
|
|
757
|
+
* `NotFound` where the root is not there.
|
|
758
|
+
*/
|
|
759
|
+
export async function* walk(root, options = {}) {
|
|
760
|
+
const exts = options.exts?.map((e) => (e.startsWith(".") ? e : `.${e}`));
|
|
761
|
+
const base = p(root);
|
|
762
|
+
const s = await hostCall(base, () => fsp.stat(base));
|
|
763
|
+
if (!s.isDirectory()) {
|
|
764
|
+
// A file as the root is itself the walk.
|
|
765
|
+
if (options.includeFiles !== false && included(base, exts, options.match, options.skip)) {
|
|
766
|
+
yield { path: base, name: basename(base), isFile: s.isFile(), isDirectory: false, isSymlink: false };
|
|
767
|
+
}
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
const seen = options.followSymlinks === true ? new Set([await hostCall(base, () => fsp.realpath(base))]) : new Set();
|
|
771
|
+
yield* walkIn(base, 0, options, exts, seen);
|
|
772
|
+
}
|
|
773
|
+
const regExpOf = (glob, o) => {
|
|
774
|
+
const r = globToRegExp(glob, o);
|
|
775
|
+
if (r.kind === "err")
|
|
776
|
+
throw new InvalidData(formatGlobError(r.error), "EINVAL");
|
|
777
|
+
return r.value;
|
|
778
|
+
};
|
|
779
|
+
/**
|
|
780
|
+
* The entries a glob matches (`src/**\/*.ts`; see `../kernel/glob.ts`), as `@std/fs`'s `expandGlob` gives them, sorted in the byte order of
|
|
781
|
+
* their paths so the result is the same every time. A glob is written with `/`, also on Windows, and may be absolute (`/srv/*.log`,
|
|
782
|
+
* `C:/x/*`); a relative one is read from `root`. Entries are matched by their path relative to where the glob starts. A glob that begins
|
|
783
|
+
* with `!` is refused as `InvalidData`; a folder that is not there has no entries.
|
|
784
|
+
*/
|
|
785
|
+
export async function* expandGlob(glob, options = {}) {
|
|
786
|
+
const g = {
|
|
787
|
+
...(options.extended === undefined ? {} : { extended: options.extended }),
|
|
788
|
+
...(options.globstar === undefined ? {} : { globstar: options.globstar }),
|
|
789
|
+
...(options.caseInsensitive === undefined ? {} : { caseInsensitive: options.caseInsensitive }),
|
|
790
|
+
};
|
|
791
|
+
const absolute = isAbsolute(glob);
|
|
792
|
+
const base = absolute ? parse(glob).root : resolve(p(options.root ?? process.cwd()));
|
|
793
|
+
const pattern = absolute ? glob.slice(parse(glob).root.length) : glob;
|
|
794
|
+
const re = regExpOf(pattern, g);
|
|
795
|
+
const excluded = (options.exclude ?? []).map((x) => regExpOf(x, g));
|
|
796
|
+
const start = resolve(base, globRoot(pattern));
|
|
797
|
+
const rest = slashed(relative(base, start))
|
|
798
|
+
.split("/")
|
|
799
|
+
.filter((s) => s !== "").length;
|
|
800
|
+
const segments = pattern.split("/").filter((s) => s !== "");
|
|
801
|
+
const deep = segments.some((s) => s.includes("**")) || pattern.includes("{");
|
|
802
|
+
const out = [];
|
|
803
|
+
const walkOptions = {
|
|
804
|
+
includeDirs: options.includeDirs !== false,
|
|
805
|
+
followSymlinks: options.followSymlinks === true,
|
|
806
|
+
canonicalize: false,
|
|
807
|
+
...(deep ? {} : { maxDepth: Math.max(0, segments.length - rest) }),
|
|
808
|
+
};
|
|
809
|
+
try {
|
|
810
|
+
for await (const e of walk(start, walkOptions)) {
|
|
811
|
+
const rel = slashed(relative(base, e.path));
|
|
812
|
+
if (rel === "" || !re.test(rel))
|
|
813
|
+
continue;
|
|
814
|
+
const parts = rel.split("/");
|
|
815
|
+
if (excluded.some((x) => parts.some((_, i) => x.test(parts.slice(0, i + 1).join("/")))))
|
|
816
|
+
continue;
|
|
817
|
+
out.push({ rel, e });
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
catch (e) {
|
|
821
|
+
if (!(e instanceof NotFound))
|
|
822
|
+
throw e;
|
|
823
|
+
}
|
|
824
|
+
for (const x of out.sort((a, b) => byBytes(a.rel, b.rel)))
|
|
825
|
+
yield x.e;
|
|
826
|
+
}
|