@sous-io/sous 0.1.1 → 0.2.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 +115 -35
- package/bin/run.js +10 -1
- package/docs/markdown/README.md +27 -0
- package/docs/markdown/_sidebar.md +18 -0
- package/docs/markdown/commands.md +308 -0
- package/docs/markdown/config-discovery.md +74 -0
- package/docs/markdown/config-inspection.md +69 -0
- package/docs/markdown/config-layers.md +92 -0
- package/docs/markdown/config-variables.md +79 -0
- package/docs/markdown/configuration.md +71 -0
- package/docs/markdown/design-principles.md +59 -0
- package/docs/markdown/repositories-authoring.md +409 -0
- package/docs/markdown/repositories-consuming.md +580 -0
- package/docs/markdown/repositories-file-formats.md +1084 -0
- package/docs/markdown/repositories-variables.md +387 -0
- package/docs/markdown/repositories.md +303 -0
- package/docs/markdown/skill-categories.md +58 -0
- package/package.json +72 -8
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/SKILL.tpl.md +20 -20
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/about-something.md +2 -2
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/do-something.md +1 -1
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/advanced-patterns.md +6 -6
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/commands.md +5 -5
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/frontmatter.md +3 -3
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/SKILL.tpl.md +40 -25
- package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +70 -0
- package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +75 -0
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/create-skill/SKILL.tpl.md +8 -9
- package/recipes/core/sous-skills/sous.recipe.yaml +45 -0
- package/sous.config.schema.json +337 -0
- package/src/base-command.ts +220 -67
- package/src/commands/build.ts +150 -73
- package/src/commands/clear.ts +23 -15
- package/src/commands/compile.ts +74 -16
- package/src/commands/config/get.ts +110 -0
- package/src/commands/config/show.ts +32 -0
- package/src/commands/config/validate.ts +53 -0
- package/src/commands/help.ts +46 -0
- package/src/commands/launch.ts +36 -14
- package/src/commands/lock/rebuild.ts +241 -0
- package/src/commands/lock/show.ts +115 -0
- package/src/commands/namespace/list.ts +117 -0
- package/src/commands/namespace/show.ts +110 -0
- package/src/commands/prune.ts +3 -11
- package/src/commands/recipe/list.ts +95 -0
- package/src/commands/recipe/show.ts +301 -0
- package/src/commands/repo/add.ts +145 -0
- package/src/commands/repo/gc.ts +172 -0
- package/src/commands/repo/init.ts +136 -0
- package/src/commands/repo/link.ts +500 -0
- package/src/commands/repo/list.ts +179 -0
- package/src/commands/repo/release.ts +625 -0
- package/src/commands/repo/remove.ts +193 -0
- package/src/commands/repo/search.ts +189 -0
- package/src/commands/repo/submit.ts +133 -0
- package/src/commands/repo/unlink.ts +147 -0
- package/src/commands/subscription/add.ts +285 -0
- package/src/commands/subscription/list.ts +129 -0
- package/src/commands/subscription/remove.ts +181 -0
- package/src/commands/vars/ask.ts +374 -0
- package/src/commands/vars/index.ts +79 -0
- package/src/commands/vars/list.ts +67 -0
- package/src/commands/vars/show.ts +77 -0
- package/src/config-command.ts +30 -0
- package/src/lib/build-service.ts +206 -54
- package/src/lib/config-discovery.ts +220 -27
- package/src/lib/config-inspect.ts +145 -0
- package/src/lib/config-kernel.mjs +377 -0
- package/src/lib/config-schema.ts +361 -0
- package/src/lib/env-file.ts +328 -0
- package/src/lib/env-local.ts +18 -1
- package/src/lib/errors.ts +32 -0
- package/src/lib/include-resolver.ts +108 -15
- package/src/lib/interactive.ts +165 -0
- package/src/lib/markdown-compiler.ts +118 -37
- package/src/lib/package-info.ts +25 -0
- package/src/lib/pid-service.ts +32 -21
- package/src/lib/refs/find.ts +589 -0
- package/src/lib/refs/index.ts +12 -0
- package/src/lib/refs/pick.ts +147 -0
- package/src/lib/refs/scopes.ts +61 -0
- package/src/lib/repos/catalog-display.ts +116 -0
- package/src/lib/repos/catalog-inputs.ts +160 -0
- package/src/lib/repos/catalog.ts +722 -0
- package/src/lib/repos/core-recipe.ts +105 -0
- package/src/lib/repos/defaults.ts +175 -0
- package/src/lib/repos/formats/common.ts +389 -0
- package/src/lib/repos/formats/index-file.ts +215 -0
- package/src/lib/repos/formats/links-map.ts +96 -0
- package/src/lib/repos/formats/lockfile.ts +167 -0
- package/src/lib/repos/formats/patterns.ts +57 -0
- package/src/lib/repos/formats/recipe-manifest.ts +395 -0
- package/src/lib/repos/formats/repo-manifest.ts +88 -0
- package/src/lib/repos/formats/store-entry.ts +84 -0
- package/src/lib/repos/freshness.ts +208 -0
- package/src/lib/repos/git-clone.ts +312 -0
- package/src/lib/repos/identity.ts +89 -0
- package/src/lib/repos/index.ts +58 -0
- package/src/lib/repos/links.ts +353 -0
- package/src/lib/repos/load-manifest.ts +236 -0
- package/src/lib/repos/lock-service.ts +453 -0
- package/src/lib/repos/locked-namespace-resolver.ts +90 -0
- package/src/lib/repos/locked-recipes.ts +254 -0
- package/src/lib/repos/managed-layer.ts +422 -0
- package/src/lib/repos/namespace-resolver.ts +370 -0
- package/src/lib/repos/providers/base.ts +206 -0
- package/src/lib/repos/providers/git.ts +233 -0
- package/src/lib/repos/providers/github.ts +294 -0
- package/src/lib/repos/providers/gitlab.ts +263 -0
- package/src/lib/repos/providers/http.ts +102 -0
- package/src/lib/repos/providers/index-cache.ts +382 -0
- package/src/lib/repos/providers/index.ts +106 -0
- package/src/lib/repos/providers/local.ts +391 -0
- package/src/lib/repos/providers/provider.ts +401 -0
- package/src/lib/repos/recipe-config-layers.ts +287 -0
- package/src/lib/repos/recipe-targets.ts +223 -0
- package/src/lib/repos/ref-search.ts +46 -0
- package/src/lib/repos/ref.ts +513 -0
- package/src/lib/repos/reference-report.ts +122 -0
- package/src/lib/repos/release/bump.ts +161 -0
- package/src/lib/repos/release/git-state.ts +305 -0
- package/src/lib/repos/release/index-builder.ts +635 -0
- package/src/lib/repos/release/index.ts +16 -0
- package/src/lib/repos/release/plan.ts +512 -0
- package/src/lib/repos/release/submit-service.ts +496 -0
- package/src/lib/repos/release/tags.ts +243 -0
- package/src/lib/repos/release/validate.ts +463 -0
- package/src/lib/repos/resolver.ts +789 -0
- package/src/lib/repos/scaffold/index.ts +238 -0
- package/src/lib/repos/scaffold/templates.ts +415 -0
- package/src/lib/repos/seed.ts +414 -0
- package/src/lib/repos/store/contract.ts +64 -0
- package/src/lib/repos/store/hash.ts +114 -0
- package/src/lib/repos/store/recipe-store.ts +599 -0
- package/src/lib/repos/store/settings.ts +58 -0
- package/src/lib/repos/subscription-service.ts +2678 -0
- package/src/lib/repos/trust.ts +447 -0
- package/src/lib/settings.ts +546 -189
- package/src/lib/sous-home.ts +104 -0
- package/src/lib/state.ts +52 -20
- package/src/lib/vars/ask.ts +1152 -0
- package/src/lib/vars/definition-source.ts +252 -0
- package/src/lib/vars/display.ts +233 -0
- package/src/lib/vars/index.ts +18 -0
- package/src/lib/vars/ladder.ts +282 -0
- package/src/lib/vars/mappings.ts +265 -0
- package/src/lib/vars/names.ts +94 -0
- package/src/lib/vars/preanswers.ts +395 -0
- package/src/lib/vars/question-plan.ts +218 -0
- package/src/lib/vars/report.ts +228 -0
- package/src/lib/vars/safe-regex.ts +235 -0
- package/src/lib/vars/validate.ts +312 -0
- package/src/lib/watch-loop.ts +148 -0
- package/src/templating/init-liquid-engine.ts +58 -16
- package/src/utils/choice-prompt.ts +143 -0
- package/src/utils/command-errors.ts +186 -0
- package/src/utils/command-help.ts +45 -0
- package/src/utils/confirm-prompt.ts +110 -0
- package/src/utils/flags.ts +153 -0
- package/src/utils/formatting.ts +540 -55
- package/src/utils/prompts.ts +35 -1
- package/src/utils/sous-directory.ts +245 -0
- package/src/utils/table.ts +603 -0
- package/src/utils/value-prompt.ts +119 -0
- package/shared-prompts/_partials/resume-task.md +0 -51
- package/shared-prompts/_partials/sub-agent-delegation.md +0 -32
- package/shared-prompts/_partials/update-task-file.md +0 -52
- package/shared-prompts/memories/automated-browser-tasks/INDEX.tpl.md +0 -52
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/SKILL.tpl.md +0 -102
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/auth-failure-handling.mjs +0 -81
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/chained-workflow.mjs +0 -126
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/simple-fetch.mjs +0 -92
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/architecture.md +0 -61
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/auth-and-sessions.md +0 -65
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/ctx-api.md +0 -96
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/installation.md +0 -104
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/script-conventions.md +0 -243
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/chrome-state.mjs +0 -148
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.mjs +0 -383
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.spec.mjs +0 -267
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/eslint.config.mjs +0 -56
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/harness.mjs +0 -169
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/keyring.mjs +0 -59
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/logger.mjs +0 -25
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/params.mjs +0 -140
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/run.mjs +0 -140
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/settings.tpl.mjs +0 -1
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/utils.mjs +0 -185
- package/shared-prompts/skills/automated-browser-tasks/create-automated-browser-task/SKILL.tpl.md +0 -52
- package/shared-prompts/skills/automated-browser-tasks/running-automated-browser-tasks/SKILL.tpl.md +0 -59
- package/shared-prompts/skills/automated-browser-tasks/update-automated-browser-task/SKILL.tpl.md +0 -47
- package/shared-prompts/skills/control-flow/approve/SKILL.tpl.md +0 -26
- package/shared-prompts/skills/control-flow/opine/SKILL.tpl.md +0 -58
- package/shared-prompts/skills/control-flow/repeat/SKILL.tpl.md +0 -27
- package/shared-prompts/skills/control-flow/research/SKILL.tpl.md +0 -34
- package/shared-prompts/skills/sous-skills/about-sous/SKILL.tpl.md +0 -51
- package/shared-prompts/skills/task-files/about-task-files/SKILL.tpl.md +0 -122
- package/shared-prompts/skills/task-files/continue-task-in-new-branch/SKILL.tpl.md +0 -80
- package/shared-prompts/skills/task-files/go/SKILL.tpl.md +0 -14
- package/shared-prompts/skills/task-files/resume-task/SKILL.tpl.md +0 -13
- package/shared-prompts/skills/task-files/start-task/SKILL.tpl.md +0 -93
- package/shared-prompts/skills/task-files/update/SKILL.tpl.md +0 -14
- package/shared-prompts/skills/task-files/update-task-file/SKILL.tpl.md +0 -13
- /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/substitutions.md +0 -0
- /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/references/liquid-filters.md +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The core recipe that ships inside the sous package.
|
|
3
|
+
*
|
|
4
|
+
* One namespace is special: `core`, the skills that teach an agent what sous is
|
|
5
|
+
* and how it works. Every project gets it, and a project that has never seen a
|
|
6
|
+
* network must still get it, so a copy of the recipe lives in the package at
|
|
7
|
+
* `recipes/core/sous-skills/` and seeds the machine-wide store on first run.
|
|
8
|
+
*
|
|
9
|
+
* Two rules hold this arrangement together:
|
|
10
|
+
*
|
|
11
|
+
* - VERSION PARITY. The packaged recipe's version is always exactly the
|
|
12
|
+
* version of the sous package shipping it, and the implicit `core`
|
|
13
|
+
* subscription's range is exactly the running sous version. Core therefore
|
|
14
|
+
* always matches the CLI, and upgrading sous upgrades core with it.
|
|
15
|
+
* `core-recipe.spec.ts` enforces the first half of that rule, and
|
|
16
|
+
* `scripts/sync-core-version.mts` is what satisfies it; the release
|
|
17
|
+
* pipeline (`.github/workflows/publish.yml`) enforces it again at publish
|
|
18
|
+
* time.
|
|
19
|
+
* - NO DEPENDENCIES. The packaged copy is the offline seed, so nothing in it
|
|
20
|
+
* may point at a recipe that has to be fetched.
|
|
21
|
+
*
|
|
22
|
+
* The same recipe is also published by the official repository, so an online
|
|
23
|
+
* project resolves it exactly like any other recipe; the package copy is a seed,
|
|
24
|
+
* not a private fork.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import path from "node:path";
|
|
28
|
+
import { CLI_ROOT, SOUS_VERSION } from "../package-info.js";
|
|
29
|
+
import { ConfigError } from "../errors.js";
|
|
30
|
+
import { findRecipeManifest, loadManifestFile } from "./load-manifest.js";
|
|
31
|
+
import { parseRecipeManifest, type RecipeManifest } from "./formats/recipe-manifest.js";
|
|
32
|
+
|
|
33
|
+
/** The directory inside the package that holds every recipe sous ships. */
|
|
34
|
+
export const PACKAGED_RECIPES_DIRNAME = "recipes";
|
|
35
|
+
|
|
36
|
+
/** The short name the official repository is recorded under in every project. */
|
|
37
|
+
export const OFFICIAL_REPO_NAME = "sous-recipes";
|
|
38
|
+
|
|
39
|
+
/** Where the official repository lives. */
|
|
40
|
+
export const OFFICIAL_REPO_URL = "https://github.com/sous-io/sous-recipes";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The official repository's canonical identity: what the machine-wide store and
|
|
44
|
+
* the index cache file it under, whatever a project happens to call it.
|
|
45
|
+
*/
|
|
46
|
+
export const OFFICIAL_REPO_IDENTITY = "github.com/sous-io/sous-recipes";
|
|
47
|
+
|
|
48
|
+
/** The provider that handles the official repository. */
|
|
49
|
+
export const OFFICIAL_REPO_PROVIDER = "github";
|
|
50
|
+
|
|
51
|
+
/** The namespace every project is subscribed to unless it opts out. */
|
|
52
|
+
export const CORE_NAMESPACE = "core";
|
|
53
|
+
|
|
54
|
+
/** The name of the recipe published in that namespace. */
|
|
55
|
+
export const CORE_RECIPE_NAME = "sous-skills";
|
|
56
|
+
|
|
57
|
+
/** The core recipe's key, `core/sous-skills`. */
|
|
58
|
+
export const CORE_RECIPE_KEY = `${CORE_NAMESPACE}/${CORE_RECIPE_NAME}`;
|
|
59
|
+
|
|
60
|
+
/** The core recipe's folder, relative to the repository root, as the index records it. */
|
|
61
|
+
export const CORE_RECIPE_PATH = `${PACKAGED_RECIPES_DIRNAME}/${CORE_NAMESPACE}/${CORE_RECIPE_NAME}`;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The directory holding the packaged copy of the core recipe.
|
|
65
|
+
*
|
|
66
|
+
* @param packageRoot - The installed package's root directory. Defaults to the
|
|
67
|
+
* running CLI's own root, which is what every caller outside a test wants.
|
|
68
|
+
*/
|
|
69
|
+
export function packagedCoreRecipeDir(packageRoot: string = CLI_ROOT): string {
|
|
70
|
+
return path.join(packageRoot, PACKAGED_RECIPES_DIRNAME, CORE_NAMESPACE, CORE_RECIPE_NAME);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reads and validates the packaged core recipe's manifest.
|
|
75
|
+
*
|
|
76
|
+
* A missing or unreadable manifest is a broken installation rather than a user
|
|
77
|
+
* mistake, so the error says so plainly instead of suggesting a fix the user
|
|
78
|
+
* cannot make.
|
|
79
|
+
*
|
|
80
|
+
* @param packageRoot - The installed package's root directory.
|
|
81
|
+
*/
|
|
82
|
+
export function readPackagedCoreManifest(packageRoot: string = CLI_ROOT): RecipeManifest {
|
|
83
|
+
const dir = packagedCoreRecipeDir(packageRoot);
|
|
84
|
+
const manifestPath = findRecipeManifest(dir);
|
|
85
|
+
|
|
86
|
+
if (manifestPath === undefined) {
|
|
87
|
+
throw new ConfigError(
|
|
88
|
+
`This installation of sous is missing the core recipe it ships with.\n` +
|
|
89
|
+
` Sous expected to find a recipe manifest in ${dir}.\n` +
|
|
90
|
+
` Reinstalling the sous package will restore it.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return parseRecipeManifest(loadManifestFile(manifestPath), manifestPath);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The version of the core recipe this installation seeds, which is by rule the
|
|
99
|
+
* version of sous itself. Read from the constant rather than from the manifest
|
|
100
|
+
* so a damaged package cannot quietly seed a version that does not match the
|
|
101
|
+
* CLI; the parity test proves the two agree.
|
|
102
|
+
*/
|
|
103
|
+
export function coreRecipeVersion(): string {
|
|
104
|
+
return SOUS_VERSION;
|
|
105
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The repository and subscription every project gets without asking.
|
|
3
|
+
*
|
|
4
|
+
* Sous ships one namespace, `core`, holding the skills that teach an agent what
|
|
5
|
+
* sous is and how it works. Every project is subscribed to it, because an agent
|
|
6
|
+
* that does not know sous manages a file will happily hand-edit it. The wiring
|
|
7
|
+
* is deliberately ordinary: sous adds the same two config entries a person could
|
|
8
|
+
* have written by hand, so they can be inspected with `sous config show` and
|
|
9
|
+
* overridden or switched off in the config like anything else.
|
|
10
|
+
*
|
|
11
|
+
* - The repository `sous-recipes`, pointing at the official public repository.
|
|
12
|
+
* Trust is not a question here: sous itself ships the recipe, pins the
|
|
13
|
+
* version to its own, and seeds it from the package, so trusting it adds
|
|
14
|
+
* nothing a person did not already accept by installing sous.
|
|
15
|
+
* - The subscription `core`, a whole-namespace subscription whose range is
|
|
16
|
+
* exactly the running sous version. Core therefore always matches the CLI,
|
|
17
|
+
* and upgrading sous upgrades core with it.
|
|
18
|
+
*
|
|
19
|
+
* Two ways out, both plain config:
|
|
20
|
+
*
|
|
21
|
+
* subscriptions: { core: { enabled: false } } // keep the repo, drop the skills
|
|
22
|
+
* repos: { "sous-recipes": { enabled: false } } // drop the repository entirely
|
|
23
|
+
*
|
|
24
|
+
* A user-written entry under either key REPLACES the default outright, because
|
|
25
|
+
* the defaults are applied underneath whatever the config layers produced. That
|
|
26
|
+
* is what lets a project pin core to a different range, point the repository at
|
|
27
|
+
* a mirror, or turn either off.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { RepoEntry, Settings, SubscriptionEntry } from "../settings.js";
|
|
31
|
+
import { SOUS_VERSION } from "../package-info.js";
|
|
32
|
+
import {
|
|
33
|
+
CORE_NAMESPACE,
|
|
34
|
+
OFFICIAL_REPO_NAME,
|
|
35
|
+
OFFICIAL_REPO_PROVIDER,
|
|
36
|
+
OFFICIAL_REPO_URL,
|
|
37
|
+
} from "./core-recipe.js";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The `addedBy` value on an entry sous provides itself. It is what tells
|
|
41
|
+
* `sous repo list` to print "built in" rather than a date, and it is why these
|
|
42
|
+
* entries are never written to a managed layer: they are recreated on every run
|
|
43
|
+
* from the installed package.
|
|
44
|
+
*/
|
|
45
|
+
export const BUILT_IN_ADDED_BY = "sous";
|
|
46
|
+
|
|
47
|
+
/** The built-in repository entry, exactly as a person could have written it. */
|
|
48
|
+
export function builtInRepoEntry(): RepoEntry {
|
|
49
|
+
return {
|
|
50
|
+
url: OFFICIAL_REPO_URL,
|
|
51
|
+
provider: OFFICIAL_REPO_PROVIDER,
|
|
52
|
+
addedBy: BUILT_IN_ADDED_BY,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The built-in `core` subscription. Its range is the exact running version, not
|
|
58
|
+
* a caret range: core is published in lockstep with the CLI and is meant to
|
|
59
|
+
* match it exactly.
|
|
60
|
+
*
|
|
61
|
+
* @param version - The running sous version. Defaults to this installation's.
|
|
62
|
+
*/
|
|
63
|
+
export function builtInCoreSubscription(version: string = SOUS_VERSION): SubscriptionEntry {
|
|
64
|
+
return { range: version, addedBy: BUILT_IN_ADDED_BY };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Adds the built-in repository and subscription to a merged config, underneath
|
|
69
|
+
* anything the config layers already said.
|
|
70
|
+
*
|
|
71
|
+
* The core subscription is added only when the built-in repository survives: a
|
|
72
|
+
* project that switched the repository off would otherwise be left subscribed to
|
|
73
|
+
* a namespace nothing can resolve.
|
|
74
|
+
*
|
|
75
|
+
* @param settings - The merged, validated config.
|
|
76
|
+
* @param version - The running sous version. Defaults to this installation's.
|
|
77
|
+
*/
|
|
78
|
+
export function applyRepoDefaults(
|
|
79
|
+
settings: Settings,
|
|
80
|
+
version: string = SOUS_VERSION
|
|
81
|
+
): Settings {
|
|
82
|
+
// Anything that is not a map of entries is left exactly as written, so schema
|
|
83
|
+
// validation reports the real mistake rather than a symptom of this merge.
|
|
84
|
+
if (!isEntryMap(settings.repos) || !isEntryMap(settings.subscriptions)) return settings;
|
|
85
|
+
|
|
86
|
+
const repos: Record<string, RepoEntry> = {
|
|
87
|
+
...settings.repos,
|
|
88
|
+
[OFFICIAL_REPO_NAME]: mergeOverDefault(
|
|
89
|
+
builtInRepoEntry(),
|
|
90
|
+
settings.repos?.[OFFICIAL_REPO_NAME]
|
|
91
|
+
),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const withRepos: Settings = { ...settings, repos };
|
|
95
|
+
if (repos[OFFICIAL_REPO_NAME]?.enabled === false) return withRepos;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
...withRepos,
|
|
99
|
+
subscriptions: {
|
|
100
|
+
...settings.subscriptions,
|
|
101
|
+
[CORE_NAMESPACE]: mergeOverDefault(
|
|
102
|
+
builtInCoreSubscription(version),
|
|
103
|
+
settings.subscriptions?.[CORE_NAMESPACE]
|
|
104
|
+
),
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Lays what a project wrote over the entry sous provides.
|
|
111
|
+
*
|
|
112
|
+
* The merge is per field, not per entry, which is the whole point: the shortest
|
|
113
|
+
* possible opt-out, `{ enabled: false }`, is a complete entry once the built-in
|
|
114
|
+
* URL and provider are underneath it. A project that wants to repoint the
|
|
115
|
+
* repository writes a `url` and that field alone changes.
|
|
116
|
+
*
|
|
117
|
+
* @param fallback - The entry sous provides.
|
|
118
|
+
* @param written - What the project's config layers produced, when anything did.
|
|
119
|
+
*/
|
|
120
|
+
function mergeOverDefault<T extends object>(fallback: T, written: T | undefined): T {
|
|
121
|
+
if (written === undefined || typeof written !== "object" || Array.isArray(written)) {
|
|
122
|
+
return fallback;
|
|
123
|
+
}
|
|
124
|
+
return { ...fallback, ...written };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** True when a config value is absent or is a plain map of entries. */
|
|
128
|
+
function isEntryMap(value: unknown): boolean {
|
|
129
|
+
return value === undefined || (typeof value === "object" && value !== null && !Array.isArray(value));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* True when an entry is one sous provided rather than one the project wrote.
|
|
134
|
+
*
|
|
135
|
+
* @param entry - A repository or subscription entry.
|
|
136
|
+
*/
|
|
137
|
+
export function isBuiltInEntry(entry: { addedBy?: string } | undefined): boolean {
|
|
138
|
+
return entry?.addedBy === BUILT_IN_ADDED_BY;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The repositories a project actually uses: everything in the config except the
|
|
143
|
+
* entries switched off with `enabled: false`. A switched-off entry stays in the
|
|
144
|
+
* config, and stays visible to `sous config show`, so the opt-out is legible;
|
|
145
|
+
* it simply takes no part in resolving, fetching or trusting anything.
|
|
146
|
+
*
|
|
147
|
+
* @param settings - The merged config.
|
|
148
|
+
*/
|
|
149
|
+
export function enabledRepos(settings: Settings | undefined): Record<string, RepoEntry> {
|
|
150
|
+
return withoutDisabled(settings?.repos);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The subscriptions a project actually has, on the same terms as
|
|
155
|
+
* `enabledRepos`.
|
|
156
|
+
*
|
|
157
|
+
* @param settings - The merged config.
|
|
158
|
+
*/
|
|
159
|
+
export function enabledSubscriptions(
|
|
160
|
+
settings: Settings | undefined
|
|
161
|
+
): Record<string, SubscriptionEntry> {
|
|
162
|
+
return withoutDisabled(settings?.subscriptions);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Drops every entry whose `enabled` field says false. */
|
|
166
|
+
function withoutDisabled<T extends { enabled?: boolean }>(
|
|
167
|
+
entries: Record<string, T> | undefined
|
|
168
|
+
): Record<string, T> {
|
|
169
|
+
const kept: Record<string, T> = {};
|
|
170
|
+
for (const [key, entry] of Object.entries(entries ?? {})) {
|
|
171
|
+
if (entry?.enabled === false) continue;
|
|
172
|
+
kept[key] = entry;
|
|
173
|
+
}
|
|
174
|
+
return kept;
|
|
175
|
+
}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared building blocks for every Repositories on-disk format.
|
|
3
|
+
*
|
|
4
|
+
* Each format module (repo manifest, recipe manifest, index file, lockfile,
|
|
5
|
+
* store entry marker, links map) composes the primitives defined here, so a
|
|
6
|
+
* namespace name, a content hash or a timestamp means exactly the same thing
|
|
7
|
+
* everywhere. This module also holds the canonical file names and the shared
|
|
8
|
+
* `parseFormat` helper that turns a zod failure into a readable ConfigError.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import semver from "semver";
|
|
13
|
+
import { ConfigError } from "../../errors.js";
|
|
14
|
+
import {
|
|
15
|
+
CONTENT_HASH_PATTERN,
|
|
16
|
+
ENV_VAR_NAME_PATTERN,
|
|
17
|
+
ISO_TIMESTAMP_PATTERN,
|
|
18
|
+
KEBAB_NAME_PATTERN,
|
|
19
|
+
NAMESPACE_NAME_PATTERN,
|
|
20
|
+
RECIPE_KEY_PATTERN,
|
|
21
|
+
RECIPE_NAME_PATTERN,
|
|
22
|
+
REF_KEY_PATTERN,
|
|
23
|
+
REPO_IDENTITY_PATTERN,
|
|
24
|
+
REPO_NAME_PATTERN,
|
|
25
|
+
VARIABLE_NAME_PATTERN,
|
|
26
|
+
} from "./patterns.js";
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
CONTENT_HASH_PATTERN,
|
|
30
|
+
ENV_VAR_NAME_PATTERN,
|
|
31
|
+
ISO_TIMESTAMP_PATTERN,
|
|
32
|
+
KEBAB_NAME_PATTERN,
|
|
33
|
+
NAMESPACE_NAME_PATTERN,
|
|
34
|
+
RECIPE_KEY_PATTERN,
|
|
35
|
+
RECIPE_NAME_PATTERN,
|
|
36
|
+
REF_KEY_PATTERN,
|
|
37
|
+
REPO_IDENTITY_PATTERN,
|
|
38
|
+
REPO_NAME_PATTERN,
|
|
39
|
+
VARIABLE_NAME_PATTERN,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// --- Format version -----------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The only on-disk format version this sous understands. Every manifest, index,
|
|
46
|
+
* lockfile, store entry marker and links map carries it as `formatVersion`, so
|
|
47
|
+
* a future incompatible change can be detected instead of misread.
|
|
48
|
+
*/
|
|
49
|
+
export const SUPPORTED_FORMAT_VERSION = 1;
|
|
50
|
+
|
|
51
|
+
/** The `formatVersion` field, present in every Repositories format. */
|
|
52
|
+
export const formatVersionSchema = z.literal(SUPPORTED_FORMAT_VERSION, {
|
|
53
|
+
message:
|
|
54
|
+
`must be ${SUPPORTED_FORMAT_VERSION}; this version of sous understands no other ` +
|
|
55
|
+
`on-disk format version`,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// --- Canonical file names -----------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
/** Base name (without extension) of the repo manifest, at a repo's root. */
|
|
61
|
+
export const REPO_MANIFEST_BASENAME = "sous.repo";
|
|
62
|
+
|
|
63
|
+
/** Base name (without extension) of a recipe manifest, in each recipe folder. */
|
|
64
|
+
export const RECIPE_MANIFEST_BASENAME = "sous.recipe";
|
|
65
|
+
|
|
66
|
+
/** File name of the machine-written repo index, at a repo's root. */
|
|
67
|
+
export const INDEX_FILENAME = "sous.index.json";
|
|
68
|
+
|
|
69
|
+
/** File name of the project lockfile, inside the project's `.sous/` directory. */
|
|
70
|
+
export const LOCKFILE_FILENAME = "sous.lock.json";
|
|
71
|
+
|
|
72
|
+
/** File name of the marker written beside every store entry. */
|
|
73
|
+
export const STORE_ENTRY_FILENAME = ".sous.entry.json";
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Directory, inside the store root, that cached repository indexes live in. It
|
|
77
|
+
* is named here rather than in the index cache so the store can skip it while
|
|
78
|
+
* walking its own entries without depending on the provider layer.
|
|
79
|
+
*/
|
|
80
|
+
export const INDEX_CACHE_DIRNAME = "_indexes";
|
|
81
|
+
|
|
82
|
+
/** File name of the links map, in a project's `.sous/` directory or in `$SOUS_HOME`. */
|
|
83
|
+
export const LINKS_FILENAME = "sous.links.json";
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Extensions a hand-written manifest may use, in the order they are tried when
|
|
87
|
+
* discovering one. Both `.json` and `.jsonc` are parsed permissively (comments
|
|
88
|
+
* and trailing commas are allowed); see `load-manifest.ts`.
|
|
89
|
+
*/
|
|
90
|
+
export const MANIFEST_EXTENSIONS = [".yaml", ".yml", ".json", ".jsonc"] as const;
|
|
91
|
+
|
|
92
|
+
// --- Name primitives ----------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
/** Builds a kebab-case name schema with a message naming what is being named. */
|
|
95
|
+
function kebabName(label: string, example: string) {
|
|
96
|
+
return z
|
|
97
|
+
.string()
|
|
98
|
+
.regex(
|
|
99
|
+
KEBAB_NAME_PATTERN,
|
|
100
|
+
`a ${label} must be lowercase kebab-case: a letter, then letters, digits or ` +
|
|
101
|
+
`hyphens (for example '${example}')`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A repo's short name, as used by the `repo:` qualifier on a ref. */
|
|
106
|
+
export const repoNameSchema = kebabName("repo name", "sous-recipes");
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A repository's canonical identity, `<host>/<owner path>/<name>`. Everything
|
|
110
|
+
* shared between projects (the machine-wide store, the index cache, a
|
|
111
|
+
* lockfile's record of where a recipe came from) keys by this rather than by a
|
|
112
|
+
* short name, because a short name is one project's private label.
|
|
113
|
+
*/
|
|
114
|
+
export const repoIdentitySchema = z
|
|
115
|
+
.string()
|
|
116
|
+
.regex(
|
|
117
|
+
REPO_IDENTITY_PATTERN,
|
|
118
|
+
"a repository identity must be its host followed by the path it lives at, all " +
|
|
119
|
+
"lowercase (for example 'github.com/sous-io/sous-recipes')"
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
/** A namespace name. */
|
|
123
|
+
export const namespaceNameSchema = kebabName("namespace name", "tool-usage");
|
|
124
|
+
|
|
125
|
+
/** A recipe name, unique within its namespace. */
|
|
126
|
+
export const recipeNameSchema = kebabName("recipe name", "task-files");
|
|
127
|
+
|
|
128
|
+
/** A camelCase variable name, as declared by a recipe variable definition. */
|
|
129
|
+
export const variableNameSchema = z
|
|
130
|
+
.string()
|
|
131
|
+
.regex(
|
|
132
|
+
VARIABLE_NAME_PATTERN,
|
|
133
|
+
"a variable name must be camelCase: a lowercase letter, then letters or digits " +
|
|
134
|
+
"(for example 'apiBaseUrl')"
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
/** An explicit environment variable name for a variable definition. */
|
|
138
|
+
export const envVarNameSchema = z
|
|
139
|
+
.string()
|
|
140
|
+
.regex(
|
|
141
|
+
ENV_VAR_NAME_PATTERN,
|
|
142
|
+
"an environment variable name must be upper snake case: a capital letter, then " +
|
|
143
|
+
"capitals, digits or underscores (for example 'GITHUB_TOKEN')"
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
/** A ref key: a bare namespace, or `namespace/recipe`. Never repo-qualified or ranged. */
|
|
147
|
+
export const refKeySchema = z
|
|
148
|
+
.string()
|
|
149
|
+
.regex(
|
|
150
|
+
REF_KEY_PATTERN,
|
|
151
|
+
"a ref key must be a namespace ('workflow') or a namespace and recipe " +
|
|
152
|
+
"('workflow/task-files'), with no repo qualifier and no version range"
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
/** A recipe key: always `namespace/recipe`. */
|
|
156
|
+
export const recipeKeySchema = z
|
|
157
|
+
.string()
|
|
158
|
+
.regex(
|
|
159
|
+
RECIPE_KEY_PATTERN,
|
|
160
|
+
"a recipe key must be a namespace and recipe joined by a slash " +
|
|
161
|
+
"(for example 'workflow/task-files')"
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
// --- Value primitives ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
/** An exact semantic version, as published by a recipe. */
|
|
167
|
+
export const semverVersionSchema = z
|
|
168
|
+
.string()
|
|
169
|
+
.refine((value) => semver.valid(value) !== null, {
|
|
170
|
+
message:
|
|
171
|
+
"must be an exact semantic version, such as '1.4.0' or '2.0.0-beta.1'",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/** A semantic version range, resolved with the same rules npm uses. */
|
|
175
|
+
export const semverRangeSchema = z
|
|
176
|
+
.string()
|
|
177
|
+
.refine((value) => semver.validRange(value) !== null, {
|
|
178
|
+
message:
|
|
179
|
+
"must be a semantic version range, such as '^1.2.0', '~2.1', '>=1.0.0 <2.0.0' or '*'",
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
/** A content hash over a recipe's files, written as `sha256-` plus lowercase hex. */
|
|
183
|
+
export const contentHashSchema = z
|
|
184
|
+
.string()
|
|
185
|
+
.regex(
|
|
186
|
+
CONTENT_HASH_PATTERN,
|
|
187
|
+
"a content hash must be written as 'sha256-' followed by 64 lowercase hexadecimal characters"
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
/** An ISO 8601 timestamp with an explicit offset. */
|
|
191
|
+
export const isoTimestampSchema = z
|
|
192
|
+
.string()
|
|
193
|
+
.regex(
|
|
194
|
+
ISO_TIMESTAMP_PATTERN,
|
|
195
|
+
"must be an ISO 8601 timestamp with an offset, such as '2026-09-09T14:03:11.482Z'"
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
/** A byte count: a whole number, never negative. */
|
|
199
|
+
export const byteCountSchema = z
|
|
200
|
+
.number()
|
|
201
|
+
.int("must be a whole number of bytes")
|
|
202
|
+
.min(0, "must not be negative");
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Where a repository lives. A hosted repository is named by a URL; a repository
|
|
206
|
+
* on this machine, which the `local` provider reads, is named by an absolute
|
|
207
|
+
* path or by the same path in `file:///...` form (which is already a URL).
|
|
208
|
+
*/
|
|
209
|
+
export const repoUrlSchema = z.union([
|
|
210
|
+
z.url(),
|
|
211
|
+
z
|
|
212
|
+
.string()
|
|
213
|
+
.min(1, "must not be empty")
|
|
214
|
+
.refine((value) => value.startsWith("/") || /^[A-Za-z]:[\\/]/.test(value), {
|
|
215
|
+
message: "must be a repository URL, or an absolute path to one on this machine",
|
|
216
|
+
}),
|
|
217
|
+
]);
|
|
218
|
+
|
|
219
|
+
/** An absolute filesystem path. */
|
|
220
|
+
export const absolutePathSchema = z
|
|
221
|
+
.string()
|
|
222
|
+
.min(1, "must not be empty")
|
|
223
|
+
.refine((value) => value.startsWith("/") || /^[A-Za-z]:[\\/]/.test(value), {
|
|
224
|
+
message: "must be an absolute path",
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Builds a schema for a path that stays inside the directory holding the file
|
|
229
|
+
* that declares it. Rejects absolute paths, backslashes, `.` and `..` segments,
|
|
230
|
+
* empty segments and trailing slashes, so a manifest can never reach outside
|
|
231
|
+
* its own repo or recipe.
|
|
232
|
+
*
|
|
233
|
+
* @param label - What the path names, used in error messages.
|
|
234
|
+
* @param allowGlobs - When true, `*`, `?`, `[...]`, `{...}` and a `**` segment are allowed.
|
|
235
|
+
*/
|
|
236
|
+
export function relativePathSchema(label: string, allowGlobs = false) {
|
|
237
|
+
return z.string().superRefine((value, ctx) => {
|
|
238
|
+
const fail = (message: string) => {
|
|
239
|
+
ctx.addIssue({ code: "custom", message: `${label} ${message}` });
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
if (value.length === 0) {
|
|
243
|
+
fail("must not be empty");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (value.startsWith("/") || /^[A-Za-z]:[\\/]/.test(value)) {
|
|
247
|
+
fail("must be relative, not absolute");
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (value.includes("\\")) {
|
|
251
|
+
fail("must use forward slashes, never backslashes");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (value.endsWith("/")) {
|
|
255
|
+
fail("must not end with a slash");
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (!allowGlobs && /[*?[\]{}]/.test(value)) {
|
|
259
|
+
fail("must be a plain path, with no glob characters");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
for (const segment of value.split("/")) {
|
|
264
|
+
if (segment.length === 0) {
|
|
265
|
+
fail("must not contain an empty path segment");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (segment === "." || segment === "..") {
|
|
269
|
+
fail("must not contain a '.' or '..' segment");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// --- Object helpers -----------------------------------------------------------------------------
|
|
277
|
+
|
|
278
|
+
/** True when the value is a plain object (not null, not an array). */
|
|
279
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
280
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Drops keys in the reserved `x-` extension namespace from a plain object.
|
|
285
|
+
* Anything that is not a plain object passes through untouched, so the wrapped
|
|
286
|
+
* object schema still reports "expected object" for a string or an array.
|
|
287
|
+
*/
|
|
288
|
+
function stripExtensionKeys(value: unknown): unknown {
|
|
289
|
+
if (!isPlainObject(value)) return value;
|
|
290
|
+
let found = false;
|
|
291
|
+
for (const key of Object.keys(value)) {
|
|
292
|
+
if (key.startsWith("x-")) {
|
|
293
|
+
found = true;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (!found) return value;
|
|
298
|
+
|
|
299
|
+
const copy: Record<string, unknown> = {};
|
|
300
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
301
|
+
if (!key.startsWith("x-")) copy[key] = entry;
|
|
302
|
+
}
|
|
303
|
+
return copy;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Builds a strict object schema for a HAND-WRITTEN format: unknown keys are
|
|
308
|
+
* rejected so typos surface immediately, except keys in the reserved `x-`
|
|
309
|
+
* extension namespace, which are accepted and ignored. Machine-written formats
|
|
310
|
+
* use plain `z.strictObject` instead; nothing writes extension keys into them.
|
|
311
|
+
*/
|
|
312
|
+
export function extensibleObject<Shape extends z.ZodRawShape>(shape: Shape) {
|
|
313
|
+
return z.preprocess(stripExtensionKeys, z.strictObject(shape));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// --- Error reporting ----------------------------------------------------------------------------
|
|
317
|
+
|
|
318
|
+
/** Renders a zod issue path (`["variables",0,"name"]`) as `variables[0].name`. */
|
|
319
|
+
export function formatIssuePath(parts: ReadonlyArray<PropertyKey>): string {
|
|
320
|
+
let out = "";
|
|
321
|
+
for (const part of parts) {
|
|
322
|
+
if (typeof part === "number") out += `[${part}]`;
|
|
323
|
+
else out += out.length > 0 ? `.${String(part)}` : String(part);
|
|
324
|
+
}
|
|
325
|
+
return out;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Validates a value against a format schema, returning it typed. Throws a
|
|
330
|
+
* ConfigError (never a raw ZodError) naming the format, the file it came from
|
|
331
|
+
* and the path of every bad field.
|
|
332
|
+
*
|
|
333
|
+
* @param schema - The zod schema for the format.
|
|
334
|
+
* @param value - The already-parsed file contents.
|
|
335
|
+
* @param sourceLabel - The file path (or other label) named in error messages.
|
|
336
|
+
* @param formatLabel - Plain-language name of the format, such as "recipe manifest".
|
|
337
|
+
*/
|
|
338
|
+
export function parseFormat<Schema extends z.ZodType>(
|
|
339
|
+
schema: Schema,
|
|
340
|
+
value: unknown,
|
|
341
|
+
sourceLabel: string,
|
|
342
|
+
formatLabel: string
|
|
343
|
+
): z.output<Schema> {
|
|
344
|
+
const result = schema.safeParse(value);
|
|
345
|
+
if (result.success) return result.data;
|
|
346
|
+
|
|
347
|
+
const lines: string[] = [`Invalid ${formatLabel} at ${sourceLabel}:`];
|
|
348
|
+
for (const issue of result.error.issues) {
|
|
349
|
+
const where = formatIssuePath(issue.path);
|
|
350
|
+
if (issue.code === "unrecognized_keys") {
|
|
351
|
+
const keys = issue.keys.map((key) => `'${key}'`).join(", ");
|
|
352
|
+
const location = where.length > 0 ? `under '${where}'` : "at the top level";
|
|
353
|
+
lines.push(
|
|
354
|
+
` - unknown key ${keys} ${location}. This is likely a typo; sous ignores ` +
|
|
355
|
+
`only keys that start with 'x-'.`
|
|
356
|
+
);
|
|
357
|
+
} else if (issue.code === "invalid_key") {
|
|
358
|
+
// zod reports a bad record KEY as a generic "Invalid key in record" and
|
|
359
|
+
// hides the real reason in a nested issue list. Surface the reason, since
|
|
360
|
+
// it is the part that tells the author how to fix the key.
|
|
361
|
+
const reasons = issue.issues.map((inner) => inner.message).join("; ");
|
|
362
|
+
lines.push(` - ${where}: invalid key; ${reasons}`);
|
|
363
|
+
} else {
|
|
364
|
+
lines.push(` - ${where.length > 0 ? where : "(root)"}: ${issue.message}`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
throw new ConfigError(lines.join("\n"));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Serializes a value as pretty-printed JSON with every object key sorted, so a
|
|
373
|
+
* machine-written file produces a stable, minimal diff between runs.
|
|
374
|
+
*/
|
|
375
|
+
export function stableJsonStringify(value: unknown): string {
|
|
376
|
+
return JSON.stringify(sortKeysDeep(value), null, 2) + "\n";
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Recursively rebuilds plain objects with their keys in sorted order. */
|
|
380
|
+
function sortKeysDeep(value: unknown): unknown {
|
|
381
|
+
if (Array.isArray(value)) return value.map(sortKeysDeep);
|
|
382
|
+
if (!isPlainObject(value)) return value;
|
|
383
|
+
|
|
384
|
+
const sorted: Record<string, unknown> = {};
|
|
385
|
+
for (const key of Object.keys(value).sort()) {
|
|
386
|
+
sorted[key] = sortKeysDeep(value[key]);
|
|
387
|
+
}
|
|
388
|
+
return sorted;
|
|
389
|
+
}
|