@sous-io/sous 0.1.0 → 0.2.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 +121 -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 +408 -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 +73 -9
- 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 +619 -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 +413 -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/bin/xcv +0 -5
- 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,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod schema for a merged sous config.
|
|
3
|
+
*
|
|
4
|
+
* This mirrors the hand-written `Settings` / `RawProjectCompilation` /
|
|
5
|
+
* `RawTarget` / `RawOutput` / `RawRuntimeContext` / `ToolConfig` types in
|
|
6
|
+
* settings.ts — those remain the exported TypeScript types; this schema is the
|
|
7
|
+
* RUNTIME validator. Keep the two in sync: when a config field changes in
|
|
8
|
+
* settings.ts, change it here too.
|
|
9
|
+
*
|
|
10
|
+
* Every object level is STRICT (unknown keys are rejected), so a typo like
|
|
11
|
+
* `compilaton` is caught the moment the merged config is loaded rather than
|
|
12
|
+
* silently ignored. Validation runs on the MERGED config only (in
|
|
13
|
+
* loadSettingsWithLayers, after the kernel merges every conf.d layer and after
|
|
14
|
+
* assertFlatConfig); a single conf.d fragment need not be a complete config.
|
|
15
|
+
*
|
|
16
|
+
* The schema also drives `npm run schema:build`, which emits the committed
|
|
17
|
+
* `sous.config.schema.json` artifact via `z.toJSONSchema`.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
import { ConfigError } from "./errors.js";
|
|
22
|
+
import { repoUrlSchema, semverRangeSchema } from "./repos/formats/common.js";
|
|
23
|
+
import { REF_KEY_PATTERN, REPO_NAME_PATTERN } from "./repos/formats/patterns.js";
|
|
24
|
+
import type { Settings } from "./settings.js";
|
|
25
|
+
|
|
26
|
+
/** The only config version this sous understands. */
|
|
27
|
+
export const SUPPORTED_CONFIG_VERSION = 1;
|
|
28
|
+
|
|
29
|
+
/** A record of string → string (used for _env and every _vars block). */
|
|
30
|
+
const stringRecord = z.record(z.string(), z.string());
|
|
31
|
+
|
|
32
|
+
const outputSchema = z
|
|
33
|
+
.object({
|
|
34
|
+
_if: z.record(z.string(), z.object({ eq: z.string() }).strict()).optional(),
|
|
35
|
+
_vars: stringRecord.optional(),
|
|
36
|
+
destinationFile: z.string().optional(),
|
|
37
|
+
destinationDir: z.string().optional(),
|
|
38
|
+
})
|
|
39
|
+
.strict();
|
|
40
|
+
|
|
41
|
+
const runtimeContextSchema = z
|
|
42
|
+
.object({
|
|
43
|
+
gitRoot: z.string(),
|
|
44
|
+
outputPath: z.string(),
|
|
45
|
+
taskFileRoot: z.string(),
|
|
46
|
+
branchPattern: z.string().optional(),
|
|
47
|
+
})
|
|
48
|
+
.strict();
|
|
49
|
+
|
|
50
|
+
const targetSchema = z
|
|
51
|
+
.object({
|
|
52
|
+
_vars: stringRecord.optional(),
|
|
53
|
+
entryPoint: z.string().optional(),
|
|
54
|
+
entryGlob: z.string().optional(),
|
|
55
|
+
globBase: z.string().optional(),
|
|
56
|
+
generateRuntimeContext: z.boolean().optional(),
|
|
57
|
+
outputs: z.array(outputSchema),
|
|
58
|
+
})
|
|
59
|
+
.strict()
|
|
60
|
+
.refine((t) => (t.entryPoint !== undefined) !== (t.entryGlob !== undefined), {
|
|
61
|
+
message: "a target must have exactly one of 'entryPoint' or 'entryGlob'",
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const compilationSchema = z
|
|
65
|
+
.object({
|
|
66
|
+
_vars: stringRecord.optional(),
|
|
67
|
+
includeSourceComments: z.boolean().optional(),
|
|
68
|
+
targets: z.array(targetSchema),
|
|
69
|
+
})
|
|
70
|
+
.strict();
|
|
71
|
+
|
|
72
|
+
const toolSchema = z
|
|
73
|
+
.object({
|
|
74
|
+
command: z.string(),
|
|
75
|
+
args: z.array(z.string()).optional(),
|
|
76
|
+
promptFile: z.string().optional(),
|
|
77
|
+
})
|
|
78
|
+
.strict();
|
|
79
|
+
|
|
80
|
+
// --- Repositories -------------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* One trusted repository, keyed by the short name refs use in the `repo:`
|
|
84
|
+
* qualifier. Adding a repo IS trusting it: `sous repo add` writes the entry
|
|
85
|
+
* into the machine-written `conf.d/500-repos.jsonc` layer, and removing the
|
|
86
|
+
* entry withdraws the trust. A user may also hand-write `repos:` in the primary
|
|
87
|
+
* config; the two layers merge like anything else.
|
|
88
|
+
*/
|
|
89
|
+
const repoEntrySchema = z
|
|
90
|
+
.object({
|
|
91
|
+
/**
|
|
92
|
+
* Where the repository lives. A hosted repository is named by its URL; a
|
|
93
|
+
* repository on this machine, which the `local` provider reads, is named by
|
|
94
|
+
* an absolute path or the same path in `file:///...` form.
|
|
95
|
+
*/
|
|
96
|
+
url: repoUrlSchema,
|
|
97
|
+
/**
|
|
98
|
+
* Whether the repository takes part in anything at all. Defaults to true.
|
|
99
|
+
* Setting it to false is how a project opts out of a repository sous
|
|
100
|
+
* provides itself (the official `sous-recipes`), without having to delete an
|
|
101
|
+
* entry it never wrote.
|
|
102
|
+
*/
|
|
103
|
+
enabled: z.boolean().optional(),
|
|
104
|
+
/**
|
|
105
|
+
* Which provider handles it. Inferred from the URL when omitted; set it
|
|
106
|
+
* explicitly for a self-hosted instance the URL does not give away.
|
|
107
|
+
* `local` is a repository on this machine, for local development and tests;
|
|
108
|
+
* its trust semantics are identical to a hosted one.
|
|
109
|
+
*/
|
|
110
|
+
provider: z.enum(["github", "gitlab", "local"]).optional(),
|
|
111
|
+
/**
|
|
112
|
+
* When true, sous installs a newer in-range version whenever one exists
|
|
113
|
+
* rather than holding the locked one. The flag never widens the range a
|
|
114
|
+
* subscription or a dependency declared.
|
|
115
|
+
*/
|
|
116
|
+
alwaysPull: z.boolean().optional(),
|
|
117
|
+
/** When the repo was added, for provenance. */
|
|
118
|
+
addedAt: z.string().optional(),
|
|
119
|
+
/**
|
|
120
|
+
* Who required the repo: the literal "user" for a deliberate add, or the ref
|
|
121
|
+
* of the recipe whose dependency pulled it in. Removal hygiene reads this.
|
|
122
|
+
*/
|
|
123
|
+
addedBy: z.string().optional(),
|
|
124
|
+
})
|
|
125
|
+
.strict();
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* One subscription, keyed by a ref key: a bare namespace (every recipe in it,
|
|
129
|
+
* including ones published later) or `namespace/recipe`. Written by
|
|
130
|
+
* `sous subscribe` into the machine-written `conf.d/510-subscriptions.jsonc`
|
|
131
|
+
* layer, and hand-writable in the primary config.
|
|
132
|
+
*/
|
|
133
|
+
const subscriptionEntrySchema = z
|
|
134
|
+
.object({
|
|
135
|
+
/**
|
|
136
|
+
* Whether the subscription takes part in anything at all. Defaults to true.
|
|
137
|
+
* Setting it to false is how a project opts out of the `core` namespace sous
|
|
138
|
+
* subscribes every project to.
|
|
139
|
+
*/
|
|
140
|
+
enabled: z.boolean().optional(),
|
|
141
|
+
/** The semantic version range to resolve within. Defaults to "*". */
|
|
142
|
+
range: semverRangeSchema.optional(),
|
|
143
|
+
/** When true, prerelease versions take part in range matching. */
|
|
144
|
+
prerelease: z.boolean().optional(),
|
|
145
|
+
/** Per-subscription form of the repo-level always-pull flag. */
|
|
146
|
+
alwaysPull: z.boolean().optional(),
|
|
147
|
+
/** When the subscription was added, for provenance. */
|
|
148
|
+
addedAt: z.string().optional(),
|
|
149
|
+
/** Who required it: "user", or the ref of the recipe that co-subscribed it. */
|
|
150
|
+
addedBy: z.string().optional(),
|
|
151
|
+
})
|
|
152
|
+
.strict();
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Knobs for the machine-wide recipe store under the user-level sous directory.
|
|
156
|
+
* Every value here is a number the user can change; the numbers sous ships are
|
|
157
|
+
* defaults, not assumptions. Phase 2 applies them, so all three are optional
|
|
158
|
+
* here and the defaults live with the store itself: one gigabyte for maxBytes,
|
|
159
|
+
* 300 seconds for freshnessSeconds, and 300 seconds for watchPollSeconds.
|
|
160
|
+
*/
|
|
161
|
+
const storeSchema = z
|
|
162
|
+
.object({
|
|
163
|
+
/** Size cap for the store, past which least-recently-used entries are collected. */
|
|
164
|
+
maxBytes: z.number().int().positive().optional(),
|
|
165
|
+
/**
|
|
166
|
+
* How long a fetched index stays fresh. A non-watch build checks upstream
|
|
167
|
+
* only once this window has lapsed, and a failed check never breaks a
|
|
168
|
+
* build; the last good answer stands.
|
|
169
|
+
*/
|
|
170
|
+
freshnessSeconds: z.number().int().nonnegative().optional(),
|
|
171
|
+
/** How often watch mode polls upstream for a newer in-range version. */
|
|
172
|
+
watchPollSeconds: z.number().int().nonnegative().optional(),
|
|
173
|
+
})
|
|
174
|
+
.strict();
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Where the files a subscribed recipe contributes are written, one list of
|
|
178
|
+
* destination directories per content kind. Each destination is `${var}`
|
|
179
|
+
* substituted like any other config path, and a kind may name several so the
|
|
180
|
+
* same recipe feeds more than one agent directory (`.claude/skills` and
|
|
181
|
+
* `.codex/skills`, say).
|
|
182
|
+
*
|
|
183
|
+
* Only `skills` has a default: `<project root>/.claude/skills`, the project root
|
|
184
|
+
* being the parent of the discovered `.sous/` directory. A kind with no
|
|
185
|
+
* destination is skipped, with one warning naming this key, because sous cannot
|
|
186
|
+
* guess where a project wants its memories or its prompts. A recipe's `config`
|
|
187
|
+
* contents are not listed here; they are loaded as config layers rather than
|
|
188
|
+
* written anywhere.
|
|
189
|
+
*/
|
|
190
|
+
const recipeOutputsSchema = z
|
|
191
|
+
.object({
|
|
192
|
+
/** Where recipe skill bundles are written. */
|
|
193
|
+
skills: z.array(z.string()).optional(),
|
|
194
|
+
/** Where recipe memory files are written. */
|
|
195
|
+
memories: z.array(z.string()).optional(),
|
|
196
|
+
/** Where recipe prompt files are written. */
|
|
197
|
+
prompts: z.array(z.string()).optional(),
|
|
198
|
+
})
|
|
199
|
+
.strict();
|
|
200
|
+
|
|
201
|
+
// --- Variable mappings --------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* A mapping record's target: one variable, named in full, as
|
|
205
|
+
* `namespace/recipe/variableName` with an optional `repo:` qualifier. Mapping
|
|
206
|
+
* records are the top rung of the answer resolution ladder and the universal
|
|
207
|
+
* resolver when two recipes want the same environment variable name.
|
|
208
|
+
*/
|
|
209
|
+
const mappingTargetSchema = z
|
|
210
|
+
.string()
|
|
211
|
+
.regex(
|
|
212
|
+
/^([a-z][a-z0-9-]*:)?[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*\/[a-z][a-zA-Z0-9]*$/,
|
|
213
|
+
"a variable mapping target must be written as 'namespace/recipe/variableName', " +
|
|
214
|
+
"optionally qualified with a repository as 'repo:namespace/recipe/variableName'"
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The full merged-config schema. `version`, when present, must be exactly
|
|
219
|
+
* `SUPPORTED_CONFIG_VERSION` — but validateSettings pre-checks it with a clearer
|
|
220
|
+
* message before this schema runs, so a bad version never reaches the generic
|
|
221
|
+
* literal error here.
|
|
222
|
+
*/
|
|
223
|
+
export const settingsSchema = z
|
|
224
|
+
.object({
|
|
225
|
+
// Allowed so a JSON config can bind itself to the shipped
|
|
226
|
+
// `sous.config.schema.json` via the standard `"$schema": "..."` property
|
|
227
|
+
// for editor autocompletion / external validation. Editors treat `$schema`
|
|
228
|
+
// as reserved and never flag it, so rejecting it here would break the
|
|
229
|
+
// documented workflow. sous itself ignores the value.
|
|
230
|
+
$schema: z.string().optional(),
|
|
231
|
+
version: z.literal(SUPPORTED_CONFIG_VERSION).optional(),
|
|
232
|
+
_env: stringRecord.optional(),
|
|
233
|
+
_vars: stringRecord.optional(),
|
|
234
|
+
_aliases: z
|
|
235
|
+
.record(z.string(), z.union([z.string(), z.array(z.string())]))
|
|
236
|
+
.optional(),
|
|
237
|
+
name: z.string().optional(),
|
|
238
|
+
compilation: compilationSchema.optional(),
|
|
239
|
+
runtimeContext: runtimeContextSchema.optional(),
|
|
240
|
+
tools: z.record(z.string(), toolSchema).optional(),
|
|
241
|
+
/** Trusted repositories, keyed by the short name refs use. */
|
|
242
|
+
repos: z
|
|
243
|
+
.record(
|
|
244
|
+
z
|
|
245
|
+
.string()
|
|
246
|
+
.regex(
|
|
247
|
+
REPO_NAME_PATTERN,
|
|
248
|
+
"a repo name must be lowercase kebab-case: a letter, then letters, " +
|
|
249
|
+
"digits or hyphens"
|
|
250
|
+
),
|
|
251
|
+
repoEntrySchema
|
|
252
|
+
)
|
|
253
|
+
.optional(),
|
|
254
|
+
/** Subscriptions, keyed by ref key (`namespace` or `namespace/recipe`). */
|
|
255
|
+
subscriptions: z
|
|
256
|
+
.record(
|
|
257
|
+
z
|
|
258
|
+
.string()
|
|
259
|
+
.regex(
|
|
260
|
+
REF_KEY_PATTERN,
|
|
261
|
+
"a subscription key must be a namespace such as 'workflow', or a " +
|
|
262
|
+
"namespace and recipe such as 'workflow/task-files', with no repo " +
|
|
263
|
+
"qualifier and no version range"
|
|
264
|
+
),
|
|
265
|
+
subscriptionEntrySchema
|
|
266
|
+
)
|
|
267
|
+
.optional(),
|
|
268
|
+
/** Knobs for the machine-wide recipe store. */
|
|
269
|
+
store: storeSchema.optional(),
|
|
270
|
+
/** Where the files subscribed recipes contribute are written, per content kind. */
|
|
271
|
+
recipeOutputs: recipeOutputsSchema.optional(),
|
|
272
|
+
/**
|
|
273
|
+
* Variable mapping records, keyed by environment variable name. Each entry
|
|
274
|
+
* binds that name to one recipe variable, which is how an answer is stored
|
|
275
|
+
* under a name of your choosing when the generated names are taken. Written
|
|
276
|
+
* by `sous vars ask` into `conf.d/520-var-mappings.jsonc`, and hand-writable
|
|
277
|
+
* in the primary config.
|
|
278
|
+
*/
|
|
279
|
+
varMappings: z
|
|
280
|
+
.record(
|
|
281
|
+
z
|
|
282
|
+
.string()
|
|
283
|
+
.regex(
|
|
284
|
+
/^[A-Z][A-Z0-9_]*$/,
|
|
285
|
+
"an environment variable name must be upper snake case: a capital " +
|
|
286
|
+
"letter, then capitals, digits or underscores"
|
|
287
|
+
),
|
|
288
|
+
mappingTargetSchema
|
|
289
|
+
)
|
|
290
|
+
.optional(),
|
|
291
|
+
})
|
|
292
|
+
.strict();
|
|
293
|
+
|
|
294
|
+
/** Renders a zod issue path (e.g. `["compilation","targets",0,"entryPoint"]`) as `compilation.targets[0].entryPoint`. */
|
|
295
|
+
function formatIssuePath(parts: ReadonlyArray<PropertyKey>): string {
|
|
296
|
+
let out = "";
|
|
297
|
+
for (const part of parts) {
|
|
298
|
+
if (typeof part === "number") out += `[${part}]`;
|
|
299
|
+
else out += out.length > 0 ? `.${String(part)}` : String(part);
|
|
300
|
+
}
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Turns a ZodError into a readable, per-issue ConfigError message. Never leaks
|
|
306
|
+
* the raw zod JSON dump. Unknown-key issues are surfaced as likely typos and
|
|
307
|
+
* name the config file.
|
|
308
|
+
*/
|
|
309
|
+
function formatZodError(error: z.ZodError, configPath: string): ConfigError {
|
|
310
|
+
const lines: string[] = [`Invalid sous config at ${configPath}:`];
|
|
311
|
+
|
|
312
|
+
for (const issue of error.issues) {
|
|
313
|
+
const where = formatIssuePath(issue.path);
|
|
314
|
+
if (issue.code === "unrecognized_keys") {
|
|
315
|
+
const keys = issue.keys.map((k) => `'${k}'`).join(", ");
|
|
316
|
+
const loc = where.length > 0 ? `under '${where}'` : "at the top level";
|
|
317
|
+
lines.push(
|
|
318
|
+
` - unknown key(s) ${keys} ${loc} — likely a typo. Check ${configPath}.`
|
|
319
|
+
);
|
|
320
|
+
} else if (issue.code === "invalid_key") {
|
|
321
|
+
// zod reports a bad record KEY as a bare "Invalid key in record" and hides
|
|
322
|
+
// the reason in a nested issue list. Surface the reason, since that is the
|
|
323
|
+
// part telling the user how to fix the key.
|
|
324
|
+
const reasons = issue.issues.map((inner) => inner.message).join("; ");
|
|
325
|
+
lines.push(` - ${where}: invalid key; ${reasons}`);
|
|
326
|
+
} else {
|
|
327
|
+
lines.push(` - ${where.length > 0 ? where : "(root)"}: ${issue.message}`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return new ConfigError(lines.join("\n"));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Validates a merged config object against the schema, returning it typed as
|
|
336
|
+
* `Settings`. Throws a ConfigError (never a raw ZodError) on any problem.
|
|
337
|
+
*
|
|
338
|
+
* @param raw - The merged config produced by the kernel (post assertFlatConfig).
|
|
339
|
+
* @param configPath - The primary config file path, named in error messages.
|
|
340
|
+
*/
|
|
341
|
+
export function validateSettings(raw: unknown, configPath: string): Settings {
|
|
342
|
+
// Version gets a dedicated, friendlier message than the generic literal error.
|
|
343
|
+
if (raw !== null && typeof raw === "object" && "version" in raw) {
|
|
344
|
+
const version = (raw as { version: unknown }).version;
|
|
345
|
+
if (version !== SUPPORTED_CONFIG_VERSION) {
|
|
346
|
+
throw new ConfigError(
|
|
347
|
+
`Config at ${configPath} declares version ${JSON.stringify(version)}, which is not ` +
|
|
348
|
+
`supported by this version of sous.\n` +
|
|
349
|
+
` This sous understands config version ${SUPPORTED_CONFIG_VERSION}. Omit the ` +
|
|
350
|
+
`'version' field or set it to ${SUPPORTED_CONFIG_VERSION}.`
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const result = settingsSchema.safeParse(raw);
|
|
356
|
+
if (!result.success) {
|
|
357
|
+
throw formatZodError(result.error, configPath);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return result.data as Settings;
|
|
361
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A line-preserving editor for `.sous/.env` and `.sous/.env.local`.
|
|
3
|
+
*
|
|
4
|
+
* These files belong to the user: they are hand-written, commented, ordered
|
|
5
|
+
* deliberately, and (for `.env`) committed. So sous edits them the way a
|
|
6
|
+
* careful person would. The file is parsed into a line model, exactly one line
|
|
7
|
+
* is rewritten or appended, and every other byte comes back out unchanged:
|
|
8
|
+
* comments, blank lines, ordering, quoting style and the `export ` prefix all
|
|
9
|
+
* survive.
|
|
10
|
+
*
|
|
11
|
+
* Comments are OUTPUT ONLY. sous writes a short generated header above each
|
|
12
|
+
* entry it appends, explaining where the value came from, and never reads a
|
|
13
|
+
* comment back or tries to keep one up to date. The header says as much, so
|
|
14
|
+
* nobody wonders whether editing it will confuse the tool.
|
|
15
|
+
*
|
|
16
|
+
* The parser here recognizes the same syntax `env-local.ts` reads, since these
|
|
17
|
+
* two modules are the write and read halves of one small format.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
|
|
23
|
+
/** The key syntax accepted on an assignment line, matching the env file parser. */
|
|
24
|
+
const KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
25
|
+
|
|
26
|
+
/** One line of an env file, classified. */
|
|
27
|
+
export type EnvFileLine =
|
|
28
|
+
/** An empty or whitespace-only line. */
|
|
29
|
+
| { kind: "blank"; text: string }
|
|
30
|
+
/** A whole-line comment. */
|
|
31
|
+
| { kind: "comment"; text: string }
|
|
32
|
+
/** A `KEY=value` assignment, with everything needed to rewrite just the value. */
|
|
33
|
+
| {
|
|
34
|
+
kind: "assignment";
|
|
35
|
+
text: string;
|
|
36
|
+
/** Leading whitespace, preserved on rewrite. */
|
|
37
|
+
indent: string;
|
|
38
|
+
/** True when the line carried an `export ` prefix. */
|
|
39
|
+
exported: boolean;
|
|
40
|
+
/** The variable's name. */
|
|
41
|
+
key: string;
|
|
42
|
+
/** The value as written, before unquoting. */
|
|
43
|
+
rawValue: string;
|
|
44
|
+
/** A trailing ` # comment`, when the line had one, including its spacing. */
|
|
45
|
+
inlineComment: string;
|
|
46
|
+
}
|
|
47
|
+
/** Anything else: kept verbatim, never interpreted. */
|
|
48
|
+
| { kind: "other"; text: string };
|
|
49
|
+
|
|
50
|
+
/** A parsed env file: its lines, and how to put them back together. */
|
|
51
|
+
export interface EnvFileModel {
|
|
52
|
+
/** Every line, in order. */
|
|
53
|
+
lines: EnvFileLine[];
|
|
54
|
+
/** The line ending the file uses. */
|
|
55
|
+
eol: "\n" | "\r\n";
|
|
56
|
+
/** True when the file ended with a newline (or is empty and will). */
|
|
57
|
+
trailingNewline: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Splits an assignment line's right-hand side into the value and any trailing
|
|
62
|
+
* comment. Quoted values are scanned to their closing quote first, so a `#`
|
|
63
|
+
* inside quotes is part of the value.
|
|
64
|
+
*/
|
|
65
|
+
function splitValueAndComment(raw: string): { rawValue: string; inlineComment: string } {
|
|
66
|
+
const leadingSpaces = raw.length - raw.trimStart().length;
|
|
67
|
+
const body = raw.trimStart();
|
|
68
|
+
|
|
69
|
+
const quote = body.startsWith('"') ? '"' : body.startsWith("'") ? "'" : undefined;
|
|
70
|
+
if (quote !== undefined) {
|
|
71
|
+
for (let i = 1; i < body.length; i++) {
|
|
72
|
+
if (body[i] === "\\") {
|
|
73
|
+
i++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (body[i] === quote) {
|
|
77
|
+
return {
|
|
78
|
+
rawValue: raw.slice(0, leadingSpaces + i + 1),
|
|
79
|
+
inlineComment: raw.slice(leadingSpaces + i + 1),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { rawValue: raw, inlineComment: "" };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const commentAt = body.search(/\s#/);
|
|
87
|
+
if (commentAt === -1) return { rawValue: raw, inlineComment: "" };
|
|
88
|
+
|
|
89
|
+
// Walk back over every space before the comment, so the gap belongs to the
|
|
90
|
+
// comment and survives a rewrite of the value.
|
|
91
|
+
let start = leadingSpaces + commentAt;
|
|
92
|
+
while (start > 0 && /\s/.test(raw[start - 1]!)) start--;
|
|
93
|
+
return { rawValue: raw.slice(0, start), inlineComment: raw.slice(start) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Parses env file text into a line model. Nothing is dropped: a line the
|
|
98
|
+
* parser does not recognize is kept verbatim as an `other` line.
|
|
99
|
+
*
|
|
100
|
+
* @param content - The file's contents.
|
|
101
|
+
*/
|
|
102
|
+
export function parseEnvFile(content: string): EnvFileModel {
|
|
103
|
+
const eol: "\n" | "\r\n" = content.includes("\r\n") ? "\r\n" : "\n";
|
|
104
|
+
const trailingNewline = content.length === 0 || content.endsWith("\n");
|
|
105
|
+
const body = content.endsWith("\r\n")
|
|
106
|
+
? content.slice(0, -2)
|
|
107
|
+
: content.endsWith("\n")
|
|
108
|
+
? content.slice(0, -1)
|
|
109
|
+
: content;
|
|
110
|
+
const rawLines = content.length === 0 ? [] : body.split(/\r?\n/);
|
|
111
|
+
|
|
112
|
+
const lines: EnvFileLine[] = rawLines.map((text) => {
|
|
113
|
+
if (text.trim() === "") return { kind: "blank", text };
|
|
114
|
+
if (text.trimStart().startsWith("#")) return { kind: "comment", text };
|
|
115
|
+
|
|
116
|
+
const indent = text.slice(0, text.length - text.trimStart().length);
|
|
117
|
+
const withoutIndent = text.slice(indent.length);
|
|
118
|
+
const exported = withoutIndent.startsWith("export ");
|
|
119
|
+
const assignment = exported ? withoutIndent.slice(7).trimStart() : withoutIndent;
|
|
120
|
+
|
|
121
|
+
const equals = assignment.indexOf("=");
|
|
122
|
+
if (equals <= 0) return { kind: "other", text };
|
|
123
|
+
|
|
124
|
+
const key = assignment.slice(0, equals).trim();
|
|
125
|
+
if (!KEY_PATTERN.test(key)) return { kind: "other", text };
|
|
126
|
+
|
|
127
|
+
const { rawValue, inlineComment } = splitValueAndComment(assignment.slice(equals + 1));
|
|
128
|
+
return { kind: "assignment", text, indent, exported, key, rawValue, inlineComment };
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return { lines, eol, trailingNewline };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Renders a line model back to text.
|
|
136
|
+
*
|
|
137
|
+
* @param model - The model to render.
|
|
138
|
+
*/
|
|
139
|
+
export function renderEnvFile(model: EnvFileModel): string {
|
|
140
|
+
if (model.lines.length === 0) return "";
|
|
141
|
+
const body = model.lines.map((line) => line.text).join(model.eol);
|
|
142
|
+
return model.trailingNewline ? body + model.eol : body;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Quotes a value if it needs quoting so the env file parser reads back exactly
|
|
147
|
+
* what was written. An ordinary word is left bare, which keeps the file
|
|
148
|
+
* readable.
|
|
149
|
+
*
|
|
150
|
+
* @param value - The value to write.
|
|
151
|
+
*/
|
|
152
|
+
export function quoteEnvValue(value: string): string {
|
|
153
|
+
const needsQuotes =
|
|
154
|
+
value.length === 0 ||
|
|
155
|
+
/[\s#"'\\]/.test(value) ||
|
|
156
|
+
value.startsWith("'") ||
|
|
157
|
+
value.startsWith('"');
|
|
158
|
+
|
|
159
|
+
if (!needsQuotes) return value;
|
|
160
|
+
|
|
161
|
+
const escaped = value
|
|
162
|
+
.replace(/\\/g, "\\\\")
|
|
163
|
+
.replace(/"/g, '\\"')
|
|
164
|
+
.replace(/\n/g, "\\n")
|
|
165
|
+
.replace(/\t/g, "\\t");
|
|
166
|
+
return `"${escaped}"`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Options for `setEntry`. */
|
|
170
|
+
export interface SetEntryOptions {
|
|
171
|
+
/**
|
|
172
|
+
* Comment lines written above the entry when it is APPENDED. An existing
|
|
173
|
+
* entry keeps whatever comment is already above it, because sous only ever
|
|
174
|
+
* rewrites the value line. Lines may be passed with or without their leading
|
|
175
|
+
* `#`.
|
|
176
|
+
*/
|
|
177
|
+
header?: string | string[];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Turns header text into `#`-prefixed comment lines. */
|
|
181
|
+
function headerLines(header: string | string[]): string[] {
|
|
182
|
+
const source = Array.isArray(header) ? header : header.split("\n");
|
|
183
|
+
return source.map((line) => {
|
|
184
|
+
const trimmed = line.trimEnd();
|
|
185
|
+
if (trimmed.length === 0) return "#";
|
|
186
|
+
return trimmed.startsWith("#") ? trimmed : `# ${trimmed}`;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Sets one variable in a line model, in place.
|
|
192
|
+
*
|
|
193
|
+
* An existing assignment is rewritten where it stands, keeping its indentation,
|
|
194
|
+
* its `export ` prefix and any trailing comment. When the key appears more than
|
|
195
|
+
* once, the LAST one is rewritten, because that is the one the parser honors.
|
|
196
|
+
* A key that is not there yet is appended at the end of the file, under its
|
|
197
|
+
* generated header comment.
|
|
198
|
+
*
|
|
199
|
+
* @param model - The model to edit.
|
|
200
|
+
* @param key - The variable's name.
|
|
201
|
+
* @param value - The value to store, quoted as needed.
|
|
202
|
+
* @param options - Header comment for a newly appended entry.
|
|
203
|
+
* @returns Whether the entry was updated in place or appended.
|
|
204
|
+
*/
|
|
205
|
+
export function setEntry(
|
|
206
|
+
model: EnvFileModel,
|
|
207
|
+
key: string,
|
|
208
|
+
value: string,
|
|
209
|
+
options: SetEntryOptions = {}
|
|
210
|
+
): "updated" | "appended" {
|
|
211
|
+
const quoted = quoteEnvValue(value);
|
|
212
|
+
|
|
213
|
+
for (let i = model.lines.length - 1; i >= 0; i--) {
|
|
214
|
+
const line = model.lines[i]!;
|
|
215
|
+
if (line.kind !== "assignment" || line.key !== key) continue;
|
|
216
|
+
|
|
217
|
+
const prefix = line.exported ? "export " : "";
|
|
218
|
+
const text = `${line.indent}${prefix}${key}=${quoted}${line.inlineComment}`;
|
|
219
|
+
model.lines[i] = { ...line, text, rawValue: quoted };
|
|
220
|
+
return "updated";
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Appending: separate the new entry from whatever came before with one blank
|
|
224
|
+
// line, unless the file is empty or already ends with one.
|
|
225
|
+
const last = model.lines[model.lines.length - 1];
|
|
226
|
+
if (last !== undefined && last.kind !== "blank") {
|
|
227
|
+
model.lines.push({ kind: "blank", text: "" });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (options.header !== undefined) {
|
|
231
|
+
for (const text of headerLines(options.header)) {
|
|
232
|
+
model.lines.push({ kind: "comment", text });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
model.lines.push({
|
|
237
|
+
kind: "assignment",
|
|
238
|
+
text: `${key}=${quoted}`,
|
|
239
|
+
indent: "",
|
|
240
|
+
exported: false,
|
|
241
|
+
key,
|
|
242
|
+
rawValue: quoted,
|
|
243
|
+
inlineComment: "",
|
|
244
|
+
});
|
|
245
|
+
model.trailingNewline = true;
|
|
246
|
+
return "appended";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Removes every assignment of one variable from a line model, leaving comments
|
|
251
|
+
* and blank lines alone (sous does not read comments, so it does not presume to
|
|
252
|
+
* know which ones belonged to the entry).
|
|
253
|
+
*
|
|
254
|
+
* @param model - The model to edit.
|
|
255
|
+
* @param key - The variable's name.
|
|
256
|
+
* @returns How many assignment lines were removed.
|
|
257
|
+
*/
|
|
258
|
+
export function removeEntry(model: EnvFileModel, key: string): number {
|
|
259
|
+
const before = model.lines.length;
|
|
260
|
+
model.lines = model.lines.filter(
|
|
261
|
+
(line) => !(line.kind === "assignment" && line.key === key)
|
|
262
|
+
);
|
|
263
|
+
return before - model.lines.length;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Reads an env file into a line model, returning an empty model when the file
|
|
268
|
+
* does not exist yet.
|
|
269
|
+
*
|
|
270
|
+
* @param filePath - Absolute path to the env file.
|
|
271
|
+
*/
|
|
272
|
+
export function readEnvFile(filePath: string): EnvFileModel {
|
|
273
|
+
if (!fs.existsSync(filePath)) return { lines: [], eol: "\n", trailingNewline: true };
|
|
274
|
+
return parseEnvFile(fs.readFileSync(filePath, "utf8"));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Writes a line model back to disk atomically: the new contents go to a
|
|
279
|
+
* temporary file in the same directory and are renamed over the original, so an
|
|
280
|
+
* interrupted write can never leave a half-written env file behind.
|
|
281
|
+
*
|
|
282
|
+
* A file that does not exist yet is created readable and writable by its owner
|
|
283
|
+
* only, since `.env.local` holds secrets; an existing file keeps its mode.
|
|
284
|
+
*
|
|
285
|
+
* @param filePath - Absolute path to the env file.
|
|
286
|
+
* @param model - The model to write.
|
|
287
|
+
*/
|
|
288
|
+
export function writeEnvFile(filePath: string, model: EnvFileModel): void {
|
|
289
|
+
const directory = path.dirname(filePath);
|
|
290
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
291
|
+
|
|
292
|
+
const temporary = path.join(
|
|
293
|
+
directory,
|
|
294
|
+
`.${path.basename(filePath)}.sous-${process.pid}-${Date.now()}.tmp`
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
let mode = 0o600;
|
|
298
|
+
try {
|
|
299
|
+
mode = fs.statSync(filePath).mode & 0o777;
|
|
300
|
+
} catch {
|
|
301
|
+
// No existing file, so the restrictive default stands.
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
fs.writeFileSync(temporary, renderEnvFile(model), { encoding: "utf8", mode });
|
|
305
|
+
fs.renameSync(temporary, filePath);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Reads, edits and rewrites one env file in a single call: the usual way to
|
|
310
|
+
* store an answer.
|
|
311
|
+
*
|
|
312
|
+
* @param filePath - Absolute path to the env file.
|
|
313
|
+
* @param key - The variable's name.
|
|
314
|
+
* @param value - The value to store.
|
|
315
|
+
* @param options - Header comment for a newly appended entry.
|
|
316
|
+
* @returns Whether the entry was updated in place or appended.
|
|
317
|
+
*/
|
|
318
|
+
export function updateEnvFile(
|
|
319
|
+
filePath: string,
|
|
320
|
+
key: string,
|
|
321
|
+
value: string,
|
|
322
|
+
options: SetEntryOptions = {}
|
|
323
|
+
): "updated" | "appended" {
|
|
324
|
+
const model = readEnvFile(filePath);
|
|
325
|
+
const outcome = setEntry(model, key, value, options);
|
|
326
|
+
writeEnvFile(filePath, model);
|
|
327
|
+
return outcome;
|
|
328
|
+
}
|