@better-fs-tools/node 0.2.0 → 0.3.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @better-fs-tools/node
2
2
 
3
- The Node filesystem for Better FS Tools, a SHA-256 digest, `createNodeReadTool()`, the zero-config local read tool, and `createNodeFsTools()`, which adds `edit` and `write` over the same filesystem, and a read store, `apply_patch`, and `bash` when you ask for them. It works on Node 24 or later, and on Bun.
3
+ The Node backends for Better FS Tools: the local filesystem, a SHA-256 digest on `node:crypto`, and a local command runner. Pass `nodeFileSystem()` as `fs` to any tool or bundle, such as `createReadTool()` or `createFsTools()`, and `nodeCommandRunner()` as the bash `runner`. It works on Node 24 or later, and on Bun.
4
4
 
5
5
  ## Install
6
6
 
@@ -13,15 +13,20 @@ The package has no peers. It needs Node 24 or later (`engines.node` is `>=24`),
13
13
  ## Example
14
14
 
15
15
  ```ts
16
- import { createNodeReadTool, nodeFileSystem } from "@better-fs-tools/node";
17
- import { textOf } from "@better-fs-tools/read";
18
-
19
- // Zero config: rooted at process.cwd(), with SHA-256 observations.
20
- const read = createNodeReadTool();
16
+ import { nodeDigest, nodeFileSystem } from "@better-fs-tools/node";
17
+ import { createReadTool, textOf } from "@better-fs-tools/read";
18
+
19
+ // The local filesystem, rooted at process.cwd(). nodeDigest() is optional: it
20
+ // gives the same values as the default sha256Digest(), faster, on node:crypto.
21
+ const cwd = process.cwd();
22
+ const read = createReadTool({
23
+ fs: nodeFileSystem({ cwd, allowedRoots: [cwd] }),
24
+ digest: nodeDigest(),
25
+ });
21
26
  console.log(textOf(await read({ path: "package.json", limit: 1 }))); // "1|{" and a continue note
22
27
 
23
28
  // Your own roots and policy.
24
- export const docsOnly = createNodeReadTool({
29
+ export const docsOnly = createReadTool({
25
30
  fs: nodeFileSystem({
26
31
  cwd: "/srv/app",
27
32
  allowedRoots: ["/srv/app/docs"],
@@ -31,17 +36,18 @@ export const docsOnly = createNodeReadTool({
31
36
  });
32
37
  ```
33
38
 
34
- All four tools, with one read store, so `edit` and `write` need a read first. The store and `apply_patch` are opt-in:
39
+ All four tools over one `nodeFileSystem`, with `createFsTools()`. The read store makes `edit` and `write` need a read first. The store and `apply_patch` are opt-in:
35
40
 
