@sous-io/sous 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/README.md +115 -35
  2. package/bin/run.js +10 -1
  3. package/docs/markdown/README.md +27 -0
  4. package/docs/markdown/_sidebar.md +18 -0
  5. package/docs/markdown/commands.md +308 -0
  6. package/docs/markdown/config-discovery.md +74 -0
  7. package/docs/markdown/config-inspection.md +69 -0
  8. package/docs/markdown/config-layers.md +92 -0
  9. package/docs/markdown/config-variables.md +79 -0
  10. package/docs/markdown/configuration.md +71 -0
  11. package/docs/markdown/design-principles.md +59 -0
  12. package/docs/markdown/repositories-authoring.md +408 -0
  13. package/docs/markdown/repositories-consuming.md +580 -0
  14. package/docs/markdown/repositories-file-formats.md +1084 -0
  15. package/docs/markdown/repositories-variables.md +387 -0
  16. package/docs/markdown/repositories.md +303 -0
  17. package/docs/markdown/skill-categories.md +58 -0
  18. package/package.json +72 -8
  19. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/SKILL.tpl.md +20 -20
  20. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/about-something.md +2 -2
  21. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/examples/do-something.md +1 -1
  22. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/advanced-patterns.md +6 -6
  23. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/commands.md +5 -5
  24. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/frontmatter.md +3 -3
  25. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/SKILL.tpl.md +40 -25
  26. package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +70 -0
  27. package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +75 -0
  28. package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/create-skill/SKILL.tpl.md +8 -9
  29. package/recipes/core/sous-skills/sous.recipe.yaml +45 -0
  30. package/sous.config.schema.json +337 -0
  31. package/src/base-command.ts +220 -67
  32. package/src/commands/build.ts +150 -73
  33. package/src/commands/clear.ts +23 -15
  34. package/src/commands/compile.ts +74 -16
  35. package/src/commands/config/get.ts +110 -0
  36. package/src/commands/config/show.ts +32 -0
  37. package/src/commands/config/validate.ts +53 -0
  38. package/src/commands/help.ts +46 -0
  39. package/src/commands/launch.ts +36 -14
  40. package/src/commands/lock/rebuild.ts +241 -0
  41. package/src/commands/lock/show.ts +115 -0
  42. package/src/commands/namespace/list.ts +117 -0
  43. package/src/commands/namespace/show.ts +110 -0
  44. package/src/commands/prune.ts +3 -11
  45. package/src/commands/recipe/list.ts +95 -0
  46. package/src/commands/recipe/show.ts +301 -0
  47. package/src/commands/repo/add.ts +145 -0
  48. package/src/commands/repo/gc.ts +172 -0
  49. package/src/commands/repo/init.ts +136 -0
  50. package/src/commands/repo/link.ts +500 -0
  51. package/src/commands/repo/list.ts +179 -0
  52. package/src/commands/repo/release.ts +619 -0
  53. package/src/commands/repo/remove.ts +193 -0
  54. package/src/commands/repo/search.ts +189 -0
  55. package/src/commands/repo/submit.ts +133 -0
  56. package/src/commands/repo/unlink.ts +147 -0
  57. package/src/commands/subscription/add.ts +285 -0
  58. package/src/commands/subscription/list.ts +129 -0
  59. package/src/commands/subscription/remove.ts +181 -0
  60. package/src/commands/vars/ask.ts +374 -0
  61. package/src/commands/vars/index.ts +79 -0
  62. package/src/commands/vars/list.ts +67 -0
  63. package/src/commands/vars/show.ts +77 -0
  64. package/src/config-command.ts +30 -0
  65. package/src/lib/build-service.ts +206 -54
  66. package/src/lib/config-discovery.ts +220 -27
  67. package/src/lib/config-inspect.ts +145 -0
  68. package/src/lib/config-kernel.mjs +377 -0
  69. package/src/lib/config-schema.ts +361 -0
  70. package/src/lib/env-file.ts +328 -0
  71. package/src/lib/env-local.ts +18 -1
  72. package/src/lib/errors.ts +32 -0
  73. package/src/lib/include-resolver.ts +108 -15
  74. package/src/lib/interactive.ts +165 -0
  75. package/src/lib/markdown-compiler.ts +118 -37
  76. package/src/lib/package-info.ts +25 -0
  77. package/src/lib/pid-service.ts +32 -21
  78. package/src/lib/refs/find.ts +589 -0
  79. package/src/lib/refs/index.ts +12 -0
  80. package/src/lib/refs/pick.ts +147 -0
  81. package/src/lib/refs/scopes.ts +61 -0
  82. package/src/lib/repos/catalog-display.ts +116 -0
  83. package/src/lib/repos/catalog-inputs.ts +160 -0
  84. package/src/lib/repos/catalog.ts +722 -0
  85. package/src/lib/repos/core-recipe.ts +105 -0
  86. package/src/lib/repos/defaults.ts +175 -0
  87. package/src/lib/repos/formats/common.ts +389 -0
  88. package/src/lib/repos/formats/index-file.ts +215 -0
  89. package/src/lib/repos/formats/links-map.ts +96 -0
  90. package/src/lib/repos/formats/lockfile.ts +167 -0
  91. package/src/lib/repos/formats/patterns.ts +57 -0
  92. package/src/lib/repos/formats/recipe-manifest.ts +395 -0
  93. package/src/lib/repos/formats/repo-manifest.ts +88 -0
  94. package/src/lib/repos/formats/store-entry.ts +84 -0
  95. package/src/lib/repos/freshness.ts +208 -0
  96. package/src/lib/repos/git-clone.ts +312 -0
  97. package/src/lib/repos/identity.ts +89 -0
  98. package/src/lib/repos/index.ts +58 -0
  99. package/src/lib/repos/links.ts +353 -0
  100. package/src/lib/repos/load-manifest.ts +236 -0
  101. package/src/lib/repos/lock-service.ts +453 -0
  102. package/src/lib/repos/locked-namespace-resolver.ts +90 -0
  103. package/src/lib/repos/locked-recipes.ts +254 -0
  104. package/src/lib/repos/managed-layer.ts +422 -0
  105. package/src/lib/repos/namespace-resolver.ts +370 -0
  106. package/src/lib/repos/providers/base.ts +206 -0
  107. package/src/lib/repos/providers/git.ts +233 -0
  108. package/src/lib/repos/providers/github.ts +294 -0
  109. package/src/lib/repos/providers/gitlab.ts +263 -0
  110. package/src/lib/repos/providers/http.ts +102 -0
  111. package/src/lib/repos/providers/index-cache.ts +382 -0
  112. package/src/lib/repos/providers/index.ts +106 -0
  113. package/src/lib/repos/providers/local.ts +391 -0
  114. package/src/lib/repos/providers/provider.ts +401 -0
  115. package/src/lib/repos/recipe-config-layers.ts +287 -0
  116. package/src/lib/repos/recipe-targets.ts +223 -0
  117. package/src/lib/repos/ref-search.ts +46 -0
  118. package/src/lib/repos/ref.ts +513 -0
  119. package/src/lib/repos/reference-report.ts +122 -0
  120. package/src/lib/repos/release/bump.ts +161 -0
  121. package/src/lib/repos/release/git-state.ts +305 -0
  122. package/src/lib/repos/release/index-builder.ts +635 -0
  123. package/src/lib/repos/release/index.ts +16 -0
  124. package/src/lib/repos/release/plan.ts +512 -0
  125. package/src/lib/repos/release/submit-service.ts +496 -0
  126. package/src/lib/repos/release/tags.ts +243 -0
  127. package/src/lib/repos/release/validate.ts +463 -0
  128. package/src/lib/repos/resolver.ts +789 -0
  129. package/src/lib/repos/scaffold/index.ts +238 -0
  130. package/src/lib/repos/scaffold/templates.ts +413 -0
  131. package/src/lib/repos/seed.ts +414 -0
  132. package/src/lib/repos/store/contract.ts +64 -0
  133. package/src/lib/repos/store/hash.ts +114 -0
  134. package/src/lib/repos/store/recipe-store.ts +599 -0
  135. package/src/lib/repos/store/settings.ts +58 -0
  136. package/src/lib/repos/subscription-service.ts +2678 -0
  137. package/src/lib/repos/trust.ts +447 -0
  138. package/src/lib/settings.ts +546 -189
  139. package/src/lib/sous-home.ts +104 -0
  140. package/src/lib/state.ts +52 -20
  141. package/src/lib/vars/ask.ts +1152 -0
  142. package/src/lib/vars/definition-source.ts +252 -0
  143. package/src/lib/vars/display.ts +233 -0
  144. package/src/lib/vars/index.ts +18 -0
  145. package/src/lib/vars/ladder.ts +282 -0
  146. package/src/lib/vars/mappings.ts +265 -0
  147. package/src/lib/vars/names.ts +94 -0
  148. package/src/lib/vars/preanswers.ts +395 -0
  149. package/src/lib/vars/question-plan.ts +218 -0
  150. package/src/lib/vars/report.ts +228 -0
  151. package/src/lib/vars/safe-regex.ts +235 -0
  152. package/src/lib/vars/validate.ts +312 -0
  153. package/src/lib/watch-loop.ts +148 -0
  154. package/src/templating/init-liquid-engine.ts +58 -16
  155. package/src/utils/choice-prompt.ts +143 -0
  156. package/src/utils/command-errors.ts +186 -0
  157. package/src/utils/command-help.ts +45 -0
  158. package/src/utils/confirm-prompt.ts +110 -0
  159. package/src/utils/flags.ts +153 -0
  160. package/src/utils/formatting.ts +540 -55
  161. package/src/utils/prompts.ts +35 -1
  162. package/src/utils/sous-directory.ts +245 -0
  163. package/src/utils/table.ts +603 -0
  164. package/src/utils/value-prompt.ts +119 -0
  165. package/shared-prompts/_partials/resume-task.md +0 -51
  166. package/shared-prompts/_partials/sub-agent-delegation.md +0 -32
  167. package/shared-prompts/_partials/update-task-file.md +0 -52
  168. package/shared-prompts/memories/automated-browser-tasks/INDEX.tpl.md +0 -52
  169. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/SKILL.tpl.md +0 -102
  170. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/auth-failure-handling.mjs +0 -81
  171. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/chained-workflow.mjs +0 -126
  172. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/examples/simple-fetch.mjs +0 -92
  173. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/architecture.md +0 -61
  174. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/auth-and-sessions.md +0 -65
  175. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/ctx-api.md +0 -96
  176. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/installation.md +0 -104
  177. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/references/script-conventions.md +0 -243
  178. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/chrome-state.mjs +0 -148
  179. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.mjs +0 -383
  180. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/debug.spec.mjs +0 -267
  181. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/eslint.config.mjs +0 -56
  182. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/harness.mjs +0 -169
  183. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/keyring.mjs +0 -59
  184. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/logger.mjs +0 -25
  185. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/params.mjs +0 -140
  186. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/run.mjs +0 -140
  187. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/settings.tpl.mjs +0 -1
  188. package/shared-prompts/skills/automated-browser-tasks/about-automated-browser-tasks/scripts/utils.mjs +0 -185
  189. package/shared-prompts/skills/automated-browser-tasks/create-automated-browser-task/SKILL.tpl.md +0 -52
  190. package/shared-prompts/skills/automated-browser-tasks/running-automated-browser-tasks/SKILL.tpl.md +0 -59
  191. package/shared-prompts/skills/automated-browser-tasks/update-automated-browser-task/SKILL.tpl.md +0 -47
  192. package/shared-prompts/skills/control-flow/approve/SKILL.tpl.md +0 -26
  193. package/shared-prompts/skills/control-flow/opine/SKILL.tpl.md +0 -58
  194. package/shared-prompts/skills/control-flow/repeat/SKILL.tpl.md +0 -27
  195. package/shared-prompts/skills/control-flow/research/SKILL.tpl.md +0 -34
  196. package/shared-prompts/skills/sous-skills/about-sous/SKILL.tpl.md +0 -51
  197. package/shared-prompts/skills/task-files/about-task-files/SKILL.tpl.md +0 -122
  198. package/shared-prompts/skills/task-files/continue-task-in-new-branch/SKILL.tpl.md +0 -80
  199. package/shared-prompts/skills/task-files/go/SKILL.tpl.md +0 -14
  200. package/shared-prompts/skills/task-files/resume-task/SKILL.tpl.md +0 -13
  201. package/shared-prompts/skills/task-files/start-task/SKILL.tpl.md +0 -93
  202. package/shared-prompts/skills/task-files/update/SKILL.tpl.md +0 -14
  203. package/shared-prompts/skills/task-files/update-task-file/SKILL.tpl.md +0 -13
  204. /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-agent-skills/references/substitutions.md +0 -0
  205. /package/{shared-prompts/skills/sous-skills → recipes/core/sous-skills/skills}/about-liquid-templates/references/liquid-filters.md +0 -0
