@sous-io/sous 0.2.37 → 1.0.2

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 (244) hide show
  1. package/README.md +14 -256
  2. package/dist/bin.js +11 -0
  3. package/dist/sous.js +1094 -0
  4. package/docs/markdown/README.md +5 -4
  5. package/docs/markdown/_sidebar.md +21 -1
  6. package/docs/markdown/commands.md +294 -431
  7. package/docs/markdown/config-discovery.md +25 -14
  8. package/docs/markdown/config-inspection.md +53 -31
  9. package/docs/markdown/config-layers.md +168 -66
  10. package/docs/markdown/config-variables.md +44 -11
  11. package/docs/markdown/configuration.md +104 -17
  12. package/docs/markdown/design-principles.md +4 -1
  13. package/docs/markdown/repositories-authoring.md +30 -0
  14. package/docs/markdown/repositories-consuming.md +72 -14
  15. package/docs/markdown/repositories-file-formats.md +31 -13
  16. package/docs/markdown/repositories-providers.md +48 -57
  17. package/docs/markdown/repositories-quickstart.md +3 -2
  18. package/docs/markdown/repositories-troubleshooting.md +27 -1
  19. package/docs/markdown/repositories-variables.md +6 -1
  20. package/docs/markdown/repositories.md +18 -8
  21. package/package.json +48 -107
  22. package/recipes/core/sous-skills/skills/about-liquid-templates/SKILL.tpl.md +4 -6
  23. package/recipes/core/sous-skills/skills/about-liquid-templates/references/liquid-filters.tpl.md +2 -0
  24. package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +10 -9
  25. package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +35 -14
  26. package/recipes/core/sous-skills/skills/about-sous-recipes/SKILL.tpl.md +51 -20
  27. package/recipes/core/sous-skills/sous.recipe.yaml +1 -1
  28. package/sous.config.schema.json +351 -132
  29. package/bin/run.js +0 -51
  30. package/src/base-command.ts +0 -457
  31. package/src/commands/build.ts +0 -181
  32. package/src/commands/clear.ts +0 -79
  33. package/src/commands/compile.ts +0 -155
  34. package/src/commands/config/get.ts +0 -110
  35. package/src/commands/config/show.ts +0 -32
  36. package/src/commands/config/validate.ts +0 -53
  37. package/src/commands/help.ts +0 -46
  38. package/src/commands/init.ts +0 -289
  39. package/src/commands/launch.ts +0 -139
  40. package/src/commands/lock/rebuild.ts +0 -239
  41. package/src/commands/lock/show.ts +0 -115
  42. package/src/commands/namespace/list.ts +0 -138
  43. package/src/commands/namespace/show.ts +0 -130
  44. package/src/commands/prune.ts +0 -43
  45. package/src/commands/recipe/list.ts +0 -112
  46. package/src/commands/recipe/show.ts +0 -439
  47. package/src/commands/repo/add.ts +0 -145
  48. package/src/commands/repo/contribute.ts +0 -604
  49. package/src/commands/repo/gc.ts +0 -173
  50. package/src/commands/repo/init.ts +0 -136
  51. package/src/commands/repo/link.ts +0 -899
  52. package/src/commands/repo/list.ts +0 -236
  53. package/src/commands/repo/release.ts +0 -685
  54. package/src/commands/repo/remove.ts +0 -194
  55. package/src/commands/repo/search.ts +0 -243
  56. package/src/commands/repo/submit.ts +0 -341
  57. package/src/commands/repo/unlink.ts +0 -461
  58. package/src/commands/subscription/add.ts +0 -286
  59. package/src/commands/subscription/list.ts +0 -208
  60. package/src/commands/subscription/remove.ts +0 -182
  61. package/src/commands/subscription/update.ts +0 -216
  62. package/src/commands/update.ts +0 -178
  63. package/src/commands/vars/ask.ts +0 -417
  64. package/src/commands/vars/index.ts +0 -79
  65. package/src/commands/vars/list.ts +0 -67
  66. package/src/commands/vars/show.ts +0 -77
  67. package/src/config-command.ts +0 -30
  68. package/src/lib/build-preparation.ts +0 -128
  69. package/src/lib/build-service.ts +0 -580
  70. package/src/lib/config-discovery.ts +0 -378
  71. package/src/lib/config-inspect.ts +0 -145
  72. package/src/lib/config-kernel.mjs +0 -379
  73. package/src/lib/config-schema.ts +0 -401
  74. package/src/lib/env-file.ts +0 -328
  75. package/src/lib/env-local.ts +0 -212
  76. package/src/lib/errors.ts +0 -32
  77. package/src/lib/glob-pattern.ts +0 -24
  78. package/src/lib/include-resolver.ts +0 -523
  79. package/src/lib/interactive.ts +0 -165
  80. package/src/lib/markdown-compiler.ts +0 -947
  81. package/src/lib/package-info.ts +0 -25
  82. package/src/lib/pid-service.ts +0 -99
  83. package/src/lib/project-install.d.mts +0 -44
  84. package/src/lib/project-install.mjs +0 -234
  85. package/src/lib/project-scaffold/index.ts +0 -332
  86. package/src/lib/project-scaffold/templates.ts +0 -240
  87. package/src/lib/repos/catalog-display.ts +0 -215
  88. package/src/lib/repos/catalog-inputs.ts +0 -288
  89. package/src/lib/repos/catalog.ts +0 -842
  90. package/src/lib/repos/contribute.ts +0 -452
  91. package/src/lib/repos/core-recipe.ts +0 -105
  92. package/src/lib/repos/declarations.ts +0 -187
  93. package/src/lib/repos/defaults.ts +0 -175
  94. package/src/lib/repos/formats/common.ts +0 -440
  95. package/src/lib/repos/formats/index-file.ts +0 -253
  96. package/src/lib/repos/formats/links-map.ts +0 -98
  97. package/src/lib/repos/formats/lockfile.ts +0 -169
  98. package/src/lib/repos/formats/patterns.ts +0 -57
  99. package/src/lib/repos/formats/recipe-manifest.ts +0 -205
  100. package/src/lib/repos/formats/repo-manifest.ts +0 -97
  101. package/src/lib/repos/formats/store-entry.ts +0 -84
  102. package/src/lib/repos/formats/variable-definition.ts +0 -268
  103. package/src/lib/repos/freshness.ts +0 -264
  104. package/src/lib/repos/git-clone.ts +0 -850
  105. package/src/lib/repos/identity.ts +0 -89
  106. package/src/lib/repos/index.ts +0 -57
  107. package/src/lib/repos/links.ts +0 -356
  108. package/src/lib/repos/load-manifest.ts +0 -237
  109. package/src/lib/repos/lock-service.ts +0 -454
  110. package/src/lib/repos/locked-namespace-resolver.ts +0 -232
  111. package/src/lib/repos/locked-recipe-lookup.ts +0 -123
  112. package/src/lib/repos/locked-recipes.ts +0 -254
  113. package/src/lib/repos/managed-layer.ts +0 -422
  114. package/src/lib/repos/memories-hash-name.ts +0 -33
  115. package/src/lib/repos/namespace-resolver.ts +0 -527
  116. package/src/lib/repos/providers/base.ts +0 -268
  117. package/src/lib/repos/providers/git.ts +0 -245
  118. package/src/lib/repos/providers/github.ts +0 -589
  119. package/src/lib/repos/providers/gitlab.ts +0 -332
  120. package/src/lib/repos/providers/http.ts +0 -128
  121. package/src/lib/repos/providers/index-cache.ts +0 -421
  122. package/src/lib/repos/providers/index.ts +0 -106
  123. package/src/lib/repos/providers/local.ts +0 -410
  124. package/src/lib/repos/providers/provider.ts +0 -615
  125. package/src/lib/repos/recipe-config-layers.ts +0 -316
  126. package/src/lib/repos/recipe-key-matcher.ts +0 -62
  127. package/src/lib/repos/recipe-memories.ts +0 -178
  128. package/src/lib/repos/recipe-targets.ts +0 -236
  129. package/src/lib/repos/ref-search.ts +0 -46
  130. package/src/lib/repos/release/bump.ts +0 -161
  131. package/src/lib/repos/release/changelog.ts +0 -461
  132. package/src/lib/repos/release/git-state.ts +0 -460
  133. package/src/lib/repos/release/index-builder.ts +0 -1108
  134. package/src/lib/repos/release/index.ts +0 -19
  135. package/src/lib/repos/release/plan.ts +0 -521
  136. package/src/lib/repos/release/settle.ts +0 -154
  137. package/src/lib/repos/release/submissions.ts +0 -214
  138. package/src/lib/repos/release/submit-checkout.ts +0 -290
  139. package/src/lib/repos/release/submit-questions.ts +0 -153
  140. package/src/lib/repos/release/submit-service.ts +0 -898
  141. package/src/lib/repos/release/tags.ts +0 -239
  142. package/src/lib/repos/release/validate.ts +0 -463
  143. package/src/lib/repos/resolver.ts +0 -1028
  144. package/src/lib/repos/scaffold/index.ts +0 -245
  145. package/src/lib/repos/scaffold/templates.ts +0 -517
  146. package/src/lib/repos/seed.ts +0 -473
  147. package/src/lib/repos/store/contract.ts +0 -64
  148. package/src/lib/repos/store/hash.ts +0 -191
  149. package/src/lib/repos/store/recipe-store.ts +0 -599
  150. package/src/lib/repos/store/settings.ts +0 -58
  151. package/src/lib/repos/subscription-service.ts +0 -3859
  152. package/src/lib/repos/trust.ts +0 -447
  153. package/src/lib/repos/update-plan.ts +0 -235
  154. package/src/lib/self-update/index.ts +0 -14
  155. package/src/lib/self-update/installs.ts +0 -413
  156. package/src/lib/self-update/managers.ts +0 -126
  157. package/src/lib/self-update/plan.ts +0 -315
  158. package/src/lib/self-update/registry.ts +0 -99
  159. package/src/lib/self-update/run.ts +0 -506
  160. package/src/lib/self-update/versions.ts +0 -243
  161. package/src/lib/settings.ts +0 -1107
  162. package/src/lib/sous-home.ts +0 -104
  163. package/src/lib/state.ts +0 -167
  164. package/src/lib/vars/answers.ts +0 -192
  165. package/src/lib/vars/ask.ts +0 -1152
  166. package/src/lib/vars/definition-source.ts +0 -261
  167. package/src/lib/vars/display.ts +0 -231
  168. package/src/lib/vars/index.ts +0 -20
  169. package/src/lib/vars/ladder.ts +0 -282
  170. package/src/lib/vars/mappings.ts +0 -260
  171. package/src/lib/vars/names.ts +0 -94
  172. package/src/lib/vars/preanswers.ts +0 -395
  173. package/src/lib/vars/question-plan.ts +0 -241
  174. package/src/lib/vars/report.ts +0 -228
  175. package/src/lib/vars/safe-regex.ts +0 -235
  176. package/src/lib/vars/secrets.ts +0 -131
  177. package/src/lib/vars/validate.ts +0 -312
  178. package/src/lib/version-report.ts +0 -90
  179. package/src/lib/watch-loop.ts +0 -148
  180. package/src/lib/watch-service.ts +0 -116
  181. package/src/services/ref-resolver/container.ts +0 -107
  182. package/src/services/ref-resolver/dependency.ts +0 -60
  183. package/src/services/ref-resolver/format.ts +0 -197
  184. package/src/services/ref-resolver/glob.ts +0 -115
  185. package/src/services/ref-resolver/hash-names.ts +0 -155
  186. package/src/services/ref-resolver/index.ts +0 -55
  187. package/src/services/ref-resolver/injectable.ts +0 -36
  188. package/src/services/ref-resolver/location.ts +0 -62
  189. package/src/services/ref-resolver/lookups/cached-index-lookup.ts +0 -53
  190. package/src/services/ref-resolver/lookups/catalog-lookup.ts +0 -23
  191. package/src/services/ref-resolver/lookups/catalog-matcher.ts +0 -261
  192. package/src/services/ref-resolver/lookups/chained-lookup.ts +0 -27
  193. package/src/services/ref-resolver/lookups/env-var-lookup.ts +0 -65
  194. package/src/services/ref-resolver/lookups/fetched-index-lookup.ts +0 -150
  195. package/src/services/ref-resolver/lookups/recorded-dependency-lookup.ts +0 -62
  196. package/src/services/ref-resolver/lookups/ref-lookup.ts +0 -54
  197. package/src/services/ref-resolver/lookups/variable-lookup.ts +0 -123
  198. package/src/services/ref-resolver/named-refs.ts +0 -144
  199. package/src/services/ref-resolver/parser/env-var-splitter.ts +0 -27
  200. package/src/services/ref-resolver/parser/location-splitter.ts +0 -239
  201. package/src/services/ref-resolver/parser/name-path-splitter.ts +0 -200
  202. package/src/services/ref-resolver/parser/partial-ref.ts +0 -84
  203. package/src/services/ref-resolver/parser/query-splitter.ts +0 -61
  204. package/src/services/ref-resolver/parser/range-splitter.ts +0 -52
  205. package/src/services/ref-resolver/parser/ref-parser.ts +0 -115
  206. package/src/services/ref-resolver/parser/repo-qualifier-splitter.ts +0 -51
  207. package/src/services/ref-resolver/parts.ts +0 -115
  208. package/src/services/ref-resolver/pruners/command-line-pruner.ts +0 -47
  209. package/src/services/ref-resolver/pruners/include-pruner.ts +0 -66
  210. package/src/services/ref-resolver/pruners/manifest-pruner.ts +0 -130
  211. package/src/services/ref-resolver/pruners/ref-pruner.ts +0 -135
  212. package/src/services/ref-resolver/pruners/rule-helpers.ts +0 -44
  213. package/src/services/ref-resolver/pruners/stored-key-pruner.ts +0 -136
  214. package/src/services/ref-resolver/ref-helpers.ts +0 -62
  215. package/src/services/ref-resolver/ref-picker-service.ts +0 -204
  216. package/src/services/ref-resolver/ref-report.ts +0 -84
  217. package/src/services/ref-resolver/ref-resolve-arguments.ts +0 -47
  218. package/src/services/ref-resolver/ref-resolve-result.ts +0 -36
  219. package/src/services/ref-resolver/ref-resolver-service.ts +0 -323
  220. package/src/services/ref-resolver/source.ts +0 -48
  221. package/src/services/ref-resolver/tokens.ts +0 -25
  222. package/src/services/ref-resolver/types.ts +0 -155
  223. package/src/templating/filters/bullet-list.ts +0 -9
  224. package/src/templating/filters/index.ts +0 -8
  225. package/src/templating/init-liquid-engine.ts +0 -139
  226. package/src/templating/lib/glob-files.ts +0 -74
  227. package/src/templating/lib/import-export.ts +0 -32
  228. package/src/templating/lib/secret-scope.ts +0 -41
  229. package/src/templating/lib/tag-args.ts +0 -19
  230. package/src/templating/tags/exportScalarVarsJs.ts +0 -46
  231. package/src/templating/tags/getFiles.ts +0 -89
  232. package/src/templating/tags/index.ts +0 -14
  233. package/src/templating/tags/listFiles.ts +0 -54
  234. package/src/templating/tags/showVars.ts +0 -28
  235. package/src/utils/choice-prompt.ts +0 -143
  236. package/src/utils/command-errors.ts +0 -186
  237. package/src/utils/command-help.ts +0 -45
  238. package/src/utils/confirm-prompt.ts +0 -110
  239. package/src/utils/flags.ts +0 -177
  240. package/src/utils/formatting.ts +0 -849
  241. package/src/utils/prompts.ts +0 -53
  242. package/src/utils/sous-directory.ts +0 -245
  243. package/src/utils/table.ts +0 -603
  244. package/src/utils/value-prompt.ts +0 -119