36
41
  ```ts
37
- import { createNodeFsTools } from "@better-fs-tools/node";
42
+ import { nodeFileSystem } from "@better-fs-tools/node";
38
43
  import { memoryStore, textOf } from "@better-fs-tools/read";
39
- import { protectPaths } from "@better-fs-tools/write";
44
+ import { createFsTools, protectPaths } from "@better-fs-tools/write";
40
45
 
41
46
  // read, edit, write, and apply_patch over one nodeFileSystem rooted at
42
47
  // process.cwd(), with one read store, one SHA-256 digest, and one lock manager.
43
48
  // The read store and apply_patch are opt-in.
44
- const tools = createNodeFsTools({
49
+ const tools = createFsTools({
50
+ fs: nodeFileSystem({ allowedRoots: [process.cwd()] }),
45
51
  state: memoryStore(),
46
52
  edit: { authorize: protectPaths() },
47
53
  write: { authorize: protectPaths() },
@@ -64,15 +70,11 @@ export async function afterShell(path: string): Promise<boolean> {
64
70
 
65
71
  ## Contents
66
72
 
67
- | Export | Use |
68
- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
69
- | `createNodeReadTool(options?)` | `createReadTool()` from `@better-fs-tools/read` with Node defaults. `fs` defaults to `nodeFileSystem` rooted at `process.cwd()`. `digest` defaults to `nodeDigest()`. Pass `digest: null` to turn observations off. Every other option goes to the core. |
70
- | `createNodeFsTools(options?)` | `createFsTools()` from `@better-fs-tools/write` over one `nodeFileSystem`, with `nodeDigest()`: `read`, `edit`, and `write` share one digest, one `memoryLocks()`, and one clock. Options: `cwd`, `allowedRoots` (default `[cwd]`), `denyRoots`, `symlinks`, `hardLinks`, `newFileMode`, `newDirectoryMode`, `state` (default `null`, so read-before-write is off; `memoryStore()` turns it on), `digest`, `locks`, `clock`, per-tool options under `read`, `edit`, and `write`, `applyPatch`, and `bash`. `apply_patch` is off unless `applyPatch` is `true` or an options object. Bash is off unless `bash` is `true` or an options object. The result also has `applyPatch` and `bash` (`null` when off), `fs`, `state`, `digest`, `locks`, `clock`, and `invalidate(path, call?)`, which returns an `InvalidateOutcome`. The Node filesystem is fixed, so `call` is not needed. An unknown option key, or `fs`, `state`, `digest`, `locks`, or `clock` inside a per-tool object (`digest` or `clock` inside `bash`), throws `TypeError` |
71
- | `createNodeEditTool(deps?)`, `createNodeWriteTool(deps?)`, `createNodeApplyPatchTool(deps?)` | One write tool each. `fs` defaults to `nodeFileSystem` rooted at `process.cwd()`, `digest` to `nodeDigest()`. `state` stays `null`, so read-before-write is off |
72
- | `createNodeBashTool(deps?)` | The local `bash` tool. See [Bash tool](#bash-tool) |
73
- | `nodeCommandRunner(options?)` | The command runner that `createNodeBashTool()` uses: `bash -c` in its own process group. POSIX only |
74
- | `nodeFileSystem(options)` | A `WritableFileSystem` over Node file descriptors |
75
- | `nodeDigest()` | SHA-256 over `node:crypto`. Values look like `sha256:<hex>`, the same as `sha256Digest()` from `@better-fs-tools/read`. |
73
+ | Export | Use |
74
+ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
75
+ | `nodeFileSystem(options)` | A `WritableFileSystem` over Node file descriptors |
76
+ | `nodeCommandRunner(options?)` | A bash `CommandRunner`: `bash -c` in its own process group. POSIX only. See [Bash tool](#bash-tool) |
77
+ | `nodeDigest()` | SHA-256 over `node:crypto`. Values look like `sha256:<hex>`, the same as the default `sha256Digest()` from `@better-fs-tools/read`, so records made with one are valid for the other. Faster |
76
78
 
77
79
  `nodeFileSystem` options. The first six are the shared `FileSystemRootOptions` from `@better-fs-tools/fs`. The filesystem also exposes the resolved `cwd`, `allowedRoots`, `denyRoots`, `symlinks`, and `identity`:
78
80
 
@@ -107,22 +109,25 @@ A write goes to a `0o600` temp file next to the target. The bytes are written an
107
109
 
108
110
  ## Bash tool
109
111
 
110
- `createNodeBashTool()` is the zero-config local `bash` tool from [`@better-fs-tools/shell`](https://www.npmjs.com/package/@better-fs-tools/shell). It uses `nodeCommandRunner()`: `bash -c` in its own process group, stdin on `/dev/null`, SIGTERM to the group on a stop, and SIGKILL after the grace time. The environment is `process.env`, read on each call, with the pager and colour defaults over it. `createNodeFsTools()` starts no process unless you ask: `bash: true`, or a `bash` options object, adds the same tool as `bash`, in the same cwd, with the bundle's digest and clock. Its `env` also defaults to `process.env`, since a host that turns on a local bash in a Node bundle asks for a local shell. Pass `env` to narrow it, for example `shellEnv({ PATH: process.env.PATH })`. With your own `bash.runner`, an explicit `cwd` still applies: it becomes the bash `cwd` dependency. `bash.cwd` wins over both. `nodeCommandRunner({ cwd })` resolves a relative `cwd` against `process.cwd()`, as `nodeFileSystem` does. So does the `cwd` dependency of `createNodeBashTool({ cwd })` and `bash.cwd`; the portable core keeps its absolute rule.
112
+ `nodeCommandRunner()` runs `bash -c` in its own process group, with stdin on `/dev/null`. A stop sends SIGTERM to the group, and SIGKILL after the grace time. `nodeCommandRunner({ cwd })` resolves a relative `cwd` against `process.cwd()`, as `nodeFileSystem` does. Give it to `createBashTool()` from [`@better-fs-tools/shell`](https://www.npmjs.com/package/@better-fs-tools/shell), or to the `bash` options of a bundle, with an `env`. `shellEnv(() => process.env)` passes `process.env`, read on each call, with the pager and colour defaults over it. Pass a smaller `env` to narrow it, for example `shellEnv({ PATH: process.env.PATH })`. In a bundle, the bash tool gets the digest and clock of the set.
111
113
 
112
114
  ```ts
