@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.
Files changed (206) hide show
  1. package/README.md +121 -35
  2. package/bin/run.js +10 -1
  3. package/docs/markdown/README.md +27 -0
  4. package/docs/markdown/_sidebar.md +18 -0
  5. package/docs/markdown/commands.md +308 -0
  6. package/docs/markdown/config-discovery.md +74 -0
  7. package/docs/markdown/config-inspection.md +69 -0
  8. package/docs/markdown/config-layers.md +92 -0
  9. package/docs/markdown/config-variables.md +79 -0
  10. package/docs/markdown/configuration.md +71 -0
  11. package/docs/markdown/design-principles.md +59 -0
  12. package/docs/markdown/repositories-authoring.md +408 -0
  13. package/docs/markdown/repositories-consuming.md +580 -0
  14. package/docs/markdown/repositories-file-formats.md +1084 -0
  15. package/docs/markdown/repositories-variables.md +387 -0
  16. package/docs/markdown/repositories.md +303 -0
  17. package/docs/markdown/skill-categories.md +58 -0
  18. package/package.json +73 -9
  19. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/SKILL.tpl.md +20 -20
  20. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/about-something.md +2 -2
  21. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/do-something.md +1 -1
  22. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/advanced-patterns.md +6 -6
  23. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/commands.md +5 -5
  24. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/frontmatter.md +3 -3
  25. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/SKILL.tpl.md +40 -25
  26. package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +70 -0
  27. package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +75 -0
  28. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/create-skill/SKILL.tpl.md +8 -9
  29. package/recipes/core/sous-skills/sous.recipe.yaml +45 -0
  30. package/sous.config.schema.json +337 -0
  31. package/src/base-command.ts +220 -67
  32. package/src/commands/build.ts +150 -73
  33. package/src/commands/clear.ts +23 -15
  34. package/src/commands/compile.ts +74 -16
  35. package/src/commands/config/get.ts +110 -0
  36. package/src/commands/config/show.ts +32 -0
  37. package/src/commands/config/validate.ts +53 -0
  38. package/src/commands/help.ts +46 -0
  39. package/src/commands/launch.ts +36 -14
  40. package/src/commands/lock/rebuild.ts +241 -0
  41. package/src/commands/lock/show.ts +115 -0
  42. package/src/commands/namespace/list.ts +117 -0
  43. package/src/commands/namespace/show.ts +110 -0
  44. package/src/commands/prune.ts +3 -11
  45. package/src/commands/recipe/list.ts +95 -0
  46. package/src/commands/recipe/show.ts +301 -0
  47. package/src/commands/repo/add.ts +145 -0
  48. package/src/commands/repo/gc.ts +172 -0
  49. package/src/commands/repo/init.ts +136 -0
  50. package/src/commands/repo/link.ts +500 -0
  51. package/src/commands/repo/list.ts +179 -0
  52. package/src/commands/repo/release.ts +619 -0
  53. package/src/commands/repo/remove.ts +193 -0
  54. package/src/commands/repo/search.ts +189 -0
  55. package/src/commands/repo/submit.ts +133 -0
  56. package/src/commands/repo/unlink.ts +147 -0
  57. package/src/commands/subscription/add.ts +285 -0
  58. package/src/commands/subscription/list.ts +129 -0
  59. package/src/commands/subscription/remove.ts +181 -0
  60. package/src/commands/vars/ask.ts +374 -0
  61. package/src/commands/vars/index.ts +79 -0
  62. package/src/commands/vars/list.ts +67 -0
  63. package/src/commands/vars/show.ts +77 -0
  64. package/src/config-command.ts +30 -0
  65. package/src/lib/build-service.ts +206 -54
  66. package/src/lib/config-discovery.ts +220 -27
  67. package/src/lib/config-inspect.ts +145 -0
  68. package/src/lib/config-kernel.mjs +377 -0
  69. package/src/lib/config-schema.ts +361 -0
  70. package/src/lib/env-file.ts +328 -0
  71. package/src/lib/env-local.ts +18 -1
  72. package/src/lib/errors.ts +32 -0
  73. package/src/lib/include-resolver.ts +108 -15
  74. package/src/lib/interactive.ts +165 -0
  75. package/src/lib/markdown-compiler.ts +118 -37
  76. package/src/lib/package-info.ts +25 -0
  77. package/src/lib/pid-service.ts +32 -21
  78. package/src/lib/refs/find.ts +589 -0
  79. package/src/lib/refs/index.ts +12 -0
  80. package/src/lib/refs/pick.ts +147 -0
  81. package/src/lib/refs/scopes.ts +61 -0
  82. package/src/lib/repos/catalog-display.ts +116 -0
  83. package/src/lib/repos/catalog-inputs.ts +160 -0
  84. package/src/lib/repos/catalog.ts +722 -0
  85. package/src/lib/repos/core-recipe.ts +105 -0
  86. package/src/lib/repos/defaults.ts +175 -0
  87. package/src/lib/repos/formats/common.ts +389 -0
  88. package/src/lib/repos/formats/index-file.ts +215 -0
  89. package/src/lib/repos/formats/links-map.ts +96 -0
  90. package/src/lib/repos/formats/lockfile.ts +167 -0
  91. package/src/lib/repos/formats/patterns.ts +57 -0
  92. package/src/lib/repos/formats/recipe-manifest.ts +395 -0
  93. package/src/lib/repos/formats/repo-manifest.ts +88 -0
  94. package/src/lib/repos/formats/store-entry.ts +84 -0
  95. package/src/lib/repos/freshness.ts +208 -0
  96. package/src/lib/repos/git-clone.ts +312 -0
  97. package/src/lib/repos/identity.ts +89 -0
  98. package/src/lib/repos/index.ts +58 -0
  99. package/src/lib/repos/links.ts +353 -0
  100. package/src/lib/repos/load-manifest.ts +236 -0
  101. package/src/lib/repos/lock-service.ts +453 -0
  102. package/src/lib/repos/locked-namespace-resolver.ts +90 -0
  103. package/src/lib/repos/locked-recipes.ts +254 -0
  104. package/src/lib/repos/managed-layer.ts +422 -0
  105. package/src/lib/repos/namespace-resolver.ts +370 -0
  106. package/src/lib/repos/providers/base.ts +206 -0
  107. package/src/lib/repos/providers/git.ts +233 -0
  108. package/src/lib/repos/providers/github.ts +294 -0
  109. package/src/lib/repos/providers/gitlab.ts +263 -0
  110. package/src/lib/repos/providers/http.ts +102 -0
  111. package/src/lib/repos/providers/index-cache.ts +382 -0
  112. package/src/lib/repos/providers/index.ts +106 -0
  113. package/src/lib/repos/providers/local.ts +391 -0
  114. package/src/lib/repos/providers/provider.ts +401 -0
  115. package/src/lib/repos/recipe-config-layers.ts +287 -0
  116. package/src/lib/repos/recipe-targets.ts +223 -0
  117. package/src/lib/repos/ref-search.ts +46 -0
  118. package/src/lib/repos/ref.ts +513 -0
  119. package/src/lib/repos/reference-report.ts +122 -0
  120. package/src/lib/repos/release/bump.ts +161 -0
  121. package/src/lib/repos/release/git-state.ts +305 -0
  122. package/src/lib/repos/release/index-builder.ts +635 -0
  123. package/src/lib/repos/release/index.ts +16 -0
  124. package/src/lib/repos/release/plan.ts +512 -0
  125. package/src/lib/repos/release/submit-service.ts +496 -0
  126. package/src/lib/repos/release/tags.ts +243 -0
  127. package/src/lib/repos/release/validate.ts +463 -0
  128. package/src/lib/repos/resolver.ts +789 -0
  129. package/src/lib/repos/scaffold/index.ts +238 -0
  130. package/src/lib/repos/scaffold/templates.ts +413 -0
  131. package/src/lib/repos/seed.ts +414 -0
  132. package/src/lib/repos/store/contract.ts +64 -0
  133. package/src/lib/repos/store/hash.ts +114 -0
  134. package/src/lib/repos/store/recipe-store.ts +599 -0
  135. package/src/lib/repos/store/settings.ts +58 -0
  136. package/src/lib/repos/subscription-service.ts +2678 -0
  137. package/src/lib/repos/trust.ts +447 -0
  138. package/src/lib/settings.ts +546 -189
  139. package/src/lib/sous-home.ts +104 -0
  140. package/src/lib/state.ts +52 -20
  141. package/src/lib/vars/ask.ts +1152 -0
  142. package/src/lib/vars/definition-source.ts +252 -0
  143. package/src/lib/vars/display.ts +233 -0
  144. package/src/lib/vars/index.ts +18 -0
  145. package/src/lib/vars/ladder.ts +282 -0
  146. package/src/lib/vars/mappings.ts +265 -0
  147. package/src/lib/vars/names.ts +94 -0
  148. package/src/lib/vars/preanswers.ts +395 -0
  149. package/src/lib/vars/question-plan.ts +218 -0
  150. package/src/lib/vars/report.ts +228 -0
  151. package/src/lib/vars/safe-regex.ts +235 -0
  152. package/src/lib/vars/validate.ts +312 -0
  153. package/src/lib/watch-loop.ts +148 -0
  154. package/src/templating/init-liquid-engine.ts +58 -16
  155. package/src/utils/choice-prompt.ts +143 -0
  156. package/src/utils/command-errors.ts +186 -0
  157. package/src/utils/command-help.ts +45 -0
  158. package/src/utils/confirm-prompt.ts +110 -0
  159. package/src/utils/flags.ts +153 -0
  160. package/src/utils/formatting.ts +540 -55
  161. package/src/utils/prompts.ts +35 -1
  162. package/src/utils/sous-directory.ts +245 -0
  163. package/src/utils/table.ts +603 -0
  164. package/src/utils/value-prompt.ts +119 -0
  165. package/bin/xcv +0 -5
  166. package/shared-prompts/_partials/resume-task.md +0 -51
  167. package/shared-prompts/_partials/sub-agent-delegation.md +0 -32
  168. package/shared-prompts/_partials/update-task-file.md +0 -52
  169. package/shared-prompts/memories/automated-browser-tasks/INDEX.tpl.md +0 -52
  170. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/SKILL.tpl.md +0 -102
  171. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/auth-failure-handling.mjs +0 -81
  172. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/chained-workflow.mjs +0 -126
  173. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/simple-fetch.mjs +0 -92
  174. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/architecture.md +0 -61
  175. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/auth-and-sessions.md +0 -65
  176. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/ctx-api.md +0 -96
  177. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/installation.md +0 -104
  178. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/script-conventions.md +0 -243
  179. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/chrome-state.mjs +0 -148
  180. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.mjs +0 -383
  181. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.spec.mjs +0 -267
  182. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/eslint.config.mjs +0 -56
  183. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/harness.mjs +0 -169
  184. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/keyring.mjs +0 -59
  185. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/logger.mjs +0 -25
  186. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/params.mjs +0 -140
  187. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/run.mjs +0 -140
  188. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/settings.tpl.mjs +0 -1
  189. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/utils.mjs +0 -185
  190. package/shared-prompts/skills/automated-browser-tasks/create-automated-browser-task/SKILL.tpl.md +0 -52
  191. package/shared-prompts/skills/automated-browser-tasks/running-automated-browser-tasks/SKILL.tpl.md +0 -59
  192. package/shared-prompts/skills/automated-browser-tasks/update-automated-browser-task/SKILL.tpl.md +0 -47
  193. package/shared-prompts/skills/control-flow/approve/SKILL.tpl.md +0 -26
  194. package/shared-prompts/skills/control-flow/opine/SKILL.tpl.md +0 -58
  195. package/shared-prompts/skills/control-flow/repeat/SKILL.tpl.md +0 -27
  196. package/shared-prompts/skills/control-flow/research/SKILL.tpl.md +0 -34
  197. package/shared-prompts/skills/sous-skills/about-sous/SKILL.tpl.md +0 -51
  198. package/shared-prompts/skills/task-files/about-task-files/SKILL.tpl.md +0 -122
  199. package/shared-prompts/skills/task-files/continue-task-in-new-branch/SKILL.tpl.md +0 -80
  200. package/shared-prompts/skills/task-files/go/SKILL.tpl.md +0 -14
  201. package/shared-prompts/skills/task-files/resume-task/SKILL.tpl.md +0 -13
  202. package/shared-prompts/skills/task-files/start-task/SKILL.tpl.md +0 -93
  203. package/shared-prompts/skills/task-files/update/SKILL.tpl.md +0 -14
  204. package/shared-prompts/skills/task-files/update-task-file/SKILL.tpl.md +0 -13
  205. /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/substitutions.md +0 -0
  206. /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/references/liquid-filters.md +0 -0
