@better-fs-tools/node 0.2.0 → 0.3.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/README.md +35 -28
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -4
- package/package.json +7 -6
- package/dist/create-node-bash-tool.d.ts +0 -10
- package/dist/create-node-bash-tool.js +0 -31
- package/dist/create-node-fs-tools.d.ts +0 -90
- package/dist/create-node-fs-tools.js +0 -114
- package/dist/create-node-read-tool.d.ts +0 -9
- package/dist/create-node-read-tool.js +0 -22
- package/dist/create-node-write-tools.d.ts +0 -19
- package/dist/create-node-write-tools.js +0 -42
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
<a href="https://betterfs.dev"><img src="https://betterfs.dev/brand/D-wordmark-auto.svg" alt="Better FS Tools" height="64"></a>
|
|
2
|
+
|
|
1
3
|
# @better-fs-tools/node
|
|
2
4
|
|
|
3
|
-
The Node
|
|
5
|
+
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
6
|
|
|
5
7
|
## Install
|
|
6
8
|
|
|
@@ -13,15 +15,20 @@ The package has no peers. It needs Node 24 or later (`engines.node` is `>=24`),
|
|
|
13
15
|
## Example
|
|
14
16
|
|
|
15
17
|
```ts
|
|
16
|
-
import {
|
|
17
|
-
import { textOf } from "@better-fs-tools/read";
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
18
|
+
import { nodeDigest, nodeFileSystem } from "@better-fs-tools/node";
|
|
19
|
+
import { createReadTool, textOf } from "@better-fs-tools/read";
|
|
20
|
+
|
|
21
|
+
// The local filesystem, rooted at process.cwd(). nodeDigest() is optional: it
|
|
22
|
+
// gives the same values as the default sha256Digest(), faster, on node:crypto.
|
|
23
|
+
const cwd = process.cwd();
|
|
24
|
+
const read = createReadTool({
|
|
25
|
+
fs: nodeFileSystem({ cwd, allowedRoots: [cwd] }),
|
|
26
|
+
digest: nodeDigest(),
|
|
27
|
+
});
|
|
21
28
|
console.log(textOf(await read({ path: "package.json", limit: 1 }))); // "1|{" and a continue note
|
|
22
29
|
|
|
23
30
|
// Your own roots and policy.
|
|
24
|
-
export const docsOnly =
|
|
31
|
+
export const docsOnly = createReadTool({
|
|
25
32
|
fs: nodeFileSystem({
|
|
26
33
|
cwd: "/srv/app",
|
|
27
34
|
allowedRoots: ["/srv/app/docs"],
|
|
@@ -31,17 +38,18 @@ export const docsOnly = createNodeReadTool({
|
|
|
31
38
|
});
|
|
32
39
|
```
|
|
33
40
|
|
|
34
|
-
All four tools
|
|
41
|
+
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
42
|
|
|
36
43
|
```ts
|
|
37
|
-
import {
|
|
44
|
+
import { nodeFileSystem } from "@better-fs-tools/node";
|
|
38
45
|
import { memoryStore, textOf } from "@better-fs-tools/read";
|
|
39
|
-
import { protectPaths } from "@better-fs-tools/write";
|
|
46
|
+
import { createFsTools, protectPaths } from "@better-fs-tools/write";
|
|
40
47
|
|
|
41
48
|
// read, edit, write, and apply_patch over one nodeFileSystem rooted at
|
|
42
49
|
// process.cwd(), with one read store, one SHA-256 digest, and one lock manager.
|
|
43
50
|
// The read store and apply_patch are opt-in.
|
|
44
|
-
const tools =
|
|
51
|
+
const tools = createFsTools({
|
|
52
|
+
fs: nodeFileSystem({ allowedRoots: [process.cwd()] }),
|
|
45
53
|
state: memoryStore(),
|
|
46
54
|
edit: { authorize: protectPaths() },
|
|
47
55
|
write: { authorize: protectPaths() },
|
|
@@ -64,15 +72,11 @@ export async function afterShell(path: string): Promise<boolean> {
|
|
|
64
72
|
|
|
65
73
|
## Contents
|
|
66
74
|
|
|
67
|
-
| Export
|
|
68
|
-
|
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
71
|
-
| `
|
|
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`. |
|
|
75
|
+
| Export | Use |
|
|
76
|
+
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
77
|
+
| `nodeFileSystem(options)` | A `WritableFileSystem` over Node file descriptors |
|
|
78
|
+
| `nodeCommandRunner(options?)` | A bash `CommandRunner`: `bash -c` in its own process group. POSIX only. See [Bash tool](#bash-tool) |
|
|
79
|
+
| `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
80
|
|
|
77
81
|
`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
82
|
|
|
@@ -107,22 +111,25 @@ A write goes to a `0o600` temp file next to the target. The bytes are written an
|
|
|
107
111
|
|
|
108
112
|
## Bash tool
|
|
109
113
|
|
|
110
|
-
`
|
|
114
|
+
`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
115
|
|
|
112
116
|
```ts
|
|
113
|
-
import {
|
|
114
|
-
import type { NodeFsToolsWithBash } from "@better-fs-tools/node";
|
|
117
|
+
import { nodeCommandRunner, nodeFileSystem } from "@better-fs-tools/node";
|
|
115
118
|
import { memoryStore } from "@better-fs-tools/read";
|
|
119
|
+
import { shellEnv } from "@better-fs-tools/shell";
|
|
120
|
+
import { createFsTools } from "@better-fs-tools/write";
|
|
121
|
+
import type { FsToolsWithBash } from "@better-fs-tools/write";
|
|
116
122
|
|
|
117
123
|
// bash is created only because `bash` is set. With a read store, the hook makes
|
|
118
124
|
// the next edit of package.json need a read first, since a command may have
|
|
119
125
|
// changed it.
|
|
120
|
-
let tools:
|
|
121
|
-
tools =
|
|
122
|
-
cwd: "/srv/project",
|
|
126
|
+
let tools: FsToolsWithBash | undefined;
|
|
127
|
+
tools = createFsTools({
|
|
128
|
+
fs: nodeFileSystem({ cwd: "/srv/project", allowedRoots: ["/srv/project"] }),
|
|
123
129
|
state: memoryStore(),
|
|
124
130
|
bash: {
|
|
125
131
|
runner: nodeCommandRunner({ cwd: "/srv/project", shell: "/bin/bash" }),
|
|
132
|
+
env: shellEnv(() => process.env),
|
|
126
133
|
afterRun: [
|
|
127
134
|
{
|
|
128
135
|
id: "invalidate-package-json",
|
|
@@ -149,6 +156,6 @@ The allowed roots do not limit what a command touches. The runner is POSIX only.
|
|
|
149
156
|
|
|
150
157
|
## Links
|
|
151
158
|
|
|
152
|
-
- `docs/hosts.md` in [`@better-fs-tools/write`](https://www.npmjs.com/package/@better-fs-tools/write)
|
|
159
|
+
- [Choose an integration](https://betterfs.dev/integrations) and [Filesystems](https://betterfs.dev/filesystems) on the docs site: the defaults of every host and bundle, and what each backend can do. `docs/hosts.md` in [`@better-fs-tools/write`](https://www.npmjs.com/package/@better-fs-tools/write) has the same tables
|
|
153
160
|
- [`@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):
|
|
161
|
+
- [`@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,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-fs-tools/node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The Node filesystem, a SHA-256 digest
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "The Node filesystem, a SHA-256 digest on node:crypto, and a bash runner for Node and Bun.",
|
|
5
|
+
"homepage": "https://betterfs.dev",
|
|
5
6
|
"license": "MIT",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
@@ -29,10 +30,10 @@
|
|
|
29
30
|
"access": "public"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@better-fs-tools/fs": "^0.
|
|
33
|
-
"@better-fs-tools/read": "^0.
|
|
34
|
-
"@better-fs-tools/shell": "^0.
|
|
35
|
-
"@better-fs-tools/write": "^0.
|
|
33
|
+
"@better-fs-tools/fs": "^0.3.0",
|
|
34
|
+
"@better-fs-tools/read": "^0.3.0",
|
|
35
|
+
"@better-fs-tools/shell": "^0.3.0",
|
|
36
|
+
"@better-fs-tools/write": "^0.3.0"
|
|
36
37
|
},
|
|
37
38
|
"engines": {
|
|
38
39
|
"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
|
-
}
|