@juicesharp/rpiv-config 1.20.0 → 2.1.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [2.1.0] - 2026-07-23
2
+
3
+ ### Changed
4
+ - README rewritten to follow the documentation standard shared across all packages.
5
+
6
+ ## [2.0.0] - 2026-07-21
7
+
8
+ ### Added
9
+ - Add `resolveConfigDir()`, which honors `XDG_CONFIG_HOME` per the XDG spec: unset, empty, whitespace, or relative values fall back to `~/.config`; a leading `~/` is expanded (a `~user` form is not) and the result must be absolute.
10
+ - Add `loadJsonConfigWithLegacyFallback()`, which prefers the XDG config path and reads the legacy `~/.config` file only when the XDG file is absent; a malformed XDG file warns and returns an empty config rather than silently falling back.
11
+
12
+ ### Fixed
13
+ - Load correctly under installers that do not materialize peer dependencies by declaring the schema library as a direct dependency.
14
+
1
15
  ## [1.20.0] - 2026-06-15
2
16
 
3
17
  ## [1.19.1] - 2026-06-10
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @juicesharp/rpiv-config
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@juicesharp/rpiv-config.svg)](https://www.npmjs.com/package/@juicesharp/rpiv-config)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Shared JSON config I/O for the `@juicesharp/rpiv-*` packages — XDG-aware path resolution, loads
7
+ that return `{}` instead of throwing, saves that report failure and chmod to `0600`, and
8
+ TypeBox-driven defaults. It is an internal utility of the
9
+ [rpiv-mono](https://github.com/juicesharp/rpiv-mono) monorepo, published only so its sibling
10
+ packages can depend on it: there is no CLI, and it registers nothing with [Pi Agent](https://github.com/badlogic/pi-mono).
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm install @juicesharp/rpiv-config
16
+ ```
17
+
18
+ ESM only. The package ships TypeScript source — `exports` points at `./index.ts`, with no
19
+ `main`, no `types`, and no compiled build — so you need a bundler or a TypeScript-aware runtime
20
+ (`tsx`, Bun). Plain `node` cannot load it, and `--experimental-strip-types` refuses `.ts` files under `node_modules`.
21
+
22
+ ## Quick start
23
+
24
+ ```ts
25
+ import { configPath, loadJsonConfig } from "@juicesharp/rpiv-config";
26
+
27
+ const path = configPath("rpiv-todo"); // ~/.config/rpiv-todo/config.json
28
+ const config = loadJsonConfig<{ theme?: string }>(path); // {} if the file is missing
29
+ ```
30
+
31
+ ## What it provides
32
+
33
+ Eleven exports, all stateless — no singletons, no caches, no import-time side effects.
34
+
35
+ | Export | What it does |
36
+ | --- | --- |
37
+ | `configPath(name, file?)` | Resolves `<config dir>/<name>/<file>`, defaulting to `~/.config/<name>/config.json`. An absolute `XDG_CONFIG_HOME` (or `~`/`~/…`) overrides the directory; anything else falls back. |
38
+ | `loadJsonConfig(path)` | Parses a JSON file. Returns `{}` for a missing file, a non-plain-object value, or malformed JSON (which also warns) — a hand-edited config cannot crash the caller. |
39
+ | `loadJsonConfigWithLegacyFallback(name, file?)` | Reads the XDG path; only when that file is absent does it read the pre-XDG `~/.config/<name>/<file>`. Corruption is surfaced, never masked by the legacy file. |
40
+ | `saveJsonConfig(path, data)` | `mkdir -p`, writes pretty JSON with a trailing newline, then chmods `0600` (best effort). Returns `true`/`false` instead of throwing — guard your "Saved" message on it. |
41
+ | `validateConfig(schema, value)` | Strips unknown keys, layers schema defaults underneath, and returns `{}` on any failure. |
42
+ | `validateGuidanceFields(fields)` | Keeps `promptSnippet` only if it is a non-empty string, and `promptGuidelines` only if it is a non-empty array of non-empty strings. Everything else is dropped. |
43
+ | `GuidanceFieldsSchema` | TypeBox object for the two guidance fields, with `additionalProperties: true` so consumers can nest it in a larger config schema. |
44
+ | `GuidanceFields` | Type for the same shape: `{ promptSnippet?: string; promptGuidelines?: string[] }`. |
45
+ | `parseModelKey(key)` | Splits `provider/modelId`, also accepting the legacy `provider:modelId`; slash wins when both are present. Returns `undefined` if there is no separator at index ≥ 1. |
46
+ | `modelKey({ provider, id })` | Emits the canonical `provider/id`. Paired with `parseModelKey`, persisted colon-form keys migrate on the next save. |
47
+ | `readEnvVar(key, fallback?)` | Returns the trimmed variable, or `fallback` when it is unset or empty after trimming. |
48
+
49
+ `XDG_CONFIG_HOME` is the only variable this package reads, and it governs the `rpiv-*` config layer only — Pi's own `~/.pi/…` paths are a separate concern.
50
+
51
+ ## Used by
52
+
53
+ | Package | Uses |
54
+ | --- | --- |
55
+ | [`rpiv-pi`](https://www.npmjs.com/package/@juicesharp/rpiv-pi) | Path resolution, model-key codec, schema validation for model config |
56
+ | [`rpiv-advisor`](https://www.npmjs.com/package/@juicesharp/rpiv-advisor) | Model-key codec, guidance fields, config load/save |
57
+ | [`rpiv-web-tools`](https://www.npmjs.com/package/@juicesharp/rpiv-web-tools) | Provider config load/save, guidance schema and validation |
58
+ | [`rpiv-telemetry`](https://github.com/juicesharp/rpiv-mono/tree/main/packages/rpiv-telemetry) (internal, never published) | Config load/save, schema validation, env-var reads |
59
+ | [`rpiv-voice`](https://www.npmjs.com/package/@juicesharp/rpiv-voice) | Config load/save |
60
+ | [`rpiv-i18n`](https://www.npmjs.com/package/@juicesharp/rpiv-i18n) | Config load/save |
61
+ | [`rpiv-todo`](https://www.npmjs.com/package/@juicesharp/rpiv-todo) | Config load, guidance fields |
62
+ | [`rpiv-ask-user-question`](https://www.npmjs.com/package/@juicesharp/rpiv-ask-user-question) | Config load, guidance fields |
63
+ | [`rpiv-warp`](https://www.npmjs.com/package/@juicesharp/rpiv-warp) | Config load |
64
+ | [`rpiv-workflow`](https://www.npmjs.com/package/@juicesharp/rpiv-workflow) | Path resolution |
65
+
66
+ These are versioned in lockstep with this package.
67
+
68
+ ## License
69
+
70
+ MIT — see [LICENSE](https://github.com/juicesharp/rpiv-mono/blob/main/LICENSE).
package/config.ts CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
10
10
  import { homedir } from "node:os";
11
- import { dirname, join } from "node:path";
11
+ import { dirname, isAbsolute, join } from "node:path";
12
12
  import { type Static, type TObject, Type } from "typebox";
13
13
  import { Value } from "typebox/value";
14
14
 
@@ -17,14 +17,72 @@ import { Value } from "typebox/value";
17
17
  // ---------------------------------------------------------------------------
18
18
 
19
19
  /**
20
- * Resolve a config file path under `~/.config/<name>/`.
20
+ * Expand a leading `~` to the user's home directory. Mirrors Pi's
21
+ * `expandTildePath`. Uses `join` (not string concatenation) so the path
22
+ * separator is correct on every platform.
23
+ *
24
+ * Contract: only bare `~` and `~/…` expand. A `~user` form is returned
25
+ * unchanged (the XDG spec defines no `~user` expansion), so it fails the
26
+ * downstream `isAbsolute` check and routes to the default config dir.
27
+ */
28
+ function expandTilde(p: string): string {
29
+ if (p === "~") return homedir();
30
+ if (p.startsWith("~/")) return join(homedir(), p.slice(2));
31
+ return p;
32
+ }
33
+
34
+ /** Default config directory: `~/.config`. Single source of the `.config` literal. */
35
+ function defaultConfigDir(): string {
36
+ return join(homedir(), ".config");
37
+ }
38
+
39
+ /**
40
+ * Resolve the config directory honoring `XDG_CONFIG_HOME`.
41
+ *
42
+ * Boundary: `XDG_CONFIG_HOME` governs the rpiv-* sibling config layer only.
43
+ * Pi-native paths (`~/.pi/…`, driven by `PI_CODING_AGENT_DIR`) are a separate,
44
+ * orthogonal concern and are intentionally NOT unified here.
45
+ *
46
+ * XDG spec compliance:
47
+ * - unset / empty-after-trim / whitespace-only → default (`~/.config`)
48
+ * - relative path → default (XDG mandates absolute)
49
+ * - `"~"` or `"~/…"` → expand the tilde, then require absolute
50
+ * - `"~user…"` → NOT expanded (XDG defines no `~user` form); silently
51
+ * routes to the default like any other relative path
52
+ * - absolute → used verbatim
53
+ */
54
+ function resolveConfigDir(): string {
55
+ const xdg = readEnvVar("XDG_CONFIG_HOME");
56
+ if (!xdg) return defaultConfigDir();
57
+ const expanded = expandTilde(xdg);
58
+ return isAbsolute(expanded) ? expanded : defaultConfigDir();
59
+ }
60
+
61
+ /**
62
+ * Always-legacy config path under `~/.config`. Used by the legacy-fallback
63
+ * reader so a pre-XDG config file is still discovered after an operator sets
64
+ * `XDG_CONFIG_HOME`. Ignores `XDG_CONFIG_HOME` by design.
65
+ */
66
+ function legacyConfigPath(name: string, file: string = "config.json"): string {
67
+ return join(defaultConfigDir(), name, file);
68
+ }
69
+
70
+ /**
71
+ * Resolve a config file path under the XDG-aware config directory.
72
+ *
73
+ * `<resolveConfigDir()>/<name>/<file>` — defaults to `~/.config/<name>/config.json`
74
+ * when `XDG_CONFIG_HOME` is unset / empty / whitespace / relative.
75
+ *
76
+ * Boundary: `XDG_CONFIG_HOME` governs the rpiv-* sibling config layer only;
77
+ * Pi-native paths (`~/.pi/…`, via `PI_CODING_AGENT_DIR`) are orthogonal and
78
+ * not unified here.
21
79
  *
22
80
  * @param name — package directory name (e.g. "rpiv-todo")
23
81
  * @param file — config filename (defaults to "config.json")
24
82
  * @returns absolute path to the config file
25
83
  */
26
84
  export function configPath(name: string, file: string = "config.json"): string {
27
- return join(homedir(), ".config", name, file);
85
+ return join(resolveConfigDir(), name, file);
28
86
  }
29
87
 
30
88
  // ---------------------------------------------------------------------------
@@ -57,6 +115,28 @@ export function loadJsonConfig<T>(path: string): T {
57
115
  }
58
116
  }
59
117
 
118
+ /**
119
+ * Load a JSON config, preferring the XDG-resolved path and falling back to the
120
+ * legacy `~/.config/<name>/<file>` location only when the XDG path is missing.
121
+ *
122
+ * - XDG file present (valid or malformed) → its parse result wins. A malformed
123
+ * XDG file warns and returns `{}` but does NOT silently fall back to legacy —
124
+ * corruption is surfaced, not masked.
125
+ * - XDG file missing → read the legacy path (which returns `{}` if also missing,
126
+ * or warns + `{}` if malformed).
127
+ * - Both missing → `{}`.
128
+ *
129
+ * @param name — package directory name (e.g. "rpiv-todo")
130
+ * @param file — config filename (defaults to "config.json")
131
+ */
132
+ export function loadJsonConfigWithLegacyFallback<T>(name: string, file: string = "config.json"): T {
133
+ const xdgPath = configPath(name, file);
134
+ if (existsSync(xdgPath)) {
135
+ return loadJsonConfig<T>(xdgPath);
136
+ }
137
+ return loadJsonConfig<T>(legacyConfigPath(name, file));
138
+ }
139
+
60
140
  // ---------------------------------------------------------------------------
61
141
  // JSON config save — best-effort void
62
142
  // ---------------------------------------------------------------------------
package/index.ts CHANGED
@@ -3,6 +3,7 @@ export {
3
3
  type GuidanceFields,
4
4
  GuidanceFieldsSchema,
5
5
  loadJsonConfig,
6
+ loadJsonConfigWithLegacyFallback,
6
7
  modelKey,
7
8
  parseModelKey,
8
9
  readEnvVar,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juicesharp/rpiv-config",
3
- "version": "1.20.0",
3
+ "version": "2.1.0",
4
4
  "description": "Shared config I/O utilities for rpiv-mono sibling packages",
5
5
  "keywords": [
6
6
  "rpiv",
@@ -33,7 +33,7 @@
33
33
  "exports": {
34
34
  ".": "./index.ts"
35
35
  },
36
- "peerDependencies": {
37
- "typebox": "*"
36
+ "dependencies": {
37
+ "typebox": "^1.1.24"
38
38
  }
39
39
  }