@@ -1,43 +1,63 @@
1
+ import path from "node:path";
1
2
  import { Command, Flags } from "@oclif/core";
2
3
  import {
3
4
  discoverConfig,
5
+ expandHome,
4
6
  formatNotFoundMessage,
7
+ refreshDiscoveredConfig,
5
8
  resolveConfigFlag,
6
9
  type DiscoveredConfig,
7
10
  } from "./lib/config-discovery.js";
8
11
  import { loadEnvFiles } from "./lib/env-local.js";
9
- import {
10
- isConfigError,
11
- loadSettings,
12
- type ConfigContext,
13
- type RawProject,
14
- type Settings,
15
- } from "./lib/settings.js";
16
- import { displayError, displayErrorBlock, header } from "./utils/formatting.js";
12
+ import { loadSettings, type ConfigContext, type Settings } from "./lib/settings.js";
13
+ import { isInteractive } from "./lib/interactive.js";
14
+ import { displayError, displayErrorBlock, header, log, warning } from "./utils/formatting.js";
15
+ import { nonInteractiveFlag } from "./utils/flags.js";
16
+ import { reportCommandError } from "./utils/command-errors.js";
17
17
 
18
18
  /**
19
19
  * Base class for all CLI commands.
20
20
  *
21
21
  * Startup sequence:
22
- * 1. Locate the config — `--config <path>` wins, otherwise walk up from cwd
23
- * looking for a `.sous/` directory holding sous.config.{js,mjs,json}.
22
+ * 1. Locate the config. Primary-config precedence, highest first:
23
+ * `--config`/`-c`/`--sous-config` flag > `SOUS_CONFIG` env >
24
+ * `--sous-dir` flag > `SOUS_DIR` env > walk up from cwd looking for a
25
+ * `.sous/` directory holding sous.config.{js,mjs,json,yaml}. The conf.d
26
+ * drop-in directory precedence is: `--sous-confd` flag > `SOUS_CONFD` env >
27
+ * `<sousDir>/conf.d`. Env vars are read from the REAL environment only
28
+ * (never `.env.local`), because they decide where `.env.local` lives.
24
29
  * 2. Load `<.sous>/.env.local`, then `<.sous>/.env`, into process.env (never
25
30
  * overwriting real env vars). Precedence: shell > .env.local > .env.
26
- * 3. Load the config file. Variable resolution happens later, per command.
31
+ * 3. Load every config layer (primary + conf.d) through the config kernel and
32
+ * deep-merge them. Variable resolution happens later, per command.
27
33
  *
28
34
  * There is no user-level config: nothing is read from `~/.sous`.
35
+ *
36
+ * Every command also carries `--non-interactive`, which tells sous never to ask
37
+ * a question: a run that would have prompted fails instead, naming the prompt
38
+ * and the flag (or environment variables) that would have answered it, and this
39
+ * class prints the command's own help underneath that error. The rule itself
40
+ * lives in `lib/interactive.ts`, which also treats a truthy `CI` and a
41
+ * non-terminal stdin or stdout the same way.
29
42
  */
