@sous-io/sous 0.2.29 → 0.2.31

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 (37) hide show
  1. package/README.md +12 -0
  2. package/docs/markdown/README.md +52 -0
  3. package/docs/markdown/_sidebar.md +2 -0
  4. package/docs/markdown/commands.md +31 -6
  5. package/docs/markdown/repositories-authoring.md +7 -3
  6. package/docs/markdown/repositories-consuming.md +26 -12
  7. package/docs/markdown/repositories-file-formats.md +35 -5
  8. package/docs/markdown/repositories-variables.md +4 -1
  9. package/package.json +2 -1
  10. package/recipes/core/sous-skills/skills/about-sous-repos/SKILL.tpl.md +7 -4
  11. package/recipes/core/sous-skills/sous.recipe.yaml +1 -1
  12. package/src/base-command.ts +75 -2
  13. package/src/commands/lock/rebuild.ts +5 -4
  14. package/src/commands/recipe/show.ts +163 -64
  15. package/src/commands/update.ts +178 -0
  16. package/src/lib/project-install.d.mts +4 -0
  17. package/src/lib/project-install.mjs +46 -8
  18. package/src/lib/repos/catalog.ts +43 -55
  19. package/src/lib/repos/declarations.ts +177 -0
  20. package/src/lib/repos/formats/common.ts +14 -0
  21. package/src/lib/repos/formats/index-file.ts +40 -1
  22. package/src/lib/repos/formats/lockfile.ts +5 -3
  23. package/src/lib/repos/formats/recipe-manifest.ts +6 -204
  24. package/src/lib/repos/formats/variable-definition.ts +268 -0
  25. package/src/lib/repos/providers/http.ts +37 -16
  26. package/src/lib/repos/release/index-builder.ts +168 -32
  27. package/src/lib/repos/resolver.ts +53 -12
  28. package/src/lib/repos/subscription-service.ts +93 -11
  29. package/src/lib/repos/update-plan.ts +3 -2
  30. package/src/lib/self-update/index.ts +14 -0
  31. package/src/lib/self-update/installs.ts +412 -0
  32. package/src/lib/self-update/managers.ts +126 -0
  33. package/src/lib/self-update/plan.ts +315 -0
  34. package/src/lib/self-update/registry.ts +99 -0
  35. package/src/lib/self-update/run.ts +506 -0
  36. package/src/lib/self-update/versions.ts +243 -0
  37. package/src/lib/vars/question-plan.ts +44 -21
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Which version of sous an update moves an install to.
3
+ *
4
+ * Everything here is pure: it reads the registry's answer (the abbreviated
5
+ * package document) and the version an install holds, and decides. Nothing is
6
+ * fetched or installed, so a dry run and a real run choose exactly alike.
7
+ */
8
+
9
+ import semver from "semver";
10
+ import { z } from "zod";
11
+ import { ConfigError } from "../errors.js";
12
+
13
+ /** What the registry publishes for the package, reduced to what a choice needs. */
14
+ export type RegistryMetadata = {
15
+ /** The package name the registry answered for. */
16
+ name: string;
17
+ /** Every published version that is valid semver, newest first. */
18
+ versions: string[];
19
+ /** Each dist-tag (`latest`, `next`) and the version it points at. */
20
+ distTags: Record<string, string>;
21
+ };
22
+
23
+ /**
24
+ * What the person asked for.
25
+ *
26
+ * - `default`: the newest version in the installed major.
27
+ * - `major`: the newest version, whatever its major.
28
+ * - `spec`: an exact version, a dist-tag name, or a semver range.
29
+ */
30
+ export type VersionRequest =
31
+ | { kind: "default" }
32
+ | { kind: "major" }
33
+ | { kind: "spec"; spec: string };
34
+
35
+ /** How the chosen version relates to the installed one. */
36
+ export type VersionDirection = "upgrade" | "downgrade" | "current";
37
+
38
+ /** The version an install moves to, and how the choice was read. */
39
+ export type VersionChoice = {
40
+ /** The version to install; equal to the installed version when `direction` is `current`. */
41
+ target: string;
42
+ direction: VersionDirection;
43
+ /** Whether prerelease versions counted as candidates, after the default was applied. */
44
+ prerelease: boolean;
45
+ /**
46
+ * How a `spec` request was read: an exact version, a dist-tag or a range.
47
+ * Undefined for the other requests.
48
+ */
49
+ specKind?: "version" | "dist-tag" | "range";
50
+ };
51
+
52
+ /** Input to `chooseVersion`. */
53
+ export type ChooseVersionInput = {
54
+ /** The version the install holds now. */
55
+ current: string;
56
+ /** Every published version (any order; invalid entries are ignored). */
57
+ versions: readonly string[];
58
+ /** The registry's dist-tags. */
59
+ distTags: Readonly<Record<string, string>>;
60
+ request: VersionRequest;
61
+ /**
62
+ * Whether prerelease versions count as candidates. Undefined means "on when
63
+ * the installed version is itself a prerelease".
64
+ */
65
+ prerelease?: boolean;
66
+ };
67
+
68
+ /** How many of the newest versions an error lists. */
69
+ const NEWEST_LISTED = 5;
70
+
71
+ /**
72
+ * The abbreviated package document, as far as sous reads it. Every other field
73
+ * the registry sends is kept and ignored, so a registry that adds one breaks
74
+ * nothing.
75
+ */
76
+ const abbreviatedDocumentSchema = z.looseObject({
77
+ name: z.string(),
78
+ "dist-tags": z.record(z.string(), z.string()).default({}),
79
+ versions: z.record(z.string(), z.unknown()),
80
+ });
81
+
82
+ /**
83
+ * Reads the registry's abbreviated package document (the answer to a request
84
+ * sent with `Accept: application/vnd.npm.install-v1+json`).
85
+ *
86
+ * Keys of `versions` that are not valid semver are dropped, and so is a
87
+ * dist-tag that points at one. A body that is not JSON, or lacks `name` or
88
+ * `versions`, is a ConfigError naming `source`.
89
+ *
90
+ * @param text - The response body.
91
+ * @param source - Where the body came from (the URL), named in errors.
92
+ */
93
+ export function parseRegistryMetadata(text: string, source: string): RegistryMetadata {
94
+ let raw: unknown;
95
+ try {
96
+ raw = JSON.parse(text);
97
+ } catch (error) {
98
+ throw new ConfigError(
99
+ `The registry's answer from ${source} is not JSON.\n ${(error as Error).message}`
100
+ );
101
+ }
102
+ const parsed = abbreviatedDocumentSchema.safeParse(raw);
103
+ if (!parsed.success) {
104
+ const problems = parsed.error.issues.map(
105
+ (issue) => ` ${issue.path.join(".") || "(top level)"}: ${issue.message}`
106
+ );
107
+ throw new ConfigError(
108
+ [`The registry's answer from ${source} is not a package document.`, ...problems].join("\n")
109
+ );
110
+ }
111
+
112
+ const versions = Object.keys(parsed.data.versions)
113
+ .filter((version) => semver.valid(version) === version)
114
+ .sort(semver.rcompare);
115
+ const distTags: Record<string, string> = {};
116
+ for (const [tag, version] of Object.entries(parsed.data["dist-tags"])) {
117
+ if (semver.valid(version) === version) distTags[tag] = version;
118
+ }
119
+ return { name: parsed.data.name, versions, distTags };
120
+ }
121
+
122
+ /**
123
+ * The range the default request looks in: at least the installed version and
124
+ * below the next major. For a 0.x install that is `<1.0.0`, deliberately NOT
125
+ * caret semantics (which would stop at the next minor): the default is "the
126
+ * newest published version in the current major" (sous-io/sous#135), and a
127
+ * 0.x install's major is 0.
128
+ *
129
+ * The upper bound is written `-0` so a prerelease of the next major
130
+ * (`1.0.0-rc.1`, which sorts below `1.0.0`) is outside it too.
131
+ *
132
+ * defaultRange("0.2.30") -> ">=0.2.30 <1.0.0-0"
133
+ */
134
+ export function defaultRange(current: string): string {
135
+ return `>=${current} <${semver.major(current) + 1}.0.0-0`;
136
+ }
137
+
138
+ /**
139
+ * Chooses the version an install moves to.
140
+ *
141
+ * - `default`: the newest candidate in `defaultRange(current)`; nothing newer
142
+ * means `current`. Never a downgrade.
143
+ * - `major`: the newest candidate overall; when that is older than the
144
+ * installed version, `current`. Never a downgrade.
145
+ * - `spec`: an exact version (a leading `v` is accepted) that must be
146
+ * published, a dist-tag's version, or the newest candidate a range admits.
147
+ * A spec may downgrade, and `direction` then says so.
148
+ *
149
+ * A candidate is a published version that is not a prerelease, or any
150
+ * published version when prereleases are on. An exact version and a dist-tag
151
+ * are taken as named, whatever the prerelease setting.
152
+ *
153
+ * @throws ConfigError when `current` is not a version, or a spec matches
154
+ * nothing; the message lists the dist-tags and the newest versions.
155
+ */
156
+ export function chooseVersion(input: ChooseVersionInput): VersionChoice {
157
+ const current = semver.valid(input.current);
158
+ if (current === null) {
159
+ throw new ConfigError(
160
+ `The installed version "${input.current}" is not a version sous can compare with the published ones.`
161
+ );
162
+ }
163
+ const prerelease = input.prerelease ?? semver.prerelease(current) !== null;
164
+ const published = input.versions.filter((version) => semver.valid(version) === version);
165
+ const candidates = prerelease
166
+ ? published
167
+ : published.filter((version) => semver.prerelease(version) === null);
168
+ const range = { includePrerelease: true };
169
+
170
+ const settle = (target: string | null, specKind?: VersionChoice["specKind"]): VersionChoice => {
171
+ const choice: VersionChoice = {
172
+ target: target ?? current,
173
+ direction: directionOf(target ?? current, current),
174
+ prerelease,
175
+ };
176
+ if (specKind !== undefined) choice.specKind = specKind;
177
+ return choice;
178
+ };
179
+ const noDowngrade = (target: string | null): string | null =>
180
+ target !== null && semver.gt(target, current) ? target : null;
181
+
182
+ const { request } = input;
183
+ if (request.kind === "default") {
184
+ return settle(noDowngrade(semver.maxSatisfying(candidates, defaultRange(current), range)));
185
+ }
186
+ if (request.kind === "major") {
187
+ return settle(noDowngrade(semver.maxSatisfying(candidates, "*", range)));
188
+ }
189
+
190
+ const spec = request.spec.trim();
191
+ const exact = semver.valid(spec);
192
+ if (exact !== null) {
193
+ if (!published.includes(exact)) throw noMatch(spec, "is not a published version", input);
194
+ return settle(exact, "version");
195
+ }
196
+ const tagged = input.distTags[spec];
197
+ if (tagged !== undefined) return settle(tagged, "dist-tag");
198
+ if (semver.validRange(spec) !== null) {
199
+ const newest = semver.maxSatisfying(candidates, spec, range);
200
+ if (newest === null) {
201
+ throw noMatch(
202
+ spec,
203
+ prerelease
204
+ ? "matches no published version"
205
+ : "matches no published version that is not a prerelease",
206
+ input
207
+ );
208
+ }
209
+ return settle(newest, "range");
210
+ }
211
+ throw noMatch(spec, "is not a version, a range or a dist-tag", input);
212
+ }
213
+
214
+ /** How `target` relates to `current`. */
215
+ function directionOf(target: string, current: string): VersionDirection {
216
+ const order = semver.compare(target, current);
217
+ if (order > 0) return "upgrade";
218
+ if (order < 0) return "downgrade";
219
+ return "current";
220
+ }
221
+
222
+ /**
223
+ * The error for a spec that names nothing: what was asked, why it matched
224
+ * nothing, and what the registry does publish.
225
+ */
226
+ function noMatch(spec: string, why: string, input: ChooseVersionInput): ConfigError {
227
+ const lines = [`The requested version "${spec}" ${why}.`];
228
+ const tags = Object.entries(input.distTags).sort(([a], [b]) => a.localeCompare(b));
229
+ if (tags.length > 0) {
230
+ lines.push(" The dist-tags point at:");
231
+ for (const [tag, version] of tags) lines.push(` ${tag}: ${version}`);
232
+ }
233
+ const newest = input.versions
234
+ .filter((version) => semver.valid(version) === version)
235
+ .sort(semver.rcompare)
236
+ .slice(0, NEWEST_LISTED);
237
+ if (newest.length > 0) {
238
+ lines.push(` The newest published versions are ${newest.join(", ")}.`);
239
+ } else {
240
+ lines.push(" The registry lists no published versions.");
241
+ }
242
+ return new ConfigError(lines.join("\n"));
243
+ }
@@ -93,11 +93,19 @@ function questionCount(count: number): string {
93
93
  return count === 1 ? "1 question" : `${count} questions`;
94
94
  }
