@sous-io/sous 0.2.4 → 0.2.6

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 CHANGED
@@ -26,12 +26,19 @@ formats agents actually read: `.claude/` plus `CLAUDE.md` for Claude Code, `.cod
26
26
 
27
27
  ## Quickstart
28
28
 
29
- Install the CLI:
29
+ Install the CLI globally, or add it to a project, or both:
30
30
 
31
31
  ```bash
32
- npm install -g @sous-io/sous
32
+ npm install -g @sous-io/sous # one sous for every project on the machine
33
+ npm install -D @sous-io/sous # the version this project's templates were written against
33
34
  ```
34
35
 
36
+ A project install pins the sous version a project builds with, and it always does the
37
+ building: a global `sous` run anywhere inside a project that holds `node_modules/@sous-io/sous`
38
+ hands the command to that copy, so `sous`, `npx sous` and a package script all produce the same
39
+ output. When the two versions differ, one line on standard error says which copy ran. Set
40
+ `SOUS_NO_DELEGATE=1` to run the copy you invoked instead.
41
+
35
42
  Or run it from a clone (useful when developing sous itself):
36
43
 
37
44
  ```bash
package/bin/run.js CHANGED
@@ -1,26 +1,38 @@
1
1
  #!/usr/bin/env node
2
- import { register } from "tsx/esm/api";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { handOffToProjectInstall, isEnvFlagOn } from "../src/lib/project-install.mjs";
3
5
 
4
- // The CLI runs from TypeScript source; register tsx before oclif dynamically
5
- // imports any command module. Resolving "tsx" from this file (rather than a
6
- // $PKG_ROOT/node_modules path) works in every install layout: repo clone,
7
- // global install (nested deps), and local/npx installs (hoisted deps).
8
- register();
6
+ // A project that installs @sous-io/sous itself has pinned the version its
7
+ // templates and lockfile were written against, so that copy does the work.
8
+ // This runs before anything else is loaded: when a project copy is found, its
9
+ // own bin is imported into this process and this one loads nothing further
10
+ // (see src/lib/project-install.mjs for the rules, and SOUS_NO_DELEGATE to
11
+ // keep the invoked copy running).
12
+ const ownRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
13
+ const handedOff = await handOffToProjectInstall({ ownRoot });
9
14
 
10
- const { execute, settings } = await import("@oclif/core");
15
+ if (!handedOff) {
16
+ // The CLI runs from TypeScript source; register tsx before oclif dynamically
17
+ // imports any command module. Resolving "tsx" from this file (rather than a
18
+ // $PKG_ROOT/node_modules path) works in every install layout: repo clone,
19
+ // global install (nested deps), and local/npx installs (hoisted deps).
20
+ const { register } = await import("tsx/esm/api");
21
+ register();
11
22
 
12
- // tsx (above) already makes .ts imports work, so oclif's own auto-transpile
13
- // machinery is redundant; leaving it on makes every downstream run warn that
14
- // the (unshipped) typescript devDependency is missing.
15
- settings.enableAutoTranspile = false;
23
+ const { execute, settings } = await import("@oclif/core");
16
24
 
17
- // oclif's development mode turns on its debug setting, which makes every error
18
- // it prints a raw stack trace. Sous reports its own errors as sentences (see
19
- // src/utils/command-errors.ts), so development mode is switched on only when
20
- // SOUS_DEBUG asks for the traces; anything that gets past a command's own
21
- // reporting then prints its stack too.
22
- const debugRequested = !["", "0", "false", "no", "off"].includes(
23
- (process.env.SOUS_DEBUG ?? "").trim().toLowerCase()
24
- );
25
+ // tsx (above) already makes .ts imports work, so oclif's own auto-transpile
26
+ // machinery is redundant; leaving it on makes every downstream run warn that
27
+ // the (unshipped) typescript devDependency is missing.
28
+ settings.enableAutoTranspile = false;
25
29
 
26
- await execute({ development: debugRequested, dir: import.meta.url });
30
+ // oclif's development mode turns on its debug setting, which makes every error
31
+ // it prints a raw stack trace. Sous reports its own errors as sentences (see
32
+ // src/utils/command-errors.ts), so development mode is switched on only when
33
+ // SOUS_DEBUG asks for the traces; anything that gets past a command's own
34
+ // reporting then prints its stack too.
35
+ const debugRequested = isEnvFlagOn(process.env.SOUS_DEBUG);
36
+
37
+ await execute({ development: debugRequested, dir: import.meta.url });
38
+ }
@@ -17,6 +17,37 @@ compiled 4 targets, pruned 1 stale file
17
17
  ?> These docs are young. **Configuration** and **Repositories** are the reference material so