113
- import { createNodeFsTools, nodeCommandRunner } from "@better-fs-tools/node";
114
- import type { NodeFsToolsWithBash } from "@better-fs-tools/node";
115
+ import { nodeCommandRunner, nodeFileSystem } from "@better-fs-tools/node";
115
116
  import { memoryStore } from "@better-fs-tools/read";
117
+ import { shellEnv } from "@better-fs-tools/shell";
118
+ import { createFsTools } from "@better-fs-tools/write";
119
+ import type { FsToolsWithBash } from "@better-fs-tools/write";
116
120
 
117
121
  // bash is created only because `bash` is set. With a read store, the hook makes
118
122
  // the next edit of package.json need a read first, since a command may have
119
123
  // changed it.
120
- let tools: NodeFsToolsWithBash | undefined;
121
- tools = createNodeFsTools({
122
- cwd: "/srv/project",
124
+ let tools: FsToolsWithBash | undefined;
125
+ tools = createFsTools({
126
+ fs: nodeFileSystem({ cwd: "/srv/project", allowedRoots: ["/srv/project"] }),
123
127
  state: memoryStore(),
124
128
  bash: {
125
129
  runner: nodeCommandRunner({ cwd: "/srv/project", shell: "/bin/bash" }),
130
+ env: shellEnv(() => process.env),
126
131
  afterRun: [
127
132
  {
128
133
  id: "invalidate-package-json",
@@ -151,4 +156,4 @@ The allowed roots do not limit what a command touches. The runner is POSIX only.
151
156
 
152
157
  - `docs/hosts.md` in [`@better-fs-tools/write`](https://www.npmjs.com/package/@better-fs-tools/write): the defaults of every host and bundle, and what each backend can do
153
158
  - [`@better-fs-tools/read`](https://www.npmjs.com/package/@better-fs-tools/read): every read option
154
- - [`@better-fs-tools/pi`](https://www.npmjs.com/package/@better-fs-tools/pi): the Pi tool, which builds on `nodeFileSystem`
159
+ - [`@better-fs-tools/pi`](https://www.npmjs.com/package/@better-fs-tools/pi): `piCwdFileSystem()` and `piCwdCommandRunner()` on its `./node` subpath build on these backends
package/dist/index.d.ts CHANGED
@@ -1,12 +1,5 @@
1
1
  export { nodeCommandRunner } from "./command-runner.ts";
2
2
  export type { NodeCommandRunnerOptions } from "./command-runner.ts";
3
- export { createNodeBashTool } from "./create-node-bash-tool.ts";
4
- export { createNodeFsTools } from "./create-node-fs-tools.ts";
5
- export type { CreateNodeFsToolsOptions, NodeFsTools, NodeFsToolsBashOptions, NodeFsToolsWithApplyPatch, NodeFsToolsWithBash, } from "./create-node-fs-tools.ts";
6
- export { createNodeReadTool } from "./create-node-read-tool.ts";
7
- export type { CreateNodeReadToolOptions } from "./create-node-read-tool.ts";
8
- export { createNodeApplyPatchTool, createNodeEditTool, createNodeWriteTool, } from "./create-node-write-tools.ts";
9
- export type { CreateNodeApplyPatchToolOptions, CreateNodeEditToolOptions, CreateNodeWriteToolOptions, } from "./create-node-write-tools.ts";
10
3
  export { nodeDigest } from "./digest.ts";
11
4
  export { nodeFileSystem } from "./filesystem.ts";
12
5
  export type { NodeFileSystem } from "./filesystem.ts";
package/dist/index.js CHANGED
@@ -1,7 +1,3 @@
1
1
  export { nodeCommandRunner } from "./command-runner.js";
2
- export { createNodeBashTool } from "./create-node-bash-tool.js";
3
- export { createNodeFsTools } from "./create-node-fs-tools.js";
4
- export { createNodeReadTool } from "./create-node-read-tool.js";
5
- export { createNodeApplyPatchTool, createNodeEditTool, createNodeWriteTool, } from "./create-node-write-tools.js";
6
2
  export { nodeDigest } from "./digest.js";
7
3
  export { nodeFileSystem } from "./filesystem.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@better-fs-tools/node",
3
- "version": "0.2.0",
4
- "description": "The Node filesystem, a SHA-256 digest, a bash runner, and the zero-config read, edit, write, apply_patch, and bash tools for Node and Bun.",
3
+ "version": "0.3.0",
4
+ "description": "The Node filesystem, a SHA-256 digest on node:crypto, and a bash runner for Node and Bun.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -29,10 +29,10 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@better-fs-tools/fs": "^0.2.0",
33
- "@better-fs-tools/read": "^0.2.0",
34
- "@better-fs-tools/shell": "^0.2.0",
35
- "@better-fs-tools/write": "^0.2.0"
32
+ "@better-fs-tools/fs": "^0.3.0",
33
+ "@better-fs-tools/read": "^0.3.0",
34
+ "@better-fs-tools/shell": "^0.3.0",
35
+ "@better-fs-tools/write": "^0.3.0"
36
36
  },
37
37
  "engines": {
38
38
  "node": ">=24"
@@ -1,10 +0,0 @@
1
- import type { BashTool, ShellToolDeps } from "@better-fs-tools/shell";
2
- /**
3
- * Creates a bash tool that runs commands on the local machine. `runner`
4
- * defaults to nodeCommandRunner() at process.cwd(). `env` defaults to
5
- * process.env, read on each call, with defaultShellEnv over it. A relative
6
- * `cwd` resolves against process.cwd(). There is no authorizer, hook, or
7
- * spill unless you pass one.
8
- */
9
- export declare function createNodeBashTool<THost = undefined>(deps?: Partial<ShellToolDeps<THost>>): BashTool<THost>;
10
- export declare function withNodeShellDefaults<THost>(deps: Partial<ShellToolDeps<THost>>, cwd?: string): ShellToolDeps<THost>;
@@ -1,31 +0,0 @@
1
- import path from "node:path";
2
- import { createBashTool, shellEnv } from "@better-fs-tools/shell";
3
- import { nodeCommandRunner } from "./command-runner.js";
4
- /**
5
- * Creates a bash tool that runs commands on the local machine. `runner`
6
- * defaults to nodeCommandRunner() at process.cwd(). `env` defaults to
7
- * process.env, read on each call, with defaultShellEnv over it. A relative
8
- * `cwd` resolves against process.cwd(). There is no authorizer, hook, or
9
- * spill unless you pass one.
10
- */
11
- export function createNodeBashTool(deps = {}) {
12
- if (deps === null || typeof deps !== "object" || Array.isArray(deps)) {
13
- throw new TypeError("bash tool dependencies must be an object");
14
- }
15
- return createBashTool(withNodeShellDefaults(deps));
16
- }
17
- export function withNodeShellDefaults(deps, cwd) {
18
- const given = deps.cwd;
19
- return {
20
- ...deps,
21
- ...(isValidRelativePath(given) ? { cwd: path.resolve(given) } : {}),
22
- runner: deps.runner ?? nodeCommandRunner(cwd === undefined ? {} : { cwd }),
23
- env: deps.env ?? shellEnv(() => process.env),
24
- };
25
- }
26
- function isValidRelativePath(given) {
27
- return (typeof given === "string" &&
28
- given.trim() !== "" &&
29
- !given.includes("\0") &&
30
- !path.isAbsolute(given));
31
- }
@@ -1,90 +0,0 @@
1
- import type { SymlinkPolicy } from "@better-fs-tools/fs";
2
- import type { Clock, Digest, ReadStateStore, ReadTool, ReadToolDeps, ToolCallContext } from "@better-fs-tools/read";
3
- import type { ApplyPatchTool, ApplyPatchToolDeps, EditTool, EditToolDeps, FsToolsSharedKey, InvalidateOutcome, LockManager, WriteTool, WriteToolDeps } from "@better-fs-tools/write";
4
- import type { BashTool, ShellToolDeps } from "@better-fs-tools/shell";
5
- import type { NodeFileSystem } from "./filesystem.ts";
6
- /**
7
- * Options for the bundle's bash tool, all optional. `runner` defaults to
8
- * nodeCommandRunner({ cwd }), and `env` to shellEnv(() => process.env).
9
- */
10
- export type NodeFsToolsBashOptions<THost = undefined> = Omit<Partial<ShellToolDeps<THost>>, "digest" | "clock">;
11
- export interface CreateNodeFsToolsOptions<THost = undefined> {
12
- /** Default process.cwd(). */
13
- readonly cwd?: string;
14
- /** Default [cwd]. They do not limit a bash command. */
15
- readonly allowedRoots?: readonly string[];
16
- readonly denyRoots?: readonly string[];
17
- readonly symlinks?: SymlinkPolicy;
18
- /** Default "in-place". "refuse" fails a replace of a file with more than one hard link. */
19
- readonly hardLinks?: "refuse" | "in-place";
20
- /** Mode of a new file, exactly. Default 0o666 less the process umask. */
21
- readonly newFileMode?: number;
22
- /** Mode of a directory that createParents makes, exactly. Default 0o777 less the umask. */
23
- readonly newDirectoryMode?: number;
24
- /** Default null: no read-before-write. A store, for example memoryStore(), turns it on. */
25
- readonly state?: ReadStateStore | null;
26
- /** Default nodeDigest(). */
27
- readonly digest?: Digest;
28
- /** Default memoryLocks(). */
29
- readonly locks?: LockManager;
30
- /** Default () => new Date(). */
31
- readonly clock?: Clock;
32
- readonly read?: Omit<Partial<ReadToolDeps<THost>>, FsToolsSharedKey>;
33
- readonly edit?: Omit<Partial<EditToolDeps<THost>>, FsToolsSharedKey>;
34
- readonly write?: Omit<Partial<WriteToolDeps<THost>>, FsToolsSharedKey>;
35
- /** `true` or an options object adds an apply_patch tool. Off by default. */
36
- readonly applyPatch?: boolean | Omit<Partial<ApplyPatchToolDeps<THost>>, FsToolsSharedKey>;
37
- /**
38
- * `true` or an options object adds a bash tool. Off by default. The runner
39
- * defaults to nodeCommandRunner({ cwd }), and env to
40
- * shellEnv(() => process.env). With your own runner, commands still run in
41
- * an explicit top-level cwd. bash.cwd wins over both.
42
- */
43
- readonly bash?: boolean | NodeFsToolsBashOptions<THost>;
44
- }
45
- export interface NodeFsTools<THost = undefined> {
46
- readonly read: ReadTool<THost>;
47
- readonly edit: EditTool<THost>;
48
- readonly write: WriteTool<THost>;
49
- /** null unless options.applyPatch is true or an object. */
50
- readonly applyPatch: ApplyPatchTool<THost> | null;
51
- /** null unless options.bash is set. Runs in cwd. It does not share the read state: see invalidate. */
52
- readonly bash: BashTool<THost> | null;
53
- readonly fs: NodeFileSystem;
54
- readonly state: ReadStateStore | null;
55
- readonly digest: Digest;
56
- readonly locks: LockManager;
57
- readonly clock: Clock;
58
- /**
59
- * Forgets the read record for a path, so the next edit needs a fresh read.
60
- * With state null it reports recorded: false. `call` is ignored.
61
- */
62
- invalidate(path: string, call?: ToolCallContext<THost>): Promise<InvalidateOutcome>;
63
- }
64
- /** The result when options.bash is true or an object: bash is there. */
65
- export interface NodeFsToolsWithBash<THost = undefined> extends NodeFsTools<THost> {
66
- readonly bash: BashTool<THost>;
67
- }
68
- /** The result when options.applyPatch is true or an object: applyPatch is there. */
69
- export interface NodeFsToolsWithApplyPatch<THost = undefined> extends NodeFsTools<THost> {
70
- readonly applyPatch: ApplyPatchTool<THost>;
71
- }
72
- /**
73
- * Creates read, edit, and write tools over one local filesystem. They share
74
- * one digest, lock manager, and clock, and the read store when `state` is
75
- * given. With `applyPatch`, adds an apply_patch tool. With `bash`, adds a bash
76
- * tool in the same cwd. The allowed roots do not limit
77
- * what a bash command touches. To make the next edit after a command need a
78
- * read, call invalidate(path) from a bash afterRun hook.
79
- */
80
- export declare function createNodeFsTools<THost = undefined>(options: CreateNodeFsToolsOptions<THost> & {
81
- readonly bash: true | NodeFsToolsBashOptions<THost>;
82
- readonly applyPatch: true | Omit<Partial<ApplyPatchToolDeps<THost>>, FsToolsSharedKey>;
83
- }): NodeFsToolsWithBash<THost> & NodeFsToolsWithApplyPatch<THost>;
84
- export declare function createNodeFsTools<THost = undefined>(options: CreateNodeFsToolsOptions<THost> & {
85
- readonly bash: true | NodeFsToolsBashOptions<THost>;
86
- }): NodeFsToolsWithBash<THost>;
87
- export declare function createNodeFsTools<THost = undefined>(options: CreateNodeFsToolsOptions<THost> & {
88
- readonly applyPatch: true | Omit<Partial<ApplyPatchToolDeps<THost>>, FsToolsSharedKey>;
89
- }): NodeFsToolsWithApplyPatch<THost>;
90
- export declare function createNodeFsTools<THost = undefined>(options?: CreateNodeFsToolsOptions<THost>): NodeFsTools<THost>;
@@ -1,114 +0,0 @@
1
- import path from "node:path";
2
- import { createFsTools } from "@better-fs-tools/write";
3
- import { withNodeShellDefaults } from "./create-node-bash-tool.js";
4
- import { nodeDigest } from "./digest.js";
5
- import { nodeFileSystem } from "./filesystem.js";
6
- const KNOWN_OPTIONS = new Set([
7
- "cwd",
8
- "allowedRoots",
9
- "denyRoots",
10
- "symlinks",
11
- "hardLinks",
12
- "newFileMode",
13
- "newDirectoryMode",
14
- "state",
15
- "digest",
16
- "locks",
17
- "clock",
18
- "read",
19
- "edit",
20
- "write",
21
- "applyPatch",
22
- "bash",
23
- ]);
24
- const BUNDLE_SHARED_KEYS_BY_TOOL = {
25
- read: ["fs", "state", "digest", "clock"],
26
- edit: ["fs", "state", "digest", "locks", "clock"],
27
- write: ["fs", "state", "digest", "locks", "clock"],
28
- applyPatch: ["fs", "state", "digest", "locks", "clock"],
29
- bash: ["digest", "clock"],
30
- };
31
- export function createNodeFsTools(options = {}) {
32
- checkOptions(options);
33
- const cwd = path.resolve(options.cwd ?? process.cwd());
34
- const fs = nodeFileSystem({
35
- cwd,
36
- allowedRoots: options.allowedRoots ?? [cwd],
37
- ...pickDefined(options, [
38
- "denyRoots",
39
- "symlinks",
40
- "hardLinks",
41
- "newFileMode",
42
- "newDirectoryMode",
43
- ]),
44
- });
45
- const bash = options.bash === true ? {} : options.bash;
46
- const tools = createFsTools({
47
- fs,
48
- digest: options.digest ?? nodeDigest(),
49
- ...pickDefined(options, ["state", "locks", "clock", "read", "edit", "write", "applyPatch"]),
50
- ...(bash === undefined || bash === false ? {} : { bash: bashDeps(bash, options.cwd, cwd) }),
51
- });
52
- return Object.freeze({
53
- read: tools.read,
54
- edit: tools.edit,
55
- write: tools.write,
56
- applyPatch: tools.applyPatch,
57
- bash: tools.bash,
58
- fs,
59
- state: tools.state,
60
- digest: tools.digest,
61
- locks: tools.locks,
62
- clock: tools.clock,
63
- invalidate: (target) => tools.invalidate(target),
64
- });
65
- }
66
- function checkOptions(options) {
67
- if (options === null || typeof options !== "object" || Array.isArray(options)) {
68
- throw new TypeError("createNodeFsTools options must be an object");
69
- }
70
- for (const key of Object.keys(options)) {
71
- if (!KNOWN_OPTIONS.has(key))
72
- throw new TypeError(`Unknown createNodeFsTools option: ${key}`);
73
- }
74
- const state = options.state;
75
- if (state !== undefined && state !== null && !isReadStateStore(state)) {
76
- throw new TypeError(`createNodeFsTools state must be a read state store or null: a bundle takes one store, not a per-call factory`);
77
- }
78
- for (const [tool, keys] of Object.entries(BUNDLE_SHARED_KEYS_BY_TOOL)) {
79
- const part = options[tool];
80
- if (part === undefined)
81
- continue;
82
- if ((tool === "bash" || tool === "applyPatch") && typeof part === "boolean")
83
- continue;
84
- if (part === null || typeof part !== "object" || Array.isArray(part)) {
85
- throw new TypeError(`createNodeFsTools ${tool} options must be an object`);
86
- }
87
- for (const key of keys) {
88
- if (Object.hasOwn(part, key)) {
89
- throw new TypeError(`createNodeFsTools ${tool} options cannot set ${key}: set it once at the top level`);
90
- }
91
- }
92
- }
93
- }
94
- function pickDefined(options, keys) {
95
- const picked = {};
96
- for (const key of keys) {
97
- if (options[key] !== undefined)
98
- picked[key] = options[key];
99
- }
100
- return picked;
101
- }
102
- function bashDeps(bash, explicitCwd, cwd) {
103
- const deps = withNodeShellDefaults(bash, cwd);
104
- const customRunnerNeedsExplicitCwd = bash.runner !== undefined && explicitCwd !== undefined && bash.cwd === undefined;
105
- if (!customRunnerNeedsExplicitCwd)
106
- return deps;
107
- return { ...deps, cwd };
108
- }
109
- function isReadStateStore(value) {
110
- if (value === null || typeof value !== "object")
111
- return false;
112
- const store = value;
113
- return ["get", "put", "delete"].every((key) => typeof store[key] === "function");
114
- }
@@ -1,9 +0,0 @@
1
- import type { ReadTool, ReadToolDeps, StateNeedsDigestOrDefault } from "@better-fs-tools/read";
2
- /** Options for createNodeReadTool, all optional. A `state` needs a digest that is not null. */
3
- export type CreateNodeReadToolOptions<THost = undefined> = Partial<ReadToolDeps<THost>> & StateNeedsDigestOrDefault;
4
- /**
5
- * Creates a read tool for local files. `fs` defaults to nodeFileSystem rooted
6
- * at process.cwd(). `digest` defaults to nodeDigest(); pass `digest: null` to
7
- * turn observations off.
8
- */
9
- export declare function createNodeReadTool<THost = undefined>(deps?: CreateNodeReadToolOptions<THost>): ReadTool<THost>;
@@ -1,22 +0,0 @@
1
- import { createReadTool } from "@better-fs-tools/read";
2
- import { nodeDigest } from "./digest.js";
3
- import { nodeFileSystem } from "./filesystem.js";
4
- /**
5
- * Creates a read tool for local files. `fs` defaults to nodeFileSystem rooted
6
- * at process.cwd(). `digest` defaults to nodeDigest(); pass `digest: null` to
7
- * turn observations off.
8
- */
9
- export function createNodeReadTool(deps = {}) {
10
- if (deps === null || typeof deps !== "object" || Array.isArray(deps)) {
11
- throw new TypeError("read tool dependencies must be an object");
12
- }
13
- const fs = deps.fs ?? defaultFileSystem();
14
- // digest: null narrows the options to the branch without a state.
15
- if (deps.digest === null)
16
- return createReadTool({ ...deps, fs, digest: null });
17
- return createReadTool({ ...deps, fs, digest: deps.digest ?? nodeDigest() });
18
- }
19
- function defaultFileSystem() {
20
- const cwd = process.cwd();
21
- return nodeFileSystem({ cwd, allowedRoots: [cwd] });
22
- }
@@ -1,19 +0,0 @@
1
- import type { ApplyPatchTool, ApplyPatchToolDeps, EditTool, EditToolDeps, WriteTool, WriteToolDeps } from "@better-fs-tools/write";
2
- import type { StateNeedsDigestOrDefault } from "@better-fs-tools/read";
3
- /** Options for createNodeEditTool, all optional. A `state` needs a digest that is not null. */
4
- export type CreateNodeEditToolOptions<THost = undefined> = Partial<EditToolDeps<THost>> & StateNeedsDigestOrDefault;
5
- /** As CreateNodeEditToolOptions, for write. */
6
- export type CreateNodeWriteToolOptions<THost = undefined> = Partial<WriteToolDeps<THost>> & StateNeedsDigestOrDefault;
7
- /** As CreateNodeEditToolOptions, for apply_patch. */
8
- export type CreateNodeApplyPatchToolOptions<THost = undefined> = Partial<ApplyPatchToolDeps<THost>> & StateNeedsDigestOrDefault;
9
- /**
10
- * Creates an edit tool for local files. `fs` defaults to nodeFileSystem rooted
11
- * at process.cwd(). `digest` defaults to nodeDigest(); pass `digest: null` to
12
- * turn it off. Read-before-write is off unless you pass a `state`; use
13
- * createNodeFsTools({ state }) for a store shared with read.
14
- */
15
- export declare function createNodeEditTool<THost = undefined>(deps?: CreateNodeEditToolOptions<THost>): EditTool<THost>;
16
- /** Creates a write tool for local files, with the same defaults as createNodeEditTool. */
17
- export declare function createNodeWriteTool<THost = undefined>(deps?: CreateNodeWriteToolOptions<THost>): WriteTool<THost>;
18
- /** Creates an apply_patch tool for local files, with the same defaults as createNodeEditTool. */
19
- export declare function createNodeApplyPatchTool<THost = undefined>(deps?: CreateNodeApplyPatchToolOptions<THost>): ApplyPatchTool<THost>;
@@ -1,42 +0,0 @@
1
- import { createApplyPatchTool, createEditTool, createWriteTool } from "@better-fs-tools/write";
2
- import { nodeDigest } from "./digest.js";
3
- import { nodeFileSystem } from "./filesystem.js";
4
- /**
5
- * Creates an edit tool for local files. `fs` defaults to nodeFileSystem rooted
6
- * at process.cwd(). `digest` defaults to nodeDigest(); pass `digest: null` to
7
- * turn it off. Read-before-write is off unless you pass a `state`; use
8
- * createNodeFsTools({ state }) for a store shared with read.
9
- */
10
- export function createNodeEditTool(deps = {}) {
11
- checkDeps(deps, "edit");
12
- const fs = deps.fs ?? defaultFileSystem();
13
- // digest: null narrows the options to the branch without a state.
14
- if (deps.digest === null)
15
- return createEditTool({ ...deps, fs, digest: null });
16
- return createEditTool({ ...deps, fs, digest: deps.digest ?? nodeDigest() });
17
- }
18
- /** Creates a write tool for local files, with the same defaults as createNodeEditTool. */
19
- export function createNodeWriteTool(deps = {}) {
20
- checkDeps(deps, "write");
21
- const fs = deps.fs ?? defaultFileSystem();
22
- if (deps.digest === null)
23
- return createWriteTool({ ...deps, fs, digest: null });
24
- return createWriteTool({ ...deps, fs, digest: deps.digest ?? nodeDigest() });
25
- }
26
- /** Creates an apply_patch tool for local files, with the same defaults as createNodeEditTool. */
27
- export function createNodeApplyPatchTool(deps = {}) {
28
- checkDeps(deps, "apply_patch");
29
- const fs = deps.fs ?? defaultFileSystem();
30
- if (deps.digest === null)
31
- return createApplyPatchTool({ ...deps, fs, digest: null });
32
- return createApplyPatchTool({ ...deps, fs, digest: deps.digest ?? nodeDigest() });
33
- }
34
- function checkDeps(deps, tool) {
35
- if (deps === null || typeof deps !== "object" || Array.isArray(deps)) {
36
- throw new TypeError(`${tool} tool dependencies must be an object`);
37
- }
38
- }
39
- function defaultFileSystem() {
40
- const cwd = process.cwd();
41
- return nodeFileSystem({ cwd, allowedRoots: [cwd] });
42
- }