@@ -0,0 +1,401 @@
1
+ /**
2
+ * The provider interface.
3
+ *
4
+ * A provider knows how to talk to one kind of repository host. Version one
5
+ * ships exactly two, GitHub and GitLab, and the interface stays INTERNAL: it is
6
+ * not a published plugin API yet, so it can still change shape while the rest
7
+ * of the Repositories system settles.
8
+ *
9
+ * A provider does only two things on the read path, and both are deliberately
10
+ * small: hand back a repo's index file, and fetch one recipe's subtree at one
11
+ * tag. Anything larger (a full clone) belongs to the authoring workflow, not to
12
+ * installing recipes.
13
+ *
14
+ * The write path is just as small, and it is the ONLY place a host's own
15
+ * mechanics are allowed to live. Everything host-specific (which command line
16
+ * tool is driven, how a fork is made, what a proposal is called, how one is
17
+ * opened) belongs to a provider; the services above it ask in order and report
18
+ * what came back. A provider declares `submit` in its features once it answers
19
+ * the write side; one that does not may leave those methods out entirely, and
20
+ * `ProviderBase` answers them with a clear refusal naming the provider and the
21
+ * feature.
22
+ */
23
+
24
+ import { ConfigError } from "../../errors.js";
25
+ import type { CommandRunner } from "./git.js";
26
+ import type { FetchLike } from "./http.js";
27
+
28
+ /**
29
+ * What a provider can do. `fetch` is the read path every provider implements;
30
+ * `submit` is the propose-a-change path, which arrives in a later phase. A
31
+ * provider declares the feature only once it genuinely supports it.
32
+ */
33
+ export type ProviderFeature = "fetch" | "submit";
34
+
35
+ /**
36
+ * The identifier a repo entry uses to name its provider explicitly. `local` is a
37
+ * repository on this machine, for local development and for tests; its trust
38
+ * semantics are identical to a hosted one.
39
+ */
40
+ export type ProviderId = "github" | "gitlab" | "local";
41
+
42
+ /** A repository URL, taken apart into the pieces every provider needs. */
43
+ export type CanonicalRepo = {
44
+ /** The host the repository lives on, such as `github.com`. */
45
+ host: string;
46
+ /** The owning user, organization or group path. */
47
+ owner: string;
48
+ /** The repository's own name, with any `.git` suffix removed. */
49
+ name: string;
50
+ /** The canonical HTTPS clone URL. */
51
+ httpsUrl: string;
52
+ /** The canonical SSH clone URL. */
53
+ sshUrl: string;
54
+ };
55
+
56
+ /** Options every provider call accepts, all of them for testing seams. */
57
+ export type ProviderOptions = {
58
+ /**
59
+ * The directory subprocesses run in. A write-path call is made from inside
60
+ * the contributor's checkout, so the host's own command line tool reads the
61
+ * repository the contributor is standing in.
62
+ */
63
+ cwd?: string;
64
+ /** The environment to read tokens from. Defaults to `process.env`. */
65
+ env?: NodeJS.ProcessEnv;
66
+ /** The fetch implementation to use. Defaults to the global `fetch`. */
67
+ fetchImpl?: FetchLike;
68
+ /** How subprocesses are run. Defaults to spawning a real process. */
69
+ run?: CommandRunner;
70
+ };
71
+
72
+ /** What an index fetch returns. */
73
+ export type FetchedIndex = {
74
+ /** The raw text of the index file, still to be parsed and validated. */
75
+ text: string;
76
+ /** The git ref the file was read at, for the record. */
77
+ ref: string;
78
+ /** The entity tag the host sent, when it sent one. */
79
+ etag?: string;
80
+ };
81
+
82
+ /** What a provider's command line tool is called, and where to get it. */
83
+ export type ProviderCli = {
84
+ /** The executable, spelled the way it is typed. */
85
+ command: string;
86
+ /** The plain-language name used in messages, such as `the GitHub CLI`. */
87
+ label: string;
88
+ /** Where the tool is installed from, for the message that says it is missing. */
89
+ install: string;
90
+ };
91
+
92
+ /** What an authentication check found. */
93
+ export type AuthStatus = {
94
+ /** True when sous can act on the contributor's behalf here. */
95
+ ok: boolean;
96
+ /**
97
+ * One plain-language explanation, ready to print: what is signed in when the
98
+ * check passed, and what to install or run when it did not.
99
+ */
100
+ detail: string;
101
+ };
102
+
103
+ /** Where a fork of a repository ended up. */
104
+ export type ForkedRepo = {
105
+ /** The account the fork lives under. */
106
+ owner: string;
107
+ /** The fork's repository name. */
108
+ name: string;
109
+ /** The fork's HTTPS clone URL. */
110
+ httpsUrl: string;
111
+ /** The fork's SSH clone URL. */
112
+ sshUrl: string;
113
+ };
114
+
115
+ /** One change, described the way every provider needs to hear about it. */
116
+ export type ChangeProposal = {
117
+ /** The branch the change is on. */
118
+ branch: string;
119
+ /** The branch the proposal targets, when the caller knows it. */
120
+ base?: string;
121
+ /** The proposal's title. */
122
+ title: string;
123
+ /** The proposal's body. */
124
+ body: string;
125
+ /** True when the proposal should be opened as a draft. */
126
+ draft: boolean;
127
+ /**
128
+ * The account the branch was pushed to, when that is not the repository
129
+ * itself. Only a provider knows how a cross-repository proposal is spelled,
130
+ * so it is handed the owner and composes the rest.
131
+ */
132
+ head?: { owner: string };
133
+ };
134
+
135
+ /** What proposing a change produced. */
136
+ export type ProposedChange = {
137
+ /** The proposal's address, when the provider gave one. */
138
+ url?: string;
139
+ /** One plain-language line about what happened, ready to print. */
140
+ detail: string;
141
+ };
142
+
143
+ /** One repository host sous knows how to read from. */
144
+ export interface RepoProvider {
145
+ /** The provider's stable identifier, as written in a repo config entry. */
146
+ readonly id: ProviderId;
147
+ /** What this provider can do; see ProviderFeature. */
148
+ readonly features: ProviderFeature[];
149
+ /** True when this provider handles the given repository URL. */
150
+ matches(url: string): boolean;
151
+ /** Takes a repository URL apart, raising a ConfigError when it does not fit. */
152
+ canonicalize(url: string): CanonicalRepo;
153
+ /** Fetches the repo's `sous.index.json` at its default branch. */
154
+ fetchIndex(repo: CanonicalRepo, options?: ProviderOptions): Promise<FetchedIndex>;
155
+ /**
156
+ * Fetches ONE recipe's subtree at one tag into `destDir`, never the whole
157
+ * repository.
158
+ *
159
+ * @param repo - The canonicalized repository.
160
+ * @param recipePath - The recipe folder's path, relative to the repo root.
161
+ * @param tag - The git tag carrying the version to fetch.
162
+ * @param destDir - Where the recipe's files should end up.
163
+ * @param options - Testing seams.
164
+ */
165
+ fetchRecipeTree(
166
+ repo: CanonicalRepo,
167
+ recipePath: string,
168
+ tag: string,
169
+ destDir: string,
170
+ options?: ProviderOptions
171
+ ): Promise<void>;
172
+
173
+ // --- The write path, answered by a provider that declares `submit` --------
174
+
175
+ /** The command line tool this provider drives, when it has one. */
176
+ readonly cli?: ProviderCli;
177
+ /**
178
+ * What this provider calls a proposal, such as `pull request` or `merge
179
+ * request`. It is what the submission prints as it goes.
180
+ */
181
+ readonly proposalNoun?: string;
182
+ /** Whether sous can act on the contributor's behalf here, and why not. */
183
+ authStatus?(options?: ProviderOptions): Promise<AuthStatus>;
184
+ /**
185
+ * Whether the contributor may push to the repository itself. Undefined means
186
+ * the provider genuinely cannot tell, which is not the same as `false`.
187
+ */
188
+ canPush?(repo: CanonicalRepo, options?: ProviderOptions): Promise<boolean | undefined>;
189
+ /** Forks the repository onto the contributor's own account. */
190
+ fork?(repo: CanonicalRepo, options?: ProviderOptions): Promise<ForkedRepo>;
191
+ /** Opens a proposal for a branch that has already been pushed. */
192
+ proposeChange?(
193
+ repo: CanonicalRepo,
194
+ proposal: ChangeProposal,
195
+ options?: ProviderOptions
196
+ ): Promise<ProposedChange>;
197
+ }
198
+
199
+ /**
200
+ * A provider that answers the whole write path. This is what declaring the
201
+ * `submit` feature promises, and `supportsSubmit` is how a caller gets from the
202
+ * one to the other without ever naming a provider.
203
+ */
204
+ export type SubmitCapableProvider = RepoProvider &
205
+ Required<Pick<RepoProvider, "authStatus" | "canPush" | "fork" | "proposeChange">>;
206
+
207
+ /**
208
+ * True when a provider declares the `submit` feature and really does answer
209
+ * every write-path call. It narrows the type, so a caller that has asked once
210
+ * never has to test a method for existence again.
211
+ *
212
+ * @param provider - The provider to test.
213
+ */
214
+ export function supportsSubmit(provider: RepoProvider): provider is SubmitCapableProvider {
215
+ return (
216
+ provider.features.includes("submit") &&
217
+ typeof provider.authStatus === "function" &&
218
+ typeof provider.canPush === "function" &&
219
+ typeof provider.fork === "function" &&
220
+ typeof provider.proposeChange === "function"
221
+ );
222
+ }
223
+
224
+ /**
225
+ * Normalizes a repository URL for matching: trims it, drops a trailing slash
226
+ * and a trailing `.git`, and lowercases the scheme and host only.
227
+ *
228
+ * @param url - The URL as configured.
229
+ */
230
+ export function normalizeRepoUrl(url: string): string {
231
+ let value = url.trim();
232
+ while (value.endsWith("/")) value = value.slice(0, -1);
233
+ if (value.endsWith(".git")) value = value.slice(0, -4);
234
+ return value;
235
+ }
236
+
237
+ /**
238
+ * Takes a repository URL apart into host, owner and name. Accepts the HTTPS
239
+ * form, the `scp`-style SSH form (`git@host:owner/name.git`) and the
240
+ * `ssh://host/owner/name` form, because all three are what people paste.
241
+ *
242
+ * Returns undefined rather than throwing, so a provider's `matches` can use it.
243
+ *
244
+ * @param url - The URL as configured.
245
+ */
246
+ export function splitRepoUrl(
247
+ url: string
248
+ ): { host: string; owner: string; name: string } | undefined {
249
+ const normalized = normalizeRepoUrl(url);
250
+ if (normalized.length === 0) return undefined;
251
+
252
+ let host: string;
253
+ let repoPath: string;
254
+
255
+ const scpMatch = /^(?:([^@\s/]+)@)?([^@\s/:]+):(.+)$/.exec(normalized);
256
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(normalized)) {
257
+ let parsed: URL;
258
+ try {
259
+ parsed = new URL(normalized);
260
+ } catch {
261
+ return undefined;
262
+ }
263
+ host = parsed.host.toLowerCase();
264
+ repoPath = parsed.pathname.replace(/^\/+/, "");
265
+ } else if (scpMatch !== null) {
266
+ host = scpMatch[2]!.toLowerCase();
267
+ repoPath = scpMatch[3]!.replace(/^\/+/, "");
268
+ } else {
269
+ return undefined;
270
+ }
271
+
272
+ const segments = repoPath.split("/").filter((segment) => segment.length > 0);
273
+ if (segments.length < 2 || host.length === 0) return undefined;
274
+
275
+ const name = segments[segments.length - 1]!;
276
+ const owner = segments.slice(0, -1).join("/");
277
+ return { host, owner, name };
278
+ }
279
+
280
+ /**
281
+ * Builds the canonical form of a repository URL for a given host style.
282
+ *
283
+ * @param host - The repository host.
284
+ * @param owner - The owning user, organization or group path.
285
+ * @param name - The repository name.
286
+ */
287
+ export function buildCanonicalRepo(
288
+ host: string,
289
+ owner: string,
290
+ name: string
291
+ ): CanonicalRepo {
292
+ return {
293
+ host,
294
+ owner,
295
+ name,
296
+ httpsUrl: `https://${host}/${owner}/${name}.git`,
297
+ sshUrl: `git@${host}:${owner}/${name}.git`,
298
+ };
299
+ }
300
+
301
+ /**
302
+ * Raises the standard ConfigError for a URL a provider cannot take apart.
303
+ *
304
+ * @param providerId - Which provider rejected it.
305
+ * @param url - The offending URL.
306
+ */
307
+ export function invalidRepoUrl(providerId: ProviderId, url: string): ConfigError {
308
+ return new ConfigError(
309
+ `'${url}' is not a ${providerId} repository URL that sous can read.\n` +
310
+ ` A repository URL names an owner and a repository, as in ` +
311
+ `'https://${providerId}.com/owner/repository'.`
312
+ );
313
+ }
314
+
315
+ /**
316
+ * Finds the provider that handles a repository URL among the ones given, or
317
+ * undefined when none does. `providers/index.ts` wraps this with the built-in
318
+ * provider list, which is what callers normally use.
319
+ *
320
+ * @param url - The repository URL.
321
+ * @param providers - The providers to consider.
322
+ */
323
+ export function detectProviderIn(
324
+ url: string,
325
+ providers: RepoProvider[]
326
+ ): RepoProvider | undefined {
327
+ return providers.find((provider) => provider.matches(url));
328
+ }
329
+
330
+ /**
331
+ * Looks a provider up by its identifier among the ones given, or undefined when
332
+ * there is none.
333
+ *
334
+ * @param id - The provider identifier from a repo config entry.
335
+ * @param providers - The providers to consider.
336
+ */
337
+ export function providerByIdIn(
338
+ id: string,
339
+ providers: RepoProvider[]
340
+ ): RepoProvider | undefined {
341
+ return providers.find((provider) => provider.id === id);
342
+ }
343
+
344
+ /**
345
+ * Finds the provider for a repo entry: the one it names, otherwise the one that
346
+ * recognizes its URL. Raises a ConfigError naming the URL and listing the
347
+ * providers sous knows when neither works, and another when the named provider
348
+ * contradicts a URL a different provider plainly owns.
349
+ *
350
+ * @param url - The repository URL.
351
+ * @param providerId - The provider named by the repo entry, when it named one.
352
+ * @param providers - The providers to consider.
353
+ */
354
+ export function requireProviderIn(
355
+ url: string,
356
+ providerId: string | undefined,
357
+ providers: RepoProvider[]
358
+ ): RepoProvider {
359
+ const known = providers.map((entry) => entry.id).join(", ");
360
+
361
+ if (providerId !== undefined) {
362
+ const named = providerByIdIn(providerId, providers);
363
+ if (named === undefined) {
364
+ throw new ConfigError(
365
+ `The repository at ${url} names the provider '${providerId}', which sous does not ` +
366
+ `have.\n Sous ships these providers: ${known}.`
367
+ );
368
+ }
369
+
370
+ // A named provider is honoured for a host nobody recognizes, which is what a
371
+ // self-hosted instance needs. It is refused only when a DIFFERENT provider
372
+ // plainly owns the URL, because that is a contradiction rather than a hint.
373
+ if (!named.matches(url)) {
374
+ const owner = detectProviderIn(url, providers);
375
+ if (owner !== undefined) {
376
+ throw new ConfigError(
377
+ `The ${named.id} provider does not handle ${url}; that is ` +
378
+ `${describeRepoUrl(owner.id)}, which the ${owner.id} provider handles.\n` +
379
+ ` Drop '--provider' and let sous work it out, or name '${owner.id}'.`
380
+ );
381
+ }
382
+ }
383
+
384
+ return named;
385
+ }
386
+
387
+ const detected = detectProviderIn(url, providers);
388
+ if (detected !== undefined) return detected;
389
+
390
+ throw new ConfigError(
391
+ `Sous does not recognize the host in the repository URL ${url}.\n` +
392
+ ` Sous ships these providers: ${known}. For a self-hosted instance, name the ` +
393
+ `provider that host runs on the repository entry, as in ` +
394
+ `'sous repo add ${url} --provider <provider>'.`
395
+ );
396
+ }
397
+
398
+ /** How a URL a provider recognizes is described in a mismatch message. */
399
+ function describeRepoUrl(providerId: ProviderId): string {
400
+ return providerId === "local" ? "a local path" : `a ${providerId} URL`;
401
+ }
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Config layers that come from subscribed recipes.
3
+ *
4
+ * A recipe may contribute `config` content: files that are merged into the
5
+ * subscribing project's configuration rather than written anywhere in it. They
6
+ * are loaded AFTER the primary config and BEFORE the `conf.d/` drop-ins, so a
7
+ * recipe can supply defaults and the project always wins over them.
8
+ *
9
+ * This has to work before variable resolution, and before the settings even
10
+ * exist, because these layers are part of what the settings are built from. It
11
+ * therefore reads nothing but the lockfile, the links map and the store, all of
12
+ * which are locatable from the `.sous/` directory and the environment alone.
13
+ *
14
+ * A recipe layer is JSON (`.json`, or `.jsonc` for JSON with comments) or YAML
15
+ * only. The config kernel would happily import a
16
+ * `.js` layer, and a repository's whole trust story rests on sous being able to
17
+ * read what it publishes without running any of it, so an executable layer from
18
+ * a recipe is refused rather than loaded.
19
+ *
20
+ * A recipe layer is also read HERE rather than by the config kernel, and only
21
+ * the keys on the allowlist below survive the reading. A recipe is content you
22
+ * subscribed to; it does not get to decide what sous trusts or what sous runs.
23
+ * See RECIPE_CONFIG_ALLOWED_KEYS.
24
+ */
25
+
26
+ import fs from "node:fs";
27
+ import path from "node:path";
28
+ import { globSync } from "glob";
29
+ import { parse as parseYaml } from "yaml";
30
+ import { parseJsoncText } from "./load-manifest.js";
31
+ import { listLockedRecipes, readRecipeManifestIn } from "./locked-recipes.js";
32
+
33
+ /** The layer extensions a recipe may contribute; the executable ones are refused. */
34
+ export const RECIPE_LAYER_EXTENSIONS = [".json", ".jsonc", ".yaml", ".yml"] as const;
35
+
36
+ /** Extensions sous can load as a config layer but deliberately will not take from a recipe. */
37
+ export const RECIPE_LAYER_EXECUTABLE_EXTENSIONS = [".js", ".mjs", ".cjs", ".ts"] as const;
38
+
39
+ /**
40
+ * The only top-level config keys a recipe's config layer may set.
41
+ *
42
+ * A recipe layer is merged into the project's configuration, so without this
43
+ * allowlist a recipe could add a repository to `repos:` (granting itself, and
44
+ * anything published beside it, trust the person never gave), add a
45
+ * subscription, or point a `tools.<name>.command` at a program that `sous
46
+ * launch` then spawns. Every key here is inert with respect to trust and to
47
+ * what sous executes: a recipe can supply variables, aliases, compilation
48
+ * targets, runtime context, where recipe output lands, store knobs and variable
49
+ * mappings, and nothing else.
50
+ *
51
+ * Anything not on this list is dropped with a warning naming the recipe and the
52
+ * key, including keys sous does not recognise at all.
53
+ */
54
+ export const RECIPE_CONFIG_ALLOWED_KEYS = [
55
+ "_aliases",
56
+ "_vars",
57
+ "compilation",
58
+ "recipeOutputs",
59
+ "runtimeContext",
60
+ "store",
61
+ "varMappings",
62
+ ] as const;
63
+
64
+ /**
65
+ * Plain-language reasons for the refused keys a recipe is most likely to try,
66
+ * so the warning says why rather than only that.
67
+ */
68
+ const REFUSAL_REASONS: Record<string, string> = {
69
+ repos:
70
+ "only you decide which repositories this project trusts, so a recipe may not add one",
71
+ subscriptions:
72
+ "only you decide what this project subscribes to, so a recipe may not subscribe on your behalf",
73
+ tools:
74
+ "a tool entry names a program sous launches, so a recipe may not add one or change one",
75
+ _env: "an _env mapping decides which environment variables reach the build",
76
+ version: "the config version is the project's own to declare",
77
+ name: "the project's display name is the project's own to declare",
78
+ $schema: "the schema binding is the project's own editor setting",
79
+ };
80
+
81
+ /** One recipe config layer, already read and already filtered. */
82
+ export type RecipeConfigLayer = {
83
+ /** Absolute path of the file it was read from, used for reporting and tracing. */
84
+ path: string;
85
+ /** The `namespace/recipe` key of the recipe that contributed it. */
86
+ recipeKey: string;
87
+ /** The layer's content, with every key outside the allowlist removed. */
88
+ config: Record<string, unknown>;
89
+ };
90
+
91
+ /** What a recipe's config contents came to. */
92
+ export type RecipeConfigLayers = {
93
+ /** The layers, in the order they should be merged. */
94
+ layers: RecipeConfigLayer[];
95
+ /** Absolute paths of those layers, in the same order. */
96
+ paths: string[];
97
+ /** Complete, plain-language sentences about anything that was skipped. */
98
+ warnings: string[];
99
+ };
100
+
101
+ /**
102
+ * Removes every top-level key a recipe is not allowed to set.
103
+ *
104
+ * Pure, so the rule can be tested on its own: give it whatever a layer file
105
+ * parsed to and it returns what may be merged, plus a sentence for everything
106
+ * it took out.
107
+ *
108
+ * @param recipeKey - The `namespace/recipe` key, named in every warning.
109
+ * @param layerPath - The layer file, named in every warning.
110
+ * @param raw - Whatever the layer file parsed to.
111
+ */
112
+ export function filterRecipeConfigLayer(
113
+ recipeKey: string,
114
+ layerPath: string,
115
+ raw: unknown
116
+ ): { config: Record<string, unknown>; warnings: string[] } {
117
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
118
+ return {
119
+ config: {},
120
+ warnings: [
121
+ `The recipe ${recipeKey} contributes a config layer that is not a set of ` +
122
+ `configuration keys, so none of it was merged:\n ${layerPath}\n` +
123
+ `A config layer has to be an object with configuration keys at its top level.`,
124
+ ],
125
+ };
126
+ }
127
+
128
+ const warnings: string[] = [];
129
+ const allowed = new Set<string>(RECIPE_CONFIG_ALLOWED_KEYS);
130
+ const config: Record<string, unknown> = {};
131
+
132
+ for (const key of Object.keys(raw)) {
133
+ if (allowed.has(key)) {
134
+ config[key] = (raw as Record<string, unknown>)[key];
135
+ continue;
136
+ }
137
+ const reason =
138
+ REFUSAL_REASONS[key] ?? "sous does not recognise it as a key a recipe may set";
139
+ warnings.push(
140
+ `The recipe ${recipeKey} tried to set '${key}' in a config layer, and sous ` +
141
+ `removed it before merging anything:\n ${layerPath}\n` +
142
+ `That key is not one a recipe may set (${reason}). A recipe's config layer may ` +
143
+ `set only: ${RECIPE_CONFIG_ALLOWED_KEYS.join(", ")}.`
144
+ );
145
+ }
146
+
147
+ return { config, warnings };
148
+ }
149
+
150
+ /**
151
+ * Every config layer the recipes this project subscribes to contribute, read
152
+ * and filtered, ordered by recipe key and then by path so the merge order is
153
+ * the same on every machine.
154
+ *
155
+ * Only recipes held through `subscribes` contribute; a build dependency is
156
+ * addressable from the recipe that declared it and changes nothing about the
157
+ * project, its configuration included.
158
+ *
159
+ * @param sousDir - The project's `.sous/` directory.
160
+ * @param env - The environment to read; decides where the store is.
161
+ */
162
+ export function listRecipeConfigLayers(
163
+ sousDir: string,
164
+ env: NodeJS.ProcessEnv = process.env
165
+ ): RecipeConfigLayers {
166
+ const result: RecipeConfigLayers = { layers: [], paths: [], warnings: [] };
167
+
168
+ let locked;
169
+ try {
170
+ locked = listLockedRecipes({ sousDir, env });
171
+ } catch {
172
+ // Config discovery runs before anything can report a problem nicely. A
173
+ // lockfile that does not parse is reported by every other reader of it, so
174
+ // contributing no layers here is the quiet, correct answer.
175
+ return result;
176
+ }
177
+
178
+ const executable: string[] = [];
179
+ const unsupported: string[] = [];
180
+
181
+ for (const recipe of locked) {
182
+ if (recipe.kind !== "subscribes") continue;
183
+ if (!recipe.present) continue;
184
+
185
+ let manifest;
186
+ try {
187
+ manifest = readRecipeManifestIn(recipe.dir);
188
+ } catch {
189
+ continue;
190
+ }
191
+ if (manifest === undefined) continue;
192
+
193
+ const found: string[] = [];
194
+
195
+ for (const content of manifest.contents) {
196
+ if (content.kind !== "config") continue;
197
+
198
+ const ignore = (content.exclude ?? []).map((pattern) =>
199
+ path.join(recipe.dir, pattern)
200
+ );
201
+
202
+ for (const include of content.include) {
203
+ for (const filePath of globSync(path.join(recipe.dir, include), {
204
+ absolute: true,
205
+ ignore,
206
+ })) {
207
+ if (!isFile(filePath)) continue;
208
+ const extension = path.extname(filePath).toLowerCase();
209
+ if (
210
+ (RECIPE_LAYER_EXECUTABLE_EXTENSIONS as readonly string[]).includes(extension)
211
+ ) {
212
+ executable.push(filePath);
213
+ continue;
214
+ }
215
+ if (!(RECIPE_LAYER_EXTENSIONS as readonly string[]).includes(extension)) {
216
+ unsupported.push(filePath);
217
+ continue;
218
+ }
219
+ found.push(path.normalize(filePath));
220
+ }
221
+ }
222
+ }
223
+
224
+ for (const layerPath of [...new Set(found)].sort()) {
225
+ let raw: unknown;
226
+ try {
227
+ raw = readLayerFile(layerPath);
228
+ } catch (error) {
229
+ result.warnings.push(
230
+ `The recipe ${recipe.key} contributes a config layer sous could not read, so ` +
231
+ `none of it was merged:\n ${layerPath}\n` +
232
+ `${error instanceof Error ? error.message : String(error)}`
233
+ );
234
+ continue;
235
+ }
236
+
237
+ const filtered = filterRecipeConfigLayer(recipe.key, layerPath, raw);
238
+ result.warnings.push(...filtered.warnings);
239
+ result.layers.push({
240
+ path: layerPath,
241
+ recipeKey: recipe.key,
242
+ config: filtered.config,
243
+ });
244
+ result.paths.push(layerPath);
245
+ }
246
+ }
247
+
248
+ if (executable.length > 0) {
249
+ result.warnings.push(
250
+ `Some subscribed recipes contribute config layers that are program code, and sous ` +
251
+ `refused to load them:\n` +
252
+ executable.map((entry) => ` ${entry}`).join("\n") +
253
+ `\nSous must be able to read everything a repository publishes without running ` +
254
+ `any of it, so an executable layer from a recipe is never loaded. Publish the ` +
255
+ `same settings as ${RECIPE_LAYER_EXTENSIONS.join(", ")} instead.`
256
+ );
257
+ }
258
+
259
+ if (unsupported.length > 0) {
260
+ result.warnings.push(
261
+ `Some subscribed recipes contribute config layers written in a format sous does ` +
262
+ `not read, and they were skipped:\n` +
263
+ unsupported.map((entry) => ` ${entry}`).join("\n") +
264
+ `\nA recipe's config layer is written as ${RECIPE_LAYER_EXTENSIONS.join(", ")}.`
265
+ );
266
+ }
267
+
268
+ return result;
269
+ }
270
+
271
+ /** Reads and parses one recipe config layer according to its extension. */
272
+ function readLayerFile(layerPath: string): unknown {
273
+ const text = fs.readFileSync(layerPath, "utf8");
274
+ const extension = path.extname(layerPath).toLowerCase();
275
+ if (extension === ".jsonc") return parseJsoncText(text, layerPath);
276
+ if (extension === ".json") return JSON.parse(text);
277
+ return parseYaml(text);
278
+ }
279
+
280
+ /** True when the path is a regular file. */
281
+ function isFile(candidate: string): boolean {
282
+ try {
283
+ return fs.statSync(candidate).isFile();
284
+ } catch {
285
+ return false;
286
+ }
287
+ }