@archildata/just-bash 0.8.18 → 0.8.20

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/index.js DELETED
@@ -1,652 +0,0 @@
1
- // src/ArchilFs.ts
2
- import createDebug from "debug";
3
- import { MAXIMUM_READ_SIZE } from "@archildata/native";
4
- var debug = createDebug("archil:fs");
5
- var ArchilFs = class _ArchilFs {
6
- client;
7
- user;
8
- rootInodeId = 1;
9
- constructor(client, options) {
10
- this.client = client;
11
- this.user = options?.user;
12
- }
13
- /**
14
- * Create an ArchilFs adapter, optionally rooted at a subdirectory.
15
- *
16
- * The subdirectory path is resolved eagerly: if any component does not
17
- * exist or is not a directory, this method throws immediately.
18
- *
19
- * @param client - Connected ArchilClient instance
20
- * @param options - Optional configuration
21
- * @param options.user - Unix user context for permission checks
22
- * @param options.subdirectory - Subdirectory to treat as the root of the filesystem
23
- */
24
- static async create(client, options) {
25
- const fs = new _ArchilFs(client, { user: options?.user });
26
- if (options?.subdirectory) {
27
- const resolved = await fs.resolveFollow(options.subdirectory);
28
- if (resolved.attributes.inodeType !== "Directory") {
29
- throw new Error(`ENOTDIR: subdirectory '${options.subdirectory}' is not a directory`);
30
- }
31
- fs.rootInodeId = resolved.inodeId;
32
- debug("resolved subdirectory '%s' to inode %d", options.subdirectory, fs.rootInodeId);
33
- }
34
- return fs;
35
- }
36
- // ========================================================================
37
- // Path Resolution
38
- // ========================================================================
39
- /**
40
- * Normalize a path (remove . and .., ensure leading /)
41
- */
42
- normalizePath(path) {
43
- if (!path || path === "") {
44
- return "/";
45
- }
46
- if (!path.startsWith("/")) {
47
- path = "/" + path;
48
- }
49
- const parts = path.split("/").filter((p) => p !== "" && p !== ".");
50
- const result = [];
51
- for (const part of parts) {
52
- if (part === "..") {
53
- result.pop();
54
- } else {
55
- result.push(part);
56
- }
57
- }
58
- return "/" + result.join("/");
59
- }
60
- static MAX_SYMLINKS = 40;
61
- /**
62
- * Resolve a path to its inode ID, walking the directory tree.
63
- * Follows symlinks on intermediate components but NOT on the final
64
- * component (matching lstat/readlink semantics).
65
- */
66
- async resolve(path, symlinkDepth = 0) {
67
- const normalizedPath = this.normalizePath(path);
68
- const parts = normalizedPath.split("/").filter((p) => p !== "");
69
- let currentInodeId = this.rootInodeId;
70
- let resolvedPath = "/";
71
- for (let i = 0; i < parts.length; i++) {
72
- const part = parts[i];
73
- const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
74
- if (response === null) {
75
- throw new Error(`ENOENT: no such file or directory, '${path}'`);
76
- }
77
- const isLast = i === parts.length - 1;
78
- if (!isLast && response.attributes.inodeType === "Symlink" && response.attributes.symlinkTarget) {
79
- if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
80
- throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
81
- }
82
- const targetPath = response.attributes.symlinkTarget.startsWith("/") ? response.attributes.symlinkTarget : this.resolvePath(resolvedPath, response.attributes.symlinkTarget);
83
- const remaining = parts.slice(i + 1).join("/");
84
- const fullPath = remaining ? targetPath + "/" + remaining : targetPath;
85
- return this.resolve(fullPath, symlinkDepth + 1);
86
- }
87
- resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
88
- currentInodeId = response.inodeId;
89
- }
90
- const attributes = await this.client.getAttributes(currentInodeId, { user: this.user });
91
- return { inodeId: currentInodeId, attributes };
92
- }
93
- /**
94
- * Resolve a path, following symlinks on ALL components including the
95
- * final one (like stat(2)). Use resolve() when you need lstat semantics.
96
- */
97
- async resolveFollow(path, symlinkDepth = 0) {
98
- if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
99
- throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
100
- }
101
- const resolved = await this.resolve(path, symlinkDepth);
102
- if (resolved.attributes.inodeType === "Symlink" && resolved.attributes.symlinkTarget) {
103
- const targetPath = resolved.attributes.symlinkTarget.startsWith("/") ? resolved.attributes.symlinkTarget : this.resolvePath(path, "..", resolved.attributes.symlinkTarget);
104
- return this.resolveFollow(targetPath, symlinkDepth + 1);
105
- }
106
- return resolved;
107
- }
108
- /**
109
- * Resolve parent directory and get child name
110
- */
111
- async resolveParent(path) {
112
- debug("resolveParent raw path=%j (bytes: %o)", path, Buffer.from(path));
113
- const normalizedPath = this.normalizePath(path);
114
- const lastSlash = normalizedPath.lastIndexOf("/");
115
- const parentPath = lastSlash === 0 ? "/" : normalizedPath.substring(0, lastSlash);
116
- const name = normalizedPath.substring(lastSlash + 1);
117
- debug("resolveParent extracted name=%j (bytes: %o)", name, Buffer.from(name));
118
- const { inodeId: parentInodeId } = await this.resolve(parentPath);
119
- return { parentInodeId, name };
120
- }
121
- /**
122
- * Convert InodeAttributes to FsStat
123
- */
124
- toStat(attrs) {
125
- return {
126
- isFile: attrs.inodeType === "File",
127
- isDirectory: attrs.inodeType === "Directory",
128
- isSymbolicLink: attrs.inodeType === "Symlink",
129
- mode: attrs.mode,
130
- size: Number(attrs.size),
131
- mtime: new Date(attrs.mtimeMs)
132
- };
133
- }
134
- static DIR_PAGE_SIZE = 1e3;
135
- /**
136
- * Read all directory entries using the paginated API.
137
- */
138
- async readAllDirectoryEntries(inodeId) {
139
- const handle = await this.client.openDirectory(inodeId, { user: this.user });
140
- try {
141
- const allEntries = [];
142
- let cursor;
143
- for (; ; ) {
144
- const page = await this.client.readDirectory(
145
- inodeId,
146
- handle,
147
- _ArchilFs.DIR_PAGE_SIZE,
148
- cursor,
149
- { user: this.user }
150
- );
151
- allEntries.push(...page.entries);
152
- if (!page.nextCursor) break;
153
- cursor = page.nextCursor;
154
- }
155
- return allEntries;
156
- } finally {
157
- this.client.closeDirectory(inodeId, handle);
158
- }
159
- }
160
- // ========================================================================
161
- // IFileSystem Implementation - Read Operations
162
- // ========================================================================
163
- resolvePath(base, ...paths) {
164
- debug("resolvePath base=%s paths=%o", base, paths);
165
- let result = base;
166
- for (const p of paths) {
167
- if (p.startsWith("/")) {
168
- result = p;
169
- } else {
170
- result = result.endsWith("/") ? result + p : result + "/" + p;
171
- }
172
- }
173
- const normalized = this.normalizePath(result);
174
- debug("resolvePath result=%s", normalized);
175
- return normalized;
176
- }
177
- async readFile(path, encoding) {
178
- debug("readFile path=%s encoding=%s", path, encoding);
179
- try {
180
- const buffer = await this.readFileBuffer(path);
181
- debug("readFile got buffer length=%d", buffer.length);
182
- const enc = encoding || "utf-8";
183
- let result;
184
- if (enc === "base64" || enc === "hex" || enc === "binary") {
185
- result = Buffer.from(buffer).toString(enc);
186
- } else {
187
- result = new TextDecoder(enc).decode(buffer);
188
- }
189
- debug("readFile decoded to string length=%d", result.length);
190
- return result;
191
- } catch (err) {
192
- debug("readFile FAILED: %O", err);
193
- throw err;
194
- }
195
- }
196
- async readFileBuffer(path) {
197
- debug("readFileBuffer path=%s", path);
198
- const { inodeId, attributes } = await this.resolveFollow(path);
199
- debug("readFileBuffer resolved inodeId=%d type=%s size=%d", inodeId, attributes.inodeType, attributes.size);
200
- if (attributes.inodeType !== "File") {
201
- throw new Error(`EISDIR: illegal operation on a directory, read '${path}'`);
202
- }
203
- const size = Number(attributes.size);
204
- if (size === 0) {
205
- debug("readFileBuffer file is empty, returning empty buffer");
206
- return new Uint8Array(0);
207
- }
208
- if (size <= MAXIMUM_READ_SIZE) {
209
- const buffer = await this.client.readInode(inodeId, 0, size, { user: this.user });
210
- return new Uint8Array(buffer);
211
- }
212
- const result = new Uint8Array(size);
213
- let offset = 0;
214
- while (offset < size) {
215
- const chunkSize = Math.min(MAXIMUM_READ_SIZE, size - offset);
216
- const chunk = await this.client.readInode(inodeId, offset, chunkSize, { user: this.user });
217
- result.set(new Uint8Array(chunk), offset);
218
- offset += chunkSize;
219
- }
220
- return result;
221
- }
222
- async readdir(path) {
223
- debug("readdir path=%s", path);
224
- const { inodeId, attributes } = await this.resolveFollow(path);
225
- if (attributes.inodeType !== "Directory") {
226
- throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
227
- }
228
- const entries = await this.readAllDirectoryEntries(inodeId);
229
- for (const e of entries) {
230
- debug("readdir entry name=%j (bytes: %o)", e.name, Buffer.from(e.name));
231
- }
232
- return entries.map((e) => e.name).filter((name) => name !== "." && name !== "..");
233
- }
234
- async readdirWithFileTypes(path) {
235
- const { inodeId, attributes } = await this.resolveFollow(path);
236
- if (attributes.inodeType !== "Directory") {
237
- throw new Error(`ENOTDIR: not a directory, scandir '${path}'`);
238
- }
239
- const entries = await this.readAllDirectoryEntries(inodeId);
240
- return entries.filter((e) => e.name !== "." && e.name !== "..").map((e) => ({
241
- name: e.name,
242
- isFile: e.inodeType === "File",
243
- isDirectory: e.inodeType === "Directory",
244
- isSymbolicLink: e.inodeType === "Symlink"
245
- }));
246
- }
247
- async stat(path) {
248
- debug("stat path=%s", path);
249
- const { attributes } = await this.resolveFollow(path);
250
- return this.toStat(attributes);
251
- }
252
- async lstat(path) {
253
- debug("lstat path=%s", path);
254
- const { attributes } = await this.resolve(path);
255
- return this.toStat(attributes);
256
- }
257
- async exists(path) {
258
- debug("exists path=%s", path);
259
- try {
260
- await this.resolve(path);
261
- debug("exists path=%s -> true", path);
262
- return true;
263
- } catch {
264
- debug("exists path=%s -> false", path);
265
- return false;
266
- }
267
- }
268
- async readlink(path) {
269
- const { attributes } = await this.resolve(path);
270
- if (attributes.inodeType !== "Symlink") {
271
- throw new Error(`EINVAL: invalid argument, readlink '${path}'`);
272
- }
273
- return attributes.symlinkTarget || "";
274
- }
275
- async realpath(path, symlinkDepth = 0) {
276
- const normalizedPath = this.normalizePath(path);
277
- const parts = normalizedPath.split("/").filter((p) => p !== "");
278
- let resolvedPath = "/";
279
- let currentInodeId = this.rootInodeId;
280
- for (const part of parts) {
281
- const response = await this.client.lookupInode(currentInodeId, part, { user: this.user });
282
- if (response === null) {
283
- throw new Error(`ENOENT: no such file or directory, realpath '${path}'`);
284
- }
285
- const attrs = response.attributes;
286
- if (attrs.inodeType === "Symlink" && attrs.symlinkTarget) {
287
- if (symlinkDepth >= _ArchilFs.MAX_SYMLINKS) {
288
- throw new Error(`ELOOP: too many levels of symbolic links, '${path}'`);
289
- }
290
- const targetPath = attrs.symlinkTarget.startsWith("/") ? attrs.symlinkTarget : this.resolvePath(resolvedPath, attrs.symlinkTarget);
291
- const resolved = await this.realpath(targetPath, symlinkDepth + 1);
292
- resolvedPath = resolved;
293
- const { inodeId } = await this.resolve(resolved);
294
- currentInodeId = inodeId;
295
- } else {
296
- resolvedPath = resolvedPath === "/" ? "/" + part : resolvedPath + "/" + part;
297
- currentInodeId = response.inodeId;
298
- }
299
- }
300
- return resolvedPath;
301
- }
302
- // ========================================================================
303
- // IFileSystem Implementation - Write Operations
304
- // ========================================================================
305
- async writeFile(path, content) {
306
- debug("writeFile path=%s contentLength=%d", path, content.length);
307
- const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
308
- let resolved;
309
- try {
310
- resolved = await this.resolveFollow(path);
311
- } catch (err) {
312
- if (err instanceof Error && err.message.includes("ENOENT")) {
313
- resolved = null;
314
- } else {
315
- throw err;
316
- }
317
- }
318
- if (resolved !== null) {
319
- debug("writeFile resolved existing file path=%s inodeId=%d", path, resolved.inodeId);
320
- if (resolved.attributes.inodeType !== "File") {
321
- throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
322
- }
323
- const realPath = await this.realpath(path);
324
- const { parentInodeId: parentInodeId2, name: name2 } = await this.resolveParent(realPath);
325
- const tmpName = `.~tmp-${Math.random().toString(36).slice(2)}`;
326
- debug("writeFile atomic overwrite via temp=%s", tmpName);
327
- const tmpResult = await this.client.create(
328
- parentInodeId2,
329
- tmpName,
330
- {
331
- inodeType: "File",
332
- uid: resolved.attributes.uid,
333
- gid: resolved.attributes.gid,
334
- mode: resolved.attributes.mode
335
- },
336
- { user: this.user }
337
- );
338
- try {
339
- await this.client.writeData(tmpResult.inodeId, 0, Buffer.from(data), { user: this.user });
340
- await this.client.rename(parentInodeId2, tmpName, parentInodeId2, name2, { user: this.user });
341
- debug("writeFile atomic overwrite succeeded");
342
- } catch (writeErr) {
343
- try {
344
- await this.client.unlink(parentInodeId2, tmpName, { user: this.user });
345
- } catch {
346
- }
347
- throw writeErr;
348
- }
349
- return;
350
- }
351
- debug("writeFile file doesn't exist, creating: %s", path);
352
- const { parentInodeId, name } = await this.resolveParent(path);
353
- debug("writeFile resolved parent parentInodeId=%d name=%s", parentInodeId, name);
354
- const result = await this.client.create(
355
- parentInodeId,
356
- name,
357
- {
358
- inodeType: "File",
359
- uid: this.user?.uid ?? 0,
360
- gid: this.user?.gid ?? 0,
361
- mode: 420
362
- },
363
- { user: this.user }
364
- );
365
- debug("writeFile create succeeded inodeId=%d", result.inodeId);
366
- await this.client.writeData(result.inodeId, 0, Buffer.from(data), { user: this.user });
367
- debug("writeFile write succeeded");
368
- }
369
- async appendFile(path, content) {
370
- const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
371
- let inodeId;
372
- let size;
373
- try {
374
- const resolved = await this.resolveFollow(path);
375
- if (resolved.attributes.inodeType !== "File") {
376
- throw new Error(`EISDIR: illegal operation on a directory, write '${path}'`);
377
- }
378
- inodeId = resolved.inodeId;
379
- size = Number(resolved.attributes.size);
380
- } catch (err) {
381
- if (err instanceof Error && err.message.includes("ENOENT")) {
382
- await this.writeFile(path, data);
383
- return;
384
- }
385
- throw err;
386
- }
387
- await this.client.writeData(inodeId, size, Buffer.from(data), { user: this.user });
388
- }
389
- async mkdir(path, options) {
390
- const normalizedPath = this.normalizePath(path);
391
- if (options?.recursive) {
392
- const parts = normalizedPath.split("/").filter((p) => p !== "");
393
- let currentPath = "";
394
- for (const part of parts) {
395
- currentPath += "/" + part;
396
- const exists = await this.exists(currentPath);
397
- if (exists) {
398
- continue;
399
- }
400
- await this.mkdirSingle(currentPath);
401
- }
402
- } else {
403
- await this.mkdirSingle(normalizedPath);
404
- }
405
- }
406
- async mkdirSingle(path) {
407
- const { parentInodeId, name } = await this.resolveParent(path);
408
- await this.client.create(
409
- parentInodeId,
410
- name,
411
- {
412
- inodeType: "Directory",
413
- uid: this.user?.uid ?? 0,
414
- gid: this.user?.gid ?? 0,
415
- mode: 493
416
- },
417
- { user: this.user }
418
- );
419
- }
420
- async rm(path, options) {
421
- let resolved;
422
- try {
423
- resolved = await this.resolve(path);
424
- } catch (err) {
425
- if (err instanceof Error && err.message.includes("ENOENT")) {
426
- if (options?.force) {
427
- return;
428
- }
429
- throw err;
430
- }
431
- throw err;
432
- }
433
- if (resolved.attributes.inodeType === "Directory") {
434
- if (!options?.recursive) {
435
- throw new Error(`EISDIR: illegal operation on a directory, rm '${path}'`);
436
- }
437
- const entries = await this.readdir(path);
438
- for (const entry of entries) {
439
- await this.rm(this.resolvePath(path, entry), options);
440
- }
441
- }
442
- const { parentInodeId, name } = await this.resolveParent(path);
443
- await this.client.unlink(parentInodeId, name, { user: this.user });
444
- }
445
- async cp(src, dest, options) {
446
- const srcResolved = await this.resolveFollow(src);
447
- if (srcResolved.attributes.inodeType === "Directory") {
448
- if (!options?.recursive) {
449
- throw new Error(`EISDIR: illegal operation on a directory, cp '${src}'`);
450
- }
451
- await this.mkdir(dest, { recursive: true });
452
- const entries = await this.readdir(src);
453
- for (const entry of entries) {
454
- await this.cp(
455
- this.resolvePath(src, entry),
456
- this.resolvePath(dest, entry),
457
- options
458
- );
459
- }
460
- } else {
461
- const content = await this.readFileBuffer(src);
462
- await this.writeFile(dest, content);
463
- }
464
- }
465
- async mv(src, dest) {
466
- const { parentInodeId: srcParentInodeId, name: srcName } = await this.resolveParent(src);
467
- const { parentInodeId: destParentInodeId, name: destName } = await this.resolveParent(dest);
468
- await this.client.rename(
469
- srcParentInodeId,
470
- srcName,
471
- destParentInodeId,
472
- destName,
473
- { user: this.user }
474
- );
475
- }
476
- async symlink(target, path) {
477
- const { parentInodeId, name } = await this.resolveParent(path);
478
- await this.client.create(
479
- parentInodeId,
480
- name,
481
- {
482
- inodeType: "Symlink",
483
- uid: this.user?.uid ?? 0,
484
- gid: this.user?.gid ?? 0,
485
- mode: 511,
486
- symlinkTarget: target
487
- },
488
- { user: this.user }
489
- );
490
- }
491
- async link(existingPath, newPath) {
492
- throw new Error(
493
- "Hard link operations not yet implemented. The archil-node bindings need to expose link for hard links."
494
- );
495
- }
496
- async chmod(path, mode) {
497
- const { inodeId } = await this.resolveFollow(path);
498
- await this.client.setattr(inodeId, { mode }, { user: this.user ?? { uid: 0, gid: 0 } });
499
- }
500
- async utimes(path, atime, mtime) {
501
- debug("utimes path=%s atime=%s mtime=%s", path, atime.toISOString(), mtime.toISOString());
502
- const { inodeId } = await this.resolveFollow(path);
503
- debug("utimes resolved path=%s inodeId=%d", path, inodeId);
504
- const atimeMs = atime.getTime();
505
- const mtimeMs = mtime.getTime();
506
- debug("utimes calling setattr inodeId=%d atimeMs=%d mtimeMs=%d", inodeId, atimeMs, mtimeMs);
507
- await this.client.setattr(
508
- inodeId,
509
- { atimeMs, mtimeMs },
510
- { user: this.user ?? { uid: 0, gid: 0 } }
511
- );
512
- debug("utimes setattr succeeded inodeId=%d", inodeId);
513
- }
514
- // ========================================================================
515
- // Utility Methods
516
- // ========================================================================
517
- getAllPaths() {
518
- return [];
519
- }
520
- /**
521
- * Resolve a path to its inode ID (public wrapper for delegation operations)
522
- */
523
- async resolveInodeId(path) {
524
- const { inodeId } = await this.resolve(path);
525
- return inodeId;
526
- }
527
- };
528
-
529
- // src/commands.ts
530
- import { defineCommand } from "just-bash";
531
- function createArchilCommand(client, fs) {
532
- return defineCommand("archil", async (args, ctx) => {
533
- const ok = (stdout) => ({ stdout, stderr: "", exitCode: 0 });
534
- const fail = (stderr) => ({ stdout: "", stderr, exitCode: 1 });
535
- const subcommand = args[0];
536
- if (subcommand === "checkout") {
537
- const rest = args.slice(1);
538
- const force = rest.includes("--force") || rest.includes("-f");
539
- const pathArg = rest.find((a) => !a.startsWith("-"));
540
- if (!pathArg) {
541
- return fail("Usage: archil checkout [--force|-f] <path>\n");
542
- }
543
- const fullPath = fs.resolvePath(ctx.cwd, pathArg);
544
- try {
545
- const inodeId = await fs.resolveInodeId(fullPath);
546
- await client.checkout(inodeId, { force });
547
- return ok(`Checked out: ${fullPath} (inode ${inodeId})${force ? " (forced)" : ""}
548
- `);
549
- } catch (err) {
550
- return fail(`Failed to checkout ${fullPath}: ${err instanceof Error ? err.message : err}
551
- `);
552
- }
553
- }
554
- if (subcommand === "checkin") {
555
- const pathArg = args[1];
556
- if (!pathArg) {
557
- return fail("Usage: archil checkin <path>\n");
558
- }
559
- const fullPath = fs.resolvePath(ctx.cwd, pathArg);
560
- try {
561
- const inodeId = await fs.resolveInodeId(fullPath);
562
- await client.checkin(inodeId);
563
- return ok(`Checked in: ${fullPath} (inode ${inodeId})
564
- `);
565
- } catch (err) {
566
- return fail(`Failed to checkin ${fullPath}: ${err instanceof Error ? err.message : err}
567
- `);
568
- }
569
- }
570
- if (subcommand === "list-delegations" || subcommand === "delegations") {
571
- try {
572
- const delegations = client.listDelegations();
573
- if (delegations.length === 0) {
574
- return ok("No delegations held\n");
575
- }
576
- const lines = delegations.map((d) => ` inode ${d.inodeId}: ${d.state}`);
577
- return ok("Delegations:\n" + lines.join("\n") + "\n");
578
- } catch (err) {
579
- return fail(`Failed to list delegations: ${err instanceof Error ? err.message : err}
580
- `);
581
- }
582
- }
583
- if (subcommand === "invalidate-cache") {
584
- const pathArg = args.slice(1).find((a) => !a.startsWith("-"));
585
- let scopeLabel = "(mount-wide)";
586
- if (pathArg) {
587
- const fullPath = fs.resolvePath(ctx.cwd, pathArg);
588
- try {
589
- await fs.resolveInodeId(fullPath);
590
- scopeLabel = `(via ${fullPath})`;
591
- } catch (err) {
592
- return fail(`Failed to resolve ${fullPath}: ${err instanceof Error ? err.message : err}
593
- `);
594
- }
595
- }
596
- try {
597
- const stats = client.invalidateCache();
598
- return ok(
599
- `Invalidated cache ${scopeLabel}: ${stats.totalEvictedInodes} inodes evicted (attr=${stats.attribute}, xattr=${stats.extendedAttribute}, dirent=${stats.dirent}, file=${stats.fileData})
600
- `
601
- );
602
- } catch (err) {
603
- return fail(`Failed to invalidate cache: ${err instanceof Error ? err.message : err}
604
- `);
605
- }
606
- }
607
- if (subcommand === "set-cache-expiry") {
608
- const rest = args.slice(1);
609
- const flagIdx = rest.indexOf("--readdir-expiry");
610
- if (flagIdx === -1 || flagIdx === rest.length - 1) {
611
- return fail("Usage: archil set-cache-expiry <path> --readdir-expiry <seconds>\n");
612
- }
613
- const flagValueIdx = flagIdx + 1;
614
- const expirySeconds = Number(rest[flagValueIdx]);
615
- if (!Number.isInteger(expirySeconds) || expirySeconds < 0) {
616
- return fail("--readdir-expiry must be a non-negative integer (seconds)\n");
617
- }
618
- const pathArg = rest.find((a, i) => i !== flagIdx && i !== flagValueIdx && !a.startsWith("-"));
619
- if (!pathArg) {
620
- return fail("Usage: archil set-cache-expiry <path> --readdir-expiry <seconds>\n");
621
- }
622
- const fullPath = fs.resolvePath(ctx.cwd, pathArg);
623
- try {
624
- const inodeId = await fs.resolveInodeId(fullPath);
625
- client.setCacheExpiry(inodeId, expirySeconds);
626
- return ok(`Set readdir cache expiry for ${fullPath} (inode ${inodeId}) to ${expirySeconds}s
627
- `);
628
- } catch (err) {
629
- return fail(`Failed to set cache expiry for ${fullPath}: ${err instanceof Error ? err.message : err}
630
- `);
631
- }
632
- }
633
- if (subcommand === "help" || !subcommand) {
634
- return ok(
635
- "Archil commands:\n archil checkout [--force|-f] <path> - Acquire write delegation\n archil checkin <path> - Release write delegation\n archil list-delegations - Show held delegations\n archil invalidate-cache [<path>] - Drop every evictable cache entry\n archil set-cache-expiry <path> --readdir-expiry <secs>\n - Set readdir cache TTL on a directory\n archil help - Show this help message\n\nThe --force flag revokes any existing delegation from other clients.\n"
636
- );
637
- }
638
- return fail(`Unknown archil command: ${subcommand}
639
- Run 'archil help' for available commands
640
- `);
641
- });
642
- }
643
-
644
- // src/index.ts
645
- async function createArchilFs(client, options) {
646
- return ArchilFs.create(client, options);
647
- }
648
- export {
649
- ArchilFs,
650
- createArchilCommand,
651
- createArchilFs
652
- };