@@ -1,268 +0,0 @@
1
- /**
2
- * The base class every built-in provider extends.
3
- *
4
- * It carries the plumbing no provider should repeat: running a subprocess
5
- * through the injectable runner, capturing what one printed, finding a token in
6
- * the environment or from the host's own command line tool, and pulling the
7
- * address out of a tool's output.
8
- *
9
- * It also answers the whole write path with a refusal. A provider that does not
10
- * declare the `submit` feature (the local one, for instance) inherits four
11
- * methods that raise a ConfigError naming the provider and what was asked of
12
- * it, so a caller that skips the feature check gets a sentence rather than a
13
- * `TypeError`. The three calls behind the `proposals` feature are refused the
14
- * same way.
15
- */
16
-
17
- import { ConfigError } from "../../errors.js";
18
- import {
19
- spawnCommand,
20
- tryCommand,
21
- type CommandResult,
22
- type CommandRunner,
23
- } from "./git.js";
24
- import type {
25
- AuthStatus,
26
- CanonicalRepo,
27
- ChangeProposal,
28
- FetchedIndex,
29
- ForkedRepo,
30
- ProposalQuery,
31
- ProposalStatus,
32
- ProposalSummary,
33
- ProposalUpdate,
34
- ProposedChange,
35
- ProviderCli,
36
- ProviderFeature,
37
- ProviderId,
38
- ProviderOptions,
39
- RepoProvider,
40
- LocationReading,
41
- WrittenLocation,
42
- } from "./provider.js";
43
-
44
- /**
45
- * The first URL in a command's output, which is where a host's own tool prints
46
- * the thing it just created.
47
- *
48
- * firstUrlIn("https://github.com/o/r/pull/7\n"); // -> "https://github.com/o/r/pull/7"
49
- *
50
- * @param output - Whatever the command printed.
51
- */
52
- export function firstUrlIn(output: string): string | undefined {
53
- const match = /https?:\/\/\S+/.exec(output);
54
- return match === null ? undefined : match[0];
55
- }
56
-
57
- /** Everything a provider inherits rather than writes for itself. */
58
- export abstract class ProviderBase implements RepoProvider {
59
- abstract readonly id: ProviderId;
60
- abstract readonly features: ProviderFeature[];
61
-
62
- abstract matches(url: string): boolean;
63
- abstract canonicalize(url: string): CanonicalRepo;
64
- abstract fetchIndex(repo: CanonicalRepo, options?: ProviderOptions): Promise<FetchedIndex>;
65
- abstract fetchRecipeTree(
66
- repo: CanonicalRepo,
67
- recipePath: string,
68
- tag: string,
69
- destDir: string,
70
- options?: ProviderOptions
71
- ): Promise<void>;
72
-
73
- // --- Reading a location ----------------------------------------------------
74
-
75
- /** This provider's public host, declared by a provider that has one. */
76
- declare readonly defaultHost?: string;
77
-
78
- /**
79
- * No readings: a provider that does not override this reads no location
80
- * inside its repositories, so a ref naming one is refused with a sentence.
81
- *
82
- * @param _location - The written location; unused.
83
- */
84
- readLocation(_location: WrittenLocation): LocationReading[] {
85
- return [];
86
- }
87
-
88
- /**
89
- * `<id>://<host>/<repoPath>/<rest>`, with the host left out when it is this
90
- * provider's default.
91
- *
92
- * @param host - The repository's host.
93
- * @param repoPath - The repository's path on that host.
94
- * @param rest - What is named inside it.
95
- */
96
- formatLocator(host: string, repoPath: string, rest: string): string {
97
- const where = host === this.defaultHost ? repoPath : `${host}/${repoPath}`;
98
- return `${this.id}://${where}${rest.length === 0 ? "" : `/${rest}`}`;
99
- }
100
-
101
- // --- Subprocess plumbing ---------------------------------------------------
102
-
103
- /**
104
- * The runner a call should use: the injected one when a caller supplied it,
105
- * and a real process otherwise. Tests substitute their own, which is why no
106
- * provider reaches for `spawn` directly.
107
- *
108
- * @param options - The call's options.
109
- */
110
- protected runnerFor(options: ProviderOptions): CommandRunner {
111
- return options.run ?? spawnCommand;
112
- }
113
-
114
- /**
115
- * Runs a command and hands back everything it reported, including a non-zero
116
- * exit. A command that cannot be started at all comes back as exit code 127.
117
- *
118
- * @param command - The executable to run.
119
- * @param args - Its arguments, already split.
120
- * @param options - The call's options; `cwd` and `run` are used.
121
- */
122
- protected async runCommand(
123
- command: string,
124
- args: string[],
125
- options: ProviderOptions = {}
126
- ): Promise<CommandResult> {
127
- return this.runnerFor(options)(command, args, { cwd: options.cwd });
128
- }
129
-
130
- /**
131
- * True when a command ran and exited successfully, whatever it printed. Used
132
- * for the checks whose answer is the exit code itself, such as `auth status`.
133
- *
134
- * @param command - The executable to run.
135
- * @param args - Its arguments.
136
- * @param options - The call's options.
137
- */
138
- protected async commandSucceeds(
139
- command: string,
140
- args: string[],
141
- options: ProviderOptions = {}
142
- ): Promise<boolean> {
143
- try {
144
- const result = await this.runCommand(command, args, options);
145
- return result.code === 0;
146
- } catch {
147
- return false;
148
- }
149
- }
150
-
151
- /**
152
- * A command's trimmed standard output, or undefined when it did not succeed,
153
- * is not installed, or printed nothing at all.
154
- *
155
- * @param command - The executable to run.
156
- * @param args - Its arguments.
157
- * @param options - The call's options.
158
- */
159
- protected async capturedOutput(
160
- command: string,
161
- args: string[],
162
- options: ProviderOptions = {}
163
- ): Promise<string | undefined> {
164
- return tryCommand(command, args, {
165
- ...(options.cwd === undefined ? {} : { cwd: options.cwd }),
166
- ...(options.run === undefined ? {} : { run: options.run }),
167
- });
168
- }
169
-
170
- /**
171
- * Finds a host token: the environment first, then the host's own command line
172
- * tool when it is installed and signed in. Undefined is a normal answer,
173
- * because a public repository needs no token at all.
174
- *
175
- * @param envName - The environment variable to read, such as `GITHUB_TOKEN`.
176
- * @param args - The arguments that make the tool print a token.
177
- * @param options - Environment and subprocess runner overrides.
178
- */
179
- protected async findToken(
180
- envName: string,
181
- args: string[],
182
- options: ProviderOptions = {}
183
- ): Promise<string | undefined> {
184
- const env = options.env ?? process.env;
185
- const fromEnv = env[envName];
186
- if (fromEnv !== undefined && fromEnv.trim().length > 0) return fromEnv.trim();
187
- if (this.cli === undefined) return undefined;
188
- return this.capturedOutput(this.cli.command, args, options);
189
- }
190
-
191
- // --- The write path, refused unless a provider overrides it ----------------
192
-
193
- /**
194
- * The command line tool this provider drives, and what its proposals are
195
- * called. Both are declared by the provider that has them; `declare` here
196
- * only tells the type system they may exist, so a subclass's own field is the
197
- * one that ends up on the instance.
198
- */
199
- declare readonly cli?: ProviderCli;
200
- declare readonly proposalNoun?: string;
201
-
202
- async authStatus(_options: ProviderOptions = {}): Promise<AuthStatus> {
203
- throw this.unsupported("submit", "check whether you are signed in to it");
204
- }
205
-
206
- async canPush(
207
- _repo: CanonicalRepo,
208
- _options: ProviderOptions = {}
209
- ): Promise<boolean | undefined> {
210
- throw this.unsupported("submit", "check whether you can push to it");
211
- }
212
-
213
- async fork(_repo: CanonicalRepo, _options: ProviderOptions = {}): Promise<ForkedRepo> {
214
- throw this.unsupported("submit", "fork it on your behalf");
215
- }
216
-
217
- async proposeChange(
218
- _repo: CanonicalRepo,
219
- _proposal: ChangeProposal,
220
- _options: ProviderOptions = {}
221
- ): Promise<ProposedChange> {
222
- throw this.unsupported("submit", "propose a change to it");
223
- }
224
-
225
- // --- Proposals after the fact, refused unless a provider overrides them ----
226
-
227
- async findProposal(
228
- _repo: CanonicalRepo,
229
- _query: ProposalQuery,
230
- _options: ProviderOptions = {}
231
- ): Promise<ProposalSummary | undefined> {
232
- throw this.unsupported("proposals", "look for a proposal that is already open");
233
- }
234
-
235
- async proposalStatus(
236
- _repo: CanonicalRepo,
237
- _id: string,
238
- _options: ProviderOptions = {}
239
- ): Promise<ProposalStatus> {
240
- throw this.unsupported("proposals", "report where a proposal stands");
241
- }
242
-
243
- async updateProposal(
244
- _repo: CanonicalRepo,
245
- _id: string,
246
- _update: ProposalUpdate,
247
- _options: ProviderOptions = {}
248
- ): Promise<ProposedChange> {
249
- throw this.unsupported("proposals", "change a proposal's title or body");
250
- }
251
-
252
- /**
253
- * The refusal a provider gives when it is asked for something it never
254
- * claimed. It names the provider and the feature, so the caller learns why
255
- * rather than only that.
256
- *
257
- * @param feature - The feature the call belongs to.
258
- * @param what - What was being attempted, in plain language.
259
- */
260
- protected unsupported(feature: ProviderFeature, what: string): ConfigError {
261
- return new ConfigError(
262
- `The '${this.id}' provider does not support the '${feature}' feature, so sous cannot ` +
263
- `${what}.\n` +
264
- ` A provider answers only what its features promise; this one promises ` +
265
- `${this.features.map((entry) => `'${entry}'`).join(", ")}.`
266
- );
267
- }
268
- }
@@ -1,245 +0,0 @@
1
- /**
2
- * The one place the Repositories layer shells out.
3
- *
4
- * Every subprocess a provider runs (git itself, and the `gh` / `glab` CLIs when
5
- * they are present and can hand over a token) goes through the helpers here, so
6
- * failures are reported the same way everywhere: the command that was run, the
7
- * exit code it returned, and whatever it wrote to stderr.
8
- *
9
- * The runner is injectable. Tests substitute their own, so no test in this
10
- * layer ever spawns a process or touches the network.
11
- */
12
-
13
- import { spawn } from "node:child_process";
14
- import fs from "node:fs/promises";
15
- import path from "node:path";
16
- import { ConfigError } from "../../errors.js";
17
- import { LF_CHECKOUT_OPTIONS } from "../store/hash.js";
18
-
19
- /** What a finished subprocess reports back. */
20
- export type CommandResult = {
21
- /** The process exit code; 0 on success. */
22
- code: number;
23
- stdout: string;
24
- stderr: string;
25
- };
26
-
27
- /** How a command is run. Tests replace this with a function of their own. */
28
- export type CommandRunner = (
29
- command: string,
30
- args: string[],
31
- options: {
32
- cwd?: string;
33
- /**
34
- * Start the command through the shell. Only for a fixed command line: it
35
- * is how Windows finds a `.cmd` shim such as npm's.
36
- */
37
- shell?: boolean;
38
- }
39
- ) => Promise<CommandResult>;
40
-
41
- /**
42
- * The default runner: spawns the command, captures both streams, and resolves
43
- * once it exits. A command that cannot be started at all (it is not installed)
44
- * resolves with exit code 127, so callers treat it as an ordinary failure.
45
- *
46
- * @param command - The executable to run.
47
- * @param args - Its arguments, already split.
48
- * @param options - Optional working directory, and whether to start it through the shell.
49
- */
50
- export const spawnCommand: CommandRunner = (command, args, options = {}) =>
51
- new Promise<CommandResult>((resolve) => {
52
- const child = spawn(command, args, {
53
- cwd: options.cwd,
54
- stdio: ["ignore", "pipe", "pipe"],
55
- shell: options.shell === true,
56
- });
57
-
58
- let stdout = "";
59
- let stderr = "";
60
- child.stdout?.on("data", (chunk: Buffer) => {
61
- stdout += chunk.toString();
62
- });
63
- child.stderr?.on("data", (chunk: Buffer) => {
64
- stderr += chunk.toString();
65
- });
66
-
67
- child.on("error", (error: Error) => {
68
- resolve({ code: 127, stdout, stderr: `${stderr}${error.message}` });
69
- });
70
- child.on("close", (code) => {
71
- resolve({ code: code ?? 1, stdout, stderr });
72
- });
73
- });
74
-
75
- /** Options shared by every subprocess helper here. */
76
- export type RunOptions = {
77
- /** Directory to run in. */
78
- cwd?: string;
79
- /** The runner to use. Defaults to spawning a real process. */
80
- run?: CommandRunner;
81
- };
82
-
83
- /** Renders a command and its arguments the way a user would have typed them. */
84
- function describeCommand(command: string, args: string[]): string {
85
- return [command, ...args].join(" ");
86
- }
87
-
88
- /**
89
- * Runs `git` with the given arguments and returns its standard output, trimmed.
90
- * A non-zero exit is a ConfigError naming the command, the exit code and the
91
- * error output, because that is what a user needs in order to fix it.
92
- *
93
- * @param args - Arguments to pass to git.
94
- * @param options - Working directory and an optional runner override.
95
- */
96
- export async function runGit(args: string[], options: RunOptions = {}): Promise<string> {
97
- const run = options.run ?? spawnCommand;
98
- const result = await run("git", args, { cwd: options.cwd });
99
-
100
- if (result.code !== 0) {
101
- const lines = [
102
- `The command '${describeCommand("git", args)}' failed with exit code ${result.code}.`,
103
- ];
104
- if (options.cwd !== undefined) lines.push(` It was run in ${options.cwd}.`);
105
- const detail = result.stderr.trim() || result.stdout.trim();
106
- if (detail.length > 0) {
107
- for (const line of detail.split("\n")) lines.push(` ${line}`);
108
- }
109
- if (result.code === 127) {
110
- lines.push(
111
- " Sous fetches recipes with git, so git must be installed and on your PATH."
112
- );
113
- }
114
- throw new ConfigError(lines.join("\n"));
115
- }
116
-
117
- return result.stdout.trim();
118
- }
119
-
120
- /**
121
- * Runs a command that sous can do without, returning its trimmed output or
122
- * undefined when it is missing, unauthenticated, or otherwise unhappy. Used for
123
- * the optional `gh auth token` and `glab auth token` lookups: a missing CLI is
124
- * never a reason to fail.
125
- *
126
- * @param command - The executable to try.
127
- * @param args - Its arguments.
128
- * @param options - Working directory and an optional runner override.
129
- */
130
- export async function tryCommand(
131
- command: string,
132
- args: string[],
133
- options: RunOptions = {}
134
- ): Promise<string | undefined> {
135
- const run = options.run ?? spawnCommand;
136
- try {
137
- const result = await run(command, args, { cwd: options.cwd });
138
- if (result.code !== 0) return undefined;
139
- const output = result.stdout.trim();
140
- return output.length > 0 ? output : undefined;
141
- } catch {
142
- return undefined;
143
- }
144
- }
145
-
146
- /**
147
- * Fetches ONE subtree of a repository at one tag, and nothing else.
148
- *
149
- * A shallow, blobless, sparse checkout is what makes this cheap: git downloads
150
- * the commit at the tag, then only the blobs inside the recipe folder. Sous
151
- * never copies a whole repository in order to install one recipe.
152
- *
153
- * The temporary checkout is made beside the destination rather than in the
154
- * system temporary directory, so moving the subtree into place is a rename on
155
- * one filesystem; a cross-device move still works, it just copies.
156
- *
157
- * @param options - The clone URL, the tag, the subtree and where it should land.
158
- */
159
- export async function fetchSubtree(options: {
160
- /** The repository's HTTPS clone URL. */
161
- cloneUrl: string;
162
- /** The git tag to fetch. */
163
- tag: string;
164
- /** The subtree's path, relative to the repository root. */
165
- subPath: string;
166
- /** Where the subtree's contents should end up. */
167
- destDir: string;
168
- /** How subprocesses are run. Defaults to spawning a real process. */
169
- run?: CommandRunner;
170
- }): Promise<void> {
171
- const { cloneUrl, tag, subPath, destDir } = options;
172
- const parent = path.dirname(destDir);
173
- await fs.mkdir(parent, { recursive: true });
174
-
175
- const workDir = await fs.mkdtemp(path.join(parent, ".sous-fetch-"));
176
- const checkoutDir = path.join(workDir, "checkout");
177
-
178
- try {
179
- await runGit(
180
- [
181
- "clone",
182
- "--depth",
183
- "1",
184
- "--filter=blob:none",
185
- "--sparse",
186
- // The fetched subtree is hashed against its pin, so its bytes must be the
187
- // committed ones whatever this machine's line-ending settings are.
188
- ...LF_CHECKOUT_OPTIONS,
189
- "--branch",
190
- tag,
191
- // Everything after this is a path or a URL, never an option, whatever it
192
- // starts with. `repoUrlSchema` already refuses a leading hyphen; this is
193
- // the second lock on the same door, and it is what `git-clone.ts` does.
194
- "--",
195
- cloneUrl,
196
- checkoutDir,
197
- ],
198
- { run: options.run }
199
- );
200
- await runGit(["sparse-checkout", "set", subPath], {
201
- cwd: checkoutDir,
202
- run: options.run,
203
- });
204
-
205
- const source = path.join(checkoutDir, subPath);
206
- if (!(await isDirectory(source))) {
207
- throw new ConfigError(
208
- `The repository ${cloneUrl} has no folder '${subPath}' at the tag '${tag}'.\n` +
209
- ` The repository's index says the recipe lives there, so either the index is ` +
210
- `out of date or the tag points at the wrong commit.`
211
- );
212
- }
213
-
214
- await fs.rm(destDir, { recursive: true, force: true });
215
- await movePath(source, destDir);
216
- } finally {
217
- await fs.rm(workDir, { recursive: true, force: true });
218
- }
219
- }
220
-
221
- /** True when the path exists and is a directory. */
222
- async function isDirectory(candidate: string): Promise<boolean> {
223
- try {
224
- return (await fs.stat(candidate)).isDirectory();
225
- } catch {
226
- return false;
227
- }
228
- }
229
-
230
- /**
231
- * Moves a directory, falling back to a recursive copy when the source and the
232
- * destination live on different filesystems.
233
- *
234
- * @param source - The directory to move.
235
- * @param destination - Where it should end up.
236
- */
237
- async function movePath(source: string, destination: string): Promise<void> {
238
- try {
239
- await fs.rename(source, destination);
240
- } catch (error) {
241
- if ((error as NodeJS.ErrnoException).code !== "EXDEV") throw error;
242
- await fs.cp(source, destination, { recursive: true });
243
- await fs.rm(source, { recursive: true, force: true });
244
- }
245
- }