@sous-io/sous 0.2.36 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -256
- package/dist/bin.js +11 -0
- package/dist/sous.js +1093 -0
- package/docs/markdown/README.md +5 -4
- package/docs/markdown/_sidebar.md +21 -1
- package/docs/markdown/commands.md +294 -431
- package/docs/markdown/config-discovery.md +25 -14
- package/docs/markdown/config-inspection.md +53 -31
- package/docs/markdown/config-layers.md +168 -66
- package/docs/markdown/config-variables.md +44 -11
- package/docs/markdown/configuration.md +104 -17
- package/docs/markdown/design-principles.md +4 -1
- package/docs/markdown/repositories-authoring.md +30 -0
- package/docs/markdown/repositories-consuming.md +72 -14
- package/docs/markdown/repositories-file-formats.md +31 -13
- package/docs/markdown/repositories-providers.md +48 -57
- package/docs/markdown/repositories-quickstart.md +3 -2
- package/docs/markdown/repositories-troubleshooting.md +27 -1
- package/docs/markdown/repositories-variables.md +6 -1
- package/docs/markdown/repositories.md +18 -8
- package/package.json +48 -107
- package/recipes/core/sous-skills/skills/about-liquid-templates/SKILL.tpl.md +4 -6
- package/recipes/core/sous-skills/skills/about-liquid-templates/references/liquid-filters.tpl.md +2 -0
- package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +10 -9
- package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +35 -14
- package/recipes/core/sous-skills/skills/about-sous-recipes/SKILL.tpl.md +51 -20
- package/recipes/core/sous-skills/sous.recipe.yaml +1 -1
- package/sous.config.schema.json +351 -132
- package/bin/run.js +0 -51
- package/src/base-command.ts +0 -457
- package/src/commands/build.ts +0 -181
- package/src/commands/clear.ts +0 -79
- package/src/commands/compile.ts +0 -155
- package/src/commands/config/get.ts +0 -110
- package/src/commands/config/show.ts +0 -32
- package/src/commands/config/validate.ts +0 -53
- package/src/commands/help.ts +0 -46
- package/src/commands/init.ts +0 -289
- package/src/commands/launch.ts +0 -139
- package/src/commands/lock/rebuild.ts +0 -239
- package/src/commands/lock/show.ts +0 -115
- package/src/commands/namespace/list.ts +0 -138
- package/src/commands/namespace/show.ts +0 -130
- package/src/commands/prune.ts +0 -43
- package/src/commands/recipe/list.ts +0 -112
- package/src/commands/recipe/show.ts +0 -439
- package/src/commands/repo/add.ts +0 -145
- package/src/commands/repo/contribute.ts +0 -604
- package/src/commands/repo/gc.ts +0 -173
- package/src/commands/repo/init.ts +0 -136
- package/src/commands/repo/link.ts +0 -899
- package/src/commands/repo/list.ts +0 -236
- package/src/commands/repo/release.ts +0 -685
- package/src/commands/repo/remove.ts +0 -194
- package/src/commands/repo/search.ts +0 -243
- package/src/commands/repo/submit.ts +0 -341
- package/src/commands/repo/unlink.ts +0 -461
- package/src/commands/subscription/add.ts +0 -286
- package/src/commands/subscription/list.ts +0 -208
- package/src/commands/subscription/remove.ts +0 -182
- package/src/commands/subscription/update.ts +0 -216
- package/src/commands/update.ts +0 -178
- package/src/commands/vars/ask.ts +0 -417
- package/src/commands/vars/index.ts +0 -79
- package/src/commands/vars/list.ts +0 -67
- package/src/commands/vars/show.ts +0 -77
- package/src/config-command.ts +0 -30
- package/src/lib/build-preparation.ts +0 -128
- package/src/lib/build-service.ts +0 -580
- package/src/lib/config-discovery.ts +0 -378
- package/src/lib/config-inspect.ts +0 -145
- package/src/lib/config-kernel.mjs +0 -379
- package/src/lib/config-schema.ts +0 -401
- package/src/lib/env-file.ts +0 -328
- package/src/lib/env-local.ts +0 -212
- package/src/lib/errors.ts +0 -32
- package/src/lib/glob-pattern.ts +0 -24
- package/src/lib/include-resolver.ts +0 -523
- package/src/lib/interactive.ts +0 -165
- package/src/lib/markdown-compiler.ts +0 -947
- package/src/lib/package-info.ts +0 -25
- package/src/lib/pid-service.ts +0 -99
- package/src/lib/project-install.d.mts +0 -44
- package/src/lib/project-install.mjs +0 -234
- package/src/lib/project-scaffold/index.ts +0 -332
- package/src/lib/project-scaffold/templates.ts +0 -240
- package/src/lib/repos/catalog-display.ts +0 -215
- package/src/lib/repos/catalog-inputs.ts +0 -288
- package/src/lib/repos/catalog.ts +0 -842
- package/src/lib/repos/contribute.ts +0 -452
- package/src/lib/repos/core-recipe.ts +0 -105
- package/src/lib/repos/declarations.ts +0 -187
- package/src/lib/repos/defaults.ts +0 -175
- package/src/lib/repos/formats/common.ts +0 -440
- package/src/lib/repos/formats/index-file.ts +0 -253
- package/src/lib/repos/formats/links-map.ts +0 -98
- package/src/lib/repos/formats/lockfile.ts +0 -169
- package/src/lib/repos/formats/patterns.ts +0 -57
- package/src/lib/repos/formats/recipe-manifest.ts +0 -205
- package/src/lib/repos/formats/repo-manifest.ts +0 -97
- package/src/lib/repos/formats/store-entry.ts +0 -84
- package/src/lib/repos/formats/variable-definition.ts +0 -268
- package/src/lib/repos/freshness.ts +0 -264
- package/src/lib/repos/git-clone.ts +0 -850
- package/src/lib/repos/identity.ts +0 -89
- package/src/lib/repos/index.ts +0 -57
- package/src/lib/repos/links.ts +0 -356
- package/src/lib/repos/load-manifest.ts +0 -237
- package/src/lib/repos/lock-service.ts +0 -454
- package/src/lib/repos/locked-namespace-resolver.ts +0 -232
- package/src/lib/repos/locked-recipe-lookup.ts +0 -123
- package/src/lib/repos/locked-recipes.ts +0 -254
- package/src/lib/repos/managed-layer.ts +0 -422
- package/src/lib/repos/memories-hash-name.ts +0 -33
- package/src/lib/repos/namespace-resolver.ts +0 -527
- package/src/lib/repos/providers/base.ts +0 -268
- package/src/lib/repos/providers/git.ts +0 -237
- package/src/lib/repos/providers/github.ts +0 -589
- package/src/lib/repos/providers/gitlab.ts +0 -332
- package/src/lib/repos/providers/http.ts +0 -128
- package/src/lib/repos/providers/index-cache.ts +0 -421
- package/src/lib/repos/providers/index.ts +0 -106
- package/src/lib/repos/providers/local.ts +0 -391
- package/src/lib/repos/providers/provider.ts +0 -615
- package/src/lib/repos/recipe-config-layers.ts +0 -316
- package/src/lib/repos/recipe-key-matcher.ts +0 -62
- package/src/lib/repos/recipe-memories.ts +0 -178
- package/src/lib/repos/recipe-targets.ts +0 -236
- package/src/lib/repos/ref-search.ts +0 -46
- package/src/lib/repos/release/bump.ts +0 -161
- package/src/lib/repos/release/changelog.ts +0 -461
- package/src/lib/repos/release/git-state.ts +0 -460
- package/src/lib/repos/release/index-builder.ts +0 -1108
- package/src/lib/repos/release/index.ts +0 -19
- package/src/lib/repos/release/plan.ts +0 -521
- package/src/lib/repos/release/settle.ts +0 -154
- package/src/lib/repos/release/submissions.ts +0 -214
- package/src/lib/repos/release/submit-checkout.ts +0 -290
- package/src/lib/repos/release/submit-questions.ts +0 -153
- package/src/lib/repos/release/submit-service.ts +0 -898
- package/src/lib/repos/release/tags.ts +0 -239
- package/src/lib/repos/release/validate.ts +0 -463
- package/src/lib/repos/resolver.ts +0 -1028
- package/src/lib/repos/scaffold/index.ts +0 -245
- package/src/lib/repos/scaffold/templates.ts +0 -517
- package/src/lib/repos/seed.ts +0 -473
- package/src/lib/repos/store/contract.ts +0 -64
- package/src/lib/repos/store/hash.ts +0 -191
- package/src/lib/repos/store/recipe-store.ts +0 -599
- package/src/lib/repos/store/settings.ts +0 -58
- package/src/lib/repos/subscription-service.ts +0 -3859
- package/src/lib/repos/trust.ts +0 -447
- package/src/lib/repos/update-plan.ts +0 -235
- package/src/lib/self-update/index.ts +0 -14
- package/src/lib/self-update/installs.ts +0 -412
- package/src/lib/self-update/managers.ts +0 -126
- package/src/lib/self-update/plan.ts +0 -315
- package/src/lib/self-update/registry.ts +0 -99
- package/src/lib/self-update/run.ts +0 -506
- package/src/lib/self-update/versions.ts +0 -243
- package/src/lib/settings.ts +0 -1105
- package/src/lib/sous-home.ts +0 -104
- package/src/lib/state.ts +0 -167
- package/src/lib/vars/answers.ts +0 -192
- package/src/lib/vars/ask.ts +0 -1152
- package/src/lib/vars/definition-source.ts +0 -261
- package/src/lib/vars/display.ts +0 -231
- package/src/lib/vars/index.ts +0 -20
- package/src/lib/vars/ladder.ts +0 -282
- package/src/lib/vars/mappings.ts +0 -260
- package/src/lib/vars/names.ts +0 -94
- package/src/lib/vars/preanswers.ts +0 -395
- package/src/lib/vars/question-plan.ts +0 -241
- package/src/lib/vars/report.ts +0 -228
- package/src/lib/vars/safe-regex.ts +0 -235
- package/src/lib/vars/secrets.ts +0 -131
- package/src/lib/vars/validate.ts +0 -312
- package/src/lib/version-report.ts +0 -90
- package/src/lib/watch-loop.ts +0 -148
- package/src/lib/watch-service.ts +0 -116
- package/src/services/ref-resolver/container.ts +0 -107
- package/src/services/ref-resolver/dependency.ts +0 -60
- package/src/services/ref-resolver/format.ts +0 -197
- package/src/services/ref-resolver/glob.ts +0 -115
- package/src/services/ref-resolver/hash-names.ts +0 -155
- package/src/services/ref-resolver/index.ts +0 -55
- package/src/services/ref-resolver/injectable.ts +0 -36
- package/src/services/ref-resolver/location.ts +0 -62
- package/src/services/ref-resolver/lookups/cached-index-lookup.ts +0 -53
- package/src/services/ref-resolver/lookups/catalog-lookup.ts +0 -23
- package/src/services/ref-resolver/lookups/catalog-matcher.ts +0 -261
- package/src/services/ref-resolver/lookups/chained-lookup.ts +0 -27
- package/src/services/ref-resolver/lookups/env-var-lookup.ts +0 -65
- package/src/services/ref-resolver/lookups/fetched-index-lookup.ts +0 -150
- package/src/services/ref-resolver/lookups/recorded-dependency-lookup.ts +0 -62
- package/src/services/ref-resolver/lookups/ref-lookup.ts +0 -54
- package/src/services/ref-resolver/lookups/variable-lookup.ts +0 -123
- package/src/services/ref-resolver/named-refs.ts +0 -144
- package/src/services/ref-resolver/parser/env-var-splitter.ts +0 -27
- package/src/services/ref-resolver/parser/location-splitter.ts +0 -239
- package/src/services/ref-resolver/parser/name-path-splitter.ts +0 -200
- package/src/services/ref-resolver/parser/partial-ref.ts +0 -84
- package/src/services/ref-resolver/parser/query-splitter.ts +0 -61
- package/src/services/ref-resolver/parser/range-splitter.ts +0 -52
- package/src/services/ref-resolver/parser/ref-parser.ts +0 -115
- package/src/services/ref-resolver/parser/repo-qualifier-splitter.ts +0 -51
- package/src/services/ref-resolver/parts.ts +0 -115
- package/src/services/ref-resolver/pruners/command-line-pruner.ts +0 -47
- package/src/services/ref-resolver/pruners/include-pruner.ts +0 -66
- package/src/services/ref-resolver/pruners/manifest-pruner.ts +0 -130
- package/src/services/ref-resolver/pruners/ref-pruner.ts +0 -135
- package/src/services/ref-resolver/pruners/rule-helpers.ts +0 -44
- package/src/services/ref-resolver/pruners/stored-key-pruner.ts +0 -136
- package/src/services/ref-resolver/ref-helpers.ts +0 -62
- package/src/services/ref-resolver/ref-picker-service.ts +0 -204
- package/src/services/ref-resolver/ref-report.ts +0 -84
- package/src/services/ref-resolver/ref-resolve-arguments.ts +0 -47
- package/src/services/ref-resolver/ref-resolve-result.ts +0 -36
- package/src/services/ref-resolver/ref-resolver-service.ts +0 -323
- package/src/services/ref-resolver/source.ts +0 -48
- package/src/services/ref-resolver/tokens.ts +0 -25
- package/src/services/ref-resolver/types.ts +0 -155
- package/src/templating/filters/bullet-list.ts +0 -9
- package/src/templating/filters/index.ts +0 -8
- package/src/templating/init-liquid-engine.ts +0 -139
- package/src/templating/lib/glob-files.ts +0 -74
- package/src/templating/lib/import-export.ts +0 -32
- package/src/templating/lib/secret-scope.ts +0 -41
- package/src/templating/lib/tag-args.ts +0 -19
- package/src/templating/tags/exportScalarVarsJs.ts +0 -46
- package/src/templating/tags/getFiles.ts +0 -89
- package/src/templating/tags/index.ts +0 -14
- package/src/templating/tags/listFiles.ts +0 -54
- package/src/templating/tags/showVars.ts +0 -28
- package/src/utils/choice-prompt.ts +0 -143
- package/src/utils/command-errors.ts +0 -186
- package/src/utils/command-help.ts +0 -45
- package/src/utils/confirm-prompt.ts +0 -110
- package/src/utils/flags.ts +0 -177
- package/src/utils/formatting.ts +0 -849
- package/src/utils/prompts.ts +0 -53
- package/src/utils/sous-directory.ts +0 -245
- package/src/utils/table.ts +0 -603
- package/src/utils/value-prompt.ts +0 -119
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reading Repositories files off disk.
|
|
3
|
-
*
|
|
4
|
-
* Manifests are HAND-WRITTEN, and deliberately never JavaScript: a manifest is
|
|
5
|
-
* data, so sous can read, validate and browse it without executing anything,
|
|
6
|
-
* including before the repository is trusted. So a manifest is YAML (`.yaml`, `.yml`) or
|
|
7
|
-
* JSON (`.json`, `.jsonc`), and the JSON dialect is permissive, allowing line
|
|
8
|
-
* comments, block comments and trailing commas, so a manifest can explain
|
|
9
|
-
* itself.
|
|
10
|
-
*
|
|
11
|
-
* Machine-written files (the index, the lockfile, store entry markers, the
|
|
12
|
-
* links map) are strict JSON; nothing writes a comment into them, so nothing
|
|
13
|
-
* needs to tolerate one.
|
|
14
|
-
*
|
|
15
|
-
* Every function here returns raw, unvalidated data. Pass the result to the
|
|
16
|
-
* matching `parseX` helper in `formats/` to validate it.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import fs from "node:fs";
|
|
20
|
-
import path from "node:path";
|
|
21
|
-
import YAML from "yaml";
|
|
22
|
-
import { parse as parseJsonc, printParseErrorCode, type ParseError } from "jsonc-parser";
|
|
23
|
-
import { ConfigError } from "../errors.js";
|
|
24
|
-
import {
|
|
25
|
-
MANIFEST_EXTENSIONS,
|
|
26
|
-
RECIPE_MANIFEST_BASENAME,
|
|
27
|
-
REPO_MANIFEST_BASENAME,
|
|
28
|
-
} from "./formats/common.js";
|
|
29
|
-
|
|
30
|
-
/** Reads a file as UTF-8, raising a ConfigError naming it when that fails. */
|
|
31
|
-
function readFileText(filePath: string, label: string): string {
|
|
32
|
-
try {
|
|
33
|
-
return fs.readFileSync(filePath, "utf8");
|
|
34
|
-
} catch (error) {
|
|
35
|
-
const reason = (error as NodeJS.ErrnoException).code === "ENOENT" ? "does not exist" : "could not be read";
|
|
36
|
-
throw new ConfigError(
|
|
37
|
-
`The ${label} at ${filePath} ${reason}.\n` +
|
|
38
|
-
` ${(error as Error).message}`
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Turns a jsonc-parser error offset into a `line N, column N` string. */
|
|
44
|
-
function describeOffset(text: string, offset: number): string {
|
|
45
|
-
const before = text.slice(0, offset);
|
|
46
|
-
const line = before.split("\n").length;
|
|
47
|
-
const column = offset - before.lastIndexOf("\n");
|
|
48
|
-
return `line ${line}, column ${column}`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Parses permissive JSON: standard JSON plus line comments, block comments and
|
|
53
|
-
* trailing commas. Used for hand-written `.json` and `.jsonc` files, and for
|
|
54
|
-
* `.jsonc` config layers.
|
|
55
|
-
*
|
|
56
|
-
* @param text - The file's contents.
|
|
57
|
-
* @param sourceLabel - The file path, named in error messages.
|
|
58
|
-
*/
|
|
59
|
-
export function parseJsoncText(text: string, sourceLabel: string): unknown {
|
|
60
|
-
const errors: ParseError[] = [];
|
|
61
|
-
const value = parseJsonc(text, errors, {
|
|
62
|
-
allowTrailingComma: true,
|
|
63
|
-
disallowComments: false,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
if (errors.length > 0) {
|
|
67
|
-
const first = errors[0]!;
|
|
68
|
-
throw new ConfigError(
|
|
69
|
-
`Could not parse ${sourceLabel} as JSON:\n` +
|
|
70
|
-
` ${printParseErrorCode(first.error)} at ${describeOffset(text, first.offset)}.\n` +
|
|
71
|
-
` Comments and trailing commas are allowed; anything else must be valid JSON.`
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return value;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Parses YAML, raising a ConfigError that carries the parser's own message.
|
|
80
|
-
*
|
|
81
|
-
* @param text - The file's contents.
|
|
82
|
-
* @param sourceLabel - The file path, named in error messages.
|
|
83
|
-
*/
|
|
84
|
-
export function parseYamlText(text: string, sourceLabel: string): unknown {
|
|
85
|
-
try {
|
|
86
|
-
return YAML.parse(text);
|
|
87
|
-
} catch (error) {
|
|
88
|
-
throw new ConfigError(
|
|
89
|
-
`Could not parse ${sourceLabel} as YAML:\n ${(error as Error).message}`
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Loads a hand-written manifest, picking the parser by extension: `.yaml` and
|
|
96
|
-
* `.yml` are YAML, `.json` and `.jsonc` are permissive JSON. Returns raw,
|
|
97
|
-
* unvalidated data.
|
|
98
|
-
*
|
|
99
|
-
* @param filePath - Absolute path to the manifest file.
|
|
100
|
-
*/
|
|
101
|
-
export function loadManifestFile(filePath: string): unknown {
|
|
102
|
-
const extension = path.extname(filePath).toLowerCase();
|
|
103
|
-
const text = readFileText(filePath, "manifest");
|
|
104
|
-
|
|
105
|
-
if (extension === ".yaml" || extension === ".yml") {
|
|
106
|
-
return parseYamlText(text, filePath);
|
|
107
|
-
}
|
|
108
|
-
if (extension === ".json" || extension === ".jsonc") {
|
|
109
|
-
return parseJsoncText(text, filePath);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
throw new ConfigError(
|
|
113
|
-
`Cannot read the manifest at ${filePath}: '${extension}' is not a manifest format.\n` +
|
|
114
|
-
` A manifest is written as ${MANIFEST_EXTENSIONS.join(", ")}. Manifests are never ` +
|
|
115
|
-
`JavaScript, because a manifest is data that sous reads, validates and browses ` +
|
|
116
|
-
`without executing anything, including before the repository is trusted.`
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Loads a machine-written JSON file (the index, a lockfile, a store entry
|
|
122
|
-
* marker, a links map) with strict JSON parsing. Returns raw, unvalidated data.
|
|
123
|
-
*
|
|
124
|
-
* @param filePath - Absolute path to the file.
|
|
125
|
-
* @param label - Plain-language name of the file, used in error messages.
|
|
126
|
-
*/
|
|
127
|
-
export function loadJsonFile(filePath: string, label: string): unknown {
|
|
128
|
-
const text = readFileText(filePath, label);
|
|
129
|
-
try {
|
|
130
|
-
return JSON.parse(text);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
throw new ConfigError(
|
|
133
|
-
`Could not parse the ${label} at ${filePath} as JSON:\n ${(error as Error).message}\n` +
|
|
134
|
-
` This file is written by sous; if it has been edited by hand, restoring it from ` +
|
|
135
|
-
`version control is usually the quickest fix.`
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** True when the path exists and is a regular file. */
|
|
141
|
-
function isFile(candidate: string): boolean {
|
|
142
|
-
try {
|
|
143
|
-
return fs.statSync(candidate).isFile();
|
|
144
|
-
} catch {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Finds the one manifest with the given base name in a directory, trying each
|
|
151
|
-
* supported extension. Returns undefined when there is none.
|
|
152
|
-
*
|
|
153
|
-
* Two manifests in one directory is a hard error rather than a first-match-win,
|
|
154
|
-
* mirroring how sous treats two primary configs in one `.sous/` directory: a
|
|
155
|
-
* silent winner would make the repo's behavior depend on an implementation
|
|
156
|
-
* detail.
|
|
157
|
-
*
|
|
158
|
-
* @param directory - The directory to look in.
|
|
159
|
-
* @param baseName - The manifest base name, without an extension.
|
|
160
|
-
* @param label - Plain-language name of the manifest, used in error messages.
|
|
161
|
-
*/
|
|
162
|
-
export function findManifest(
|
|
163
|
-
directory: string,
|
|
164
|
-
baseName: string,
|
|
165
|
-
label: string
|
|
166
|
-
): string | undefined {
|
|
167
|
-
const found: string[] = [];
|
|
168
|
-
for (const extension of MANIFEST_EXTENSIONS) {
|
|
169
|
-
const candidate = path.join(directory, `${baseName}${extension}`);
|
|
170
|
-
if (isFile(candidate)) found.push(candidate);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (found.length > 1) {
|
|
174
|
-
const names = found.map((entry) => path.basename(entry)).join(", ");
|
|
175
|
-
throw new ConfigError(
|
|
176
|
-
`Found more than one ${label} in ${directory}: ${names}.\n` +
|
|
177
|
-
` A directory holds exactly one ${label}. Remove the copies you do not want, ` +
|
|
178
|
-
`so it is never ambiguous which one sous reads.`
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return found[0];
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Finds the repo manifest at the root of a repository.
|
|
187
|
-
*
|
|
188
|
-
* @param repoRoot - The repository's root directory.
|
|
189
|
-
*/
|
|
190
|
-
export function findRepoManifest(repoRoot: string): string | undefined {
|
|
191
|
-
return findManifest(repoRoot, REPO_MANIFEST_BASENAME, "repo manifest");
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Finds the recipe manifest in a recipe folder.
|
|
196
|
-
*
|
|
197
|
-
* @param recipeDir - The recipe's directory.
|
|
198
|
-
*/
|
|
199
|
-
export function findRecipeManifest(recipeDir: string): string | undefined {
|
|
200
|
-
return findManifest(recipeDir, RECIPE_MANIFEST_BASENAME, "recipe manifest");
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Finds the repo manifest and raises a ConfigError naming the directory when
|
|
205
|
-
* there is none, for callers that require one.
|
|
206
|
-
*
|
|
207
|
-
* @param repoRoot - The repository's root directory.
|
|
208
|
-
*/
|
|
209
|
-
export function requireRepoManifest(repoRoot: string): string {
|
|
210
|
-
const found = findRepoManifest(repoRoot);
|
|
211
|
-
if (found === undefined) {
|
|
212
|
-
throw new ConfigError(
|
|
213
|
-
`No repo manifest in ${repoRoot}.\n` +
|
|
214
|
-
` A sous repository declares itself with a '${REPO_MANIFEST_BASENAME}${MANIFEST_EXTENSIONS[0]}' ` +
|
|
215
|
-
`file at its root.`
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
return found;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Finds the recipe manifest and raises a ConfigError naming the directory when
|
|
223
|
-
* there is none, for callers that require one.
|
|
224
|
-
*
|
|
225
|
-
* @param recipeDir - The recipe's directory.
|
|
226
|
-
*/
|
|
227
|
-
export function requireRecipeManifest(recipeDir: string): string {
|
|
228
|
-
const found = findRecipeManifest(recipeDir);
|
|
229
|
-
if (found === undefined) {
|
|
230
|
-
throw new ConfigError(
|
|
231
|
-
`No recipe manifest in ${recipeDir}.\n` +
|
|
232
|
-
` Every directory listed under 'recipes' in a repo manifest holds a ` +
|
|
233
|
-
`'${RECIPE_MANIFEST_BASENAME}${MANIFEST_EXTENSIONS[0]}' file.`
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
return found;
|
|
237
|
-
}
|
|
@@ -1,454 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The lockfile service.
|
|
3
|
-
*
|
|
4
|
-
* `.sous/sous.lock.json` records the exact version and content hash of
|
|
5
|
-
* everything a project uses, so a fresh clone restores to the same bytes with
|
|
6
|
-
* no prompts and no version drift. Together with repo trust it is the supply
|
|
7
|
-
* chain defense: nothing new enters a project except through an explicit,
|
|
8
|
-
* visible change to these files.
|
|
9
|
-
*
|
|
10
|
-
* Two behaviors are worth stating outright. Removal is REFCOUNTED: every entry
|
|
11
|
-
* lists who holds it, and an entry goes only when its last holder does, so
|
|
12
|
-
* unsubscribing from one recipe never quietly removes something another recipe
|
|
13
|
-
* still needs. And RESTORE never decides anything: it fetches exactly what the
|
|
14
|
-
* lockfile pins, never a newer version, and never asks a question.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import fs from "node:fs";
|
|
18
|
-
import fsp from "node:fs/promises";
|
|
19
|
-
import path from "node:path";
|
|
20
|
-
import { ConfigError } from "../errors.js";
|
|
21
|
-
import {
|
|
22
|
-
LOCKFILE_FILENAME,
|
|
23
|
-
stableJsonStringify,
|
|
24
|
-
} from "./formats/common.js";
|
|
25
|
-
import {
|
|
26
|
-
createEmptyLockfile,
|
|
27
|
-
parseLockfile,
|
|
28
|
-
stringifyLockfile,
|
|
29
|
-
type LockedRecipe,
|
|
30
|
-
type Lockfile,
|
|
31
|
-
} from "./formats/lockfile.js";
|
|
32
|
-
import type { IndexFile } from "./formats/index-file.js";
|
|
33
|
-
import type { ResolvedRecipe } from "./resolver.js";
|
|
34
|
-
import type { RecipeStoreLike, StoreKey } from "./store/contract.js";
|
|
35
|
-
import { builtInProviders, requireProvider } from "./providers/index.js";
|
|
36
|
-
import { repoIdentity } from "./identity.js";
|
|
37
|
-
import { splitRecipeKey } from "../../services/ref-resolver/index.js";
|
|
38
|
-
import type { ProviderOptions, RepoProvider } from "./providers/provider.js";
|
|
39
|
-
|
|
40
|
-
/** What the lockfile needs to know about a repository. */
|
|
41
|
-
export type LockRepoInput = {
|
|
42
|
-
/** Where the repository lives. */
|
|
43
|
-
url: string;
|
|
44
|
-
/**
|
|
45
|
-
* The repository's canonical identity. When it is left out the lockfile
|
|
46
|
-
* derives one from the URL through the provider that handles it, so a caller
|
|
47
|
-
* that has already canonicalized the repository need not do it twice.
|
|
48
|
-
*/
|
|
49
|
-
identity?: string;
|
|
50
|
-
/** The provider the repository entry names, when it names one. */
|
|
51
|
-
provider?: string;
|
|
52
|
-
/** Content hash of the index the resolution was made against, when known. */
|
|
53
|
-
indexHash?: string;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/** One change between two lockfiles. */
|
|
57
|
-
export type LockChange = {
|
|
58
|
-
/** The recipe key. */
|
|
59
|
-
key: string;
|
|
60
|
-
/** The version before, when there was one. */
|
|
61
|
-
from?: string;
|
|
62
|
-
/** The version after, when there is one. */
|
|
63
|
-
to?: string;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/** What changed between two lockfiles, ready to be printed. */
|
|
67
|
-
export type LockDiff = {
|
|
68
|
-
added: LockChange[];
|
|
69
|
-
removed: LockChange[];
|
|
70
|
-
updated: LockChange[];
|
|
71
|
-
/** Repositories that appeared or disappeared. */
|
|
72
|
-
reposAdded: string[];
|
|
73
|
-
reposRemoved: string[];
|
|
74
|
-
/** True when nothing at all changed. */
|
|
75
|
-
unchanged: boolean;
|
|
76
|
-
/** The whole thing as plain-language lines, one per change. */
|
|
77
|
-
lines: string[];
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/** How a restore is carried out. */
|
|
81
|
-
export type RestoreOptions = {
|
|
82
|
-
/** The store to fill. */
|
|
83
|
-
store: RecipeStoreLike;
|
|
84
|
-
/** The cached index of each repository, which is where a recipe's folder path comes from. */
|
|
85
|
-
indexes: Map<string, IndexFile>;
|
|
86
|
-
/** The providers to choose from. Defaults to the built-ins. */
|
|
87
|
-
providers?: RepoProvider[];
|
|
88
|
-
/** Options handed to every provider call. */
|
|
89
|
-
providerOptions?: ProviderOptions;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/** What a restore did. */
|
|
93
|
-
export type RestoreReport = {
|
|
94
|
-
/** Recipes fetched into the store. */
|
|
95
|
-
restored: string[];
|
|
96
|
-
/** Recipes the store already held, verified against the locked hash. */
|
|
97
|
-
alreadyPresent: string[];
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/** Reads, writes and applies a project's lockfile. */
|
|
101
|
-
export class LockService {
|
|
102
|
-
private readonly sousDir: string;
|
|
103
|
-
|
|
104
|
-
constructor(sousDir: string) {
|
|
105
|
-
this.sousDir = sousDir;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** Where the lockfile lives. */
|
|
109
|
-
get filePath(): string {
|
|
110
|
-
return path.join(this.sousDir, LOCKFILE_FILENAME);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Reads and validates the lockfile. A project that has locked nothing yet has
|
|
115
|
-
* no file, and gets an empty lockfile rather than an error.
|
|
116
|
-
*/
|
|
117
|
-
read(): Lockfile {
|
|
118
|
-
let text: string;
|
|
119
|
-
try {
|
|
120
|
-
text = fs.readFileSync(this.filePath, "utf8");
|
|
121
|
-
} catch (error) {
|
|
122
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return createEmptyLockfile();
|
|
123
|
-
throw new ConfigError(
|
|
124
|
-
`Sous could not read the lockfile at ${this.filePath}.\n ${(error as Error).message}`
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
let parsed: unknown;
|
|
129
|
-
try {
|
|
130
|
-
parsed = JSON.parse(text);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
throw new ConfigError(
|
|
133
|
-
`Sous could not read the lockfile at ${this.filePath} as JSON.\n` +
|
|
134
|
-
` ${(error as Error).message}\n` +
|
|
135
|
-
` The lockfile is written by sous and committed with the project; restoring it ` +
|
|
136
|
-
`from version control is usually the quickest fix.`
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return parseLockfile(parsed, this.filePath);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Writes the lockfile, keys sorted, through a temporary file so a reader
|
|
145
|
-
* never sees a half-written lock.
|
|
146
|
-
*
|
|
147
|
-
* @param lock - The lockfile to write.
|
|
148
|
-
*/
|
|
149
|
-
write(lock: Lockfile): string {
|
|
150
|
-
fs.mkdirSync(this.sousDir, { recursive: true });
|
|
151
|
-
const temporary = path.join(this.sousDir, `.${LOCKFILE_FILENAME}.tmp-${process.pid}`);
|
|
152
|
-
try {
|
|
153
|
-
fs.writeFileSync(temporary, stringifyLockfile(lock), "utf8");
|
|
154
|
-
fs.renameSync(temporary, this.filePath);
|
|
155
|
-
} catch (error) {
|
|
156
|
-
fs.rmSync(temporary, { force: true });
|
|
157
|
-
throw new ConfigError(
|
|
158
|
-
`Sous could not write the lockfile at ${this.filePath}.\n ${(error as Error).message}`
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
return this.filePath;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Builds the lockfile a resolution implies. A recipe the resolution covered
|
|
166
|
-
* takes the resolution's version and hash; anything the resolution did not
|
|
167
|
-
* mention is carried through untouched, so a partial install never drops the
|
|
168
|
-
* rest of the project.
|
|
169
|
-
*
|
|
170
|
-
* Holders are MERGED rather than replaced. A resolution only walks the
|
|
171
|
-
* closure it was asked about, so its `requestedBy` is who holds a recipe
|
|
172
|
-
* WITHIN that closure, not who holds it in the project. Replacing the list
|
|
173
|
-
* would erase a hold recorded by an earlier resolution: subscribing to `b`,
|
|
174
|
-
* which depends on `a`, would drop the `project` hold that subscribing to `a`
|
|
175
|
-
* recorded, and unsubscribing from `b` would then delete `a` outright. The
|
|
176
|
-
* union is refcounting done right; `removeHolder` is the only thing that ever
|
|
177
|
-
* takes a holder away.
|
|
178
|
-
*
|
|
179
|
-
* `kind` merges the same way, since an entry the project still holds directly
|
|
180
|
-
* stays a subscription even when a later resolution reached it as a
|
|
181
|
-
* dependency.
|
|
182
|
-
*
|
|
183
|
-
* @param lock - The lockfile as it stands.
|
|
184
|
-
* @param resolved - What the resolver settled on.
|
|
185
|
-
* @param repos - The repositories those recipes came from.
|
|
186
|
-
*/
|
|
187
|
-
applyResolution(
|
|
188
|
-
lock: Lockfile,
|
|
189
|
-
resolved: ResolvedRecipe[],
|
|
190
|
-
repos: Record<string, LockRepoInput>
|
|
191
|
-
): Lockfile {
|
|
192
|
-
const recipes: Record<string, LockedRecipe> = { ...lock.recipes };
|
|
193
|
-
|
|
194
|
-
for (const recipe of resolved) {
|
|
195
|
-
const previous = lock.recipes[recipe.key];
|
|
196
|
-
const holders = new Set([...(previous?.requestedBy ?? []), ...recipe.requestedBy]);
|
|
197
|
-
recipes[recipe.key] = {
|
|
198
|
-
repo: recipe.repo,
|
|
199
|
-
version: recipe.version,
|
|
200
|
-
hash: recipe.hash,
|
|
201
|
-
requestedBy: [...holders].sort(),
|
|
202
|
-
kind:
|
|
203
|
-
previous?.kind === "subscribes" || recipe.kind === "subscribes"
|
|
204
|
-
? "subscribes"
|
|
205
|
-
: "depends",
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const usedRepos = new Set(Object.values(recipes).map((entry) => entry.repo));
|
|
210
|
-
const lockedRepos: Lockfile["repos"] = {};
|
|
211
|
-
for (const name of [...usedRepos].sort()) {
|
|
212
|
-
const known = repos[name];
|
|
213
|
-
const previous = lock.repos[name];
|
|
214
|
-
const url = known?.url ?? previous?.url;
|
|
215
|
-
if (url === undefined) {
|
|
216
|
-
throw new ConfigError(
|
|
217
|
-
`The lockfile cannot record the recipes from '${name}' because nothing knows ` +
|
|
218
|
-
`that repository's URL.\n` +
|
|
219
|
-
` Add it with 'sous repo add <url>', so the lockfile can name where its ` +
|
|
220
|
-
`recipes came from.`
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
const indexHash = known?.indexHash ?? previous?.indexHash;
|
|
224
|
-
// The identity is what the machine-wide store is keyed by, so it is
|
|
225
|
-
// recorded beside the short name rather than recomputed at restore time
|
|
226
|
-
// from a URL the project may since have repointed.
|
|
227
|
-
const identity =
|
|
228
|
-
known?.identity ??
|
|
229
|
-
previous?.identity ??
|
|
230
|
-
repoIdentity(
|
|
231
|
-
requireProvider(url, known?.provider, builtInProviders()).canonicalize(url)
|
|
232
|
-
);
|
|
233
|
-
lockedRepos[name] = {
|
|
234
|
-
url,
|
|
235
|
-
identity,
|
|
236
|
-
...(indexHash === undefined ? {} : { indexHash }),
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return { formatVersion: 1, repos: lockedRepos, recipes: sortRecipes(recipes) };
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Drops one holder from the lockfile. Every recipe that holder pulled in
|
|
245
|
-
* loses it too, and an entry goes only once nothing holds it any more, which
|
|
246
|
-
* is what makes an unsubscribe safe.
|
|
247
|
-
*
|
|
248
|
-
* @param lock - The lockfile as it stands.
|
|
249
|
-
* @param holder - The holder to remove: "project", or a recipe key.
|
|
250
|
-
*/
|
|
251
|
-
removeHolder(lock: Lockfile, holder: string): Lockfile {
|
|
252
|
-
const recipes: Record<string, LockedRecipe> = {};
|
|
253
|
-
for (const [key, entry] of Object.entries(lock.recipes)) {
|
|
254
|
-
recipes[key] = { ...entry, requestedBy: [...entry.requestedBy] };
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Removing a holder can orphan the recipes it held, which can orphan more
|
|
258
|
-
// in turn, so the removal repeats until a pass changes nothing.
|
|
259
|
-
let pending = [holder];
|
|
260
|
-
while (pending.length > 0) {
|
|
261
|
-
const going = pending;
|
|
262
|
-
pending = [];
|
|
263
|
-
|
|
264
|
-
for (const [key, entry] of Object.entries(recipes)) {
|
|
265
|
-
const kept = entry.requestedBy.filter((name) => !going.includes(name));
|
|
266
|
-
if (kept.length === entry.requestedBy.length) continue;
|
|
267
|
-
if (kept.length === 0) {
|
|
268
|
-
delete recipes[key];
|
|
269
|
-
pending.push(key);
|
|
270
|
-
} else {
|
|
271
|
-
entry.requestedBy = kept;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const usedRepos = new Set(Object.values(recipes).map((entry) => entry.repo));
|
|
277
|
-
const lockedRepos: Lockfile["repos"] = {};
|
|
278
|
-
const orphaned: string[] = [];
|
|
279
|
-
for (const name of [...usedRepos].sort()) {
|
|
280
|
-
const entry = lock.repos[name];
|
|
281
|
-
// A hand-edited or badly merged lockfile can pin a recipe to a repository
|
|
282
|
-
// its own `repos` block no longer describes. Asserting the entry exists
|
|
283
|
-
// wrote `undefined`, which JSON.stringify drops, producing a lockfile that
|
|
284
|
-
// fails its own validation the next time anything reads it.
|
|
285
|
-
if (entry === undefined) {
|
|
286
|
-
orphaned.push(name);
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
lockedRepos[name] = entry;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
if (orphaned.length > 0) {
|
|
293
|
-
throw new ConfigError(
|
|
294
|
-
`The lockfile at ${this.filePath} pins recipes to ${
|
|
295
|
-
orphaned.length === 1 ? "a repository" : "repositories"
|
|
296
|
-
} it does not describe: ${orphaned.join(", ")}.\n` +
|
|
297
|
-
` Every repository a locked recipe came from has to have an entry in the ` +
|
|
298
|
-
`lockfile's 'repos' block, and ${
|
|
299
|
-
orphaned.length === 1 ? "that one has" : "those have"
|
|
300
|
-
} none.\n` +
|
|
301
|
-
` This usually means the file was edited by hand or merged badly. Delete it ` +
|
|
302
|
-
`and run 'sous subscribe' again to rebuild it from your config.`
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return { formatVersion: 1, repos: lockedRepos, recipes: sortRecipes(recipes) };
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Compares two lockfiles and describes the difference in plain language, for
|
|
311
|
-
* the summary a command prints before or after it writes one.
|
|
312
|
-
*
|
|
313
|
-
* @param before - The lockfile as it was.
|
|
314
|
-
* @param after - The lockfile as it will be.
|
|
315
|
-
*/
|
|
316
|
-
diff(before: Lockfile, after: Lockfile): LockDiff {
|
|
317
|
-
const added: LockChange[] = [];
|
|
318
|
-
const removed: LockChange[] = [];
|
|
319
|
-
const updated: LockChange[] = [];
|
|
320
|
-
|
|
321
|
-
for (const [key, entry] of Object.entries(after.recipes)) {
|
|
322
|
-
const previous = before.recipes[key];
|
|
323
|
-
if (previous === undefined) {
|
|
324
|
-
added.push({ key, to: entry.version });
|
|
325
|
-
} else if (previous.version !== entry.version) {
|
|
326
|
-
updated.push({ key, from: previous.version, to: entry.version });
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
for (const [key, entry] of Object.entries(before.recipes)) {
|
|
330
|
-
if (!Object.hasOwn(after.recipes, key)) removed.push({ key, from: entry.version });
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
const reposAdded = Object.keys(after.repos)
|
|
334
|
-
.filter((name) => !Object.hasOwn(before.repos, name))
|
|
335
|
-
.sort();
|
|
336
|
-
const reposRemoved = Object.keys(before.repos)
|
|
337
|
-
.filter((name) => !Object.hasOwn(after.repos, name))
|
|
338
|
-
.sort();
|
|
339
|
-
|
|
340
|
-
const byKey = (left: LockChange, right: LockChange) => (left.key < right.key ? -1 : 1);
|
|
341
|
-
added.sort(byKey);
|
|
342
|
-
removed.sort(byKey);
|
|
343
|
-
updated.sort(byKey);
|
|
344
|
-
|
|
345
|
-
const lines: string[] = [];
|
|
346
|
-
for (const name of reposAdded) lines.push(`Repository added: ${name}`);
|
|
347
|
-
for (const change of added) lines.push(`Adding ${change.key} version ${change.to}`);
|
|
348
|
-
for (const change of updated) {
|
|
349
|
-
lines.push(`Updating ${change.key} from version ${change.from} to version ${change.to}`);
|
|
350
|
-
}
|
|
351
|
-
for (const change of removed) {
|
|
352
|
-
lines.push(`Removing ${change.key}, which nothing needs any more`);
|
|
353
|
-
}
|
|
354
|
-
for (const name of reposRemoved) {
|
|
355
|
-
lines.push(`Repository no longer needed: ${name}`);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const unchanged =
|
|
359
|
-
added.length === 0 &&
|
|
360
|
-
removed.length === 0 &&
|
|
361
|
-
updated.length === 0 &&
|
|
362
|
-
reposAdded.length === 0 &&
|
|
363
|
-
reposRemoved.length === 0;
|
|
364
|
-
if (unchanged) lines.push("Nothing changed.");
|
|
365
|
-
|
|
366
|
-
return { added, removed, updated, reposAdded, reposRemoved, unchanged, lines };
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Makes the store hold exactly what the lockfile pins. A recipe the store
|
|
371
|
-
* already has, whose hash still verifies, is left alone; anything else is
|
|
372
|
-
* fetched at the locked version's tag and stored under the locked hash, which
|
|
373
|
-
* fails loudly if what arrives does not match.
|
|
374
|
-
*
|
|
375
|
-
* Nothing here decides a version and nothing here asks a question. That is
|
|
376
|
-
* what makes a fresh clone reproducible.
|
|
377
|
-
*
|
|
378
|
-
* @param lock - The lockfile to restore.
|
|
379
|
-
* @param options - The store, the cached indexes, and the providers.
|
|
380
|
-
*/
|
|
381
|
-
async restore(lock: Lockfile, options: RestoreOptions): Promise<RestoreReport> {
|
|
382
|
-
const providers = options.providers ?? builtInProviders();
|
|
383
|
-
const report: RestoreReport = { restored: [], alreadyPresent: [] };
|
|
384
|
-
|
|
385
|
-
for (const [key, entry] of Object.entries(lock.recipes)) {
|
|
386
|
-
const { namespace, name } = splitRecipeKey(key);
|
|
387
|
-
const storeKey: StoreKey = {
|
|
388
|
-
identity: lock.repos[entry.repo]!.identity,
|
|
389
|
-
namespace,
|
|
390
|
-
name,
|
|
391
|
-
version: entry.version,
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
const hit = await options.store.get(storeKey);
|
|
395
|
-
if (hit !== undefined && hit.entry.hash === entry.hash) {
|
|
396
|
-
report.alreadyPresent.push(key);
|
|
397
|
-
continue;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const index = options.indexes.get(entry.repo);
|
|
401
|
-
const indexRecipe = index?.recipes[key];
|
|
402
|
-
const version = indexRecipe?.versions[entry.version];
|
|
403
|
-
if (index === undefined || indexRecipe === undefined || version === undefined) {
|
|
404
|
-
throw new ConfigError(
|
|
405
|
-
`The lockfile pins '${key}' at version ${entry.version} from the repository ` +
|
|
406
|
-
`'${entry.repo}', and that repository's index does not offer it.\n` +
|
|
407
|
-
` Either the index has not been fetched yet, or the version has been ` +
|
|
408
|
-
`withdrawn upstream. Run 'sous repo list' to see what sous currently has.`
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
const repoUrl = lock.repos[entry.repo]!.url;
|
|
413
|
-
const provider = requireProvider(repoUrl, undefined, providers);
|
|
414
|
-
const canonical = provider.canonicalize(repoUrl);
|
|
415
|
-
|
|
416
|
-
const workDir = await fsp.mkdtemp(path.join(options.store.root, ".sous-restore-"));
|
|
417
|
-
const fetchDir = path.join(workDir, storeKey.name);
|
|
418
|
-
try {
|
|
419
|
-
await provider.fetchRecipeTree(
|
|
420
|
-
canonical,
|
|
421
|
-
indexRecipe.path,
|
|
422
|
-
version.tag,
|
|
423
|
-
fetchDir,
|
|
424
|
-
options.providerOptions ?? {}
|
|
425
|
-
);
|
|
426
|
-
await options.store.put(storeKey, fetchDir, entry.hash);
|
|
427
|
-
report.restored.push(key);
|
|
428
|
-
} finally {
|
|
429
|
-
await fsp.rm(workDir, { recursive: true, force: true });
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
report.restored.sort();
|
|
434
|
-
report.alreadyPresent.sort();
|
|
435
|
-
return report;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* Renders a lockfile exactly as `write` would, without writing it. Used by
|
|
440
|
-
* dry runs, which show what would change and touch nothing.
|
|
441
|
-
*
|
|
442
|
-
* @param lock - The lockfile to render.
|
|
443
|
-
*/
|
|
444
|
-
preview(lock: Lockfile): string {
|
|
445
|
-
return stableJsonStringify(lock);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
/** Rebuilds a recipe map with its keys in sorted order. */
|
|
450
|
-
function sortRecipes(recipes: Record<string, LockedRecipe>): Record<string, LockedRecipe> {
|
|
451
|
-
const sorted: Record<string, LockedRecipe> = {};
|
|
452
|
-
for (const key of Object.keys(recipes).sort()) sorted[key] = recipes[key]!;
|
|
453
|
-
return sorted;
|
|
454
|
-
}
|