@sous-io/sous 0.2.2 → 0.2.4
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 +18 -9
- package/docs/markdown/_sidebar.md +2 -0
- package/docs/markdown/commands.md +21 -0
- package/docs/markdown/config-discovery.md +3 -2
- package/docs/markdown/config-inspection.md +4 -1
- package/docs/markdown/configuration.md +2 -1
- package/docs/markdown/repositories-consuming.md +3 -1
- package/docs/markdown/repositories-quickstart.md +4 -1
- package/docs/markdown/repositories-variables.md +33 -14
- package/package.json +1 -1
- package/recipes/core/sous-skills/sous.recipe.yaml +1 -1
- package/src/base-command.ts +76 -8
- package/src/commands/build.ts +3 -80
- package/src/commands/compile.ts +4 -2
- package/src/commands/init.ts +269 -0
- package/src/lib/build-preparation.ts +86 -0
- package/src/lib/build-service.ts +52 -4
- package/src/lib/config-discovery.ts +2 -16
- package/src/lib/markdown-compiler.ts +20 -1
- package/src/lib/project-scaffold/index.ts +251 -0
- package/src/lib/project-scaffold/templates.ts +230 -0
- package/src/lib/repos/links.ts +3 -1
- package/src/lib/repos/recipe-targets.ts +9 -1
- package/src/lib/settings.ts +51 -3
- package/src/lib/vars/answers.ts +184 -0
- package/src/lib/vars/definition-source.ts +9 -0
- package/src/lib/vars/index.ts +1 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sous init` sets a project up for sous.
|
|
3
|
+
*
|
|
4
|
+
* It writes the `.sous/` directory a project needs (a commented primary config,
|
|
5
|
+
* the starter prompt that config compiles, the two answers files and the
|
|
6
|
+
* sous-managed ignore block) and then runs the first build, which is what
|
|
7
|
+
* seeds the `core` recipe and pins it in the lockfile. A project that is
|
|
8
|
+
* already set up is left exactly as it is.
|
|
9
|
+
*
|
|
10
|
+
* This is the one command that runs BEFORE a project config exists, so it
|
|
11
|
+
* opts out of the config requirement every other command inherits. Discovery
|
|
12
|
+
* still runs, which is how `--sous-dir` and `SOUS_DIR` say where to write, and
|
|
13
|
+
* how a run inside an existing project is noticed.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import { Args, Flags } from "@oclif/core";
|
|
18
|
+
import { confirm } from "@inquirer/prompts";
|
|
19
|
+
import { BaseCommand } from "../base-command.js";
|
|
20
|
+
import { prepareRepositoriesForBuild } from "../lib/build-preparation.js";
|
|
21
|
+
import { buildProjectOutputs } from "../lib/build-service.js";
|
|
22
|
+
import {
|
|
23
|
+
CONFIG_FILE_NAMES,
|
|
24
|
+
SOUS_DIR_NAME,
|
|
25
|
+
discoverConfig,
|
|
26
|
+
expandHome,
|
|
27
|
+
resolveConfigFlag,
|
|
28
|
+
} from "../lib/config-discovery.js";
|
|
29
|
+
import { ConfigError } from "../lib/errors.js";
|
|
30
|
+
import { nonInteractiveError } from "../lib/interactive.js";
|
|
31
|
+
import { subscriptionServiceFor } from "../lib/repos/subscription-service.js";
|
|
32
|
+
import {
|
|
33
|
+
PROJECT_CONFIG_FORMATS,
|
|
34
|
+
STARTER_OUTPUT_NAME,
|
|
35
|
+
STARTER_PROMPT_RELATIVE_PATH,
|
|
36
|
+
scaffoldProject,
|
|
37
|
+
sousDirFor,
|
|
38
|
+
type ProjectConfigFormat,
|
|
39
|
+
} from "../lib/project-scaffold/index.js";
|
|
40
|
+
import { SOUS_VERSION } from "../lib/settings.js";
|
|
41
|
+
import { confirmationFlag } from "../utils/flags.js";
|
|
42
|
+
import {
|
|
43
|
+
blankLine,
|
|
44
|
+
dryRunNotice,
|
|
45
|
+
footer,
|
|
46
|
+
heading,
|
|
47
|
+
log,
|
|
48
|
+
paragraph,
|
|
49
|
+
section,
|
|
50
|
+
showCommandVars,
|
|
51
|
+
showVariables,
|
|
52
|
+
warning,
|
|
53
|
+
} from "../utils/formatting.js";
|
|
54
|
+
|
|
55
|
+
export default class Init extends BaseCommand {
|
|
56
|
+
static description =
|
|
57
|
+
"Set a project up for sous: write its .sous/ directory, then run the first build";
|
|
58
|
+
|
|
59
|
+
/** This command creates the config; finding none is its normal case. */
|
|
60
|
+
static override requiresConfig = false;
|
|
61
|
+
|
|
62
|
+
static examples = [
|
|
63
|
+
"<%= config.bin %> init",
|
|
64
|
+
"<%= config.bin %> init ./my-project",
|
|
65
|
+
"<%= config.bin %> init --format json",
|
|
66
|
+
"<%= config.bin %> init --name 'My Project' --no-build",
|
|
67
|
+
"<%= config.bin %> init --dry-run",
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
static args = {
|
|
71
|
+
directory: Args.string({
|
|
72
|
+
description: "Project directory to set up (defaults to the current one)",
|
|
73
|
+
required: false,
|
|
74
|
+
}),
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
static flags = {
|
|
78
|
+
...BaseCommand.baseFlags,
|
|
79
|
+
format: Flags.string({
|
|
80
|
+
description: "Format of the primary config to write",
|
|
81
|
+
options: [...PROJECT_CONFIG_FORMATS],
|
|
82
|
+
default: PROJECT_CONFIG_FORMATS[0],
|
|
83
|
+
}),
|
|
84
|
+
name: Flags.string({
|
|
85
|
+
description: "Display name for the project (defaults to the directory's own name)",
|
|
86
|
+
}),
|
|
87
|
+
// The one question this command can ask: whether to set up a project
|
|
88
|
+
// inside another one.
|
|
89
|
+
yes: confirmationFlag(),
|
|
90
|
+
"dry-run": Flags.boolean({
|
|
91
|
+
description: "Print the files that would be written without writing them",
|
|
92
|
+
default: false,
|
|
93
|
+
}),
|
|
94
|
+
"no-build": Flags.boolean({
|
|
95
|
+
description: "Write the setup without running the first build",
|
|
96
|
+
default: false,
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
async run(): Promise<void> {
|
|
101
|
+
const { args, flags } = await this.parse(Init);
|
|
102
|
+
const dryRun = flags["dry-run"];
|
|
103
|
+
const format = flags.format as ProjectConfigFormat;
|
|
104
|
+
|
|
105
|
+
const sousDir = this.targetSousDir(args.directory);
|
|
106
|
+
const projectRoot = path.dirname(sousDir);
|
|
107
|
+
|
|
108
|
+
showCommandVars({
|
|
109
|
+
Directory: projectRoot,
|
|
110
|
+
Config: path.join(sousDir, `sous.config.${format}`),
|
|
111
|
+
Name: flags.name ?? "(from the directory name)",
|
|
112
|
+
"Dry Run": dryRun,
|
|
113
|
+
Build: !dryRun && !flags["no-build"],
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
await this.confirmNesting(projectRoot, flags.yes);
|
|
117
|
+
|
|
118
|
+
section("Setting up the project");
|
|
119
|
+
|
|
120
|
+
const result = await scaffoldProject({
|
|
121
|
+
sousDir,
|
|
122
|
+
format,
|
|
123
|
+
name: flags.name,
|
|
124
|
+
dryRun,
|
|
125
|
+
sousVersion: SOUS_VERSION,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
for (const file of result.files) {
|
|
129
|
+
if (result.dryRun) dryRunNotice(`would write ${file}`);
|
|
130
|
+
else log(` wrote ${file}`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (result.dryRun) {
|
|
134
|
+
blankLine();
|
|
135
|
+
dryRunNotice("Nothing was written. Run the same command without '--dry-run' to set the project up.");
|
|
136
|
+
footer();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!flags["no-build"]) {
|
|
141
|
+
await this.buildProject(result.configPath);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
section("What was set up");
|
|
145
|
+
showVariables([
|
|
146
|
+
{ label: "Config", value: result.configPath },
|
|
147
|
+
{ label: "Prompt source", value: path.join(sousDir, STARTER_PROMPT_RELATIVE_PATH) },
|
|
148
|
+
{ label: "Compiled to", value: path.join(projectRoot, STARTER_OUTPUT_NAME) },
|
|
149
|
+
{ label: "Skills", value: path.join(projectRoot, ".claude", "skills") },
|
|
150
|
+
{ label: "Shared answers", value: path.join(sousDir, ".env"), detail: "committed" },
|
|
151
|
+
{
|
|
152
|
+
label: "Local answers",
|
|
153
|
+
value: path.join(sousDir, ".env.local"),
|
|
154
|
+
detail: "gitignored; .env.local.example shows the layout",
|
|
155
|
+
},
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
blankLine();
|
|
159
|
+
paragraph(
|
|
160
|
+
` ${STARTER_OUTPUT_NAME} and the skills directory are build output, compiled from the ` +
|
|
161
|
+
`prompt source and from the recipes this project subscribes to; a build recompiles ` +
|
|
162
|
+
`them. The config explains each of its blocks in its own comments.`
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
footer();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Where the `.sous/` directory goes. A directory argument wins; otherwise the
|
|
170
|
+
* config-locating flags say where the project is, and otherwise it is the
|
|
171
|
+
* working directory. A path that already names a `.sous/` directory, or a
|
|
172
|
+
* config file inside one, is honored as such.
|
|
173
|
+
*/
|
|
174
|
+
private targetSousDir(directory: string | undefined): string {
|
|
175
|
+
if (directory !== undefined) {
|
|
176
|
+
return sousDirFor(path.resolve(this.configLocator.cwd, expandHome(directory)));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const primary = this.configLocator.primary;
|
|
180
|
+
if (primary === undefined) return sousDirFor(this.configLocator.cwd);
|
|
181
|
+
|
|
182
|
+
const value = primary.value;
|
|
183
|
+
if (path.basename(value) === SOUS_DIR_NAME) return value;
|
|
184
|
+
if ((CONFIG_FILE_NAMES as readonly string[]).includes(path.basename(value))) {
|
|
185
|
+
return path.dirname(value);
|
|
186
|
+
}
|
|
187
|
+
return sousDirFor(value);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A project set up inside another one is a real choice, not a mistake sous
|
|
192
|
+
* should prevent: a subproject may want its own instructions. So when a walk
|
|
193
|
+
* up from the target finds a config in a parent directory, the facts are
|
|
194
|
+
* stated and the question is asked once; `--yes` answers it ahead of time,
|
|
195
|
+
* and a run with no terminal fails naming that flag.
|
|
196
|
+
*
|
|
197
|
+
* The target's own `.sous/` holding a config is a different case, refused
|
|
198
|
+
* outright by the scaffold.
|
|
199
|
+
*/
|
|
200
|
+
private async confirmNesting(projectRoot: string, confirmed: boolean): Promise<void> {
|
|
201
|
+
const enclosing = discoverConfig(projectRoot);
|
|
202
|
+
if (enclosing === null || path.dirname(enclosing.sousDir) === projectRoot) return;
|
|
203
|
+
|
|
204
|
+
const enclosingRoot = path.dirname(enclosing.sousDir);
|
|
205
|
+
|
|
206
|
+
warning(
|
|
207
|
+
`${projectRoot} is inside a project that is already set up for sous.\n` +
|
|
208
|
+
` The enclosing project's config is ${enclosing.configPath}.\n` +
|
|
209
|
+
` Setting this directory up too gives it a config of its own: commands run ` +
|
|
210
|
+
`here will find this one, and commands run from ${enclosingRoot} will keep ` +
|
|
211
|
+
`finding the other.`
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
if (confirmed) return;
|
|
215
|
+
|
|
216
|
+
if (!this.interactive) {
|
|
217
|
+
throw nonInteractiveError({
|
|
218
|
+
prompt: `whether to set up ${projectRoot} inside the project at ${enclosingRoot}`,
|
|
219
|
+
remedy: "pass --yes (also -y, --force) to set it up anyway",
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
blankLine();
|
|
224
|
+
const proceed = await confirm({
|
|
225
|
+
message: `Set up ${projectRoot} as a project of its own?`,
|
|
226
|
+
default: false,
|
|
227
|
+
});
|
|
228
|
+
if (!proceed) {
|
|
229
|
+
throw new ConfigError(
|
|
230
|
+
`Nothing was written. Run 'sous init' from a directory outside ${enclosingRoot}, ` +
|
|
231
|
+
`or pass --yes to set this one up anyway.`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Adopts the config just written and builds the project with it, in the
|
|
238
|
+
* same two steps `sous build` takes: the repositories are prepared (which
|
|
239
|
+
* seeds the core recipe into the store and pins it in the lockfile), then
|
|
240
|
+
* the outputs are compiled.
|
|
241
|
+
*
|
|
242
|
+
* @param configPath - The primary config the scaffold wrote.
|
|
243
|
+
*/
|
|
244
|
+
private async buildProject(configPath: string): Promise<void> {
|
|
245
|
+
await this.adoptConfig(
|
|
246
|
+
resolveConfigFlag(configPath, this.configLocator.cwd, this.configLocator.confDirOverride)
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
await prepareRepositoriesForBuild(
|
|
250
|
+
subscriptionServiceFor({
|
|
251
|
+
configContext: this.configContext,
|
|
252
|
+
settings: this.settings,
|
|
253
|
+
shellEnv: this.shellEnv,
|
|
254
|
+
})
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
heading("Building the project");
|
|
258
|
+
|
|
259
|
+
const succeeded = await buildProjectOutputs(this.settings, this.configContext);
|
|
260
|
+
|
|
261
|
+
if (!succeeded) {
|
|
262
|
+
throw new ConfigError(
|
|
263
|
+
`The project is set up, but the first build failed, so its outputs may be ` +
|
|
264
|
+
`incomplete. Everything 'sous init' wrote is in place; fix what the build ` +
|
|
265
|
+
`reported above and run 'sous build' again.`
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The step that runs before a project's outputs are compiled, and what it says
|
|
3
|
+
* while it runs. `sous build` runs it on every build, and `sous init` runs it
|
|
4
|
+
* for the first build of a project it has just set up; both must say the same
|
|
5
|
+
* things about the same events, so the reporting lives here rather than in
|
|
6
|
+
* either command.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SubscriptionService } from "./repos/subscription-service.js";
|
|
10
|
+
import { blankLine, footer, heading, paragraph, warning } from "../utils/formatting.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets this project's recipes ready to compile: restores whatever the store is
|
|
14
|
+
* missing (a fresh clone, or a collected store) and then asks upstream for the
|
|
15
|
+
* repositories that prefer a newer in-range version.
|
|
16
|
+
*
|
|
17
|
+
* Restoring asks nothing and decides nothing; it fetches exactly what the
|
|
18
|
+
* lockfile pins. An upstream check that fails is reported and then ignored,
|
|
19
|
+
* because a build must not depend on the network being up.
|
|
20
|
+
*
|
|
21
|
+
* @param repositories - The subscription service for this project.
|
|
22
|
+
*/
|
|
23
|
+
export async function prepareRepositoriesForBuild(
|
|
24
|
+
repositories: SubscriptionService
|
|
25
|
+
): Promise<void> {
|
|
26
|
+
const needsRestore = repositories.needsRestore();
|
|
27
|
+
if (needsRestore) {
|
|
28
|
+
heading("Restoring recipes");
|
|
29
|
+
blankLine();
|
|
30
|
+
paragraph(
|
|
31
|
+
"This project's lockfile pins recipes that are not in the store on this " +
|
|
32
|
+
"machine, so they are being fetched at exactly the versions it records."
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const { seed, subscriptions, restored, upstream } =
|
|
37
|
+
await repositories.prepareForBuild();
|
|
38
|
+
|
|
39
|
+
// Seeding the packaged core recipe is silent when it works, which is almost
|
|
40
|
+
// always; it is only worth a word when it could not be done at all.
|
|
41
|
+
if (seed.skippedBecause !== undefined) warning(seed.skippedBecause);
|
|
42
|
+
|
|
43
|
+
// A subscription the lockfile did not pin yet has just been pinned. That is
|
|
44
|
+
// a change to a committed file, so it is always announced.
|
|
45
|
+
if (subscriptions.added.length > 0 || subscriptions.moved.length > 0) {
|
|
46
|
+
heading("Locking subscribed recipes");
|
|
47
|
+
blankLine();
|
|
48
|
+
for (const entry of subscriptions.added) {
|
|
49
|
+
paragraph(` pinned: ${entry.key} at version ${entry.version}.`);
|
|
50
|
+
}
|
|
51
|
+
for (const change of subscriptions.moved) {
|
|
52
|
+
paragraph(` ${change.key} moved from version ${change.from} to version ${change.to}.`);
|
|
53
|
+
}
|
|
54
|
+
blankLine();
|
|
55
|
+
paragraph(
|
|
56
|
+
"The lockfile has been updated. Commit it, so everyone building this project " +
|
|
57
|
+
"gets exactly these versions."
|
|
58
|
+
);
|
|
59
|
+
footer();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const failure of subscriptions.failed) {
|
|
63
|
+
warning(
|
|
64
|
+
`Sous could not work out which version of '${failure.key}' to use, so nothing ` +
|
|
65
|
+
`from it was compiled.\n${failure.reason}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (restored !== undefined && restored.restored.length > 0) {
|
|
70
|
+
blankLine();
|
|
71
|
+
for (const key of restored.restored) paragraph(` restored: ${key}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const change of upstream.updated) {
|
|
75
|
+
paragraph(` ${change.key} moved from ${change.from} to ${change.to}.`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const failure of upstream.failed) {
|
|
79
|
+
warning(
|
|
80
|
+
`Sous could not check the repository '${failure.repo}' for a newer version, so ` +
|
|
81
|
+
`this build uses the versions it already had.\n${failure.reason}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (needsRestore) footer();
|
|
86
|
+
}
|
package/src/lib/build-service.ts
CHANGED
|
@@ -11,6 +11,12 @@ import type { CompilationConfig, CompilationTarget } from "./markdown-compiler.j
|
|
|
11
11
|
import { StateService } from "./state.js";
|
|
12
12
|
import { isProtectedPath } from "./state.js";
|
|
13
13
|
import { protectedRepoPaths } from "./repos/links.js";
|
|
14
|
+
import {
|
|
15
|
+
noRecipeAnswers,
|
|
16
|
+
resolveRecipeAnswers,
|
|
17
|
+
unansweredWarning,
|
|
18
|
+
type RecipeAnswers,
|
|
19
|
+
} from "./vars/answers.js";
|
|
14
20
|
import { log, warning } from "../utils/formatting.js";
|
|
15
21
|
|
|
16
22
|
export type BuildOptions = {
|
|
@@ -50,26 +56,65 @@ const NO_RECIPE_TARGETS: RecipeTargets = {
|
|
|
50
56
|
warnings: [],
|
|
51
57
|
};
|
|
52
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The answers to every recipe variable in play for the project the context
|
|
61
|
+
* describes, resolved once so a build can render with them, hand them to every
|
|
62
|
+
* scope it builds, and report what is still unanswered. Empty when the caller
|
|
63
|
+
* gave no config context.
|
|
64
|
+
*
|
|
65
|
+
* @param settings - The merged project config.
|
|
66
|
+
* @param configContext - Where the active config was discovered.
|
|
67
|
+
*/
|
|
68
|
+
export function resolveProjectAnswers(
|
|
69
|
+
settings: Settings,
|
|
70
|
+
configContext?: ConfigContext
|
|
71
|
+
): RecipeAnswers {
|
|
72
|
+
if (configContext === undefined) return noRecipeAnswers();
|
|
73
|
+
return resolveRecipeAnswers({ settings, sousDir: configContext.sousDir });
|
|
74
|
+
}
|
|
75
|
+
|
|
53
76
|
/**
|
|
54
77
|
* The compile targets a project's subscribed recipes contribute, for the project
|
|
55
78
|
* the options describe. Empty when the caller gave no config context, which is
|
|
56
79
|
* the case only in tests that build a settings object by hand.
|
|
57
80
|
*
|
|
81
|
+
* Each recipe's files render with that recipe's own view of the answers, and a
|
|
82
|
+
* required variable nothing answered is reported through `warnings`, so every
|
|
83
|
+
* caller that prints those tells the user what the build could not fill in.
|
|
84
|
+
*
|
|
58
85
|
* @param settings - The merged project config.
|
|
59
86
|
* @param rootScope - The resolved settings scope, for `${var}` in destinations.
|
|
60
87
|
* @param configContext - Where the active config was discovered.
|
|
88
|
+
* @param answers - The project's recipe answers, when the caller resolved them
|
|
89
|
+
* already; resolved here otherwise.
|
|
61
90
|
*/
|
|
62
91
|
export function resolveRecipeTargets(
|
|
63
92
|
settings: Settings,
|
|
64
93
|
rootScope: Record<string, string>,
|
|
65
|
-
configContext?: ConfigContext
|
|
94
|
+
configContext?: ConfigContext,
|
|
95
|
+
answers?: RecipeAnswers
|
|
66
96
|
): RecipeTargets {
|
|
67
97
|
if (configContext === undefined) return NO_RECIPE_TARGETS;
|
|
68
|
-
|
|
98
|
+
const resolved = answers ?? resolveProjectAnswers(settings, configContext);
|
|
99
|
+
const scopes = new Map<string, Record<string, string>>();
|
|
100
|
+
|
|
101
|
+
const recipes = buildRecipeTargets({
|
|
69
102
|
sousDir: configContext.sousDir,
|
|
70
103
|
settings,
|
|
71
104
|
scope: rootScope,
|
|
105
|
+
scopeFor: (recipe) => {
|
|
106
|
+
let scope = scopes.get(recipe.key);
|
|
107
|
+
if (scope === undefined) {
|
|
108
|
+
scope = resolveRootScope(settings, configContext, { answers: resolved, recipe: recipe.key });
|
|
109
|
+
scopes.set(recipe.key, scope);
|
|
110
|
+
}
|
|
111
|
+
return scope;
|
|
112
|
+
},
|
|
72
113
|
});
|
|
114
|
+
|
|
115
|
+
const missing = unansweredWarning(resolved, rootScope);
|
|
116
|
+
if (missing !== undefined) recipes.warnings.push(missing);
|
|
117
|
+
return recipes;
|
|
73
118
|
}
|
|
74
119
|
|
|
75
120
|
/**
|
|
@@ -232,7 +277,10 @@ export class BuildService {
|
|
|
232
277
|
* Returns true if all steps succeeded.
|
|
233
278
|
*/
|
|
234
279
|
async build(settings: Settings, options: BuildOptions = {}): Promise<boolean> {
|
|
235
|
-
|
|
280
|
+
// The recipe answers are resolved once here and handed to every scope the
|
|
281
|
+
// build assembles, so the lockfile and the manifests are read one time.
|
|
282
|
+
const answers = resolveProjectAnswers(settings, options.configContext);
|
|
283
|
+
const rootScope = resolveRootScope(settings, options.configContext, { answers });
|
|
236
284
|
const namespaceResolver = resolveNamespaceResolver(settings, options);
|
|
237
285
|
const protectedPaths = protectedPathsFor(options);
|
|
238
286
|
|
|
@@ -260,7 +308,7 @@ export class BuildService {
|
|
|
260
308
|
// targets alongside its own, so a recipe's files are compiled by exactly the
|
|
261
309
|
// same machinery as everything else, and are pruned and cleared by it too.
|
|
262
310
|
if (!options.noCompile) {
|
|
263
|
-
const recipes = resolveRecipeTargets(settings, rootScope, options.configContext);
|
|
311
|
+
const recipes = resolveRecipeTargets(settings, rootScope, options.configContext, answers);
|
|
264
312
|
for (const notice of recipes.warnings) warning(notice);
|
|
265
313
|
|
|
266
314
|
const config = withRecipeTargets(
|
|
@@ -371,22 +371,8 @@ export function formatNotFoundMessage(startDir: string = process.cwd()): string
|
|
|
371
371
|
checked + more,
|
|
372
372
|
"",
|
|
373
373
|
" To fix this, either:",
|
|
374
|
-
` 1.
|
|
374
|
+
` 1. Set the project up: run 'sous init' in its root directory, which writes`,
|
|
375
|
+
` ${SOUS_DIR_NAME}/${CONFIG_FILE_NAMES[0]} and everything else a first build needs, or`,
|
|
375
376
|
" 2. Pass the config explicitly: sous <command> --config <path>",
|
|
376
|
-
"",
|
|
377
|
-
` A minimal ${CONFIG_FILE_NAMES[0]}:`,
|
|
378
|
-
"",
|
|
379
|
-
" export const config = {",
|
|
380
|
-
' name: "My Project",',
|
|
381
|
-
' _vars: { projectRoot: "${sousDir}/.." },',
|
|
382
|
-
" compilation: {",
|
|
383
|
-
" targets: [",
|
|
384
|
-
" {",
|
|
385
|
-
' entryPoint: "${sousDir}/AGENTS.md",',
|
|
386
|
-
' outputs: [{ destinationFile: "${projectRoot}/AGENTS.md" }],',
|
|
387
|
-
" },",
|
|
388
|
-
" ],",
|
|
389
|
-
" },",
|
|
390
|
-
" };",
|
|
391
377
|
].join("\n");
|
|
392
378
|
}
|
|
@@ -129,6 +129,17 @@ export function inferGlobBase(pattern: string): string {
|
|
|
129
129
|
return joined || "/";
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/**
|
|
133
|
+
* A stable text form of an output's variable scope, for the source hash of a
|
|
134
|
+
* rendered output. Keys are sorted so two scopes holding the same values hash
|
|
135
|
+
* the same whatever order they were assembled in.
|
|
136
|
+
*
|
|
137
|
+
* @param vars - The variable scope an output renders with.
|
|
138
|
+
*/
|
|
139
|
+
export function stableVarsFingerprint(vars: Record<string, string>): string {
|
|
140
|
+
return JSON.stringify(Object.keys(vars).sort().map((key) => [key, vars[key]]));
|
|
141
|
+
}
|
|
142
|
+
|
|
132
143
|
export class CompilationService {
|
|
133
144
|
private strict: boolean;
|
|
134
145
|
private rebuild: boolean;
|
|
@@ -447,7 +458,7 @@ ${taskFileContents}
|
|
|
447
458
|
}
|
|
448
459
|
|
|
449
460
|
// Compute source hash once per target from the assembled content
|
|
450
|
-
const
|
|
461
|
+
const contentHash = hashContent(content);
|
|
451
462
|
|
|
452
463
|
let allSucceeded = true;
|
|
453
464
|
|
|
@@ -462,6 +473,14 @@ ${taskFileContents}
|
|
|
462
473
|
if (resolvedDest === undefined) continue;
|
|
463
474
|
destFile = resolvedDest;
|
|
464
475
|
|
|
476
|
+
// A rendered output depends on its variables as much as on its source: a
|
|
477
|
+
// changed answer or `_vars` value with the same template must re-render,
|
|
478
|
+
// so the variable scope is part of a `.tpl.` output's source hash. A
|
|
479
|
+
// verbatim copy hashes its content alone.
|
|
480
|
+
const srcHash = isTpl && output.vars
|
|
481
|
+
? hashContent(`${content}\n${stableVarsFingerprint(output.vars)}`)
|
|
482
|
+
: contentHash;
|
|
483
|
+
|
|
465
484
|
// Skip if content is unchanged and file already exists (unless --rebuild)
|
|
466
485
|
const existingEntry = state.files.find(f => f.dest === destFile);
|
|
467
486
|
if (
|