@hop-top/kit 0.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
package/src/xdg.test.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
import * as fs from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { configDir, dataDir, cacheDir, stateDir, mustEnsure } from './xdg';
|
|
6
|
+
|
|
7
|
+
// Helper: save + restore a set of env vars around a test.
|
|
8
|
+
function withEnv(vars: Record<string, string | undefined>, fn: () => void): void {
|
|
9
|
+
const saved: Record<string, string | undefined> = {};
|
|
10
|
+
for (const k of Object.keys(vars)) {
|
|
11
|
+
saved[k] = process.env[k];
|
|
12
|
+
if (vars[k] === undefined) {
|
|
13
|
+
delete process.env[k];
|
|
14
|
+
} else {
|
|
15
|
+
process.env[k] = vars[k] as string;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
fn();
|
|
20
|
+
} finally {
|
|
21
|
+
for (const k of Object.keys(saved)) {
|
|
22
|
+
if (saved[k] === undefined) {
|
|
23
|
+
delete process.env[k];
|
|
24
|
+
} else {
|
|
25
|
+
process.env[k] = saved[k] as string;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ─── configDir ──────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
describe('configDir', () => {
|
|
34
|
+
it('uses XDG_CONFIG_HOME when set', () => {
|
|
35
|
+
withEnv({ XDG_CONFIG_HOME: '/tmp/xdg-cfg' }, () => {
|
|
36
|
+
expect(configDir('mytool')).toBe('/tmp/xdg-cfg/mytool');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('falls back to OS config dir when XDG_CONFIG_HOME is unset', () => {
|
|
41
|
+
withEnv({ XDG_CONFIG_HOME: undefined }, () => {
|
|
42
|
+
const result = configDir('mytool');
|
|
43
|
+
// Must end with /mytool and be an absolute path.
|
|
44
|
+
expect(path.isAbsolute(result)).toBe(true);
|
|
45
|
+
expect(result.endsWith(path.sep + 'mytool') || result.endsWith('/mytool')).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('appends tool name to XDG_CONFIG_HOME', () => {
|
|
50
|
+
withEnv({ XDG_CONFIG_HOME: '/custom/cfg' }, () => {
|
|
51
|
+
expect(configDir('myapp')).toBe('/custom/cfg/myapp');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// ─── dataDir ────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
describe('dataDir', () => {
|
|
59
|
+
it('uses XDG_DATA_HOME when set', () => {
|
|
60
|
+
withEnv({ XDG_DATA_HOME: '/tmp/xdg-data' }, () => {
|
|
61
|
+
expect(dataDir('mytool')).toBe('/tmp/xdg-data/mytool');
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('falls back to OS-native path on current platform when XDG_DATA_HOME unset', () => {
|
|
66
|
+
withEnv({ XDG_DATA_HOME: undefined }, () => {
|
|
67
|
+
const result = dataDir('mytool');
|
|
68
|
+
expect(path.isAbsolute(result)).toBe(true);
|
|
69
|
+
expect(result.endsWith(path.sep + 'mytool') || result.endsWith('/mytool')).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('darwin fallback: ~/Library/Application Support/<tool>', () => {
|
|
74
|
+
withEnv({ XDG_DATA_HOME: undefined }, () => {
|
|
75
|
+
// Simulate darwin by passing override (tests the internal path logic).
|
|
76
|
+
const home = os.homedir();
|
|
77
|
+
const result = dataDir('mytool', 'darwin' as NodeJS.Platform);
|
|
78
|
+
expect(result).toBe(path.join(home, 'Library', 'Application Support', 'mytool'));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('linux fallback: ~/.local/share/<tool>', () => {
|
|
83
|
+
withEnv({ XDG_DATA_HOME: undefined }, () => {
|
|
84
|
+
const home = os.homedir();
|
|
85
|
+
const result = dataDir('mytool', 'linux' as NodeJS.Platform);
|
|
86
|
+
expect(result).toBe(path.join(home, '.local', 'share', 'mytool'));
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('win32 fallback: %LocalAppData%\\<tool>', () => {
|
|
91
|
+
withEnv({ XDG_DATA_HOME: undefined, LocalAppData: 'C:\\Users\\user\\AppData\\Local' }, () => {
|
|
92
|
+
const result = dataDir('mytool', 'win32' as NodeJS.Platform);
|
|
93
|
+
expect(result).toBe(path.win32.join('C:\\Users\\user\\AppData\\Local', 'mytool'));
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('win32 throws when LocalAppData unset', () => {
|
|
98
|
+
withEnv({ XDG_DATA_HOME: undefined, LocalAppData: undefined }, () => {
|
|
99
|
+
expect(() => dataDir('mytool', 'win32' as NodeJS.Platform)).toThrow();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// ─── cacheDir ───────────────────────────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
describe('cacheDir', () => {
|
|
107
|
+
it('uses XDG_CACHE_HOME when set', () => {
|
|
108
|
+
withEnv({ XDG_CACHE_HOME: '/tmp/xdg-cache' }, () => {
|
|
109
|
+
expect(cacheDir('mytool')).toBe('/tmp/xdg-cache/mytool');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('falls back to OS cache dir when XDG_CACHE_HOME unset', () => {
|
|
114
|
+
withEnv({ XDG_CACHE_HOME: undefined }, () => {
|
|
115
|
+
const result = cacheDir('mytool');
|
|
116
|
+
expect(path.isAbsolute(result)).toBe(true);
|
|
117
|
+
expect(result.endsWith(path.sep + 'mytool') || result.endsWith('/mytool')).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// ─── stateDir ───────────────────────────────────────────────────────────────
|
|
123
|
+
|
|
124
|
+
describe('stateDir', () => {
|
|
125
|
+
it('uses XDG_STATE_HOME when set', () => {
|
|
126
|
+
withEnv({ XDG_STATE_HOME: '/tmp/xdg-state' }, () => {
|
|
127
|
+
expect(stateDir('mytool')).toBe('/tmp/xdg-state/mytool');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('darwin fallback: ~/Library/Application Support/<tool>/state', () => {
|
|
132
|
+
withEnv({ XDG_STATE_HOME: undefined }, () => {
|
|
133
|
+
const home = os.homedir();
|
|
134
|
+
const result = stateDir('mytool', 'darwin' as NodeJS.Platform);
|
|
135
|
+
expect(result).toBe(path.join(home, 'Library', 'Application Support', 'mytool', 'state'));
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('linux fallback: ~/.local/state/<tool>', () => {
|
|
140
|
+
withEnv({ XDG_STATE_HOME: undefined }, () => {
|
|
141
|
+
const home = os.homedir();
|
|
142
|
+
const result = stateDir('mytool', 'linux' as NodeJS.Platform);
|
|
143
|
+
expect(result).toBe(path.join(home, '.local', 'state', 'mytool'));
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('win32 fallback: %LocalAppData%\\<tool>\\state', () => {
|
|
148
|
+
withEnv({ XDG_STATE_HOME: undefined, LocalAppData: 'C:\\Users\\user\\AppData\\Local' }, () => {
|
|
149
|
+
const result = stateDir('mytool', 'win32' as NodeJS.Platform);
|
|
150
|
+
expect(result).toBe(
|
|
151
|
+
path.win32.join('C:\\Users\\user\\AppData\\Local', 'mytool', 'state'),
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('win32 throws when LocalAppData unset', () => {
|
|
157
|
+
withEnv({ XDG_STATE_HOME: undefined, LocalAppData: undefined }, () => {
|
|
158
|
+
expect(() => stateDir('mytool', 'win32' as NodeJS.Platform)).toThrow();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('falls back to OS-native path on current platform when XDG_STATE_HOME unset', () => {
|
|
163
|
+
withEnv({ XDG_STATE_HOME: undefined }, () => {
|
|
164
|
+
const result = stateDir('mytool');
|
|
165
|
+
expect(path.isAbsolute(result)).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// ─── mustEnsure ─────────────────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
describe('mustEnsure', () => {
|
|
173
|
+
let tmpBase: string;
|
|
174
|
+
|
|
175
|
+
beforeEach(() => {
|
|
176
|
+
tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'xdg-test-'));
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
afterEach(() => {
|
|
180
|
+
fs.rmSync(tmpBase, { recursive: true, force: true });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('creates the directory and returns its path', () => {
|
|
184
|
+
const target = path.join(tmpBase, 'nested', 'subdir');
|
|
185
|
+
const result = mustEnsure(target);
|
|
186
|
+
expect(result).toBe(target);
|
|
187
|
+
expect(fs.existsSync(target)).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('is idempotent — no error if dir already exists', () => {
|
|
191
|
+
const target = path.join(tmpBase, 'existing');
|
|
192
|
+
fs.mkdirSync(target);
|
|
193
|
+
expect(() => mustEnsure(target)).not.toThrow();
|
|
194
|
+
expect(fs.existsSync(target)).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('throws when path is a file, not a directory', () => {
|
|
198
|
+
const file = path.join(tmpBase, 'afile');
|
|
199
|
+
fs.writeFileSync(file, 'data');
|
|
200
|
+
expect(() => mustEnsure(file)).toThrow();
|
|
201
|
+
});
|
|
202
|
+
});
|
package/src/xdg.ts
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* XDG Base Directory path resolution with OS-native fallbacks.
|
|
5
|
+
*
|
|
6
|
+
* Each function checks the corresponding XDG environment variable first
|
|
7
|
+
* (`XDG_CONFIG_HOME`, `XDG_DATA_HOME`, `XDG_CACHE_HOME`, `XDG_STATE_HOME`).
|
|
8
|
+
* When the variable is unset or empty it falls back to the platform-native
|
|
9
|
+
* directory:
|
|
10
|
+
*
|
|
11
|
+
* - macOS (`darwin`): `~/Library/Application Support/<tool>` for data/state;
|
|
12
|
+
* `~/Library/Preferences` for config;
|
|
13
|
+
* `~/Library/Caches` for cache.
|
|
14
|
+
* - Windows (`win32`): `%LocalAppData%\<tool>` for data/state;
|
|
15
|
+
* `%APPDATA%\<tool>` for config/cache.
|
|
16
|
+
* - Linux / other: `~/.config`, `~/.local/share`,
|
|
17
|
+
* `~/.cache`, `~/.local/state`.
|
|
18
|
+
*
|
|
19
|
+
* All functions are pure (no side effects). Use {@link mustEnsure} to create
|
|
20
|
+
* the directory on disk.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { configDir, mustEnsure } from '@hop-top/kit/xdg';
|
|
25
|
+
*
|
|
26
|
+
* const cfg = mustEnsure(configDir('mytool'));
|
|
27
|
+
* // cfg is guaranteed to exist on disk
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import * as os from 'os';
|
|
32
|
+
import * as path from 'path';
|
|
33
|
+
import * as fs from 'fs';
|
|
34
|
+
|
|
35
|
+
// ─── Public types ────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* All four canonical XDG-style directories for a single tool, resolved in one
|
|
39
|
+
* shot.
|
|
40
|
+
*/
|
|
41
|
+
export interface Dirs {
|
|
42
|
+
/** Resolved configuration directory. */
|
|
43
|
+
config: string;
|
|
44
|
+
/** Resolved data directory. */
|
|
45
|
+
data: string;
|
|
46
|
+
/** Resolved cache directory. */
|
|
47
|
+
cache: string;
|
|
48
|
+
/** Resolved state directory. */
|
|
49
|
+
state: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─── Internal helpers ────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns the platform string to use for OS-specific fallbacks.
|
|
56
|
+
* Accepts an optional override so callers (and tests) can simulate other
|
|
57
|
+
* platforms without mutating `process.platform`.
|
|
58
|
+
*/
|
|
59
|
+
function platform(override?: NodeJS.Platform): NodeJS.Platform {
|
|
60
|
+
return override ?? process.platform;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ─── configDir ───────────────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns the configuration directory for the named tool.
|
|
67
|
+
*
|
|
68
|
+
* Resolution order:
|
|
69
|
+
* 1. `$XDG_CONFIG_HOME/<tool>` — when the variable is set and non-empty.
|
|
70
|
+
* 2. OS-native fallback:
|
|
71
|
+
* - macOS: `~/Library/Preferences/<tool>`
|
|
72
|
+
* - Windows: `%APPDATA%\<tool>` (throws if `APPDATA` is unset)
|
|
73
|
+
* - Linux/other: `~/.config/<tool>`
|
|
74
|
+
*
|
|
75
|
+
* The returned path is **not** guaranteed to exist; call {@link mustEnsure} to
|
|
76
|
+
* create it.
|
|
77
|
+
*
|
|
78
|
+
* @param tool - Tool name appended as the final path segment.
|
|
79
|
+
* @returns Absolute path to the configuration directory.
|
|
80
|
+
* @throws {Error} When the OS-native root cannot be determined
|
|
81
|
+
* (e.g. `%APPDATA%` is unset on Windows).
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const dir = configDir('myapp');
|
|
86
|
+
* // macOS → /Users/alice/Library/Preferences/myapp
|
|
87
|
+
* // Linux → /home/alice/.config/myapp
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function configDir(tool: string, plt?: NodeJS.Platform): string {
|
|
91
|
+
const xdg = process.env['XDG_CONFIG_HOME'];
|
|
92
|
+
if (xdg) return path.join(xdg, tool);
|
|
93
|
+
|
|
94
|
+
const p = platform(plt);
|
|
95
|
+
const home = os.homedir();
|
|
96
|
+
|
|
97
|
+
switch (p) {
|
|
98
|
+
case 'darwin':
|
|
99
|
+
return path.join(home, 'Library', 'Preferences', tool);
|
|
100
|
+
case 'win32': {
|
|
101
|
+
const appdata = process.env['APPDATA'];
|
|
102
|
+
if (!appdata) throw new Error('%APPDATA% is not set');
|
|
103
|
+
return path.join(appdata, tool);
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
return path.join(home, '.config', tool);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ─── dataDir ─────────────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns the data directory for the named tool.
|
|
114
|
+
*
|
|
115
|
+
* Resolution order:
|
|
116
|
+
* 1. `$XDG_DATA_HOME/<tool>` — when the variable is set and non-empty.
|
|
117
|
+
* 2. OS-native fallback:
|
|
118
|
+
* - macOS: `~/Library/Application Support/<tool>`
|
|
119
|
+
* - Windows: `%LocalAppData%\<tool>` (throws if `LocalAppData` is unset)
|
|
120
|
+
* - Linux/other: `~/.local/share/<tool>`
|
|
121
|
+
*
|
|
122
|
+
* @param tool - Tool name appended as the final path segment.
|
|
123
|
+
* @param plt - Optional platform override (defaults to `process.platform`).
|
|
124
|
+
* @returns Absolute path to the data directory.
|
|
125
|
+
* @throws {Error} When the OS-native root cannot be determined.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const dir = dataDir('myapp');
|
|
130
|
+
* // macOS → /Users/alice/Library/Application Support/myapp
|
|
131
|
+
* // Linux → /home/alice/.local/share/myapp
|
|
132
|
+
* // Windows → C:\Users\alice\AppData\Local\myapp
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export function dataDir(tool: string, plt?: NodeJS.Platform): string {
|
|
136
|
+
const xdg = process.env['XDG_DATA_HOME'];
|
|
137
|
+
if (xdg) return path.join(xdg, tool);
|
|
138
|
+
|
|
139
|
+
const p = platform(plt);
|
|
140
|
+
const home = os.homedir();
|
|
141
|
+
|
|
142
|
+
switch (p) {
|
|
143
|
+
case 'darwin':
|
|
144
|
+
return path.join(home, 'Library', 'Application Support', tool);
|
|
145
|
+
case 'win32': {
|
|
146
|
+
const local = process.env['LocalAppData'];
|
|
147
|
+
if (!local) throw new Error('%LocalAppData% is not set');
|
|
148
|
+
return path.win32.join(local, tool);
|
|
149
|
+
}
|
|
150
|
+
default:
|
|
151
|
+
return path.join(home, '.local', 'share', tool);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ─── cacheDir ────────────────────────────────────────────────────────────────
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Returns the cache directory for the named tool.
|
|
159
|
+
*
|
|
160
|
+
* Resolution order:
|
|
161
|
+
* 1. `$XDG_CACHE_HOME/<tool>` — when the variable is set and non-empty.
|
|
162
|
+
* 2. OS-native fallback:
|
|
163
|
+
* - macOS: `~/Library/Caches/<tool>`
|
|
164
|
+
* - Windows: `%LocalAppData%\<tool>\cache` (throws if `LocalAppData` unset)
|
|
165
|
+
* - Linux/other: `~/.cache/<tool>`
|
|
166
|
+
*
|
|
167
|
+
* @param tool - Tool name appended as the final path segment.
|
|
168
|
+
* @param plt - Optional platform override (defaults to `process.platform`).
|
|
169
|
+
* @returns Absolute path to the cache directory.
|
|
170
|
+
* @throws {Error} When the OS-native root cannot be determined.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* const dir = cacheDir('myapp');
|
|
175
|
+
* // macOS → /Users/alice/Library/Caches/myapp
|
|
176
|
+
* // Linux → /home/alice/.cache/myapp
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export function cacheDir(tool: string, plt?: NodeJS.Platform): string {
|
|
180
|
+
const xdg = process.env['XDG_CACHE_HOME'];
|
|
181
|
+
if (xdg) return path.join(xdg, tool);
|
|
182
|
+
|
|
183
|
+
const p = platform(plt);
|
|
184
|
+
const home = os.homedir();
|
|
185
|
+
|
|
186
|
+
switch (p) {
|
|
187
|
+
case 'darwin':
|
|
188
|
+
return path.join(home, 'Library', 'Caches', tool);
|
|
189
|
+
case 'win32': {
|
|
190
|
+
const local = process.env['LocalAppData'];
|
|
191
|
+
if (!local) throw new Error('%LocalAppData% is not set');
|
|
192
|
+
return path.win32.join(local, tool, 'cache');
|
|
193
|
+
}
|
|
194
|
+
default:
|
|
195
|
+
return path.join(home, '.cache', tool);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ─── stateDir ────────────────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Returns the state directory for the named tool.
|
|
203
|
+
*
|
|
204
|
+
* State stores runtime artefacts that should persist across restarts but are
|
|
205
|
+
* not user-facing configuration (e.g. lock files, history, socket paths).
|
|
206
|
+
*
|
|
207
|
+
* Resolution order:
|
|
208
|
+
* 1. `$XDG_STATE_HOME/<tool>` — when the variable is set and non-empty.
|
|
209
|
+
* 2. OS-native fallback:
|
|
210
|
+
* - macOS: `~/Library/Application Support/<tool>/state`
|
|
211
|
+
* (the `/state` suffix avoids collision with {@link dataDir})
|
|
212
|
+
* - Windows: `%LocalAppData%\<tool>\state`
|
|
213
|
+
* (throws if `LocalAppData` is unset)
|
|
214
|
+
* - Linux/other: `~/.local/state/<tool>`
|
|
215
|
+
*
|
|
216
|
+
* @param tool - Tool name appended as the final path segment.
|
|
217
|
+
* @param plt - Optional platform override (defaults to `process.platform`).
|
|
218
|
+
* @returns Absolute path to the state directory.
|
|
219
|
+
* @throws {Error} When the OS-native root cannot be determined.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const dir = stateDir('myapp');
|
|
224
|
+
* // macOS → /Users/alice/Library/Application Support/myapp/state
|
|
225
|
+
* // Linux → /home/alice/.local/state/myapp
|
|
226
|
+
* // Windows → C:\Users\alice\AppData\Local\myapp\state
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export function stateDir(tool: string, plt?: NodeJS.Platform): string {
|
|
230
|
+
const xdg = process.env['XDG_STATE_HOME'];
|
|
231
|
+
if (xdg) return path.join(xdg, tool);
|
|
232
|
+
|
|
233
|
+
const p = platform(plt);
|
|
234
|
+
const home = os.homedir();
|
|
235
|
+
|
|
236
|
+
switch (p) {
|
|
237
|
+
case 'darwin':
|
|
238
|
+
return path.join(home, 'Library', 'Application Support', tool, 'state');
|
|
239
|
+
case 'win32': {
|
|
240
|
+
const local = process.env['LocalAppData'];
|
|
241
|
+
if (!local) throw new Error('%LocalAppData% is not set');
|
|
242
|
+
return path.win32.join(local, tool, 'state');
|
|
243
|
+
}
|
|
244
|
+
default:
|
|
245
|
+
return path.join(home, '.local', 'state', tool);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ─── mustEnsure ──────────────────────────────────────────────────────────────
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Creates `dir` (and any parents) with mode `0o750`, then returns `dir`.
|
|
253
|
+
*
|
|
254
|
+
* Throws an `Error` if `fs.mkdirSync` fails — for example when `dir` is an
|
|
255
|
+
* existing regular file or a permission error prevents directory creation.
|
|
256
|
+
*
|
|
257
|
+
* Intended for startup-time path resolution where a missing directory is
|
|
258
|
+
* unrecoverable:
|
|
259
|
+
*
|
|
260
|
+
* @param dir - Absolute path to the directory to create.
|
|
261
|
+
* @returns The same `dir` value, now guaranteed to exist on disk.
|
|
262
|
+
* @throws {Error} On any filesystem error (file-exists-as-non-dir, EPERM, …).
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* import { dataDir, mustEnsure } from '@hop-top/kit/xdg';
|
|
267
|
+
*
|
|
268
|
+
* const data = mustEnsure(dataDir('mytool'));
|
|
269
|
+
* // data directory now exists on disk
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export function mustEnsure(dir: string): string {
|
|
273
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o750 });
|
|
274
|
+
// mkdirSync with recursive:true only throws on real errors; verify it's a dir.
|
|
275
|
+
const stat = fs.statSync(dir);
|
|
276
|
+
if (!stat.isDirectory()) {
|
|
277
|
+
throw new Error(`mustEnsure: path exists but is not a directory: ${dir}`);
|
|
278
|
+
}
|
|
279
|
+
return dir;
|
|
280
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { dispatch } from '../../src/output/dispatch';
|
|
4
|
+
import { registerOutputFlags, resolveCols } from '../../src/output/flags';
|
|
5
|
+
import '../../src/output/builtins';
|
|
6
|
+
|
|
7
|
+
function makeProgram(argv: readonly string[]) {
|
|
8
|
+
const program = new Command()
|
|
9
|
+
.name('test')
|
|
10
|
+
.exitOverride()
|
|
11
|
+
.configureOutput({ writeOut: () => {}, writeErr: () => {} });
|
|
12
|
+
registerOutputFlags(program);
|
|
13
|
+
program.action(() => {});
|
|
14
|
+
program.parse(['node', 'test', ...argv], { from: 'node' });
|
|
15
|
+
return program;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function captureStdout(): { restore: () => void; out: () => string } {
|
|
19
|
+
const chunks: Buffer[] = [];
|
|
20
|
+
const spy = vi.spyOn(process.stdout, 'write').mockImplementation(
|
|
21
|
+
((c: string | Buffer) => {
|
|
22
|
+
chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c));
|
|
23
|
+
return true;
|
|
24
|
+
}) as typeof process.stdout.write,
|
|
25
|
+
);
|
|
26
|
+
return {
|
|
27
|
+
restore: () => spy.mockRestore(),
|
|
28
|
+
out: () => Buffer.concat(chunks).toString(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const rows = [
|
|
33
|
+
{ id: '1', name: 'Alice', notes: 'a' },
|
|
34
|
+
{ id: '2', name: 'Bob', notes: 'b' },
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
describe('resolveCols', () => {
|
|
38
|
+
it('splits comma-separated values', () => {
|
|
39
|
+
expect(resolveCols({ cols: ['a,b', 'c'] })).toEqual(['a', 'b', 'c']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('dedupes preserving first-seen order', () => {
|
|
43
|
+
expect(resolveCols({ cols: ['a,b', 'a,c'] })).toEqual(['a', 'b', 'c']);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('merges --cols + --columns', () => {
|
|
47
|
+
expect(resolveCols({ cols: ['a'], columns: ['b'] })).toEqual(['a', 'b']);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('trims whitespace', () => {
|
|
51
|
+
expect(resolveCols({ cols: [' a , b '] })).toEqual(['a', 'b']);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('dispatch — --cols on table', () => {
|
|
56
|
+
it('limits columns to subset', async () => {
|
|
57
|
+
const program = makeProgram(['--cols', 'id,name']);
|
|
58
|
+
const cap = captureStdout();
|
|
59
|
+
try {
|
|
60
|
+
await dispatch(program, rows);
|
|
61
|
+
} finally {
|
|
62
|
+
cap.restore();
|
|
63
|
+
}
|
|
64
|
+
const out = cap.out();
|
|
65
|
+
expect(out).toContain('id');
|
|
66
|
+
expect(out).toContain('name');
|
|
67
|
+
expect(out).not.toContain('notes');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('dispatch — --cols on json', () => {
|
|
72
|
+
it('projects rows to subset keys', async () => {
|
|
73
|
+
const program = makeProgram(['--format', 'json', '--cols', 'id']);
|
|
74
|
+
const cap = captureStdout();
|
|
75
|
+
try {
|
|
76
|
+
await dispatch(program, rows);
|
|
77
|
+
} finally {
|
|
78
|
+
cap.restore();
|
|
79
|
+
}
|
|
80
|
+
const parsed = JSON.parse(cap.out()) as Array<Record<string, unknown>>;
|
|
81
|
+
expect(parsed).toEqual([{ id: '1' }, { id: '2' }]);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('dispatch — --cols on yaml', () => {
|
|
86
|
+
it('projects rows to subset keys', async () => {
|
|
87
|
+
const program = makeProgram(['--format', 'yaml', '--cols', 'id']);
|
|
88
|
+
const cap = captureStdout();
|
|
89
|
+
try {
|
|
90
|
+
await dispatch(program, rows);
|
|
91
|
+
} finally {
|
|
92
|
+
cap.restore();
|
|
93
|
+
}
|
|
94
|
+
const out = cap.out();
|
|
95
|
+
expect(out).toContain('id:');
|
|
96
|
+
expect(out).not.toContain('name:');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('dispatch — --cols on csv', () => {
|
|
101
|
+
it('limits columns', async () => {
|
|
102
|
+
const program = makeProgram(['--format', 'csv', '--cols', 'name']);
|
|
103
|
+
const cap = captureStdout();
|
|
104
|
+
try {
|
|
105
|
+
await dispatch(program, rows);
|
|
106
|
+
} finally {
|
|
107
|
+
cap.restore();
|
|
108
|
+
}
|
|
109
|
+
expect(cap.out()).toBe('name\nAlice\nBob\n');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('dispatch — --cols unknown column', () => {
|
|
114
|
+
it('throws with valid list', async () => {
|
|
115
|
+
const program = makeProgram(['--cols', 'bogus']);
|
|
116
|
+
await expect(dispatch(program, rows)).rejects.toThrow(/unknown column "bogus"/);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('dispatch — --columns alias', () => {
|
|
121
|
+
it('--columns has same effect as --cols', async () => {
|
|
122
|
+
const program = makeProgram(['--format', 'csv', '--columns', 'id']);
|
|
123
|
+
const cap = captureStdout();
|
|
124
|
+
try {
|
|
125
|
+
await dispatch(program, rows);
|
|
126
|
+
} finally {
|
|
127
|
+
cap.restore();
|
|
128
|
+
}
|
|
129
|
+
expect(cap.out()).toBe('id\n1\n2\n');
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('dispatch — --cols dedupe', () => {
|
|
134
|
+
it('dedupes repeated columns', async () => {
|
|
135
|
+
const program = makeProgram([
|
|
136
|
+
'--format', 'csv',
|
|
137
|
+
'--cols', 'id,id',
|
|
138
|
+
'--cols', 'id',
|
|
139
|
+
]);
|
|
140
|
+
const cap = captureStdout();
|
|
141
|
+
try {
|
|
142
|
+
await dispatch(program, rows);
|
|
143
|
+
} finally {
|
|
144
|
+
cap.restore();
|
|
145
|
+
}
|
|
146
|
+
expect(cap.out()).toBe('id\n1\n2\n');
|
|
147
|
+
});
|
|
148
|
+
});
|