95
95
 
96
- /** The labeled facts one planned question shows. */
97
- export function plannedVariableFacts(planned: PlannedVariable): LabeledFact[] {
96
+ /**
97
+ * The labeled facts one planned question shows.
98
+ *
99
+ * @param planned - The planned question.
100
+ * @param answerHints - Whether to name the `--answer` flag that answers it ahead of time.
101
+ */
102
+ export function plannedVariableFacts(
103
+ planned: PlannedVariable,
104
+ answerHints = true
105
+ ): LabeledFact[] {
98
106
  const { definition } = planned.defined;
99
107
 
100
- return [
108
+ const facts: LabeledFact[] = [
101
109
  { label: "about", lines: [firstSentence(definition.description)] },
102
110
  { label: "example", lines: [String(definition.example)] },
103
111
  { label: "stored-as", lines: [planned.storedAs] },
@@ -112,8 +120,11 @@ export function plannedVariableFacts(planned: PlannedVariable): LabeledFact[] {
112
120
  : "no, and an answer is optional",
113
121
  ],
114
122
  },
115
- { label: "answer-with", lines: [`--answer ${definition.name}=<value>`] },
116
123
  ];
124
+ if (answerHints) {
125
+ facts.push({ label: "answer-with", lines: [`--answer ${definition.name}=<value>`] });
126
+ }
127
+ return facts;
117
128
  }
118
129
 
119
130
  /** What `formatQuestionPlan` needs beyond the questions themselves. */
@@ -121,26 +132,37 @@ export interface QuestionPlanFormatOptions {
121
132
  /** The column to wrap descriptions at. */
122
133
  width?: number;
123
134
  /**
124
- * Recipes in the closure whose files are not on this machine, so their
125
- * manifests could not be read. They are named in the plan rather than
126
- * silently left out of it.
135
+ * Recipes in the closure whose questions are unknown: their files are not on
136
+ * this machine, and their repository's index does not record their
137
+ * questions. They are named in the plan rather than silently left out of it.
127
138
  */
128
139
  unreadable?: string[];
140
+ /**
141
+ * Whether the plan says how to answer its questions ahead of time: the
142
+ * `--answer` flag under each question, and a closing sentence. True for the
143
+ * dry run of a command that asks them; false where the plan only describes.
144
+ */
145
+ answerHints?: boolean;
129
146
  }
130
147
 
131
148
  /**
132
- * The one line that names the recipes a dry run could not read. A dry run
133
- * downloads nothing, so a recipe this machine does not hold yet has no manifest
134
- * to read; saying so by name is more useful than leaving it out of the plan.
149
+ * The one line that names the recipes whose questions are unknown. Nothing is
150
+ * downloaded to plan, so a recipe this machine does not hold yet is described
151
+ * from its repository's index, and an index written before sous recorded
152
+ * questions there cannot say; naming those recipes is more useful than leaving
153
+ * them out of the plan.
135
154
  *
136
- * @param unreadable - The recipe keys whose manifests could not be read.
155
+ * @param unreadable - The recipe keys whose questions are unknown.
156
+ * @param answerHints - Whether the plan belongs to a dry run that could be run for real.
137
157
  */
138
- function unreadableLine(unreadable: string[]): string {
139
- return (
140
- `Not on this machine yet, so their questions cannot be listed: ` +
141
- `${unreadable.join(", ")}. A dry run downloads nothing; run this command ` +
142
- `again without '--dry-run' to install them and be asked.`
143
- );
158
+ function unreadableLine(unreadable: string[], answerHints: boolean): string {
159
+ const line =
160
+ `Not on this machine yet, and their repository's index does not record their ` +
161
+ `questions, so they cannot be listed: ${unreadable.join(", ")}.`;
162
+ return answerHints
163
+ ? `${line} A dry run downloads nothing; run this command again without ` +
164
+ `'--dry-run' to install them and be asked.`
165
+ : line;
144
166
  }
145
167
 
146
168
  /**
@@ -157,6 +179,7 @@ export function formatQuestionPlan(
157
179
  options: QuestionPlanFormatOptions = {}
158
180
  ): string[] {
159
181
  const unreadable = options.unreadable ?? [];
182
+ const answerHints = options.answerHints ?? true;
160
183
 
161
184
  const columns = options.width ?? wrapColumns();
162
185
  /** Wraps one sentence to the width the caller's indentation leaves for it. */
@@ -166,7 +189,7 @@ export function formatQuestionPlan(
166
189
  if (planned.length === 0) {
167
190
  return unreadable.length === 0
168
191
  ? sentence("None of these recipes ask any questions, so nothing needs answering.")
169
- : sentence(unreadableLine(unreadable), palette.note);
192
+ : sentence(unreadableLine(unreadable, answerHints), palette.note);
170
193
  }
171
194
 
172
195
  const unanswered = planned.filter((entry) => !entry.answered).length;
@@ -194,15 +217,15 @@ export function formatQuestionPlan(
194
217
  for (const entry of entries) {
195
218
  lines.push("", ` ${color.cyan(entry.defined.definition.name)}`);
196
219
  // The facts block indents itself, so the plan adds nothing on top of it.
197
- lines.push(...renderFacts(plannedVariableFacts(entry), columns - 2));
220
+ lines.push(...renderFacts(plannedVariableFacts(entry, answerHints), columns - 2));
198
221
  }
199
222
  }
200
223
 
201
224
  if (unreadable.length > 0) {
202
- lines.push("", ...sentence(unreadableLine(unreadable), palette.note));
225
+ lines.push("", ...sentence(unreadableLine(unreadable, answerHints), palette.note));
203
226
  }
204
227
 
205
- if (unanswered > 0) {
228
+ if (unanswered > 0 && answerHints) {
206
229
  lines.push(
207
230
  "",
208
231
  ...sentence(