@smeltjs/core 0.1.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 +4 -4
- package/dist/apply.d.ts.map +1 -1
- package/dist/apply.js.map +1 -1
- package/dist/cache/prefix.d.ts.map +1 -1
- package/dist/cache/prefix.js.map +1 -1
- package/dist/cli/args.d.ts +16 -59
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +18 -277
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/bin.d.ts.map +1 -1
- package/dist/cli/bin.js.map +1 -1
- package/dist/cli/config.d.ts +62 -0
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +87 -2
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/hooks.d.ts +148 -0
- package/dist/cli/hooks.d.ts.map +1 -0
- package/dist/cli/hooks.js +835 -0
- package/dist/cli/hooks.js.map +1 -0
- package/dist/cli/init.d.ts +30 -13
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +173 -38
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/report.d.ts.map +1 -1
- package/dist/cli/report.js +2 -2
- package/dist/cli/report.js.map +1 -1
- package/dist/cli/run.d.ts +25 -88
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +32 -322
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/shell.d.ts +106 -0
- package/dist/cli/shell.d.ts.map +1 -0
- package/dist/cli/shell.js +100 -0
- package/dist/cli/shell.js.map +1 -0
- package/dist/cli/subcommands/flags.d.ts +141 -0
- package/dist/cli/subcommands/flags.d.ts.map +1 -0
- package/dist/cli/subcommands/flags.js +208 -0
- package/dist/cli/subcommands/flags.js.map +1 -0
- package/dist/cli/subcommands/hooks.d.ts +18 -0
- package/dist/cli/subcommands/hooks.d.ts.map +1 -0
- package/dist/cli/subcommands/hooks.js +64 -0
- package/dist/cli/subcommands/hooks.js.map +1 -0
- package/dist/cli/subcommands/init.d.ts +19 -0
- package/dist/cli/subcommands/init.d.ts.map +1 -0
- package/dist/cli/subcommands/init.js +44 -0
- package/dist/cli/subcommands/init.js.map +1 -0
- package/dist/cli/subcommands/map.d.ts +75 -0
- package/dist/cli/subcommands/map.d.ts.map +1 -0
- package/dist/cli/subcommands/map.js +132 -0
- package/dist/cli/subcommands/map.js.map +1 -0
- package/dist/cli/subcommands/registry.d.ts +80 -0
- package/dist/cli/subcommands/registry.d.ts.map +1 -0
- package/dist/cli/subcommands/registry.js +138 -0
- package/dist/cli/subcommands/registry.js.map +1 -0
- package/dist/cli/subcommands/retrieve.d.ts +74 -0
- package/dist/cli/subcommands/retrieve.d.ts.map +1 -0
- package/dist/cli/subcommands/retrieve.js +84 -0
- package/dist/cli/subcommands/retrieve.js.map +1 -0
- package/dist/cli/subcommands/smelt.d.ts +107 -0
- package/dist/cli/subcommands/smelt.d.ts.map +1 -0
- package/dist/cli/subcommands/smelt.js +304 -0
- package/dist/cli/subcommands/smelt.js.map +1 -0
- package/dist/cli/subcommands/stats.d.ts +43 -0
- package/dist/cli/subcommands/stats.d.ts.map +1 -0
- package/dist/cli/subcommands/stats.js +62 -0
- package/dist/cli/subcommands/stats.js.map +1 -0
- package/dist/cli/subcommands/subcommand.d.ts +113 -0
- package/dist/cli/subcommands/subcommand.d.ts.map +1 -0
- package/dist/cli/subcommands/subcommand.js +3 -0
- package/dist/cli/subcommands/subcommand.js.map +1 -0
- package/dist/cli/usage.d.ts +3 -0
- package/dist/cli/usage.d.ts.map +1 -0
- package/dist/cli/usage.js +95 -0
- package/dist/cli/usage.js.map +1 -0
- package/dist/detect.d.ts.map +1 -1
- package/dist/detect.js.map +1 -1
- package/dist/errors.d.ts +21 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +33 -1
- package/dist/errors.js.map +1 -1
- package/dist/harness/aider.d.ts +10 -0
- package/dist/harness/aider.d.ts.map +1 -0
- package/dist/harness/aider.js +21 -0
- package/dist/harness/aider.js.map +1 -0
- package/dist/harness/claude-code.d.ts +3 -0
- package/dist/harness/claude-code.d.ts.map +1 -0
- package/dist/harness/claude-code.js +68 -0
- package/dist/harness/claude-code.js.map +1 -0
- package/dist/harness/cline.d.ts +3 -0
- package/dist/harness/cline.d.ts.map +1 -0
- package/dist/harness/cline.js +54 -0
- package/dist/harness/cline.js.map +1 -0
- package/dist/harness/codex.d.ts +3 -0
- package/dist/harness/codex.d.ts.map +1 -0
- package/dist/harness/codex.js +91 -0
- package/dist/harness/codex.js.map +1 -0
- package/dist/harness/cursor.d.ts +3 -0
- package/dist/harness/cursor.d.ts.map +1 -0
- package/dist/harness/cursor.js +54 -0
- package/dist/harness/cursor.js.map +1 -0
- package/dist/harness/gemini.d.ts +3 -0
- package/dist/harness/gemini.d.ts.map +1 -0
- package/dist/harness/gemini.js +59 -0
- package/dist/harness/gemini.js.map +1 -0
- package/dist/harness/grok.d.ts +3 -0
- package/dist/harness/grok.d.ts.map +1 -0
- package/dist/harness/grok.js +46 -0
- package/dist/harness/grok.js.map +1 -0
- package/dist/harness/hermes.d.ts +3 -0
- package/dist/harness/hermes.d.ts.map +1 -0
- package/dist/harness/hermes.js +71 -0
- package/dist/harness/hermes.js.map +1 -0
- package/dist/harness/kilocode.d.ts +3 -0
- package/dist/harness/kilocode.d.ts.map +1 -0
- package/dist/harness/kilocode.js +35 -0
- package/dist/harness/kilocode.js.map +1 -0
- package/dist/harness/opencode.d.ts +3 -0
- package/dist/harness/opencode.d.ts.map +1 -0
- package/dist/harness/opencode.js +97 -0
- package/dist/harness/opencode.js.map +1 -0
- package/dist/harness/paths.d.ts +17 -0
- package/dist/harness/paths.d.ts.map +1 -0
- package/dist/harness/paths.js +45 -0
- package/dist/harness/paths.js.map +1 -0
- package/dist/harness/profile.d.ts +184 -0
- package/dist/harness/profile.d.ts.map +1 -0
- package/dist/harness/profile.js +26 -0
- package/dist/harness/profile.js.map +1 -0
- package/dist/harness/registry.d.ts +51 -0
- package/dist/harness/registry.d.ts.map +1 -0
- package/dist/harness/registry.js +87 -0
- package/dist/harness/registry.js.map +1 -0
- package/dist/harness/snippet.d.ts +19 -0
- package/dist/harness/snippet.d.ts.map +1 -0
- package/dist/harness/snippet.js +41 -0
- package/dist/harness/snippet.js.map +1 -0
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js.map +1 -1
- package/dist/hooks/guard-core.d.ts +164 -0
- package/dist/hooks/guard-core.d.ts.map +1 -0
- package/dist/hooks/guard-core.js +491 -0
- package/dist/hooks/guard-core.js.map +1 -0
- package/dist/hooks/shim.d.ts +176 -0
- package/dist/hooks/shim.d.ts.map +1 -0
- package/dist/hooks/shim.js +192 -0
- package/dist/hooks/shim.js.map +1 -0
- package/dist/hooks/shims/claude-code.d.ts +10 -0
- package/dist/hooks/shims/claude-code.d.ts.map +1 -0
- package/dist/hooks/shims/claude-code.js +14 -0
- package/dist/hooks/shims/claude-code.js.map +1 -0
- package/dist/hooks/shims/cline.d.ts +10 -0
- package/dist/hooks/shims/cline.d.ts.map +1 -0
- package/dist/hooks/shims/cline.js +14 -0
- package/dist/hooks/shims/cline.js.map +1 -0
- package/dist/hooks/shims/codex.d.ts +10 -0
- package/dist/hooks/shims/codex.d.ts.map +1 -0
- package/dist/hooks/shims/codex.js +14 -0
- package/dist/hooks/shims/codex.js.map +1 -0
- package/dist/hooks/shims/cursor.d.ts +10 -0
- package/dist/hooks/shims/cursor.d.ts.map +1 -0
- package/dist/hooks/shims/cursor.js +14 -0
- package/dist/hooks/shims/cursor.js.map +1 -0
- package/dist/hooks/shims/gemini.d.ts +10 -0
- package/dist/hooks/shims/gemini.d.ts.map +1 -0
- package/dist/hooks/shims/gemini.js +14 -0
- package/dist/hooks/shims/gemini.js.map +1 -0
- package/dist/hooks/shims/grok.d.ts +10 -0
- package/dist/hooks/shims/grok.d.ts.map +1 -0
- package/dist/hooks/shims/grok.js +14 -0
- package/dist/hooks/shims/grok.js.map +1 -0
- package/dist/hooks/shims/hermes.d.ts +10 -0
- package/dist/hooks/shims/hermes.d.ts.map +1 -0
- package/dist/hooks/shims/hermes.js +14 -0
- package/dist/hooks/shims/hermes.js.map +1 -0
- package/dist/index.d.ts +33 -76
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -67
- package/dist/index.js.map +1 -1
- package/dist/lang/bash.d.ts.map +1 -1
- package/dist/lang/bash.js.map +1 -1
- package/dist/lang/c.d.ts.map +1 -1
- package/dist/lang/c.js.map +1 -1
- package/dist/lang/c_sharp.d.ts.map +1 -1
- package/dist/lang/c_sharp.js.map +1 -1
- package/dist/lang/cpp.d.ts.map +1 -1
- package/dist/lang/cpp.js.map +1 -1
- package/dist/lang/go.d.ts.map +1 -1
- package/dist/lang/go.js.map +1 -1
- package/dist/lang/java.d.ts.map +1 -1
- package/dist/lang/java.js.map +1 -1
- package/dist/lang/javascript.d.ts.map +1 -1
- package/dist/lang/javascript.js.map +1 -1
- package/dist/lang/kotlin.d.ts.map +1 -1
- package/dist/lang/kotlin.js.map +1 -1
- package/dist/lang/php.d.ts.map +1 -1
- package/dist/lang/php.js.map +1 -1
- package/dist/lang/profile.d.ts.map +1 -1
- package/dist/lang/profile.js.map +1 -1
- package/dist/lang/python.d.ts.map +1 -1
- package/dist/lang/python.js.map +1 -1
- package/dist/lang/registry.d.ts.map +1 -1
- package/dist/lang/registry.js.map +1 -1
- package/dist/lang/ruby.d.ts.map +1 -1
- package/dist/lang/ruby.js.map +1 -1
- package/dist/lang/rust.d.ts.map +1 -1
- package/dist/lang/rust.js.map +1 -1
- package/dist/lang/swift.d.ts.map +1 -1
- package/dist/lang/swift.js.map +1 -1
- package/dist/lang/tsx.d.ts.map +1 -1
- package/dist/lang/tsx.js.map +1 -1
- package/dist/lang/typescript.d.ts.map +1 -1
- package/dist/lang/typescript.js.map +1 -1
- package/dist/net/policy.d.ts +18 -1
- package/dist/net/policy.d.ts.map +1 -1
- package/dist/net/policy.js +7 -4
- package/dist/net/policy.js.map +1 -1
- package/dist/ops/index.d.ts +39 -0
- package/dist/ops/index.d.ts.map +1 -0
- package/dist/ops/index.js +37 -0
- package/dist/ops/index.js.map +1 -0
- package/dist/ops/inputs.d.ts +170 -0
- package/dist/ops/inputs.d.ts.map +1 -0
- package/dist/ops/inputs.js +142 -0
- package/dist/ops/inputs.js.map +1 -0
- package/dist/ops/verbs.d.ts +136 -0
- package/dist/ops/verbs.d.ts.map +1 -0
- package/dist/ops/verbs.js +76 -0
- package/dist/ops/verbs.js.map +1 -0
- package/dist/plan/auto.d.ts +69 -0
- package/dist/plan/auto.d.ts.map +1 -0
- package/dist/plan/auto.js +70 -0
- package/dist/plan/auto.js.map +1 -0
- package/dist/plan/budget.d.ts +21 -0
- package/dist/plan/budget.d.ts.map +1 -0
- package/dist/plan/budget.js +26 -0
- package/dist/plan/budget.js.map +1 -0
- package/dist/plan/grammar.d.ts +14 -0
- package/dist/plan/grammar.d.ts.map +1 -1
- package/dist/plan/grammar.js +47 -6
- package/dist/plan/grammar.js.map +1 -1
- package/dist/plan/lexical.d.ts.map +1 -1
- package/dist/plan/lexical.js +1 -7
- package/dist/plan/lexical.js.map +1 -1
- package/dist/plan/planners.d.ts +35 -2
- package/dist/plan/planners.d.ts.map +1 -1
- package/dist/plan/planners.js +35 -2
- package/dist/plan/planners.js.map +1 -1
- package/dist/plan/structural.d.ts +19 -5
- package/dist/plan/structural.d.ts.map +1 -1
- package/dist/plan/structural.js +180 -33
- package/dist/plan/structural.js.map +1 -1
- package/dist/repomap/cache.d.ts +48 -0
- package/dist/repomap/cache.d.ts.map +1 -1
- package/dist/repomap/cache.js +112 -13
- package/dist/repomap/cache.js.map +1 -1
- package/dist/repomap/io.d.ts +24 -0
- package/dist/repomap/io.d.ts.map +1 -0
- package/dist/repomap/io.js +34 -0
- package/dist/repomap/io.js.map +1 -0
- package/dist/repomap/map.d.ts +69 -6
- package/dist/repomap/map.d.ts.map +1 -1
- package/dist/repomap/map.js +104 -22
- package/dist/repomap/map.js.map +1 -1
- package/dist/repomap/rank.d.ts +26 -2
- package/dist/repomap/rank.d.ts.map +1 -1
- package/dist/repomap/rank.js +20 -0
- package/dist/repomap/rank.js.map +1 -1
- package/dist/repomap/reader.d.ts +73 -0
- package/dist/repomap/reader.d.ts.map +1 -0
- package/dist/repomap/reader.js +30 -0
- package/dist/repomap/reader.js.map +1 -0
- package/dist/repomap/tags.d.ts.map +1 -1
- package/dist/repomap/tags.js.map +1 -1
- package/dist/retrieve.d.ts +8 -0
- package/dist/retrieve.d.ts.map +1 -1
- package/dist/retrieve.js +12 -1
- package/dist/retrieve.js.map +1 -1
- package/dist/smelter.d.ts +79 -0
- package/dist/smelter.d.ts.map +1 -0
- package/dist/smelter.js +65 -0
- package/dist/smelter.js.map +1 -0
- package/dist/stages.d.ts.map +1 -1
- package/dist/stages.js +2 -2
- package/dist/stages.js.map +1 -1
- package/dist/stats.d.ts +2 -1
- package/dist/stats.d.ts.map +1 -1
- package/dist/stats.js +2 -1
- package/dist/stats.js.map +1 -1
- package/dist/store-dir.d.ts +43 -4
- package/dist/store-dir.d.ts.map +1 -1
- package/dist/store-dir.js +48 -5
- package/dist/store-dir.js.map +1 -1
- package/dist/store.d.ts +6 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +6 -0
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +35 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/grammars/tree-sitter-bash.wasm +0 -0
- package/grammars/tree-sitter-c.wasm +0 -0
- package/grammars/tree-sitter-c_sharp.wasm +0 -0
- package/grammars/tree-sitter-cpp.wasm +0 -0
- package/grammars/tree-sitter-go.wasm +0 -0
- package/grammars/tree-sitter-java.wasm +0 -0
- package/grammars/tree-sitter-javascript.wasm +0 -0
- package/grammars/tree-sitter-kotlin.wasm +0 -0
- package/grammars/tree-sitter-php.wasm +0 -0
- package/grammars/tree-sitter-python.wasm +0 -0
- package/grammars/tree-sitter-ruby.wasm +0 -0
- package/grammars/tree-sitter-rust.wasm +0 -0
- package/grammars/tree-sitter-swift.wasm +0 -0
- package/grammars/tree-sitter-tsx.wasm +0 -0
- package/grammars/tree-sitter-typescript.wasm +0 -0
- package/package.json +22 -14
- package/dist/cli/resolve.d.ts +0 -103
- package/dist/cli/resolve.d.ts.map +0 -1
- package/dist/cli/resolve.js +0 -106
- package/dist/cli/resolve.js.map +0 -1
package/dist/cli/args.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { parseArgs } from 'node:util';
|
|
2
|
-
import { SUPPORTED_LANGUAGES } from '../detect.js';
|
|
3
2
|
import { CliUsageError } from '../errors.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export
|
|
3
|
+
import { CLI_NAME } from './shell.js';
|
|
4
|
+
import { CLI_FLAGS } from './subcommands/flags.js';
|
|
5
|
+
import { refuseForeignFlags, subcommandFor } from './subcommands/registry.js';
|
|
6
|
+
export { CLI_NAME } from './shell.js';
|
|
7
|
+
export { cliUsage } from './usage.js';
|
|
8
8
|
/**
|
|
9
9
|
* Argument parsing on `node:util.parseArgs` — stable since Node 20, which `engines`
|
|
10
10
|
* already requires.
|
|
@@ -13,6 +13,12 @@ export const CLI_NAME = 'smelt';
|
|
|
13
13
|
* import is the reason that is free: it adds no dependency, so the argument the second
|
|
14
14
|
* package existed to win — keeping the library's dependency tree small — is already won.
|
|
15
15
|
*
|
|
16
|
+
* The shape of this function is the shape of the subcommand seam: split the argv into
|
|
17
|
+
* flags and positionals, answer the two global flags, **look the verb up in
|
|
18
|
+
* `SUBCOMMANDS`**, refuse every flag that verb does not own with one generated
|
|
19
|
+
* message, and let the verb do its own validation. There is no per-verb branching left
|
|
20
|
+
* here — a seventh verb is a seventh file, not a seventh `if`.
|
|
21
|
+
*
|
|
16
22
|
* @throws {CliUsageError} on anything the user got wrong. Never guesses.
|
|
17
23
|
*/
|
|
18
24
|
export function parseSmeltArgs(argv) {
|
|
@@ -22,18 +28,7 @@ export function parseSmeltArgs(argv) {
|
|
|
22
28
|
args: [...argv],
|
|
23
29
|
allowPositionals: true,
|
|
24
30
|
strict: true,
|
|
25
|
-
options:
|
|
26
|
-
budget: { type: 'string' },
|
|
27
|
-
focus: { type: 'string', multiple: true },
|
|
28
|
-
language: { type: 'string' },
|
|
29
|
-
strategy: { type: 'string' },
|
|
30
|
-
ignore: { type: 'string', multiple: true },
|
|
31
|
-
cache: { type: 'string' },
|
|
32
|
-
json: { type: 'boolean' },
|
|
33
|
-
reconstruct: { type: 'boolean' },
|
|
34
|
-
help: { type: 'boolean', short: 'h' },
|
|
35
|
-
version: { type: 'boolean' },
|
|
36
|
-
},
|
|
31
|
+
options: CLI_FLAGS,
|
|
37
32
|
});
|
|
38
33
|
}
|
|
39
34
|
catch (cause) {
|
|
@@ -41,268 +36,14 @@ export function parseSmeltArgs(argv) {
|
|
|
41
36
|
`Run \`${CLI_NAME} --help\`.`);
|
|
42
37
|
}
|
|
43
38
|
const { values, positionals } = parsed;
|
|
39
|
+
// Answered before any verb, so `smelt map --help` prints the help rather than being
|
|
40
|
+
// refused for a flag `map` does not own. No verb may claim these two.
|
|
44
41
|
if (values.help === true)
|
|
45
42
|
return { mode: 'help', focus: [], json: false };
|
|
46
|
-
if (values.version === true)
|
|
43
|
+
if (values.version === true)
|
|
47
44
|
return { mode: 'version', focus: [], json: false };
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (positionals.length > 1) {
|
|
52
|
-
throw new CliUsageError(`${CLI_NAME}: init takes no further arguments.`);
|
|
53
|
-
}
|
|
54
|
-
const flags = Object.entries(values).filter(([, value]) => value !== undefined);
|
|
55
|
-
if (flags.length > 0) {
|
|
56
|
-
throw new CliUsageError(`${CLI_NAME}: init is interactive and takes no flags ` +
|
|
57
|
-
`(got --${flags.map(([name]) => name).join(', --')}). It asks instead.`);
|
|
58
|
-
}
|
|
59
|
-
return { mode: 'init', focus: [], json: false };
|
|
60
|
-
}
|
|
61
|
-
if (positionals[0] === 'map') {
|
|
62
|
-
// `smelt map` is a subcommand, so a file literally named `map` needs `./map`.
|
|
63
|
-
return parseMapArgs(values, positionals);
|
|
64
|
-
}
|
|
65
|
-
if (positionals[0] === 'retrieve') {
|
|
66
|
-
// A subcommand, like map and init — a file literally named `retrieve` needs `./retrieve`.
|
|
67
|
-
return parseRetrieveArgs(values, positionals);
|
|
68
|
-
}
|
|
69
|
-
if (positionals[0] === 'stats') {
|
|
70
|
-
return parseStatsArgs(values, positionals);
|
|
71
|
-
}
|
|
72
|
-
// These two belong to `smelt map` alone; silently ignoring a flag the user typed
|
|
73
|
-
// would be a setting they believed was in force.
|
|
74
|
-
if (values.ignore !== undefined || values.cache !== undefined) {
|
|
75
|
-
throw new CliUsageError(`${CLI_NAME}: --ignore and --cache belong to \`${CLI_NAME} map\`. A single-blob ` +
|
|
76
|
-
`run reads one file or stdin; there is no tree to walk and nothing to cache.`);
|
|
77
|
-
}
|
|
78
|
-
if (positionals.length > 1) {
|
|
79
|
-
throw new CliUsageError(`${CLI_NAME}: expected at most one file, got ${String(positionals.length)} ` +
|
|
80
|
-
`(${positionals.join(', ')}). smelt reads one blob at a time.`);
|
|
81
|
-
}
|
|
82
|
-
const file = positionals[0];
|
|
83
|
-
if (values.reconstruct === true) {
|
|
84
|
-
if (values.budget !== undefined) {
|
|
85
|
-
throw new CliUsageError(`${CLI_NAME}: --budget makes no sense with --reconstruct. Reconstruction puts ` +
|
|
86
|
-
`every byte back; there is nothing to fit.`);
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
mode: 'reconstruct',
|
|
90
|
-
...(file === undefined ? {} : { file }),
|
|
91
|
-
focus: [],
|
|
92
|
-
json: false,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
const budgetBytes = parseBudget(values.budget);
|
|
96
|
-
const chosenStrategy = strategy(values.strategy);
|
|
97
|
-
return {
|
|
98
|
-
mode: 'smelt',
|
|
99
|
-
...(file === undefined ? {} : { file }),
|
|
100
|
-
...(budgetBytes === undefined ? {} : { budgetBytes }),
|
|
101
|
-
focus: values.focus ?? [],
|
|
102
|
-
...(values.language === undefined ? {} : { language: language(values.language) }),
|
|
103
|
-
...(chosenStrategy === undefined ? {} : { strategy: chosenStrategy }),
|
|
104
|
-
json: values.json === true,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* The `map` subcommand's own validation: exactly one directory, the map-only flags,
|
|
109
|
-
* and the same budget rules as everything else — a missing `--budget` is not an
|
|
110
|
-
* error *here* (the config may carry `defaultBudgetBytes`), a malformed one always is.
|
|
111
|
-
*/
|
|
112
|
-
function parseMapArgs(values, positionals) {
|
|
113
|
-
if (positionals.length < 2) {
|
|
114
|
-
throw new CliUsageError(`${CLI_NAME}: map needs the directory to read.\n` +
|
|
115
|
-
` ${CLI_NAME} map <dir> --budget <bytes> [--focus <term>]...`);
|
|
116
|
-
}
|
|
117
|
-
if (positionals.length > 2) {
|
|
118
|
-
throw new CliUsageError(`${CLI_NAME}: map takes exactly one directory, got ` +
|
|
119
|
-
`${String(positionals.length - 1)} (${positionals.slice(1).join(', ')}).`);
|
|
120
|
-
}
|
|
121
|
-
if (values.reconstruct === true) {
|
|
122
|
-
throw new CliUsageError(`${CLI_NAME}: --reconstruct makes no sense with map. A map elides nothing, so ` +
|
|
123
|
-
`there is nothing to put back.`);
|
|
124
|
-
}
|
|
125
|
-
if (values.language !== undefined || values.strategy !== undefined) {
|
|
126
|
-
throw new CliUsageError(`${CLI_NAME}: --language and --strategy apply to single-blob runs. map reads a ` +
|
|
127
|
-
`whole tree, detects each file's language itself, and is not a planner ` +
|
|
128
|
-
`strategy — it returns a map, not an elision plan.`);
|
|
129
|
-
}
|
|
130
|
-
const budgetBytes = parseBudget(values.budget);
|
|
131
|
-
return {
|
|
132
|
-
mode: 'map',
|
|
133
|
-
dir: positionals[1],
|
|
134
|
-
...(budgetBytes === undefined ? {} : { budgetBytes }),
|
|
135
|
-
focus: values.focus ?? [],
|
|
136
|
-
ignore: values.ignore ?? [],
|
|
137
|
-
...(values.cache === undefined ? {} : { cacheDir: values.cache }),
|
|
138
|
-
json: values.json === true,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* `retrieve` takes the hash and nothing else. Every flag is refused rather than
|
|
143
|
-
* ignored — the command prints the exact original bytes on stdout and nothing more,
|
|
144
|
-
* so a flag that changed the output would break the one contract it has, and a flag
|
|
145
|
-
* silently dropped would be a setting the user believed was in force. In particular
|
|
146
|
-
* there is no `--json`: the bytes ARE the output, and wrapping them would re-encode
|
|
147
|
-
* what the command exists to hand back verbatim.
|
|
148
|
-
*/
|
|
149
|
-
function parseRetrieveArgs(values, positionals) {
|
|
150
|
-
const flags = Object.entries(values).filter(([, value]) => value !== undefined);
|
|
151
|
-
if (flags.length > 0) {
|
|
152
|
-
throw new CliUsageError(`${CLI_NAME}: retrieve takes no flags (got --${flags.map(([name]) => name).join(', --')}). ` +
|
|
153
|
-
`It prints the exact original bytes for one hash, nothing else — even --json ` +
|
|
154
|
-
`would wrap what must come back verbatim.`);
|
|
155
|
-
}
|
|
156
|
-
if (positionals.length !== 2) {
|
|
157
|
-
throw new CliUsageError(`${CLI_NAME}: retrieve needs exactly one hash — the one a marker printed.\n` +
|
|
158
|
-
` ${CLI_NAME} retrieve 84998967370f38bc`);
|
|
159
|
-
}
|
|
160
|
-
return { mode: 'retrieve', hash: positionals[1] };
|
|
161
|
-
}
|
|
162
|
-
/** `stats` reads counters; `--json` is its only flag, everything else is refused. */
|
|
163
|
-
function parseStatsArgs(values, positionals) {
|
|
164
|
-
if (positionals.length > 1) {
|
|
165
|
-
throw new CliUsageError(`${CLI_NAME}: stats takes no further arguments, got ` +
|
|
166
|
-
`${positionals.slice(1).join(', ')}. It reports on the one configured store.`);
|
|
167
|
-
}
|
|
168
|
-
const flags = Object.entries(values).filter(([name, value]) => value !== undefined && name !== 'json');
|
|
169
|
-
if (flags.length > 0) {
|
|
170
|
-
throw new CliUsageError(`${CLI_NAME}: stats takes only --json ` +
|
|
171
|
-
`(got --${flags.map(([name]) => name).join(', --')}). It reads counters; there ` +
|
|
172
|
-
`is nothing to budget, focus or plan.`);
|
|
173
|
-
}
|
|
174
|
-
return { mode: 'stats', json: values['json'] === true };
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* `--budget` has no built-in default, for the same reason `smelt()` has none: a budget
|
|
178
|
-
* smelt invented would be smelt deciding how much of the caller's context to throw
|
|
179
|
-
* away, silently, at a number nobody chose. A *missing* flag is not an error here,
|
|
180
|
-
* though — `smelt.config.json` may carry a `defaultBudgetBytes` the user chose
|
|
181
|
-
* explicitly, and the runner errors only when neither exists. A malformed value is
|
|
182
|
-
* always an error.
|
|
183
|
-
*/
|
|
184
|
-
function parseBudget(raw) {
|
|
185
|
-
if (raw === undefined)
|
|
186
|
-
return undefined;
|
|
187
|
-
if (!/^\d+$/.test(raw)) {
|
|
188
|
-
throw new CliUsageError(`${CLI_NAME}: --budget must be a whole number of bytes, got "${raw}".`);
|
|
189
|
-
}
|
|
190
|
-
const value = Number(raw);
|
|
191
|
-
if (value <= 0) {
|
|
192
|
-
throw new CliUsageError(`${CLI_NAME}: --budget must be greater than zero, got "${raw}".`);
|
|
193
|
-
}
|
|
194
|
-
return value;
|
|
195
|
-
}
|
|
196
|
-
function language(raw) {
|
|
197
|
-
const known = [...SUPPORTED_LANGUAGES, 'unknown'];
|
|
198
|
-
if (!known.includes(raw)) {
|
|
199
|
-
throw new CliUsageError(`${CLI_NAME}: unknown --language "${raw}". Known: ${known.join(', ')}.`);
|
|
200
|
-
}
|
|
201
|
-
return raw;
|
|
202
|
-
}
|
|
203
|
-
/** Membership in the {@link PLANNERS} registry is the whole validation. */
|
|
204
|
-
function strategy(raw) {
|
|
205
|
-
if (raw === undefined)
|
|
206
|
-
return undefined;
|
|
207
|
-
if (!isStrategy(raw)) {
|
|
208
|
-
throw new CliUsageError(`${CLI_NAME}: unknown --strategy "${raw}". Known: ${STRATEGIES.join(', ')}.`);
|
|
209
|
-
}
|
|
210
|
-
return raw;
|
|
211
|
-
}
|
|
212
|
-
/** The help text. Also the closest thing the CLI has to documentation. */
|
|
213
|
-
export function cliUsage() {
|
|
214
|
-
return `${CLI_NAME} — shrink text for a model, without lying about what was removed.
|
|
215
|
-
|
|
216
|
-
USAGE
|
|
217
|
-
${CLI_NAME} <file> --budget <bytes> [--focus <term>]...
|
|
218
|
-
${CLI_NAME} --budget <bytes> [--focus <term>]... < input
|
|
219
|
-
${CLI_NAME} map <dir> --budget <bytes> [--focus <term>]... [--ignore <entry>]... [--cache <dir>]
|
|
220
|
-
${CLI_NAME} retrieve <hash>
|
|
221
|
-
${CLI_NAME} stats [--json]
|
|
222
|
-
${CLI_NAME} --reconstruct <result.json>
|
|
223
|
-
${CLI_NAME} --reconstruct < result.json
|
|
224
|
-
${CLI_NAME} init
|
|
225
|
-
|
|
226
|
-
Smelted text goes to stdout and the report goes to stderr, so the two can be piped
|
|
227
|
-
apart: ${CLI_NAME} big.log --budget 4000 > small.log
|
|
228
|
-
|
|
229
|
-
CONFIG
|
|
230
|
-
${CLI_NAME} init walks you through writing a smelt.config.json (and optional typed
|
|
231
|
-
stubs), one question at a time; nothing is written until a final confirm. Runs read
|
|
232
|
-
the nearest smelt.config.json, walking up from the working directory, for DEFAULTS
|
|
233
|
-
only — budget, strategy, store. An explicit flag always wins, and a malformed config
|
|
234
|
-
is a usage error, never silently ignored.
|
|
235
|
-
|
|
236
|
-
MAP
|
|
237
|
-
${CLI_NAME} map <dir> renders a ranked symbol map of a whole repository — modelled
|
|
238
|
-
on Aider's repo-map (aider.chat/docs/repomap.html, design by Paul Gauthier) — to
|
|
239
|
-
stdout, with a short report on stderr. Local files only: symlinks are never
|
|
240
|
-
followed, binary files are skipped, and the map writes nothing to disk unless
|
|
241
|
-
--cache names a directory. Every included symbol carries a receipt: its
|
|
242
|
-
definition site and the measured reference counts that ranked it. Unlike a
|
|
243
|
-
smelt run, map never exits
|
|
244
|
-
1: a plan can come back over budget because ${CLI_NAME} refuses to cut regions you
|
|
245
|
-
asked to keep, but the map fits itself to the budget by construction — symbols
|
|
246
|
-
are appended in rank order until the next line would not fit.
|
|
247
|
-
|
|
248
|
-
RETRIEVE & STATS
|
|
249
|
-
Every marker carries the hash of the bytes it replaced — <<smelt/v1: … —
|
|
250
|
-
retrieve("hash")>> — and the marker's retrieve("hash") is this command:
|
|
251
|
-
${CLI_NAME} retrieve <hash> prints the exact original bytes on stdout, byte for
|
|
252
|
-
byte, nothing else. That closes the loop from pure shell: an agent that got a
|
|
253
|
-
marker asks for the bytes back with a command instead of a tool call, and the
|
|
254
|
-
retrieval is counted — asking for material back is exactly what the expansion
|
|
255
|
-
rate measures. An unknown hash and damaged bytes are distinct refusals (exit 3):
|
|
256
|
-
"never elided" and "the store was corrupted" call for different responses.
|
|
257
|
-
|
|
258
|
-
${CLI_NAME} stats prints the same store's counters, one \`name value\` per line —
|
|
259
|
-
elisionsStored, bytesStored, retrieveCalls, uniqueRetrieved, expansionRate,
|
|
260
|
-
allElisionsRetrieved — and reading them is NOT counted as a retrieval. --json
|
|
261
|
-
emits the RetrieveStats verbatim in its own versioned envelope.
|
|
262
|
-
|
|
263
|
-
Both need somewhere for elisions to outlive the run that made them: a
|
|
264
|
-
smelt.config.json with a directory store (\`${CLI_NAME} init\` writes one). With a
|
|
265
|
-
memory store — or no config — every run's store dies with its process, so there
|
|
266
|
-
is nothing to retrieve across runs, and that is a usage error rather than a
|
|
267
|
-
quiet empty answer.
|
|
268
|
-
|
|
269
|
-
OPTIONS
|
|
270
|
-
--budget <bytes> Required, unless smelt.config.json sets defaultBudgetBytes.
|
|
271
|
-
Soft ceiling for the output, in UTF-8 bytes (for map: a hard
|
|
272
|
-
ceiling, met by construction). No built-in default: a budget
|
|
273
|
-
${CLI_NAME} invented would decide for you.
|
|
274
|
-
--focus <term> What you were looking for. Repeatable. Matching regions and
|
|
275
|
-
their context survive; the runs between them collapse. For
|
|
276
|
-
map: symbols matching a term (by name or path) are promoted
|
|
277
|
-
to the front of the fill order, ranks unchanged.
|
|
278
|
-
--language <id> Override detection. One of: ${[...SUPPORTED_LANGUAGES, 'unknown'].join(', ')}.
|
|
279
|
-
--strategy <id> ${STRATEGIES.join(' or ')}. Defaults to lexical, unless
|
|
280
|
-
smelt.config.json says otherwise. structural parses
|
|
281
|
-
${STRUCTURAL_LANGUAGES.join(', ')};
|
|
282
|
-
any other language is refused, never approximated.
|
|
283
|
-
--ignore <entry> map only. Repeatable. Replaces the default ignore list
|
|
284
|
-
(.git, node_modules): a bare name matches any path segment,
|
|
285
|
-
an entry containing / is a root-relative prefix.
|
|
286
|
-
--cache <dir> map only. Directory for the tags cache, keyed by content
|
|
287
|
-
hash. Only when given does the map write to disk at all.
|
|
288
|
-
--json Print a JSON envelope on stdout instead of the text:
|
|
289
|
-
{ format, result, elided } for a smelt run — \`result\` is
|
|
290
|
-
the SmeltResult verbatim, \`elided\` carries the bytes, so
|
|
291
|
-
the envelope can be reconstructed; feed it back with
|
|
292
|
-
--reconstruct. For map: { format, map }, the RepoMap
|
|
293
|
-
structure verbatim.
|
|
294
|
-
--reconstruct Read a --json envelope and print the original text, byte for
|
|
295
|
-
byte. This is Law 3 you can run from a shell.
|
|
296
|
-
-h, --help This text.
|
|
297
|
-
--version The package version.
|
|
298
|
-
|
|
299
|
-
EXIT CODES
|
|
300
|
-
0 under budget (map is always under budget by construction)
|
|
301
|
-
1 over budget — the plan did not fit, and the report says so. Never silent.
|
|
302
|
-
map never exits 1; see MAP above.
|
|
303
|
-
2 usage error
|
|
304
|
-
3 smelt refused (a SmeltError: an unbuilt planner, an unknown hash, a corrupt store)
|
|
305
|
-
4 unexpected internal error
|
|
306
|
-
`;
|
|
45
|
+
const command = subcommandFor(positionals);
|
|
46
|
+
refuseForeignFlags(command, values);
|
|
47
|
+
return command.parse(values, positionals);
|
|
307
48
|
}
|
|
308
49
|
//# sourceMappingURL=args.js.map
|
package/dist/cli/args.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"args.js","sourceRoot":"","sources":["../../src/cli/args.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"args.js","sourceRoot":"","sources":["../../src/cli/args.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE9E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAWtC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,cAAc,CAAC,IAAuB;IACpD,IAAI,MAAM,CAAC;IACX,IAAI,CAAC;QACH,MAAM,GAAG,SAAS,CAAC;YACjB,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YACf,gBAAgB,EAAE,IAAI;YACtB,MAAM,EAAE,IAAI;YACZ,OAAO,EAAE,SAAS;SACnB,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,aAAa,CACrB,GAAG,QAAQ,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI;YACxE,SAAS,QAAQ,YAAY,CAChC,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC;IAEvC,oFAAoF;IACpF,sEAAsE;IACtE,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI;QAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IAC1E,IAAI,MAAM,CAAC,OAAO,KAAK,IAAI;QAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IAEhF,MAAM,OAAO,GAAG,aAAa,CAAC,WAAW,CAAC,CAAC;IAC3C,kBAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACpC,OAAO,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAC5C,CAAC","sourcesContent":["import { parseArgs } from 'node:util';\n\nimport { CliUsageError } from '../errors.ts';\n\nimport { CLI_NAME } from './shell.ts';\nimport { CLI_FLAGS } from './subcommands/flags.ts';\nimport { refuseForeignFlags, subcommandFor } from './subcommands/registry.ts';\n\nexport { CLI_NAME } from './shell.ts';\nexport { cliUsage } from './usage.ts';\nexport type { CliInvocation } from './subcommands/registry.ts';\nexport type { HooksInvocation } from './subcommands/hooks.ts';\nexport type { InitInvocation } from './subcommands/init.ts';\nexport type { MapInvocation } from './subcommands/map.ts';\nexport type { RetrieveInvocation } from './subcommands/retrieve.ts';\nexport type { SmeltInvocation } from './subcommands/smelt.ts';\nexport type { StatsInvocation } from './subcommands/stats.ts';\n\nimport type { CliInvocation } from './subcommands/registry.ts';\n\n/**\n * Argument parsing on `node:util.parseArgs` — stable since Node 20, which `engines`\n * already requires.\n *\n * The CLI ships as a `bin` on the library rather than as a second package, and this\n * import is the reason that is free: it adds no dependency, so the argument the second\n * package existed to win — keeping the library's dependency tree small — is already won.\n *\n * The shape of this function is the shape of the subcommand seam: split the argv into\n * flags and positionals, answer the two global flags, **look the verb up in\n * `SUBCOMMANDS`**, refuse every flag that verb does not own with one generated\n * message, and let the verb do its own validation. There is no per-verb branching left\n * here — a seventh verb is a seventh file, not a seventh `if`.\n *\n * @throws {CliUsageError} on anything the user got wrong. Never guesses.\n */\nexport function parseSmeltArgs(argv: readonly string[]): CliInvocation {\n let parsed;\n try {\n parsed = parseArgs({\n args: [...argv],\n allowPositionals: true,\n strict: true,\n options: CLI_FLAGS,\n });\n } catch (cause) {\n throw new CliUsageError(\n `${CLI_NAME}: ${cause instanceof Error ? cause.message : String(cause)}\\n` +\n `Run \\`${CLI_NAME} --help\\`.`,\n );\n }\n\n const { values, positionals } = parsed;\n\n // Answered before any verb, so `smelt map --help` prints the help rather than being\n // refused for a flag `map` does not own. No verb may claim these two.\n if (values.help === true) return { mode: 'help', focus: [], json: false };\n if (values.version === true) return { mode: 'version', focus: [], json: false };\n\n const command = subcommandFor(positionals);\n refuseForeignFlags(command, values);\n return command.parse(values, positionals);\n}\n"]}
|
package/dist/cli/bin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../src/cli/bin.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../src/cli/bin.ts"],"names":[],"mappings":"","sourcesContent":["#!/usr/bin/env node\nimport { readFileSync, readSync } from 'node:fs';\nimport { isatty } from 'node:tty';\nimport process from 'node:process';\n\nimport { EXIT, runCli } from './run.ts';\n\n/**\n * The `smelt` binary: the thinnest possible shell around {@link runCli}.\n *\n * Everything decidable lives in `run.ts`, which returns an exit code instead of\n * calling `exit`, so the whole CLI is testable in-process. This file owns only the\n * things that cannot be tested without a real process: the shebang, stdin on fd 0, and\n * the exit code itself. `test/cli-bin.test.ts` spawns the *built* binary for exactly\n * those things — an in-process suite cannot see them.\n *\n * Two deliberate subtleties around fd 0:\n *\n * - **fd 0 may be non-blocking, and must still be read to EOF.** Two separate ways\n * that happens in the wild: merely accessing `process.stdin` (even `.isTTY`) makes\n * Node flip fd 0 into non-blocking mode, and a parent that spawned this process\n * with piped stdio may hand over a socketpair that was *born* non-blocking. Either\n * way a one-shot `readFileSync(0)` throws `EAGAIN` whenever the producer is slower\n * than Node's startup — `(sleep 1; echo hi) | smelt --budget 100` used to die with\n * an internal error. So: the TTY check is `tty.isatty(0)` (a plain syscall, no\n * stream initialization), `process.stdin` is only handed over lazily to the one\n * mode that needs a stream (`init`), and the read itself retries `EAGAIN` with a\n * synchronous back-off until EOF.\n * - **Bytes that are not UTF-8 are refused, never mangled.** Decoding invalid bytes\n * would silently replace them with U+FFFD, and the result would still smelt,\n * round-trip, and verify — of the wrong bytes. That violates the reversibility\n * story at its root, so the binary refuses (exit code {@link EXIT.refused}) and\n * names the first offending byte offset instead.\n */\n\n/** Reading fd 0 synchronously is the whole of \"works in a pipe\", with no stream plumbing. */\nfunction readStdin(): string {\n if (isatty(0)) {\n process.stderr.write(\n 'smelt: no input. Pass a file, or pipe text in:\\n' +\n ' smelt src/server.ts --budget 4000\\n' +\n ' smelt --budget 4000 --focus TypeError < build.log\\n',\n );\n process.exit(EXIT.usage);\n }\n const bytes = readAllOfFd0();\n const offset = firstInvalidUtf8Offset(bytes);\n if (offset !== -1) {\n process.stderr.write(\n `smelt: stdin is not valid UTF-8 — first invalid byte at offset ${String(offset)} ` +\n `(0x${bytes[offset]!.toString(16).padStart(2, '0')}). Decoding it would silently ` +\n `replace bytes with U+FFFD, and the \"original\" smelt could then reconstruct would ` +\n `not be your original. Refusing instead.\\n`,\n );\n process.exit(EXIT.refused);\n }\n return bytes.toString('utf8');\n}\n\n/** `Atomics.wait` is the only dependency-free synchronous sleep Node offers. */\nconst sleeper = new Int32Array(new SharedArrayBuffer(4));\n\n/**\n * Every byte of fd 0, to EOF, whether the descriptor is blocking (a shell pipe, a\n * redirected file) or non-blocking (a socketpair from a spawning parent). `EAGAIN`\n * means \"no bytes yet, not EOF\" — wait briefly and try again; only a zero-byte read\n * (or Windows' `EOF` on a closed pipe) ends the loop.\n */\nfunction readAllOfFd0(): Buffer {\n const chunks: Buffer[] = [];\n const chunk = Buffer.alloc(1 << 16);\n for (;;) {\n let bytesRead: number;\n try {\n bytesRead = readSync(0, chunk, 0, chunk.length, null);\n } catch (error) {\n const code = (error as { code?: string }).code;\n if (code === 'EAGAIN') {\n Atomics.wait(sleeper, 0, 0, 10); // producer not ready — 10ms, then retry\n continue;\n }\n if (code === 'EOF') break; // Windows raises EOF instead of returning 0\n throw error;\n }\n if (bytesRead === 0) break;\n chunks.push(Buffer.from(chunk.subarray(0, bytesRead)));\n }\n return Buffer.concat(chunks);\n}\n\n/**\n * The byte offset where `bytes` stops being well-formed UTF-8, or `-1` when it is\n * well-formed throughout. Implements the WHATWG/Unicode table exactly: continuation\n * ranges tightened for E0/ED/F0/F4 so overlongs, surrogates and > U+10FFFF are\n * invalid — the same sequences `Buffer.prototype.toString` would fold into U+FFFD.\n */\nfunction firstInvalidUtf8Offset(bytes: Uint8Array): number {\n let i = 0;\n while (i < bytes.length) {\n const lead = bytes[i]!;\n if (lead <= 0x7f) {\n i += 1;\n continue;\n }\n let length: number;\n let firstLow = 0x80;\n let firstHigh = 0xbf;\n if (lead >= 0xc2 && lead <= 0xdf) {\n length = 2;\n } else if (lead >= 0xe0 && lead <= 0xef) {\n length = 3;\n if (lead === 0xe0) firstLow = 0xa0; // overlong\n if (lead === 0xed) firstHigh = 0x9f; // UTF-16 surrogates\n } else if (lead >= 0xf0 && lead <= 0xf4) {\n length = 4;\n if (lead === 0xf0) firstLow = 0x90; // overlong\n if (lead === 0xf4) firstHigh = 0x8f; // above U+10FFFF\n } else {\n return i; // a bare continuation byte, or C0/C1/F5–FF — invalid as a lead\n }\n if (i + length > bytes.length) return i; // truncated sequence\n for (let k = 1; k < length; k += 1) {\n const continuation = bytes[i + k]!;\n const low = k === 1 ? firstLow : 0x80;\n const high = k === 1 ? firstHigh : 0xbf;\n if (continuation < low || continuation > high) return i;\n }\n i += length;\n }\n return -1;\n}\n\n/** `--version` should agree with the manifest, not with a second copy of the number. */\nfunction packageVersion(): string {\n const manifest = readFileSync(new URL('../../package.json', import.meta.url), 'utf8');\n const parsed = JSON.parse(manifest) as { version?: string };\n return parsed.version ?? '0.0.0';\n}\n\ntry {\n process.exitCode = await runCli(process.argv.slice(2), {\n stdout: (text) => void process.stdout.write(text),\n stderr: (text) => void process.stderr.write(text),\n stdin: readStdin,\n version: packageVersion(),\n cwd: process.cwd(),\n // `smelt init` reads answers line by line, so it gets the stream, not readStdin's\n // one-shot slurp of fd 0. A *getter*, because touching `process.stdin` at all\n // flips fd 0 into non-blocking mode and breaks readStdin's readFileSync(0) for\n // every other mode — only the `init` branch of runCli ever reads this property.\n get initInput() {\n return process.stdin;\n },\n });\n} catch (error) {\n process.stderr.write(\n `smelt: unexpected internal error — this is a bug, please report it.\\n` +\n `${error instanceof Error ? (error.stack ?? error.message) : String(error)}\\n`,\n );\n process.exitCode = EXIT.unexpected;\n}\n"]}
|
package/dist/cli/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../../src/cli/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,6FAA6F;AAC7F,SAAS,SAAS;IAChB,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;QACd,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,kDAAkD;YAChD,uCAAuC;YACvC,uDAAuD,CAC1D,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IACD,MAAM,KAAK,GAAG,YAAY,EAAE,CAAC;IAC7B,MAAM,MAAM,GAAG,sBAAsB,CAAC,KAAK,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,CAAC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C,KAAK,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QACjD,KAAK,EAAE,SAAS;QAChB,OAAO,EAAE,cAAc,EAAE;QACzB,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;QAClB,kFAAkF;QAClF,8EAA8E;QAC9E,+EAA+E;QAC/E,gFAAgF;QAChF,IAAI,SAAS;YACX,OAAO,OAAO,CAAC,KAAK,CAAC;QACvB,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAAC,OAAO,KAAK,EAAE,CAAC;IACf,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,uEAAuE;QACrE,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CACjF,CAAC;IACF,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC;AACrC,CAAC","sourcesContent":["#!/usr/bin/env node\nimport { readFileSync, readSync } from 'node:fs';\nimport { isatty } from 'node:tty';\nimport process from 'node:process';\n\nimport { EXIT, runCli } from './run.ts';\n\n/**\n * The `smelt` binary: the thinnest possible shell around {@link runCli}.\n *\n * Everything decidable lives in `run.ts`, which returns an exit code instead of\n * calling `exit`, so the whole CLI is testable in-process. This file owns only the\n * things that cannot be tested without a real process: the shebang, stdin on fd 0, and\n * the exit code itself. `test/cli-bin.test.ts` spawns the *built* binary for exactly\n * those things — an in-process suite cannot see them.\n *\n * Two deliberate subtleties around fd 0:\n *\n * - **fd 0 may be non-blocking, and must still be read to EOF.** Two separate ways\n * that happens in the wild: merely accessing `process.stdin` (even `.isTTY`) makes\n * Node flip fd 0 into non-blocking mode, and a parent that spawned this process\n * with piped stdio may hand over a socketpair that was *born* non-blocking. Either\n * way a one-shot `readFileSync(0)` throws `EAGAIN` whenever the producer is slower\n * than Node's startup — `(sleep 1; echo hi) | smelt --budget 100` used to die with\n * an internal error. So: the TTY check is `tty.isatty(0)` (a plain syscall, no\n * stream initialization), `process.stdin` is only handed over lazily to the one\n * mode that needs a stream (`init`), and the read itself retries `EAGAIN` with a\n * synchronous back-off until EOF.\n * - **Bytes that are not UTF-8 are refused, never mangled.** Decoding invalid bytes\n * would silently replace them with U+FFFD, and the result would still smelt,\n * round-trip, and verify — of the wrong bytes. That violates the reversibility\n * story at its root, so the binary refuses (exit code {@link EXIT.refused}) and\n * names the first offending byte offset instead.\n */\n\n/** Reading fd 0 synchronously is the whole of \"works in a pipe\", with no stream plumbing. */\nfunction readStdin(): string {\n if (isatty(0)) {\n process.stderr.write(\n 'smelt: no input. Pass a file, or pipe text in:\\n' +\n ' smelt src/server.ts --budget 4000\\n' +\n ' smelt --budget 4000 --focus TypeError < build.log\\n',\n );\n process.exit(EXIT.usage);\n }\n const bytes = readAllOfFd0();\n const offset = firstInvalidUtf8Offset(bytes);\n if (offset !== -1) {\n process.stderr.write(\n `smelt: stdin is not valid UTF-8 — first invalid byte at offset ${String(offset)} ` +\n `(0x${bytes[offset]!.toString(16).padStart(2, '0')}). Decoding it would silently ` +\n `replace bytes with U+FFFD, and the \"original\" smelt could then reconstruct would ` +\n `not be your original. Refusing instead.\\n`,\n );\n process.exit(EXIT.refused);\n }\n return bytes.toString('utf8');\n}\n\n/** `Atomics.wait` is the only dependency-free synchronous sleep Node offers. */\nconst sleeper = new Int32Array(new SharedArrayBuffer(4));\n\n/**\n * Every byte of fd 0, to EOF, whether the descriptor is blocking (a shell pipe, a\n * redirected file) or non-blocking (a socketpair from a spawning parent). `EAGAIN`\n * means \"no bytes yet, not EOF\" — wait briefly and try again; only a zero-byte read\n * (or Windows' `EOF` on a closed pipe) ends the loop.\n */\nfunction readAllOfFd0(): Buffer {\n const chunks: Buffer[] = [];\n const chunk = Buffer.alloc(1 << 16);\n for (;;) {\n let bytesRead: number;\n try {\n bytesRead = readSync(0, chunk, 0, chunk.length, null);\n } catch (error) {\n const code = (error as { code?: string }).code;\n if (code === 'EAGAIN') {\n Atomics.wait(sleeper, 0, 0, 10); // producer not ready — 10ms, then retry\n continue;\n }\n if (code === 'EOF') break; // Windows raises EOF instead of returning 0\n throw error;\n }\n if (bytesRead === 0) break;\n chunks.push(Buffer.from(chunk.subarray(0, bytesRead)));\n }\n return Buffer.concat(chunks);\n}\n\n/**\n * The byte offset where `bytes` stops being well-formed UTF-8, or `-1` when it is\n * well-formed throughout. Implements the WHATWG/Unicode table exactly: continuation\n * ranges tightened for E0/ED/F0/F4 so overlongs, surrogates and > U+10FFFF are\n * invalid — the same sequences `Buffer.prototype.toString` would fold into U+FFFD.\n */\nfunction firstInvalidUtf8Offset(bytes: Uint8Array): number {\n let i = 0;\n while (i < bytes.length) {\n const lead = bytes[i]!;\n if (lead <= 0x7f) {\n i += 1;\n continue;\n }\n let length: number;\n let firstLow = 0x80;\n let firstHigh = 0xbf;\n if (lead >= 0xc2 && lead <= 0xdf) {\n length = 2;\n } else if (lead >= 0xe0 && lead <= 0xef) {\n length = 3;\n if (lead === 0xe0) firstLow = 0xa0; // overlong\n if (lead === 0xed) firstHigh = 0x9f; // UTF-16 surrogates\n } else if (lead >= 0xf0 && lead <= 0xf4) {\n length = 4;\n if (lead === 0xf0) firstLow = 0x90; // overlong\n if (lead === 0xf4) firstHigh = 0x8f; // above U+10FFFF\n } else {\n return i; // a bare continuation byte, or C0/C1/F5–FF — invalid as a lead\n }\n if (i + length > bytes.length) return i; // truncated sequence\n for (let k = 1; k < length; k += 1) {\n const continuation = bytes[i + k]!;\n const low = k === 1 ? firstLow : 0x80;\n const high = k === 1 ? firstHigh : 0xbf;\n if (continuation < low || continuation > high) return i;\n }\n i += length;\n }\n return -1;\n}\n\n/** `--version` should agree with the manifest, not with a second copy of the number. */\nfunction packageVersion(): string {\n const manifest = readFileSync(new URL('../../package.json', import.meta.url), 'utf8');\n const parsed = JSON.parse(manifest) as { version?: string };\n return parsed.version ?? '0.0.0';\n}\n\ntry {\n process.exitCode = await runCli(process.argv.slice(2), {\n stdout: (text) => void process.stdout.write(text),\n stderr: (text) => void process.stderr.write(text),\n stdin: readStdin,\n version: packageVersion(),\n cwd: process.cwd(),\n // `smelt init` reads answers line by line, so it gets the stream, not readStdin's\n // one-shot slurp of fd 0. A *getter*, because touching `process.stdin` at all\n // flips fd 0 into non-blocking mode and breaks readStdin's readFileSync(0) for\n // every other mode — only the `init` branch of runCli ever reads this property.\n get initInput() {\n return process.stdin;\n },\n });\n} catch (error) {\n process.stderr.write(\n `smelt: unexpected internal error — this is a bug, please report it.\\n` +\n `${error instanceof Error ? (error.stack ?? error.message) : String(error)}\\n`,\n );\n process.exitCode = EXIT.unexpected;\n}\n"]}
|
package/dist/cli/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { EnforcementMode } from '../hooks/guard-core.ts';
|
|
1
2
|
import type { Strategy } from '../plan/planners.ts';
|
|
2
3
|
/**
|
|
3
4
|
* `smelt.config.json` — CLI defaults, and nothing more.
|
|
@@ -27,6 +28,25 @@ export type SmeltConfigStore = {
|
|
|
27
28
|
/** Resolved relative to the directory holding the config file, not the cwd. */
|
|
28
29
|
readonly path: string;
|
|
29
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* The `hooks` block — settings for the harness guard (`smelt hooks install`).
|
|
33
|
+
*
|
|
34
|
+
* Parsed strictly here, like every other key: the CLI refuses a malformed config.
|
|
35
|
+
* The guard core (`src/hooks/guard-core.ts`) reads the same file with its own
|
|
36
|
+
* *tolerant* reader — a guard running inside somebody's session fails open where the
|
|
37
|
+
* CLI correctly refuses — and `test/hooks-guard-core.test.ts` pins the two readers to
|
|
38
|
+
* the same key names and defaults so they cannot drift apart.
|
|
39
|
+
*/
|
|
40
|
+
export interface SmeltConfigHooks {
|
|
41
|
+
/** Reads at or under this many bytes pass the guard untouched. Default 8192. */
|
|
42
|
+
readonly thresholdBytes?: number;
|
|
43
|
+
/**
|
|
44
|
+
* `'deny'` (default): oversized raw reads are refused with a reason naming the
|
|
45
|
+
* exact replacement command. `'rewrite'`: on harnesses whose hooks can modify tool
|
|
46
|
+
* input, the command is substituted instead (never silently — the reason says so).
|
|
47
|
+
*/
|
|
48
|
+
readonly enforcement?: EnforcementMode;
|
|
49
|
+
}
|
|
30
50
|
/** The parsed shape of `smelt.config.json`. Every field beyond the version is optional. */
|
|
31
51
|
export interface SmeltConfig {
|
|
32
52
|
readonly smeltConfig: typeof CONFIG_VERSION;
|
|
@@ -36,6 +56,8 @@ export interface SmeltConfig {
|
|
|
36
56
|
readonly strategy?: Strategy;
|
|
37
57
|
/** Used for every run; there is no store flag. Defaults to memory when absent. */
|
|
38
58
|
readonly store?: SmeltConfigStore;
|
|
59
|
+
/** Settings for the harness guard. See {@link SmeltConfigHooks}. */
|
|
60
|
+
readonly hooks?: SmeltConfigHooks;
|
|
39
61
|
}
|
|
40
62
|
/** A config plus where it was found — the path matters for resolving `store.path`. */
|
|
41
63
|
export interface LoadedConfig {
|
|
@@ -68,6 +90,46 @@ export declare function loadNearestConfig(cwd: string): LoadedConfig | undefined
|
|
|
68
90
|
* @throws {CliUsageError} naming the file and the exact problem.
|
|
69
91
|
*/
|
|
70
92
|
export declare function parseConfig(text: string, path: string): SmeltConfig;
|
|
93
|
+
/**
|
|
94
|
+
* Serialize a config back to the bytes of a `smelt.config.json` — {@link parseConfig}'s
|
|
95
|
+
* inverse, and the **only** writer of this file.
|
|
96
|
+
*
|
|
97
|
+
* The module that owns the schema owns both directions. It used to own only the read:
|
|
98
|
+
* two modules hand-built a config object and stringified it — the `init` wizard and
|
|
99
|
+
* `hooks install` — and they had already drifted, one always emitting `strategy` and
|
|
100
|
+
* the other only when it was carried. A sixth key would have been written by whichever
|
|
101
|
+
* module its author was editing and silently dropped by the other, which is the
|
|
102
|
+
* "setting the user believed was in force" failure this file refuses everywhere else.
|
|
103
|
+
*
|
|
104
|
+
* Key order is fixed here — the reader's own key order, top level and inside `store`
|
|
105
|
+
* and `hooks` — so a re-run diffs cleanly no matter which verb wrote the file, and two
|
|
106
|
+
* writers can never disagree about shape. What goes *into* the config stays each
|
|
107
|
+
* verb's policy: the wizard's choices, and `hooks install`'s directory-store injection.
|
|
108
|
+
* Absent keys are omitted rather than written as `null`, so
|
|
109
|
+
* `parseConfig(renderConfig(c))` returns `c` field for field —
|
|
110
|
+
* `test/guards/config-writer.test.ts` pins that round trip.
|
|
111
|
+
*/
|
|
112
|
+
export declare function renderConfig(config: SmeltConfig): string;
|
|
71
113
|
/** `store.path` is relative to the config file, so the config works from any cwd. */
|
|
72
114
|
export declare function resolveStorePath(loaded: LoadedConfig, path: string): string;
|
|
115
|
+
/**
|
|
116
|
+
* The store decision a config carries, with `path` already resolved against the config
|
|
117
|
+
* file's directory — one config serves every subdirectory it covers without scattering
|
|
118
|
+
* store roots. `'memory'` is the built-in default: a fresh in-memory store per run.
|
|
119
|
+
*/
|
|
120
|
+
export type ConfiguredStore = {
|
|
121
|
+
readonly kind: 'memory';
|
|
122
|
+
} | {
|
|
123
|
+
readonly kind: 'directory';
|
|
124
|
+
readonly path: string;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* The store leg of every merge, read once here rather than in each verb that needs it.
|
|
128
|
+
*
|
|
129
|
+
* There is no store flag, so this leg is config-or-built-in only — which is exactly
|
|
130
|
+
* why it belongs to the config module and not to a verb: `smelt` builds a store from
|
|
131
|
+
* it, and `retrieve`/`stats` refuse when it is not a directory (see `resolveStoreRun`
|
|
132
|
+
* in `subcommands/retrieve.ts`). Three verbs, one reading of the same key.
|
|
133
|
+
*/
|
|
134
|
+
export declare function configuredStore(config: LoadedConfig | undefined): ConfiguredStore;
|
|
73
135
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/cli/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/cli/config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAIpD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M,GAAG,MAAM,GAAG,SAAS,CAS9D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAIvE;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CA+DnE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAWxD;AAiFD,qFAAqF;AACrF,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE3E;AAED;;;;GAIG;AACH,MAAM,MAAM,eAAe,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtF;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,GAAG,eAAe,CAKjF","sourcesContent":["import { existsSync, readFileSync } from 'node:fs';\nimport { dirname, join, resolve } from 'node:path';\n\nimport { CliUsageError } from '../errors.ts';\nimport { ENFORCEMENT_MODES } from '../hooks/guard-core.ts';\nimport type { EnforcementMode } from '../hooks/guard-core.ts';\nimport { isStrategy, STRATEGIES } from '../plan/planners.ts';\nimport type { Strategy } from '../plan/planners.ts';\n\nimport { CLI_NAME } from './shell.ts';\n\n/**\n * `smelt.config.json` — CLI defaults, and nothing more.\n *\n * This file exists so `smelt` can be run without retyping the same flags, and it is\n * deliberately a *CLI* concern: the programmatic API never reads it. A library that\n * reads config files off the caller's disk is a library whose behaviour depends on\n * where it was invoked from, which is exactly the class of invisible input smelt\n * refuses. `createSmelter()` takes explicit arguments; the CLI translates this file\n * into those arguments, and explicit flags always win over it.\n *\n * The schema is versioned (`\"smeltConfig\": 1`) for the same reason the marker and the\n * `--json` envelope are: files outlive the binaries that wrote them, and a mismatch\n * must be identifiable rather than half-understood. Parsing is strict — an unknown\n * key, a wrong type, or an unknown version is a {@link CliUsageError}, never a shrug.\n * A config the CLI silently ignored would be a budget the user *thought* they set.\n */\n\n/** The file name looked for, from the working directory upward. */\nexport const CONFIG_FILE_NAME = 'smelt.config.json';\n\n/** The schema version this build reads and writes. */\nexport const CONFIG_VERSION = 1;\n\n/** Where the CLI's elision store lives between runs. */\nexport type SmeltConfigStore =\n | { readonly kind: 'memory' }\n | {\n readonly kind: 'directory';\n /** Resolved relative to the directory holding the config file, not the cwd. */\n readonly path: string;\n };\n\n/**\n * The `hooks` block — settings for the harness guard (`smelt hooks install`).\n *\n * Parsed strictly here, like every other key: the CLI refuses a malformed config.\n * The guard core (`src/hooks/guard-core.ts`) reads the same file with its own\n * *tolerant* reader — a guard running inside somebody's session fails open where the\n * CLI correctly refuses — and `test/hooks-guard-core.test.ts` pins the two readers to\n * the same key names and defaults so they cannot drift apart.\n */\nexport interface SmeltConfigHooks {\n /** Reads at or under this many bytes pass the guard untouched. Default 8192. */\n readonly thresholdBytes?: number;\n /**\n * `'deny'` (default): oversized raw reads are refused with a reason naming the\n * exact replacement command. `'rewrite'`: on harnesses whose hooks can modify tool\n * input, the command is substituted instead (never silently — the reason says so).\n */\n readonly enforcement?: EnforcementMode;\n}\n\n/** The parsed shape of `smelt.config.json`. Every field beyond the version is optional. */\nexport interface SmeltConfig {\n readonly smeltConfig: typeof CONFIG_VERSION;\n /** Used when a `smelt` run omits `--budget`. UTF-8 bytes, like every smelt budget. */\n readonly defaultBudgetBytes?: number;\n /** Used when a run omits `--strategy`. Validated against the {@link PLANNERS} registry. */\n readonly strategy?: Strategy;\n /** Used for every run; there is no store flag. Defaults to memory when absent. */\n readonly store?: SmeltConfigStore;\n /** Settings for the harness guard. See {@link SmeltConfigHooks}. */\n readonly hooks?: SmeltConfigHooks;\n}\n\n/** A config plus where it was found — the path matters for resolving `store.path`. */\nexport interface LoadedConfig {\n readonly path: string;\n readonly config: SmeltConfig;\n}\n\n/**\n * The nearest `smelt.config.json`, walking up from `cwd` to the filesystem root —\n * the same discovery shape as `package.json`, so a config at a repo root covers the\n * whole tree. `undefined` when there is none, which is not an error: flags alone are\n * a complete interface.\n */\nexport function findConfigFile(cwd: string): string | undefined {\n let dir = resolve(cwd);\n for (;;) {\n const candidate = join(dir, CONFIG_FILE_NAME);\n if (existsSync(candidate)) return candidate;\n const parent = dirname(dir);\n if (parent === dir) return undefined;\n dir = parent;\n }\n}\n\n/**\n * Find and parse the nearest config. `undefined` when no file exists; a file that\n * exists but is malformed **throws** — see {@link parseConfig}. The distinction is the\n * point: \"no config\" is a fine state, \"a config you cannot have meant\" is not.\n *\n * @throws {CliUsageError} when a config file exists and is malformed.\n */\nexport function loadNearestConfig(cwd: string): LoadedConfig | undefined {\n const path = findConfigFile(cwd);\n if (path === undefined) return undefined;\n return { path, config: parseConfig(readFileSync(path, 'utf8'), path) };\n}\n\n/**\n * Parse and validate one config file, strictly.\n *\n * Strict means: unknown keys are refused, not skipped. A typo'd `defaultBudgetByte`\n * that parsed cleanly would leave the user believing a default was set while every\n * run ignored it — a config error whose failure mode is *silence*, which is the one\n * failure shape this project refuses everywhere else too.\n *\n * @throws {CliUsageError} naming the file and the exact problem.\n */\nexport function parseConfig(text: string, path: string): SmeltConfig {\n const bad = (why: string): CliUsageError =>\n new CliUsageError(\n `${CLI_NAME}: malformed ${CONFIG_FILE_NAME} at ${path}: ${why}\\n` +\n `Fix it or delete it — a config smelt cannot read is never silently ignored. ` +\n `\\`${CLI_NAME} init\\` can rewrite it.`,\n );\n\n let value: unknown;\n try {\n value = JSON.parse(text);\n } catch (cause) {\n throw bad(`not JSON (${cause instanceof Error ? cause.message : String(cause)}).`);\n }\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n throw bad('expected a JSON object at the top level.');\n }\n const fields = value as Record<string, unknown>;\n\n if (fields['smeltConfig'] !== CONFIG_VERSION) {\n throw bad(\n `\"smeltConfig\" is ${JSON.stringify(fields['smeltConfig'])}; this build reads version ` +\n `${String(CONFIG_VERSION)}. The schema is versioned so a mismatch is visible ` +\n `instead of half-understood.`,\n );\n }\n\n const known = ['smeltConfig', 'defaultBudgetBytes', 'strategy', 'store', 'hooks'];\n const unknown = Object.keys(fields).filter((key) => !known.includes(key));\n if (unknown.length > 0) {\n throw bad(\n `unknown key${unknown.length === 1 ? '' : 's'} ${unknown.map((k) => `\"${k}\"`).join(', ')}. ` +\n `Known keys: ${known.join(', ')}. Unknown keys are refused because a typo that ` +\n `parsed cleanly would be a setting you believed was set.`,\n );\n }\n\n const budget = fields['defaultBudgetBytes'];\n if (budget !== undefined && (typeof budget !== 'number' || !Number.isInteger(budget))) {\n throw bad(`\"defaultBudgetBytes\" must be a whole number of UTF-8 bytes.`);\n }\n if (typeof budget === 'number' && budget <= 0) {\n throw bad(`\"defaultBudgetBytes\" must be greater than zero, got ${String(budget)}.`);\n }\n\n const strategy = fields['strategy'];\n if (strategy !== undefined && (typeof strategy !== 'string' || !isStrategy(strategy))) {\n throw bad(\n `\"strategy\" must be ${STRATEGIES.map((s) => `\"${s}\"`).join(' or ')}, ` +\n `got ${JSON.stringify(strategy)}.`,\n );\n }\n\n const store = parseStore(fields['store'], bad);\n const hooks = parseHooks(fields['hooks'], bad);\n\n return {\n smeltConfig: CONFIG_VERSION,\n ...(budget === undefined ? {} : { defaultBudgetBytes: budget }),\n ...(strategy === undefined ? {} : { strategy }),\n ...(store === undefined ? {} : { store }),\n ...(hooks === undefined ? {} : { hooks }),\n };\n}\n\n/**\n * Serialize a config back to the bytes of a `smelt.config.json` — {@link parseConfig}'s\n * inverse, and the **only** writer of this file.\n *\n * The module that owns the schema owns both directions. It used to own only the read:\n * two modules hand-built a config object and stringified it — the `init` wizard and\n * `hooks install` — and they had already drifted, one always emitting `strategy` and\n * the other only when it was carried. A sixth key would have been written by whichever\n * module its author was editing and silently dropped by the other, which is the\n * \"setting the user believed was in force\" failure this file refuses everywhere else.\n *\n * Key order is fixed here — the reader's own key order, top level and inside `store`\n * and `hooks` — so a re-run diffs cleanly no matter which verb wrote the file, and two\n * writers can never disagree about shape. What goes *into* the config stays each\n * verb's policy: the wizard's choices, and `hooks install`'s directory-store injection.\n * Absent keys are omitted rather than written as `null`, so\n * `parseConfig(renderConfig(c))` returns `c` field for field —\n * `test/guards/config-writer.test.ts` pins that round trip.\n */\nexport function renderConfig(config: SmeltConfig): string {\n const ordered: SmeltConfig = {\n smeltConfig: CONFIG_VERSION,\n ...(config.defaultBudgetBytes === undefined\n ? {}\n : { defaultBudgetBytes: config.defaultBudgetBytes }),\n ...(config.strategy === undefined ? {} : { strategy: config.strategy }),\n ...(config.store === undefined ? {} : { store: renderStore(config.store) }),\n ...(config.hooks === undefined ? {} : { hooks: renderHooks(config.hooks) }),\n };\n return `${JSON.stringify(ordered, null, 2)}\\n`;\n}\n\nfunction renderStore(store: SmeltConfigStore): SmeltConfigStore {\n return store.kind === 'memory' ? { kind: 'memory' } : { kind: 'directory', path: store.path };\n}\n\nfunction renderHooks(hooks: SmeltConfigHooks): SmeltConfigHooks {\n return {\n ...(hooks.thresholdBytes === undefined ? {} : { thresholdBytes: hooks.thresholdBytes }),\n ...(hooks.enforcement === undefined ? {} : { enforcement: hooks.enforcement }),\n };\n}\n\nfunction parseStore(\n value: unknown,\n bad: (why: string) => CliUsageError,\n): SmeltConfigStore | undefined {\n if (value === undefined) return undefined;\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n throw bad(`\"store\" must be an object like {\"kind\":\"memory\"} or {\"kind\":\"directory\",\"path\":…}.`);\n }\n const fields = value as Record<string, unknown>;\n const kind = fields['kind'];\n if (kind === 'memory') {\n const extra = Object.keys(fields).filter((key) => key !== 'kind');\n if (extra.length > 0) throw bad(`\"store\" of kind \"memory\" takes no other keys.`);\n return { kind: 'memory' };\n }\n if (kind === 'directory') {\n const path = fields['path'];\n if (typeof path !== 'string' || path === '') {\n throw bad(`\"store\" of kind \"directory\" needs a non-empty \"path\".`);\n }\n const extra = Object.keys(fields).filter((key) => key !== 'kind' && key !== 'path');\n if (extra.length > 0) throw bad(`\"store\" of kind \"directory\" takes only \"kind\" and \"path\".`);\n return { kind: 'directory', path };\n }\n throw bad(`\"store\".kind must be \"memory\" or \"directory\", got ${JSON.stringify(kind)}.`);\n}\n\nfunction parseHooks(\n value: unknown,\n bad: (why: string) => CliUsageError,\n): SmeltConfigHooks | undefined {\n if (value === undefined) return undefined;\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n throw bad(`\"hooks\" must be an object like {\"thresholdBytes\":8192,\"enforcement\":\"deny\"}.`);\n }\n const fields = value as Record<string, unknown>;\n const extra = Object.keys(fields).filter(\n (key) => key !== 'thresholdBytes' && key !== 'enforcement',\n );\n if (extra.length > 0) {\n throw bad(\n `\"hooks\" takes only \"thresholdBytes\" and \"enforcement\", ` +\n `got ${extra.map((k) => `\"${k}\"`).join(', ')}.`,\n );\n }\n const threshold = fields['thresholdBytes'];\n if (\n threshold !== undefined &&\n (typeof threshold !== 'number' || !Number.isInteger(threshold) || threshold <= 0)\n ) {\n throw bad(`\"hooks\".thresholdBytes must be a whole number of bytes greater than zero.`);\n }\n const enforcement = fields['enforcement'];\n if (\n enforcement !== undefined &&\n !(ENFORCEMENT_MODES as readonly unknown[]).includes(enforcement)\n ) {\n throw bad(\n `\"hooks\".enforcement must be ${ENFORCEMENT_MODES.map((m) => `\"${m}\"`).join(' or ')}, ` +\n `got ${JSON.stringify(enforcement)}.`,\n );\n }\n return {\n ...(threshold === undefined ? {} : { thresholdBytes: threshold as number }),\n ...(enforcement === undefined ? {} : { enforcement: enforcement as EnforcementMode }),\n };\n}\n\n/** `store.path` is relative to the config file, so the config works from any cwd. */\nexport function resolveStorePath(loaded: LoadedConfig, path: string): string {\n return resolve(dirname(loaded.path), path);\n}\n\n/**\n * The store decision a config carries, with `path` already resolved against the config\n * file's directory — one config serves every subdirectory it covers without scattering\n * store roots. `'memory'` is the built-in default: a fresh in-memory store per run.\n */\nexport type ConfiguredStore =\n { readonly kind: 'memory' } | { readonly kind: 'directory'; readonly path: string };\n\n/**\n * The store leg of every merge, read once here rather than in each verb that needs it.\n *\n * There is no store flag, so this leg is config-or-built-in only — which is exactly\n * why it belongs to the config module and not to a verb: `smelt` builds a store from\n * it, and `retrieve`/`stats` refuse when it is not a directory (see `resolveStoreRun`\n * in `subcommands/retrieve.ts`). Three verbs, one reading of the same key.\n */\nexport function configuredStore(config: LoadedConfig | undefined): ConfiguredStore {\n if (config?.config.store === undefined || config.config.store.kind === 'memory') {\n return { kind: 'memory' };\n }\n return { kind: 'directory', path: resolveStorePath(config, config.config.store.path) };\n}\n"]}
|