@sous-io/sous 0.1.1 → 0.2.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 +115 -35
- package/bin/run.js +10 -1
- package/docs/markdown/README.md +27 -0
- package/docs/markdown/_sidebar.md +18 -0
- package/docs/markdown/commands.md +308 -0
- package/docs/markdown/config-discovery.md +74 -0
- package/docs/markdown/config-inspection.md +69 -0
- package/docs/markdown/config-layers.md +92 -0
- package/docs/markdown/config-variables.md +79 -0
- package/docs/markdown/configuration.md +71 -0
- package/docs/markdown/design-principles.md +59 -0
- package/docs/markdown/repositories-authoring.md +409 -0
- package/docs/markdown/repositories-consuming.md +580 -0
- package/docs/markdown/repositories-file-formats.md +1084 -0
- package/docs/markdown/repositories-variables.md +387 -0
- package/docs/markdown/repositories.md +303 -0
- package/docs/markdown/skill-categories.md +58 -0
- package/package.json +72 -8
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/SKILL.tpl.md +20 -20
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/about-something.md +2 -2
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/do-something.md +1 -1
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/advanced-patterns.md +6 -6
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/commands.md +5 -5
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/frontmatter.md +3 -3
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/SKILL.tpl.md +40 -25
- package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +70 -0
- package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +75 -0
- package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/create-skill/SKILL.tpl.md +8 -9
- package/recipes/core/sous-skills/sous.recipe.yaml +45 -0
- package/sous.config.schema.json +337 -0
- package/src/base-command.ts +220 -67
- package/src/commands/build.ts +150 -73
- package/src/commands/clear.ts +23 -15
- package/src/commands/compile.ts +74 -16
- package/src/commands/config/get.ts +110 -0
- package/src/commands/config/show.ts +32 -0
- package/src/commands/config/validate.ts +53 -0
- package/src/commands/help.ts +46 -0
- package/src/commands/launch.ts +36 -14
- package/src/commands/lock/rebuild.ts +241 -0
- package/src/commands/lock/show.ts +115 -0
- package/src/commands/namespace/list.ts +117 -0
- package/src/commands/namespace/show.ts +110 -0
- package/src/commands/prune.ts +3 -11
- package/src/commands/recipe/list.ts +95 -0
- package/src/commands/recipe/show.ts +301 -0
- package/src/commands/repo/add.ts +145 -0
- package/src/commands/repo/gc.ts +172 -0
- package/src/commands/repo/init.ts +136 -0
- package/src/commands/repo/link.ts +500 -0
- package/src/commands/repo/list.ts +179 -0
- package/src/commands/repo/release.ts +625 -0
- package/src/commands/repo/remove.ts +193 -0
- package/src/commands/repo/search.ts +189 -0
- package/src/commands/repo/submit.ts +133 -0
- package/src/commands/repo/unlink.ts +147 -0
- package/src/commands/subscription/add.ts +285 -0
- package/src/commands/subscription/list.ts +129 -0
- package/src/commands/subscription/remove.ts +181 -0
- package/src/commands/vars/ask.ts +374 -0
- package/src/commands/vars/index.ts +79 -0
- package/src/commands/vars/list.ts +67 -0
- package/src/commands/vars/show.ts +77 -0
- package/src/config-command.ts +30 -0
- package/src/lib/build-service.ts +206 -54
- package/src/lib/config-discovery.ts +220 -27
- package/src/lib/config-inspect.ts +145 -0
- package/src/lib/config-kernel.mjs +377 -0
- package/src/lib/config-schema.ts +361 -0
- package/src/lib/env-file.ts +328 -0
- package/src/lib/env-local.ts +18 -1
- package/src/lib/errors.ts +32 -0
- package/src/lib/include-resolver.ts +108 -15
- package/src/lib/interactive.ts +165 -0
- package/src/lib/markdown-compiler.ts +118 -37
- package/src/lib/package-info.ts +25 -0
- package/src/lib/pid-service.ts +32 -21
- package/src/lib/refs/find.ts +589 -0
- package/src/lib/refs/index.ts +12 -0
- package/src/lib/refs/pick.ts +147 -0
- package/src/lib/refs/scopes.ts +61 -0
- package/src/lib/repos/catalog-display.ts +116 -0
- package/src/lib/repos/catalog-inputs.ts +160 -0
- package/src/lib/repos/catalog.ts +722 -0
- package/src/lib/repos/core-recipe.ts +105 -0
- package/src/lib/repos/defaults.ts +175 -0
- package/src/lib/repos/formats/common.ts +389 -0
- package/src/lib/repos/formats/index-file.ts +215 -0
- package/src/lib/repos/formats/links-map.ts +96 -0
- package/src/lib/repos/formats/lockfile.ts +167 -0
- package/src/lib/repos/formats/patterns.ts +57 -0
- package/src/lib/repos/formats/recipe-manifest.ts +395 -0
- package/src/lib/repos/formats/repo-manifest.ts +88 -0
- package/src/lib/repos/formats/store-entry.ts +84 -0
- package/src/lib/repos/freshness.ts +208 -0
- package/src/lib/repos/git-clone.ts +312 -0
- package/src/lib/repos/identity.ts +89 -0
- package/src/lib/repos/index.ts +58 -0
- package/src/lib/repos/links.ts +353 -0
- package/src/lib/repos/load-manifest.ts +236 -0
- package/src/lib/repos/lock-service.ts +453 -0
- package/src/lib/repos/locked-namespace-resolver.ts +90 -0
- package/src/lib/repos/locked-recipes.ts +254 -0
- package/src/lib/repos/managed-layer.ts +422 -0
- package/src/lib/repos/namespace-resolver.ts +370 -0
- package/src/lib/repos/providers/base.ts +206 -0
- package/src/lib/repos/providers/git.ts +233 -0
- package/src/lib/repos/providers/github.ts +294 -0
- package/src/lib/repos/providers/gitlab.ts +263 -0
- package/src/lib/repos/providers/http.ts +102 -0
- package/src/lib/repos/providers/index-cache.ts +382 -0
- package/src/lib/repos/providers/index.ts +106 -0
- package/src/lib/repos/providers/local.ts +391 -0
- package/src/lib/repos/providers/provider.ts +401 -0
- package/src/lib/repos/recipe-config-layers.ts +287 -0
- package/src/lib/repos/recipe-targets.ts +223 -0
- package/src/lib/repos/ref-search.ts +46 -0
- package/src/lib/repos/ref.ts +513 -0
- package/src/lib/repos/reference-report.ts +122 -0
- package/src/lib/repos/release/bump.ts +161 -0
- package/src/lib/repos/release/git-state.ts +305 -0
- package/src/lib/repos/release/index-builder.ts +635 -0
- package/src/lib/repos/release/index.ts +16 -0
- package/src/lib/repos/release/plan.ts +512 -0
- package/src/lib/repos/release/submit-service.ts +496 -0
- package/src/lib/repos/release/tags.ts +243 -0
- package/src/lib/repos/release/validate.ts +463 -0
- package/src/lib/repos/resolver.ts +789 -0
- package/src/lib/repos/scaffold/index.ts +238 -0
- package/src/lib/repos/scaffold/templates.ts +415 -0
- package/src/lib/repos/seed.ts +414 -0
- package/src/lib/repos/store/contract.ts +64 -0
- package/src/lib/repos/store/hash.ts +114 -0
- package/src/lib/repos/store/recipe-store.ts +599 -0
- package/src/lib/repos/store/settings.ts +58 -0
- package/src/lib/repos/subscription-service.ts +2678 -0
- package/src/lib/repos/trust.ts +447 -0
- package/src/lib/settings.ts +546 -189
- package/src/lib/sous-home.ts +104 -0
- package/src/lib/state.ts +52 -20
- package/src/lib/vars/ask.ts +1152 -0
- package/src/lib/vars/definition-source.ts +252 -0
- package/src/lib/vars/display.ts +233 -0
- package/src/lib/vars/index.ts +18 -0
- package/src/lib/vars/ladder.ts +282 -0
- package/src/lib/vars/mappings.ts +265 -0
- package/src/lib/vars/names.ts +94 -0
- package/src/lib/vars/preanswers.ts +395 -0
- package/src/lib/vars/question-plan.ts +218 -0
- package/src/lib/vars/report.ts +228 -0
- package/src/lib/vars/safe-regex.ts +235 -0
- package/src/lib/vars/validate.ts +312 -0
- package/src/lib/watch-loop.ts +148 -0
- package/src/templating/init-liquid-engine.ts +58 -16
- package/src/utils/choice-prompt.ts +143 -0
- package/src/utils/command-errors.ts +186 -0
- package/src/utils/command-help.ts +45 -0
- package/src/utils/confirm-prompt.ts +110 -0
- package/src/utils/flags.ts +153 -0
- package/src/utils/formatting.ts +540 -55
- package/src/utils/prompts.ts +35 -1
- package/src/utils/sous-directory.ts +245 -0
- package/src/utils/table.ts +603 -0
- package/src/utils/value-prompt.ts +119 -0
- package/shared-prompts/_partials/resume-task.md +0 -51
- package/shared-prompts/_partials/sub-agent-delegation.md +0 -32
- package/shared-prompts/_partials/update-task-file.md +0 -52
- package/shared-prompts/memories/automated-browser-tasks/INDEX.tpl.md +0 -52
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/SKILL.tpl.md +0 -102
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/auth-failure-handling.mjs +0 -81
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/chained-workflow.mjs +0 -126
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/simple-fetch.mjs +0 -92
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/architecture.md +0 -61
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/auth-and-sessions.md +0 -65
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/ctx-api.md +0 -96
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/installation.md +0 -104
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/script-conventions.md +0 -243
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/chrome-state.mjs +0 -148
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.mjs +0 -383
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.spec.mjs +0 -267
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/eslint.config.mjs +0 -56
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/harness.mjs +0 -169
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/keyring.mjs +0 -59
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/logger.mjs +0 -25
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/params.mjs +0 -140
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/run.mjs +0 -140
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/settings.tpl.mjs +0 -1
- package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/utils.mjs +0 -185
- package/shared-prompts/skills/automated-browser-tasks/create-automated-browser-task/SKILL.tpl.md +0 -52
- package/shared-prompts/skills/automated-browser-tasks/running-automated-browser-tasks/SKILL.tpl.md +0 -59
- package/shared-prompts/skills/automated-browser-tasks/update-automated-browser-task/SKILL.tpl.md +0 -47
- package/shared-prompts/skills/control-flow/approve/SKILL.tpl.md +0 -26
- package/shared-prompts/skills/control-flow/opine/SKILL.tpl.md +0 -58
- package/shared-prompts/skills/control-flow/repeat/SKILL.tpl.md +0 -27
- package/shared-prompts/skills/control-flow/research/SKILL.tpl.md +0 -34
- package/shared-prompts/skills/sous-skills/about-sous/SKILL.tpl.md +0 -51
- package/shared-prompts/skills/task-files/about-task-files/SKILL.tpl.md +0 -122
- package/shared-prompts/skills/task-files/continue-task-in-new-branch/SKILL.tpl.md +0 -80
- package/shared-prompts/skills/task-files/go/SKILL.tpl.md +0 -14
- package/shared-prompts/skills/task-files/resume-task/SKILL.tpl.md +0 -13
- package/shared-prompts/skills/task-files/start-task/SKILL.tpl.md +0 -93
- package/shared-prompts/skills/task-files/update/SKILL.tpl.md +0 -14
- package/shared-prompts/skills/task-files/update-task-file/SKILL.tpl.md +0 -13
- /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/substitutions.md +0 -0
- /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/references/liquid-filters.md +0 -0
|
@@ -0,0 +1,453 @@
|
|
|
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 type { ProviderOptions, RepoProvider } from "./providers/provider.js";
|
|
38
|
+
|
|
39
|
+
/** What the lockfile needs to know about a repository. */
|
|
40
|
+
export type LockRepoInput = {
|
|
41
|
+
/** Where the repository lives. */
|
|
42
|
+
url: string;
|
|
43
|
+
/**
|
|
44
|
+
* The repository's canonical identity. When it is left out the lockfile
|
|
45
|
+
* derives one from the URL through the provider that handles it, so a caller
|
|
46
|
+
* that has already canonicalized the repository need not do it twice.
|
|
47
|
+
*/
|
|
48
|
+
identity?: string;
|
|
49
|
+
/** The provider the repository entry names, when it names one. */
|
|
50
|
+
provider?: string;
|
|
51
|
+
/** Content hash of the index the resolution was made against, when known. */
|
|
52
|
+
indexHash?: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** One change between two lockfiles. */
|
|
56
|
+
export type LockChange = {
|
|
57
|
+
/** The recipe key. */
|
|
58
|
+
key: string;
|
|
59
|
+
/** The version before, when there was one. */
|
|
60
|
+
from?: string;
|
|
61
|
+
/** The version after, when there is one. */
|
|
62
|
+
to?: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** What changed between two lockfiles, ready to be printed. */
|
|
66
|
+
export type LockDiff = {
|
|
67
|
+
added: LockChange[];
|
|
68
|
+
removed: LockChange[];
|
|
69
|
+
updated: LockChange[];
|
|
70
|
+
/** Repositories that appeared or disappeared. */
|
|
71
|
+
reposAdded: string[];
|
|
72
|
+
reposRemoved: string[];
|
|
73
|
+
/** True when nothing at all changed. */
|
|
74
|
+
unchanged: boolean;
|
|
75
|
+
/** The whole thing as plain-language lines, one per change. */
|
|
76
|
+
lines: string[];
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** How a restore is carried out. */
|
|
80
|
+
export type RestoreOptions = {
|
|
81
|
+
/** The store to fill. */
|
|
82
|
+
store: RecipeStoreLike;
|
|
83
|
+
/** The cached index of each repository, which is where a recipe's folder path comes from. */
|
|
84
|
+
indexes: Map<string, IndexFile>;
|
|
85
|
+
/** The providers to choose from. Defaults to the built-ins. */
|
|
86
|
+
providers?: RepoProvider[];
|
|
87
|
+
/** Options handed to every provider call. */
|
|
88
|
+
providerOptions?: ProviderOptions;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** What a restore did. */
|
|
92
|
+
export type RestoreReport = {
|
|
93
|
+
/** Recipes fetched into the store. */
|
|
94
|
+
restored: string[];
|
|
95
|
+
/** Recipes the store already held, verified against the locked hash. */
|
|
96
|
+
alreadyPresent: string[];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Reads, writes and applies a project's lockfile. */
|
|
100
|
+
export class LockService {
|
|
101
|
+
private readonly sousDir: string;
|
|
102
|
+
|
|
103
|
+
constructor(sousDir: string) {
|
|
104
|
+
this.sousDir = sousDir;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Where the lockfile lives. */
|
|
108
|
+
get filePath(): string {
|
|
109
|
+
return path.join(this.sousDir, LOCKFILE_FILENAME);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Reads and validates the lockfile. A project that has locked nothing yet has
|
|
114
|
+
* no file, and gets an empty lockfile rather than an error.
|
|
115
|
+
*/
|
|
116
|
+
read(): Lockfile {
|
|
117
|
+
let text: string;
|
|
118
|
+
try {
|
|
119
|
+
text = fs.readFileSync(this.filePath, "utf8");
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return createEmptyLockfile();
|
|
122
|
+
throw new ConfigError(
|
|
123
|
+
`Sous could not read the lockfile at ${this.filePath}.\n ${(error as Error).message}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let parsed: unknown;
|
|
128
|
+
try {
|
|
129
|
+
parsed = JSON.parse(text);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
throw new ConfigError(
|
|
132
|
+
`Sous could not read the lockfile at ${this.filePath} as JSON.\n` +
|
|
133
|
+
` ${(error as Error).message}\n` +
|
|
134
|
+
` The lockfile is written by sous and committed with the project; restoring it ` +
|
|
135
|
+
`from version control is usually the quickest fix.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return parseLockfile(parsed, this.filePath);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Writes the lockfile, keys sorted, through a temporary file so a reader
|
|
144
|
+
* never sees a half-written lock.
|
|
145
|
+
*
|
|
146
|
+
* @param lock - The lockfile to write.
|
|
147
|
+
*/
|
|
148
|
+
write(lock: Lockfile): string {
|
|
149
|
+
fs.mkdirSync(this.sousDir, { recursive: true });
|
|
150
|
+
const temporary = path.join(this.sousDir, `.${LOCKFILE_FILENAME}.tmp-${process.pid}`);
|
|
151
|
+
try {
|
|
152
|
+
fs.writeFileSync(temporary, stringifyLockfile(lock), "utf8");
|
|
153
|
+
fs.renameSync(temporary, this.filePath);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
fs.rmSync(temporary, { force: true });
|
|
156
|
+
throw new ConfigError(
|
|
157
|
+
`Sous could not write the lockfile at ${this.filePath}.\n ${(error as Error).message}`
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return this.filePath;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Builds the lockfile a resolution implies. A recipe the resolution covered
|
|
165
|
+
* takes the resolution's version and hash; anything the resolution did not
|
|
166
|
+
* mention is carried through untouched, so a partial install never drops the
|
|
167
|
+
* rest of the project.
|
|
168
|
+
*
|
|
169
|
+
* Holders are MERGED rather than replaced. A resolution only walks the
|
|
170
|
+
* closure it was asked about, so its `requestedBy` is who holds a recipe
|
|
171
|
+
* WITHIN that closure, not who holds it in the project. Replacing the list
|
|
172
|
+
* would erase a hold recorded by an earlier resolution: subscribing to `b`,
|
|
173
|
+
* which depends on `a`, would drop the `project` hold that subscribing to `a`
|
|
174
|
+
* recorded, and unsubscribing from `b` would then delete `a` outright. The
|
|
175
|
+
* union is refcounting done right; `removeHolder` is the only thing that ever
|
|
176
|
+
* takes a holder away.
|
|
177
|
+
*
|
|
178
|
+
* `kind` merges the same way, since an entry the project still holds directly
|
|
179
|
+
* stays a subscription even when a later resolution reached it as a
|
|
180
|
+
* dependency.
|
|
181
|
+
*
|
|
182
|
+
* @param lock - The lockfile as it stands.
|
|
183
|
+
* @param resolved - What the resolver settled on.
|
|
184
|
+
* @param repos - The repositories those recipes came from.
|
|
185
|
+
*/
|
|
186
|
+
applyResolution(
|
|
187
|
+
lock: Lockfile,
|
|
188
|
+
resolved: ResolvedRecipe[],
|
|
189
|
+
repos: Record<string, LockRepoInput>
|
|
190
|
+
): Lockfile {
|
|
191
|
+
const recipes: Record<string, LockedRecipe> = { ...lock.recipes };
|
|
192
|
+
|
|
193
|
+
for (const recipe of resolved) {
|
|
194
|
+
const previous = lock.recipes[recipe.key];
|
|
195
|
+
const holders = new Set([...(previous?.requestedBy ?? []), ...recipe.requestedBy]);
|
|
196
|
+
recipes[recipe.key] = {
|
|
197
|
+
repo: recipe.repo,
|
|
198
|
+
version: recipe.version,
|
|
199
|
+
hash: recipe.hash,
|
|
200
|
+
requestedBy: [...holders].sort(),
|
|
201
|
+
kind:
|
|
202
|
+
previous?.kind === "subscribes" || recipe.kind === "subscribes"
|
|
203
|
+
? "subscribes"
|
|
204
|
+
: "depends",
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const usedRepos = new Set(Object.values(recipes).map((entry) => entry.repo));
|
|
209
|
+
const lockedRepos: Lockfile["repos"] = {};
|
|
210
|
+
for (const name of [...usedRepos].sort()) {
|
|
211
|
+
const known = repos[name];
|
|
212
|
+
const previous = lock.repos[name];
|
|
213
|
+
const url = known?.url ?? previous?.url;
|
|
214
|
+
if (url === undefined) {
|
|
215
|
+
throw new ConfigError(
|
|
216
|
+
`The lockfile cannot record the recipes from '${name}' because nothing knows ` +
|
|
217
|
+
`that repository's URL.\n` +
|
|
218
|
+
` Add it with 'sous repo add <url>', so the lockfile can name where its ` +
|
|
219
|
+
`recipes came from.`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const indexHash = known?.indexHash ?? previous?.indexHash;
|
|
223
|
+
// The identity is what the machine-wide store is keyed by, so it is
|
|
224
|
+
// recorded beside the short name rather than recomputed at restore time
|
|
225
|
+
// from a URL the project may since have repointed.
|
|
226
|
+
const identity =
|
|
227
|
+
known?.identity ??
|
|
228
|
+
previous?.identity ??
|
|
229
|
+
repoIdentity(
|
|
230
|
+
requireProvider(url, known?.provider, builtInProviders()).canonicalize(url)
|
|
231
|
+
);
|
|
232
|
+
lockedRepos[name] = {
|
|
233
|
+
url,
|
|
234
|
+
identity,
|
|
235
|
+
...(indexHash === undefined ? {} : { indexHash }),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return { formatVersion: 1, repos: lockedRepos, recipes: sortRecipes(recipes) };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Drops one holder from the lockfile. Every recipe that holder pulled in
|
|
244
|
+
* loses it too, and an entry goes only once nothing holds it any more, which
|
|
245
|
+
* is what makes an unsubscribe safe.
|
|
246
|
+
*
|
|
247
|
+
* @param lock - The lockfile as it stands.
|
|
248
|
+
* @param holder - The holder to remove: "project", or a recipe key.
|
|
249
|
+
*/
|
|
250
|
+
removeHolder(lock: Lockfile, holder: string): Lockfile {
|
|
251
|
+
const recipes: Record<string, LockedRecipe> = {};
|
|
252
|
+
for (const [key, entry] of Object.entries(lock.recipes)) {
|
|
253
|
+
recipes[key] = { ...entry, requestedBy: [...entry.requestedBy] };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Removing a holder can orphan the recipes it held, which can orphan more
|
|
257
|
+
// in turn, so the removal repeats until a pass changes nothing.
|
|
258
|
+
let pending = [holder];
|
|
259
|
+
while (pending.length > 0) {
|
|
260
|
+
const going = pending;
|
|
261
|
+
pending = [];
|
|
262
|
+
|
|
263
|
+
for (const [key, entry] of Object.entries(recipes)) {
|
|
264
|
+
const kept = entry.requestedBy.filter((name) => !going.includes(name));
|
|
265
|
+
if (kept.length === entry.requestedBy.length) continue;
|
|
266
|
+
if (kept.length === 0) {
|
|
267
|
+
delete recipes[key];
|
|
268
|
+
pending.push(key);
|
|
269
|
+
} else {
|
|
270
|
+
entry.requestedBy = kept;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const usedRepos = new Set(Object.values(recipes).map((entry) => entry.repo));
|
|
276
|
+
const lockedRepos: Lockfile["repos"] = {};
|
|
277
|
+
const orphaned: string[] = [];
|
|
278
|
+
for (const name of [...usedRepos].sort()) {
|
|
279
|
+
const entry = lock.repos[name];
|
|
280
|
+
// A hand-edited or badly merged lockfile can pin a recipe to a repository
|
|
281
|
+
// its own `repos` block no longer describes. Asserting the entry exists
|
|
282
|
+
// wrote `undefined`, which JSON.stringify drops, producing a lockfile that
|
|
283
|
+
// fails its own validation the next time anything reads it.
|
|
284
|
+
if (entry === undefined) {
|
|
285
|
+
orphaned.push(name);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
lockedRepos[name] = entry;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (orphaned.length > 0) {
|
|
292
|
+
throw new ConfigError(
|
|
293
|
+
`The lockfile at ${this.filePath} pins recipes to ${
|
|
294
|
+
orphaned.length === 1 ? "a repository" : "repositories"
|
|
295
|
+
} it does not describe: ${orphaned.join(", ")}.\n` +
|
|
296
|
+
` Every repository a locked recipe came from has to have an entry in the ` +
|
|
297
|
+
`lockfile's 'repos' block, and ${
|
|
298
|
+
orphaned.length === 1 ? "that one has" : "those have"
|
|
299
|
+
} none.\n` +
|
|
300
|
+
` This usually means the file was edited by hand or merged badly. Delete it ` +
|
|
301
|
+
`and run 'sous subscribe' again to rebuild it from your config.`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return { formatVersion: 1, repos: lockedRepos, recipes: sortRecipes(recipes) };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Compares two lockfiles and describes the difference in plain language, for
|
|
310
|
+
* the summary a command prints before or after it writes one.
|
|
311
|
+
*
|
|
312
|
+
* @param before - The lockfile as it was.
|
|
313
|
+
* @param after - The lockfile as it will be.
|
|
314
|
+
*/
|
|
315
|
+
diff(before: Lockfile, after: Lockfile): LockDiff {
|
|
316
|
+
const added: LockChange[] = [];
|
|
317
|
+
const removed: LockChange[] = [];
|
|
318
|
+
const updated: LockChange[] = [];
|
|
319
|
+
|
|
320
|
+
for (const [key, entry] of Object.entries(after.recipes)) {
|
|
321
|
+
const previous = before.recipes[key];
|
|
322
|
+
if (previous === undefined) {
|
|
323
|
+
added.push({ key, to: entry.version });
|
|
324
|
+
} else if (previous.version !== entry.version) {
|
|
325
|
+
updated.push({ key, from: previous.version, to: entry.version });
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
for (const [key, entry] of Object.entries(before.recipes)) {
|
|
329
|
+
if (!Object.hasOwn(after.recipes, key)) removed.push({ key, from: entry.version });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const reposAdded = Object.keys(after.repos)
|
|
333
|
+
.filter((name) => !Object.hasOwn(before.repos, name))
|
|
334
|
+
.sort();
|
|
335
|
+
const reposRemoved = Object.keys(before.repos)
|
|
336
|
+
.filter((name) => !Object.hasOwn(after.repos, name))
|
|
337
|
+
.sort();
|
|
338
|
+
|
|
339
|
+
const byKey = (left: LockChange, right: LockChange) => (left.key < right.key ? -1 : 1);
|
|
340
|
+
added.sort(byKey);
|
|
341
|
+
removed.sort(byKey);
|
|
342
|
+
updated.sort(byKey);
|
|
343
|
+
|
|
344
|
+
const lines: string[] = [];
|
|
345
|
+
for (const name of reposAdded) lines.push(`Repository added: ${name}`);
|
|
346
|
+
for (const change of added) lines.push(`Adding ${change.key} version ${change.to}`);
|
|
347
|
+
for (const change of updated) {
|
|
348
|
+
lines.push(`Updating ${change.key} from version ${change.from} to version ${change.to}`);
|
|
349
|
+
}
|
|
350
|
+
for (const change of removed) {
|
|
351
|
+
lines.push(`Removing ${change.key}, which nothing needs any more`);
|
|
352
|
+
}
|
|
353
|
+
for (const name of reposRemoved) {
|
|
354
|
+
lines.push(`Repository no longer needed: ${name}`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const unchanged =
|
|
358
|
+
added.length === 0 &&
|
|
359
|
+
removed.length === 0 &&
|
|
360
|
+
updated.length === 0 &&
|
|
361
|
+
reposAdded.length === 0 &&
|
|
362
|
+
reposRemoved.length === 0;
|
|
363
|
+
if (unchanged) lines.push("Nothing changed.");
|
|
364
|
+
|
|
365
|
+
return { added, removed, updated, reposAdded, reposRemoved, unchanged, lines };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Makes the store hold exactly what the lockfile pins. A recipe the store
|
|
370
|
+
* already has, whose hash still verifies, is left alone; anything else is
|
|
371
|
+
* fetched at the locked version's tag and stored under the locked hash, which
|
|
372
|
+
* fails loudly if what arrives does not match.
|
|
373
|
+
*
|
|
374
|
+
* Nothing here decides a version and nothing here asks a question. That is
|
|
375
|
+
* what makes a fresh clone reproducible.
|
|
376
|
+
*
|
|
377
|
+
* @param lock - The lockfile to restore.
|
|
378
|
+
* @param options - The store, the cached indexes, and the providers.
|
|
379
|
+
*/
|
|
380
|
+
async restore(lock: Lockfile, options: RestoreOptions): Promise<RestoreReport> {
|
|
381
|
+
const providers = options.providers ?? builtInProviders();
|
|
382
|
+
const report: RestoreReport = { restored: [], alreadyPresent: [] };
|
|
383
|
+
|
|
384
|
+
for (const [key, entry] of Object.entries(lock.recipes)) {
|
|
385
|
+
const namespace = key.slice(0, key.indexOf("/"));
|
|
386
|
+
const storeKey: StoreKey = {
|
|
387
|
+
identity: lock.repos[entry.repo]!.identity,
|
|
388
|
+
namespace,
|
|
389
|
+
name: key.slice(namespace.length + 1),
|
|
390
|
+
version: entry.version,
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const hit = await options.store.get(storeKey);
|
|
394
|
+
if (hit !== undefined && hit.entry.hash === entry.hash) {
|
|
395
|
+
report.alreadyPresent.push(key);
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const index = options.indexes.get(entry.repo);
|
|
400
|
+
const indexRecipe = index?.recipes[key];
|
|
401
|
+
const version = indexRecipe?.versions[entry.version];
|
|
402
|
+
if (index === undefined || indexRecipe === undefined || version === undefined) {
|
|
403
|
+
throw new ConfigError(
|
|
404
|
+
`The lockfile pins '${key}' at version ${entry.version} from the repository ` +
|
|
405
|
+
`'${entry.repo}', and that repository's index does not offer it.\n` +
|
|
406
|
+
` Either the index has not been fetched yet, or the version has been ` +
|
|
407
|
+
`withdrawn upstream. Run 'sous repo list' to see what sous currently has.`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const repoUrl = lock.repos[entry.repo]!.url;
|
|
412
|
+
const provider = requireProvider(repoUrl, undefined, providers);
|
|
413
|
+
const canonical = provider.canonicalize(repoUrl);
|
|
414
|
+
|
|
415
|
+
const workDir = await fsp.mkdtemp(path.join(options.store.root, ".sous-restore-"));
|
|
416
|
+
const fetchDir = path.join(workDir, storeKey.name);
|
|
417
|
+
try {
|
|
418
|
+
await provider.fetchRecipeTree(
|
|
419
|
+
canonical,
|
|
420
|
+
indexRecipe.path,
|
|
421
|
+
version.tag,
|
|
422
|
+
fetchDir,
|
|
423
|
+
options.providerOptions ?? {}
|
|
424
|
+
);
|
|
425
|
+
await options.store.put(storeKey, fetchDir, entry.hash);
|
|
426
|
+
report.restored.push(key);
|
|
427
|
+
} finally {
|
|
428
|
+
await fsp.rm(workDir, { recursive: true, force: true });
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
report.restored.sort();
|
|
433
|
+
report.alreadyPresent.sort();
|
|
434
|
+
return report;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Renders a lockfile exactly as `write` would, without writing it. Used by
|
|
439
|
+
* dry runs, which show what would change and touch nothing.
|
|
440
|
+
*
|
|
441
|
+
* @param lock - The lockfile to render.
|
|
442
|
+
*/
|
|
443
|
+
preview(lock: Lockfile): string {
|
|
444
|
+
return stableJsonStringify(lock);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Rebuilds a recipe map with its keys in sorted order. */
|
|
449
|
+
function sortRecipes(recipes: Record<string, LockedRecipe>): Record<string, LockedRecipe> {
|
|
450
|
+
const sorted: Record<string, LockedRecipe> = {};
|
|
451
|
+
for (const key of Object.keys(recipes).sort()) sorted[key] = recipes[key]!;
|
|
452
|
+
return sorted;
|
|
453
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The real namespace resolver: the one the compiler uses.
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 defined the `NamespaceResolver` contract and shipped an in-memory
|
|
5
|
+
* implementation for tests. This is the implementation backed by the project's
|
|
6
|
+
* actual state: the lockfile says which recipes are in play and at what version,
|
|
7
|
+
* the links map and the store say where each one's files are, and each recipe's
|
|
8
|
+
* own manifest says what it is allowed to address.
|
|
9
|
+
*
|
|
10
|
+
* The scoping rule is the whole point, and it is enforced by handing
|
|
11
|
+
* `StaticNamespaceResolver` the right two maps:
|
|
12
|
+
*
|
|
13
|
+
* - a file inside a recipe may address only that recipe's own declared
|
|
14
|
+
* dependencies (`depends` plus `subscribes`), at their pinned versions;
|
|
15
|
+
* - a file in the project's own templates may address the project's
|
|
16
|
+
* subscriptions.
|
|
17
|
+
*
|
|
18
|
+
* A project with no lockfile entries gets no resolver at all, so a project that
|
|
19
|
+
* uses no repositories behaves exactly as it did before: `~` in an include line
|
|
20
|
+
* means an alias and nothing else.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { Settings } from "../settings.js";
|
|
24
|
+
import {
|
|
25
|
+
StaticNamespaceResolver,
|
|
26
|
+
type NamespaceResolver,
|
|
27
|
+
} from "./namespace-resolver.js";
|
|
28
|
+
import {
|
|
29
|
+
listLockedRecipes,
|
|
30
|
+
projectSubscriptionRefs,
|
|
31
|
+
readRecipeManifestIn,
|
|
32
|
+
type LockedRecipeLocation,
|
|
33
|
+
} from "./locked-recipes.js";
|
|
34
|
+
|
|
35
|
+
/** How the project's namespace resolver is built. */
|
|
36
|
+
export type ProjectNamespaceResolverOptions = {
|
|
37
|
+
/** The project's `.sous/` directory, which holds the lockfile and the links map. */
|
|
38
|
+
sousDir: string;
|
|
39
|
+
/** The merged project config, read for its subscriptions. */
|
|
40
|
+
settings: Settings;
|
|
41
|
+
/** The environment to read; decides where the store and machine-wide links map are. */
|
|
42
|
+
env?: NodeJS.ProcessEnv;
|
|
43
|
+
/** The locked recipes, when the caller has already located them. */
|
|
44
|
+
locked?: LockedRecipeLocation[];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Builds the namespace resolver for a project, or undefined when the project
|
|
49
|
+
* locks no recipes at all.
|
|
50
|
+
*
|
|
51
|
+
* createProjectNamespaceResolver({ sousDir, settings })
|
|
52
|
+
* // -> resolves "@~workflow/task-files/_partials/resume.md" to the pinned
|
|
53
|
+
* // recipe directory, when the project (or the including recipe) may address it
|
|
54
|
+
*
|
|
55
|
+
* @param options - The project's `.sous/` directory, its config and the environment.
|
|
56
|
+
*/
|
|
57
|
+
export function createProjectNamespaceResolver(
|
|
58
|
+
options: ProjectNamespaceResolverOptions
|
|
59
|
+
): NamespaceResolver | undefined {
|
|
60
|
+
const locked =
|
|
61
|
+
options.locked ??
|
|
62
|
+
listLockedRecipes({
|
|
63
|
+
sousDir: options.sousDir,
|
|
64
|
+
...(options.env === undefined ? {} : { env: options.env }),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (locked.length === 0) return undefined;
|
|
68
|
+
|
|
69
|
+
const recipes: Record<string, string> = {};
|
|
70
|
+
const dependencies: Record<string, string[]> = {};
|
|
71
|
+
|
|
72
|
+
for (const recipe of locked) {
|
|
73
|
+
recipes[recipe.key] = recipe.dir;
|
|
74
|
+
|
|
75
|
+
// A recipe declares what it may address in its own manifest. A recipe whose
|
|
76
|
+
// files are not on disk yet declares nothing, which is the conservative
|
|
77
|
+
// answer: it cannot be including anything either.
|
|
78
|
+
const manifest = recipe.present ? readRecipeManifestIn(recipe.dir) : undefined;
|
|
79
|
+
if (manifest === undefined) continue;
|
|
80
|
+
|
|
81
|
+
const declared = [...(manifest.depends ?? []), ...(manifest.subscribes ?? [])];
|
|
82
|
+
if (declared.length > 0) dependencies[recipe.key] = declared;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return new StaticNamespaceResolver({
|
|
86
|
+
recipes,
|
|
87
|
+
dependencies,
|
|
88
|
+
projectScope: projectSubscriptionRefs(options.settings, locked),
|
|
89
|
+
});
|
|
90
|
+
}
|