18
18
  far; more will follow.
19
19
 
20
+ ## Installing
21
+
22
+ Sous installs three ways, and they work together:
23
+
24
+ - **Globally** (`npm install -g @sous-io/sous`): one `sous` on the path for every project on the
25
+ machine, and the one to reach for first.
26
+ - **In a project** (`npm install -D @sous-io/sous`): the project pins the version its templates
27
+ and its lockfile were written against, and `npx sous` or a package script runs it. This is the
28
+ right choice for a team, because the implicit `core` subscription asks for exactly the running
29
+ version and a project built by two versions in turn rewrites its committed lockfile back and
30
+ forth.
31
+ - **Both**: a global `sous` run anywhere inside a project that holds `node_modules/@sous-io/sous`
32
+ hands the whole command to that copy before loading any of its own code, so the project's
33
+ version always does the building however it was invoked. The lookup walks up from the working
34
+ directory the way Node resolves a package, so a copy hoisted to a monorepo root is found from
35
+ any package inside it.
36
+
37
+ When the two versions differ, one sentence on standard error names the copy that ran and the
38
+ one you invoked; standard output is untouched, so a piped command prints exactly what it always
39
+ did. Set `SOUS_DEBUG` and the sentence prints on every hand-off. Set `SOUS_NO_DELEGATE` to
40
+ anything but `0`, `false`, `no` or `off` to run the copy you invoked instead, for debugging a
41
+ broken project install or for deliberately using the global one:
42
+
43
+ ```term
44
+ $ sous --version
45
+ Running the project's own sous 0.2.4 from /work/app/node_modules/@sous-io/sous instead of the sous 0.3.0 you invoked; set SOUS_NO_DELEGATE=1 to run the one you invoked.
46
+ @sous-io/sous/0.2.4 linux-x64 node-v22.21.0
47
+ $ SOUS_NO_DELEGATE=1 sous --version
48
+ @sous-io/sous/0.3.0 linux-x64 node-v22.21.0
49
+ ```
50
+
20
51
  ## Where to look
21
52
 
22
53
  - Watch the [animated introduction](../) for the full pitch
@@ -21,3 +21,4 @@
21
21
  - [0001: Repositories](adrs/0001-repositories.md)
22
22
  - [0002: Recipe answers in the template scope](adrs/0002-recipe-answers-in-templates.md)
23
23
  - [0003: Project setup with sous init](adrs/0003-project-init.md)