30
43
  export abstract class BaseCommand extends Command {
31
44
  static baseFlags = {
32
- project: Flags.string({
33
- char: "p",
34
- description: "Project key to operate on",
35
- }),
36
45
  config: Flags.string({
37
46
  char: "c",
38
47
  description:
39
48
  "Path to a sous config file (or a directory containing one). Overrides .sous/ discovery",
40
49
  }),
50
+ "sous-config": Flags.string({
51
+ description:
52
+ "Alias of --config: path to a sous config file (or a directory containing one)",
53
+ }),
54
+ "sous-dir": Flags.string({
55
+ description: "Path to the .sous directory to use (overrides walk-up discovery)",
56
+ }),
57
+ "sous-confd": Flags.string({
58
+ description: "Path to the conf.d drop-in layer directory (overrides <sousDir>/conf.d)",
59
+ }),
60
+ "non-interactive": nonInteractiveFlag(),
41
61
  };
42
62
 
43
63
  protected settings!: Settings;
@@ -48,29 +68,114 @@ export abstract class BaseCommand extends Command {
48
68
  /** The full discovery result, including how the config was located. */
49
69
  protected discovered!: DiscoveredConfig;
50
70
 
71
+ /**
72
+ * The real shell environment, snapshotted BEFORE the `.sous/` env files are
73
+ * injected into `process.env`. The variables layer needs it to tell a value
74
+ * the shell supplied from one an env file supplied; after injection the two
75
+ * are indistinguishable.
76
+ */
77
+ protected shellEnv: NodeJS.ProcessEnv = {};
78
+
79
+ /**
80
+ * Emits the decorative CLI header during init(). The default writes it to
81
+ * stdout. Commands whose stdout must stay machine-readable (the `sous config *`
82
+ * JSON commands) override this to route the banner to stderr.
83
+ */
84
+ protected emitHeader(): void {
85
+ header();
86
+ }
87
+
88
+ /**
89
+ * Line sink for error rendering during init()/catch(). Defaults to stdout (via
90
+ * `log`). Commands whose stdout must stay machine-readable (the `sous config *`
91
+ * JSON commands) override this to route error text to stderr, so a broken
92
+ * config never corrupts a piped stdout stream (e.g. `sous config show | jq`).
93
+ */
94
+ protected errorSink: (line: string) => void = log;
95
+
96
+ /**
97
+ * Whether this run may ask the user a question. One rule, shared by every
98
+ * prompt in sous: see `lib/interactive.ts`.
99
+ */
100
+ protected get interactive(): boolean {
101
+ return isInteractive();
102
+ }
103
+
51
104
  async init(): Promise<void> {
52
105
  await super.init();
53
- header();
106
+ this.emitHeader();
107
+
108
+ // Read config-locating flags off argv directly. oclif's parse() runs inside
109
+ // each command's run(), which is too late: the config (and thus the env
110
+ // files) must be located before any variable resolution. `SOUS_*` env vars
111
+ // are read from the REAL environment here, BEFORE loadEnvFiles below, because
112
+ // they decide where `.env.local` itself lives.
113
+ const cwd = process.cwd();
114
+ const configFlag = blankToUndefined(readConfigFlagFromArgv(this.argv));
115
+ const sousConfigFlag = blankToUndefined(readLongFlagFromArgv(this.argv, "sous-config"));
116
+ const sousDirFlag = blankToUndefined(readLongFlagFromArgv(this.argv, "sous-dir"));
117
+ const sousConfdFlag = blankToUndefined(readLongFlagFromArgv(this.argv, "sous-confd"));
54
118
 
55
- // Read --config off argv directly. oclif's parse() runs inside each command's
56
- // run(), which is too late: env vars must be injected before any resolution.
57
- const configFlag = readConfigFlagFromArgv(this.argv);
119
+ // Empty-string env vars are coerced to undefined and treated as unset. A
120
+ // bare `export SOUS_CONFD=` (or a variable that expands empty) must not turn
121
+ // cwd into the conf.d dir, and an empty SOUS_CONFIG/SOUS_DIR must not mask a
122
+ // lower-precedence source or disable walk-up discovery.
123
+ const sousConfdEnv = blankToUndefined(process.env.SOUS_CONFD);
124
+ const sousConfigEnv = blankToUndefined(process.env.SOUS_CONFIG);
125
+ const sousDirEnv = blankToUndefined(process.env.SOUS_DIR);
126
+
127
+ // conf.d directory: --sous-confd flag > SOUS_CONFD env > <sousDir>/conf.d.
128
+ const confdRaw = sousConfdFlag ?? sousConfdEnv;
129
+ const confDirOverride =
130
+ confdRaw !== undefined ? path.resolve(cwd, expandHome(confdRaw)) : undefined;
131
+
132
+ // Primary config, highest precedence first: --config/-c/--sous-config flag,
133
+ // SOUS_CONFIG env, --sous-dir flag, SOUS_DIR env. All resolve with the same
134
+ // rules as --config (a file, or a directory holding/containing a config).
135
+ // The paired source label is carried through to resolveConfigFlag so an
136
+ // error names the source the user actually set (not always `--config`).
137
+ const primaryCandidates: [string | undefined, string][] = [
138
+ [configFlag, "--config"],
139
+ [sousConfigFlag, "--sous-config"],
140
+ [sousConfigEnv, "SOUS_CONFIG"],
141
+ [sousDirFlag, "--sous-dir"],
142
+ [sousDirEnv, "SOUS_DIR"],
143
+ ];
144
+ const primary = primaryCandidates.find(([value]) => value !== undefined);
58
145
 
59
146
  let discovered: DiscoveredConfig | null;
60
147
 
61
- if (configFlag !== undefined) {
148
+ if (primary !== undefined) {
149
+ const [primarySource, sourceLabel] = primary as [string, string];
62
150
  try {
63
- discovered = resolveConfigFlag(configFlag);
151
+ discovered = resolveConfigFlag(primarySource, cwd, confDirOverride, sourceLabel);
64
152
  } catch (error) {
65
- displayError(error instanceof Error ? error.message : String(error));
153
+ displayError(error instanceof Error ? error.message : String(error), this.errorSink);
66
154
  return this.exit(1);
67
155
  }
68
156
  } else {
69
- discovered = discoverConfig();
157
+ discovered = discoverConfig(cwd, confDirOverride);
70
158
  }
71
159
 
72
160
  if (!discovered) {
73
- displayErrorBlock(formatNotFoundMessage());
161
+ displayErrorBlock(formatNotFoundMessage(), this.errorSink);
162
+ return this.exit(1);
163
+ }
164
+
165
+ // Inject .sous/.env.local and .sous/.env before anything resolves variables,
166
+ // keeping a copy of what the shell itself set so the variables layer can
167
+ // still tell the two apart.
168
+ this.shellEnv = { ...process.env };
169
+ loadEnvFiles(discovered.sousDir);
170
+
171
+ // Enumerated a second time now that the env files are loaded: `SOUS_HOME` is
172
+ // file-settable, and it decides where the store holding a subscribed
173
+ // recipe's config layers is. A first pass already ran during discovery, when
174
+ // only the real environment was known.
175
+ try {
176
+ discovered = refreshDiscoveredConfig(discovered);
177
+ } catch (error) {
178
+ displayErrorBlock(error instanceof Error ? error.message : String(error), this.errorSink);
74
179
  return this.exit(1);
75
180
  }
76
181
 
@@ -78,69 +183,70 @@ export abstract class BaseCommand extends Command {
78
183
  this.configContext = {
79
184
  sousDir: discovered.sousDir,
80
185
  configPath: discovered.configPath,
186
+ confDir: discovered.confDir,
187
+ layerPaths: discovered.layerPaths,
81
188
  };
82
189
 
83
- // Inject .sous/.env.local and .sous/.env before anything resolves variables.
84
- loadEnvFiles(discovered.sousDir);
190
+ // Routed through the command's error sink, not stdout: a recipe layer
191
+ // warning must not land in the middle of `sous config show | jq`.
192
+ for (const notice of discovered.recipeLayerWarnings) warning(notice, this.errorSink);
85
193
 
86
194
  try {
87
- this.settings = await loadSettings(discovered.configPath);
195
+ this.settings = await loadSettings(discovered);
88
196
  } catch (error) {
89
- displayErrorBlock(error instanceof Error ? error.message : String(error));
197
+ displayErrorBlock(error instanceof Error ? error.message : String(error), this.errorSink);
90
198
  return this.exit(1);
91
199
  }
92
200
  }
93
201
 
94
202
  /**
95
- * Renders a configuration error as a plain, readable message instead of an
96
- * oclif stack trace. The stack for a ConfigError points at Sous internals and
97
- * tells the user nothing about the config mistake they need to fix.
203
+ * Renders any failure as a plain, readable message instead of an oclif stack
204
+ * trace: a config mistake, a mistyped command line, or a question sous could
205
+ * not ask. A stack pointing into oclif's parser or into sous's internals
206
+ * tells the user nothing about the mistake they need to fix, so it is printed
207
+ * only when `SOUS_DEBUG` asks for it. The rules live in
208
+ * `utils/command-errors.ts`, so every command in sous fails the same way.
98
209
  *
99
- * Anything that is not a ConfigError falls through to oclif's normal handling,
100
- * where a stack trace IS useful (it is a bug in Sous).
210
+ * A clean `this.exit()` and a command rendering JSON still belong to oclif,
211
+ * and fall through to its handling untouched.
101
212
  */
102
213
  protected async catch(error: Error & { exitCode?: number }): Promise<unknown> {
103
- if (isConfigError(error)) {
104
- displayErrorBlock(error.message);
105
- return this.exit(1);
106
- }
107
- return super.catch(error);
214
+ const exitCode = await reportCommandError(this, error, { write: this.errorSink });
215
+ if (exitCode === undefined) return super.catch(error);
216
+ return this.exit(exitCode);
108
217
  }
109
218
 
110
219
  /**
111
- * Resolves the active project from the --project flag or settings.defaultProject.
112
- * Exits with an error if no project key is available or the key is not found.
220
+ * Re-runs discovery and reloads settings, committing the result onto this
221
+ * command only if everything loads cleanly (last-good semantics: a failed
222
+ * reload throws and leaves the previously loaded config untouched).
223
+ *
224
+ * Used by watch mode when a full-rebuild path (the config file, the conf.d
225
+ * directory, or the templating dir) changes: conf.d layer files can appear or
226
+ * disappear at runtime, so the ordered layer list must be rebuilt (and the
227
+ * duplicate-baseName check re-run) before settings reload.
113
228
  */
114
- protected resolveProject(flagValue: string | undefined): RawProject & { key: string } {
115
- const keys = Object.keys(this.settings.projects ?? {});
116
- const key = flagValue ?? this.settings.defaultProject ?? (keys.length === 1 ? keys[0] : undefined);
117
-
118
- if (!key) {
119
- displayError(
120
- "No project specified.\n" +
121
- ` Config: ${this.configContext.configPath}\n` +
122
- ` Projects defined: ${keys.length > 0 ? keys.join(", ") : "(none)"}\n` +
123
- " Use --project <key>, or set defaultProject in your config."
124
- );
125
- this.exit(1);
126
- }
127
-
128
- const project = this.settings.projects?.[key!];
229
+ protected async reloadDiscoveredConfig(): Promise<void> {
230
+ const refreshed = refreshDiscoveredConfig(this.discovered);
231
+ const reloaded = await loadSettings(refreshed);
129
232
 
130
- if (!project) {
131
- displayError(
132
- `Project '${key}' not found in ${this.configContext.configPath}\n` +
133
- ` Projects defined: ${keys.length > 0 ? keys.join(", ") : "(none)"}`
134
- );
135
- this.exit(1);
136
- }
137
-
138
- return { ...project!, key: key! };
233
+ // Commit only after a clean load, so a broken edit keeps the last-good config.
234
+ this.discovered = refreshed;
235
+ this.configContext = {
236
+ sousDir: refreshed.sousDir,
237
+ configPath: refreshed.configPath,
238
+ confDir: refreshed.confDir,
239
+ layerPaths: refreshed.layerPaths,
240
+ };
241
+ this.settings = reloaded;
139
242
  }
140
243
 
141
- /** Number of projects defined in the active config. */
142
- protected get projectCount(): number {
143
- return Object.keys(this.settings.projects ?? {}).length;
244
+ /**
245
+ * Human-readable label for the configured project: the config's `name` when
246
+ * set, otherwise the basename of the directory holding `.sous/`.
247
+ */
248
+ protected get projectLabel(): string {
249
+ return this.settings.name ?? path.basename(path.dirname(this.configContext.sousDir));
144
250
  }
145
251
  }
146
252
 
@@ -148,6 +254,9 @@ export abstract class BaseCommand extends Command {
148
254
  * Pulls the value of `--config` / `-c` out of a raw argv array.
149
255
  * Supports `--config X`, `--config=X`, `-c X`, and `-cX`.
150
256
  *
257
+ * Scanning stops at a bare `--`: anything after it belongs to a launched tool
258
+ * (see `launch`'s pass-through args), never to sous.
259
+ *
151
260
  * @param argv - Raw arguments (oclif's `this.argv`, i.e. argv minus the command).
152
261
  * @returns The flag value, or undefined when the flag is absent.
153
262
  */
@@ -155,9 +264,53 @@ export function readConfigFlagFromArgv(argv: string[]): string | undefined {
155
264
  for (let i = 0; i < argv.length; i++) {
156
265
  const arg = argv[i];
157
266
 
267
+ if (arg === "--") return undefined;
158
268
  if (arg === "--config" || arg === "-c") return argv[i + 1];
159
269
  if (arg.startsWith("--config=")) return arg.slice("--config=".length);
160
270
  if (arg.startsWith("-c") && arg.length > 2 && !arg.startsWith("-c-")) return arg.slice(2);
161
271
  }
162
272
  return undefined;
163
273
  }
274
+
275
+ /**
276
+ * Coerces an empty or whitespace-only string to `undefined`, passing every other
277
+ * value through unchanged.
278
+ *
279
+ * Config-locating inputs (`SOUS_CONFIG`, `SOUS_DIR`, `SOUS_CONFD`, and their flag
280
+ * aliases) must treat an empty value as "unset", never as a real path. A bare
281
+ * `export SOUS_CONFD=` (or a variable that expands empty) would otherwise resolve
282
+ * to cwd and load every file in the working directory as a config layer; an empty
283
+ * `SOUS_CONFIG` / `SOUS_DIR` would mask a lower-precedence source and disable
284
+ * walk-up discovery. Normalising to `undefined` up front lets the plain `??`
285
+ * precedence chain fall through correctly.
286
+ */
287
+ export function blankToUndefined(value: string | undefined): string | undefined {
288
+ if (value === undefined) return undefined;
289
+ return value.trim() === "" ? undefined : value;
290
+ }
291
+
292
+ /**
293
+ * Pulls the value of a long-only flag (`--<flagName> VALUE` or
294
+ * `--<flagName>=VALUE`) out of a raw argv array. Used for the `--sous-config`,
295
+ * `--sous-dir` and `--sous-confd` aliases, which (like `--config`) must be
296
+ * read before oclif's parse() so the config is located before env files load.
297
+ *
298
+ * Scanning stops at a bare `--` for the same reason as `readConfigFlagFromArgv`:
299
+ * anything after it belongs to a launched tool, never to sous.
300
+ *
301
+ * @param argv - Raw arguments (oclif's `this.argv`).
302
+ * @param flagName - The flag name without leading dashes (e.g. `sous-dir`).
303
+ * @returns The flag value, or undefined when the flag is absent (or has no value).
304
+ */
305
+ export function readLongFlagFromArgv(argv: string[], flagName: string): string | undefined {
306
+ const long = `--${flagName}`;
307
+ const eq = `${long}=`;
308
+ for (let i = 0; i < argv.length; i++) {
309
+ const arg = argv[i];
310
+
311
+ if (arg === "--") return undefined;
312
+ if (arg === long) return argv[i + 1];
313
+ if (arg.startsWith(eq)) return arg.slice(eq.length);
314
+ }
315
+ return undefined;
316
+ }
@@ -1,15 +1,30 @@
1
- import path from "node:path";
2
1
  import { Flags } from "@oclif/core";
3
2
  import { BaseCommand } from "../base-command.js";
4
3
  import { BuildService } from "../lib/build-service.js";
5
4
  import { PidService } from "../lib/pid-service.js";
6
- import { CLI_ROOT, loadSettings, resolveRootScope, resolveScope, resolveWatchConfig } from "../lib/settings.js";
5
+ import { resolveRootScope } from "../lib/settings.js";
6
+ import { describeLinkedRepos } from "../lib/repos/links.js";
7
+ import { resolveStoreSettings } from "../lib/repos/store/settings.js";
8
+ import {
9
+ subscriptionServiceFor,
10
+ type SubscriptionService,
11
+ } from "../lib/repos/subscription-service.js";
12
+ import { buildReloadWatchConfig, startConfigReloadWatch } from "../lib/watch-loop.js";
7
13
  import type { WatchHandle } from "../lib/watch-service.js";
8
14
  import { WatchService } from "../lib/watch-service.js";
9
- import { footer, heading, log, showCommandVars } from "../utils/formatting.js";
15
+ import {
16
+ blankLine,
17
+ footer,
18
+ heading,
19
+ log,
20
+ paragraph,
21
+ showCommandVars,
22
+ warning,
23
+ } from "../utils/formatting.js";
10
24
 
11
25
  export default class Build extends BaseCommand {
12
- static description = "Compile outputs and prune stale files (compile + prune)";
26
+ static description =
27
+ "Compile this project's outputs and remove the ones its config no longer produces";
13
28
 
14
29
  static examples = [
15
30
  "<%= config.bin %> build",
@@ -50,10 +65,8 @@ export default class Build extends BaseCommand {
50
65
  async run(): Promise<void> {
51
66
  const { flags } = await this.parse(Build);
52
67
 
53
- const project = this.resolveProject(flags.project);
54
-
55
68
  showCommandVars({
56
- Project: project.name,
69
+ Project: this.projectLabel,
57
70
  Config: this.configContext.configPath,
58
71
  Rebuild: flags.rebuild,
59
72
  "Dry Run": flags["dry-run"],
@@ -61,6 +74,22 @@ export default class Build extends BaseCommand {
61
74
  "No Prune": flags["no-prune"],
62
75
  });
63
76
 
77
+ // A linked repository is read from a working copy instead of a published
78
+ // version, so it is announced every single time; a build that silently
79
+ // produced something different would be far worse than a noisy one.
80
+ const linked = describeLinkedRepos(this.configContext.sousDir);
81
+ if (linked.length > 0) warning(linked.join("\n"));
82
+
83
+ const repositories = subscriptionServiceFor({
84
+ configContext: this.configContext,
85
+ settings: this.settings,
86
+ shellEnv: this.shellEnv,
87
+ });
88
+
89
+ if (!flags["dry-run"] && !flags["no-compile"]) {
90
+ await this.prepareRepositories(repositories);
91
+ }
92
+
64
93
  heading("Building");
65
94
 
66
95
  const buildOptions = {
@@ -73,7 +102,7 @@ export default class Build extends BaseCommand {
73
102
  };
74
103
 
75
104
  const buildService = new BuildService();
76
- const success = await buildService.build(project.key, this.settings, buildOptions);
105
+ const success = await buildService.build(this.settings, buildOptions);
77
106
 
78
107
  footer();
79
108
 
@@ -82,17 +111,12 @@ export default class Build extends BaseCommand {
82
111
  }
83
112
 
84
113
  if (flags.watch) {
85
- const configPath = this.configContext.configPath;
86
-
87
114
  const rootScope = resolveRootScope(this.settings, this.configContext);
88
- const projectScope = resolveScope(project._vars ?? {}, rootScope);
89
115
 
90
116
  // --- PID file enforcement ---
91
117
  const pidService = new PidService();
92
- const pidFilePath = pidService.getFilePath(project.key, projectScope, this.projectCount);
93
- await pidService.acquire(pidFilePath, project.key);
94
-
95
- let isRebuilding = false;
118
+ const pidFilePath = pidService.getFilePath(rootScope);
119
+ await pidService.acquire(pidFilePath, this.projectLabel);
96
120
 
97
121
  const cleanup = async (watchHandle?: WatchHandle) => {
98
122
  if (process.stdin.isTTY) {
@@ -108,68 +132,47 @@ export default class Build extends BaseCommand {
108
132
 
109
133
  const watchService = new WatchService();
110
134
 
111
- /**
112
- * Builds a WatchConfig from current settings, injecting the config file path
113
- * and the templating directory into fullRebuildPaths.
114
- */
115
- const buildWatchConfig = () => {
116
- const currentRootScope = resolveRootScope(this.settings, this.configContext);
117
- const config = resolveWatchConfig(project, currentRootScope, project.key);
118
- config.fullRebuildPaths = [
119
- ...(config.fullRebuildPaths ?? []),
120
- configPath,
121
- path.join(CLI_ROOT, "src", "templating"),
122
- ];
123
- return config;
124
- };
125
-
126
- /** Starts a new watcher and updates the shared handle reference. */
127
- const startWatcher = (handle: { current: WatchHandle | null }) => {
128
- const watchConfig = buildWatchConfig();
129
- handle.current = watchService.watch(watchConfig, async (event) => {
130
- if (event.type === "partial") {
131
- if (isRebuilding) return;
132
- isRebuilding = true;
133
- log(`\nChange detected: ${event.filePath}`);
134
- heading("Rebuilding");
135
- await buildService.build(project.key, this.settings, {
136
- ...buildOptions,
137
- // --rebuild means full clean build on every trigger; skip partial optimisation
138
- changedFile: buildOptions.rebuild ? undefined : event.filePath,
139
- });
140
- footer();
141
- isRebuilding = false;
142
- } else {
143
- // Full rebuild: stop current watcher, reload settings, restart
144
- if (isRebuilding) return;
145
- isRebuilding = true;
146
- log(`\nConfig changed (${event.filePath}), reloading settings and restarting watcher...`);
147
- await handle.current!.stop();
148
-
149
- this.settings = await loadSettings(configPath);
150
-
151
- heading("Rebuilding");
152
- await buildService.build(project.key, this.settings, buildOptions);
153
- footer();
154
- isRebuilding = false;
155
-
156
- startWatcher(handle);
157
- }
135
+ // Reruns compile + prune with the command's current settings. Called for
136
+ // partial rebuilds (with the changed file) and, after a clean reload, for
137
+ // full rebuilds. Owns the "Rebuilding" heading/footer.
138
+ const rebuild = async (changedFile?: string) => {
139
+ heading("Rebuilding");
140
+ await buildService.build(this.settings, {
141
+ ...buildOptions,
142
+ // --rebuild means full clean build on every trigger; skip partial optimisation
143
+ changedFile: buildOptions.rebuild ? undefined : changedFile,
158
144
  });
145
+ footer();
159
146
  };
160
147
 
161
- const handle: { current: WatchHandle | null } = { current: null };
162
- startWatcher(handle);
148
+ const { handle, triggerFullRebuild } = startConfigReloadWatch({
149
+ watchService,
150
+ buildWatchConfig: () => buildReloadWatchConfig(this.settings, this.configContext),
151
+ rebuild,
152
+ reloadConfig: () => this.reloadDiscoveredConfig(),
153
+ });
163
154
 
164
- const triggerFullRebuild = async (reason: string) => {
165
- if (isRebuilding) return;
166
- isRebuilding = true;
167
- log(`\n${reason}`);
168
- heading("Rebuilding");
169
- await buildService.build(project.key, this.settings, buildOptions);
170
- footer();
171
- isRebuilding = false;
172
- };
155
+ // Watch mode polls upstream for the repositories that prefer a newer
156
+ // in-range version. The poll is cheap (one index request per repository)
157
+ // and a failure never breaks the watch; the last good answer stands.
158
+ const pollSeconds = resolveStoreSettings(this.settings).watchPollSeconds;
159
+ if (pollSeconds > 0) {
160
+ const poll = setInterval(() => {
161
+ void repositories
162
+ .checkUpstream()
163
+ .then(async (report) => {
164
+ if (report.updated.length === 0) return;
165
+ for (const change of report.updated) {
166
+ paragraph(` ${change.key} moved from ${change.from} to ${change.to}.`);
167
+ }
168
+ await triggerFullRebuild("A newer recipe version arrived upstream.");
169
+ })
170
+ .catch(() => {
171
+ // checkUpstream already reports its own failures as warnings.
172
+ });
173
+ }, pollSeconds * 1000);
174
+ poll.unref();
175
+ }
173
176
 
174
177
  // Display the interactive prompt
175
178
  log("[ Press Q to quit | any other key: rebuild ]");
@@ -193,4 +196,78 @@ export default class Build extends BaseCommand {
193
196
  await new Promise(() => {}); // keep process alive
194
197
  }
195
198
  }
199
+
200
+ /**
201
+ * Gets this project's recipes ready to compile: restores whatever the store is
202
+ * missing (a fresh clone, or a collected store) and then asks upstream for the
203
+ * repositories that prefer a newer in-range version.
204
+ *
205
+ * Restoring asks nothing and decides nothing; it fetches exactly what the
206
+ * lockfile pins. An upstream check that fails is reported and then ignored,
207
+ * because a build must not depend on the network being up.
208
+ *
209
+ * @param repositories - The subscription service for this project.
210
+ */
211
+ private async prepareRepositories(repositories: SubscriptionService): Promise<void> {
212
+ const needsRestore = repositories.needsRestore();
213
+ if (needsRestore) {
214
+ heading("Restoring recipes");
215
+ blankLine();
216
+ paragraph(
217
+ "This project's lockfile pins recipes that are not in the store on this " +
218
+ "machine, so they are being fetched at exactly the versions it records."
219
+ );
220
+ }
221
+
222
+ const { seed, subscriptions, restored, upstream } =
223
+ await repositories.prepareForBuild();
224
+
225
+ // Seeding the packaged core recipe is silent when it works, which is almost
226
+ // always; it is only worth a word when it could not be done at all.
227
+ if (seed.skippedBecause !== undefined) warning(seed.skippedBecause);
228
+
229
+ // A subscription the lockfile did not pin yet has just been pinned. That is
230
+ // a change to a committed file, so it is always announced.
231
+ if (subscriptions.added.length > 0 || subscriptions.moved.length > 0) {
232
+ heading("Locking subscribed recipes");
233
+ blankLine();
234
+ for (const entry of subscriptions.added) {
235
+ paragraph(` pinned: ${entry.key} at version ${entry.version}.`);
236
+ }
237
+ for (const change of subscriptions.moved) {
238
+ paragraph(` ${change.key} moved from version ${change.from} to version ${change.to}.`);
239
+ }
240
+ blankLine();
241
+ paragraph(
242
+ "The lockfile has been updated. Commit it, so everyone building this project " +
243
+ "gets exactly these versions."
244
+ );
245
+ footer();
246
+ }
247
+
248
+ for (const failure of subscriptions.failed) {
249
+ warning(
250
+ `Sous could not work out which version of '${failure.key}' to use, so nothing ` +
251
+ `from it was compiled.\n${failure.reason}`
252
+ );
253
+ }
254
+
255
+ if (restored !== undefined && restored.restored.length > 0) {
256
+ blankLine();
257
+ for (const key of restored.restored) paragraph(` restored: ${key}`);
258
+ }
259
+
260
+ for (const change of upstream.updated) {
261
+ paragraph(` ${change.key} moved from ${change.from} to ${change.to}.`);
262
+ }
263
+
264
+ for (const failure of upstream.failed) {
265
+ warning(
266
+ `Sous could not check the repository '${failure.repo}' for a newer version, so ` +
267
+ `this build uses the versions it already had.\n${failure.reason}`
268
+ );
269
+ }
270
+
271
+ if (needsRestore) footer();
272
+ }
196
273
  }