24
+ - [0004: A global sous defers to the project's install](adrs/0004-project-install-handoff.md)
@@ -338,7 +338,12 @@ $ sous subscription add workflow --non-interactive
338
338
  No expected failure prints a stack trace. A failure sous did not expect prints the message and one more sentence
339
339
  asking you to set `SOUS_DEBUG=1` and run the command again. Set `SOUS_DEBUG` to anything but `0`, `false`, `no`
340
340
  or `off` and every reported failure prints its stack to standard error underneath the message:
341
- `SOUS_DEBUG=1 sous build`. Nothing else changes when it is set.
341
+ `SOUS_DEBUG=1 sous build`. The one other thing it changes is that every hand-off to a project's own install
342
+ announces itself, not only one between different versions.
343
+
344
+ `SOUS_NO_DELEGATE` is the other environment variable every command reads. Set it the same way and the copy of
345
+ sous you invoked runs the command, even inside a project that installs its own `@sous-io/sous`; see
346
+ [Installing](README.md#installing) for the hand-off it switches off.
342
347
 
343
348
  Every listing fits itself to the terminal it runs in: columns shrink, descriptions wrap, and a path or URL is
344
349
  cut in the middle so the host and the last segment both survive. On a terminal too narrow, the least important
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sous-io/sous",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Compiles AI coding agent configuration (CLAUDE.md, skills, memories) from LiquidJS templates",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -11,7 +11,7 @@ formatVersion: 1
11
11
 
12
12
  namespace: core
13
13
  name: sous-skills
14
- version: 0.2.4
14
+ version: 0.2.6
15
15
 
16
16
  description: >-
17
17
  The skills that teach an agent what sous is and how it works: which files sous
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Types for project-install.mjs, which is plain JavaScript because it runs
3
+ * before tsx is registered. Keep the two in step by hand.
4
+ */
5
+
6
+ export const PACKAGE_NAME: "@sous-io/sous";
7
+ export const NO_DELEGATE_ENV: "SOUS_NO_DELEGATE";
8
+ export const DEBUG_ENV: "SOUS_DEBUG";
9
+
10
+ export type ProjectInstall =
11
+ | { same: true; root: string }
12
+ | { same: false; root: string; version: string; bin: string };
13
+
14
+ export type HandoffPlan =
15
+ | { kind: "run-self" }
16
+ | { kind: "hand-off"; install: Extract<ProjectInstall, { same: false }>; notice: string | undefined };
17
+
18
+ export function isEnvFlagOn(value: string | undefined): boolean;
19
+ export function binEntryOf(pkg: unknown): string | undefined;
20
+ export function findProjectInstall(startDir: string, ownRoot: string): ProjectInstall | undefined;
21
+ export function planHandoff(input: {
22
+ cwd: string;
23
+ ownRoot: string;
24
+ env: Record<string, string | undefined>;
25
+ }): HandoffPlan;
26
+ export function handOffToProjectInstall(input: {
27
+ ownRoot: string;
28
+ cwd?: string;
29
+ env?: Record<string, string | undefined>;
30
+ stderr?: { write(chunk: string): unknown };
31
+ }): Promise<boolean>;
@@ -0,0 +1,175 @@
1
+ /**
2
+ * The hand-off from the sous that was invoked to the sous a project installs.
3
+ *
4
+ * A global install is a convenience launcher; a project's own `@sous-io/sous`
5
+ * dependency is the version its templates and its lockfile were written
6
+ * against, and it is the one that has to do the work (the implicit `core`
7
+ * subscription asks for exactly the running version, so two versions taking
8
+ * turns in one project rewrite the committed lockfile back and forth). The
9
+ * published bin (bin/run.js) calls `handOffToProjectInstall` before it loads
10
+ * anything else, and when a project copy is found, imports that copy's own bin
11
+ * in this same process and lets it run the command.
12
+ *
13
+ * Plain JavaScript ESM, no TypeScript syntax: this runs BEFORE tsx is
14
+ * registered, under bare Node, because the whole point is to load none of the
15
+ * invoked install's code when another copy should run. It ships to npm via the
16
+ * package.json "files": "src" allowlist, like the config kernel next to it.
17
+ *
18
+ * The rules, all of them here and nowhere else:
19
+ * - The lookup walks up from the working directory looking for
20
+ * `node_modules/@sous-io/sous`, the way Node resolves a package, so a copy
21
+ * hoisted to a monorepo root is found from any package inside it.
22
+ * - A copy whose real path is the invoked install's own root is "self", and
23
+ * self never hands off; that is what stops the project's copy from
24
+ * handing off to itself after the global copy handed off to it.
25
+ * - The copy's bin is read from its package.json `bin` field, never assumed,
26
+ * so an older layout (the bin was once called `xcv`) still works.
27
+ * - Anything unreadable or ambiguous means "run the copy that was invoked".
28
+ * A hand-off is a convenience; a refusal to run is not.
29
+ * - `SOUS_NO_DELEGATE` (anything but 0/false/no/off) runs the invoked copy.
30
+ * - The notice goes to stderr, so piped stdout stays clean, and only when
31
+ * the two versions differ; `SOUS_DEBUG` prints it on every hand-off.
32
+ */
33
+
34
+ import fs from "node:fs";
35
+ import path from "node:path";
36
+ import { pathToFileURL } from "node:url";
37
+
38
+ /** The npm package name a project copy is looked up under. */
39
+ export const PACKAGE_NAME = "@sous-io/sous";
40
+
41
+ /** The environment variable that keeps the invoked copy running. */
42
+ export const NO_DELEGATE_ENV = "SOUS_NO_DELEGATE";
43
+
44
+ /** The environment variable that makes every hand-off announce itself. */
45
+ export const DEBUG_ENV = "SOUS_DEBUG";
46
+
47
+ /**
48
+ * Whether an on/off environment variable is on: set to anything but an empty
49
+ * string, `0`, `false`, `no` or `off` (case-insensitive, whitespace trimmed).
50
+ * The same reading `SOUS_DEBUG` has always had.
51
+ */
52
+ export function isEnvFlagOn(value) {
53
+ return !["", "0", "false", "no", "off"].includes((value ?? "").trim().toLowerCase());
54
+ }
55
+
56
+ /**
57
+ * Reads a package.json, returning the parsed object or undefined for a file
58
+ * that is missing, unreadable or not JSON.
59
+ */
60
+ function readPackageJson(dir) {
61
+ try {
62
+ return JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
63
+ } catch {
64
+ return undefined;
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Works out which file a package's `bin` field names for the `sous` command,
70
+ * relative to the package root, or undefined when it cannot be told: no field,
71
+ * an object naming neither `sous` nor `xcv` with more than one entry, or a
72
+ * value that is not a string.
73
+ */
74
+ export function binEntryOf(pkg) {
75
+ const bin = pkg?.bin;
76
+ if (typeof bin === "string") return bin;
77
+ if (!bin || typeof bin !== "object") return undefined;
78
+ const named = bin.sous ?? bin.xcv;
79
+ if (typeof named === "string") return named;
80
+ const entries = Object.values(bin);
81
+ if (entries.length === 1 && typeof entries[0] === "string") return entries[0];
82
+ return undefined;
83
+ }
84
+
85
+ /**
86
+ * Looks up from `startDir` for a project install of the package.
87
+ *
88
+ * Returns undefined when no ancestor holds `node_modules/@sous-io/sous`, or
89
+ * when the first one found is not usable (its package.json does not name the
90
+ * package, or its bin cannot be determined or does not exist). Returns
91
+ * `{ same: true, root }` when the first copy found IS the invoked install
92
+ * (`ownRoot`), compared by real path, and otherwise `{ same: false, root,
93
+ * version, bin }` with `bin` as an absolute path.
94
+ *
95
+ * The walk stops at the first copy, usable or not: a broken copy nearer the
96
+ * working directory is what `npx` would run too, and skipping past it to an
97
+ * older one further up would be a guess.
98
+ */
99
+ export function findProjectInstall(startDir, ownRoot) {
100
+ let dir = path.resolve(startDir);
101
+ for (;;) {
102
+ const candidate = path.join(dir, "node_modules", ...PACKAGE_NAME.split("/"));
103
+ if (fs.existsSync(path.join(candidate, "package.json"))) {
104
+ return describeInstall(candidate, ownRoot);
105
+ }
106
+ const parent = path.dirname(dir);
107
+ if (parent === dir) return undefined;
108
+ dir = parent;
109
+ }
110
+ }
111
+
112
+ function realpathOr(p) {
113
+ try {
114
+ return fs.realpathSync(p);
115
+ } catch {
116
+ return path.resolve(p);
117
+ }
118
+ }
119
+
120
+ function describeInstall(candidate, ownRoot) {
121
+ const root = realpathOr(candidate);
122
+ if (root === realpathOr(ownRoot)) return { same: true, root };
123
+ const pkg = readPackageJson(root);
124
+ if (!pkg || pkg.name !== PACKAGE_NAME) return undefined;
125
+ const entry = binEntryOf(pkg);
126
+ if (!entry) return undefined;
127
+ const bin = path.resolve(root, entry);
128
+ if (!fs.existsSync(bin)) return undefined;
129
+ return { same: false, root, version: typeof pkg.version === "string" ? pkg.version : "unknown", bin };
130
+ }
131
+
132
+ /**
133
+ * Decides what the invoked install should do, without doing it.
134
+ *
135
+ * Returns `{ kind: "run-self" }` when the invoked copy runs the command, or
136
+ * `{ kind: "hand-off", install, notice }` naming the project copy to import
137
+ * and the sentence to print on stderr first (undefined when nothing is said).
138
+ */
139
+ export function planHandoff({ cwd, ownRoot, env }) {
140
+ if (isEnvFlagOn(env[NO_DELEGATE_ENV])) return { kind: "run-self" };
141
+ const install = findProjectInstall(cwd, ownRoot);
142
+ if (!install || install.same) return { kind: "run-self" };
143
+
144
+ const ownPkg = readPackageJson(ownRoot);
145
+ const ownVersion = typeof ownPkg?.version === "string" ? ownPkg.version : "unknown";
146
+ const differ = ownVersion !== install.version;
147
+ let notice;
148
+ if (differ) {
149
+ notice =
150
+ `Running the project's own sous ${install.version} from ${install.root} instead of the ` +
151
+ `sous ${ownVersion} you invoked; set ${NO_DELEGATE_ENV}=1 to run the one you invoked.`;
152
+ } else if (isEnvFlagOn(env[DEBUG_ENV])) {
153
+ notice = `Running the project's own sous ${install.version} from ${install.root}.`;
154
+ }
155
+ return { kind: "hand-off", install, notice };
156
+ }
157
+
158
+ /**
159
+ * Hands the current invocation to the project's copy when there is one to hand
160
+ * it to. Resolves true after that copy's bin has run (it reads the same
161
+ * process.argv and sets the same exit code), and false, having loaded nothing,
162
+ * when the invoked copy should run the command itself.
163
+ */
164
+ export async function handOffToProjectInstall({
165
+ ownRoot,
166
+ cwd = process.cwd(),
167
+ env = process.env,
168
+ stderr = process.stderr,
169
+ }) {
170
+ const plan = planHandoff({ cwd, ownRoot, env });
171
+ if (plan.kind !== "hand-off") return false;
172
+ if (plan.notice) stderr.write(`${plan.notice}\n`);
173
+ await import(pathToFileURL(plan.install.bin).href);
174
+ return true;
175
+ }