@lkangd/cc-env 1.0.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 (111) hide show
  1. package/.claude/settings.json +6 -0
  2. package/.claude/settings.local.json +3 -0
  3. package/.nvmrc +1 -0
  4. package/dist/cli.js +266 -0
  5. package/dist/commands/debug.js +17 -0
  6. package/dist/commands/init.js +64 -0
  7. package/dist/commands/preset/create.js +61 -0
  8. package/dist/commands/preset/delete.js +25 -0
  9. package/dist/commands/preset/edit.js +15 -0
  10. package/dist/commands/preset/list.js +16 -0
  11. package/dist/commands/preset/show.js +16 -0
  12. package/dist/commands/restore.js +65 -0
  13. package/dist/commands/run.js +80 -0
  14. package/dist/core/errors.js +11 -0
  15. package/dist/core/find-claude.js +64 -0
  16. package/dist/core/format.js +23 -0
  17. package/dist/core/fs.js +12 -0
  18. package/dist/core/gitignore.js +23 -0
  19. package/dist/core/lock.js +25 -0
  20. package/dist/core/logger.js +8 -0
  21. package/dist/core/mask.js +13 -0
  22. package/dist/core/paths.js +32 -0
  23. package/dist/core/process-env.js +4 -0
  24. package/dist/core/schema.js +38 -0
  25. package/dist/core/spawn.js +26 -0
  26. package/dist/flows/init-flow.js +35 -0
  27. package/dist/flows/preset-create-flow.js +80 -0
  28. package/dist/flows/restore-flow.js +75 -0
  29. package/dist/ink/init-app.js +54 -0
  30. package/dist/ink/preset-create-app.js +271 -0
  31. package/dist/ink/preset-delete-app.js +47 -0
  32. package/dist/ink/preset-list-app.js +27 -0
  33. package/dist/ink/preset-show-app.js +27 -0
  34. package/dist/ink/restore-app.js +102 -0
  35. package/dist/ink/run-preset-select-app.js +31 -0
  36. package/dist/ink/summary.js +28 -0
  37. package/dist/services/claude-settings-env-service.js +55 -0
  38. package/dist/services/config-service.js +26 -0
  39. package/dist/services/history-service.js +39 -0
  40. package/dist/services/preset-service.js +61 -0
  41. package/dist/services/project-env-service.js +90 -0
  42. package/dist/services/project-state-service.js +26 -0
  43. package/dist/services/runtime-env-service.js +13 -0
  44. package/dist/services/settings-env-service.js +36 -0
  45. package/dist/services/shell-env-service.js +77 -0
  46. package/docs/product-specs/index.draft.md +106 -0
  47. package/docs/product-specs/index.md +911 -0
  48. package/docs/product-specs/optional.md +42 -0
  49. package/docs/references/claude-code-env.md +224 -0
  50. package/docs/superpowers/plans/2026-04-24-cc-env-init-shell-migration.md +1331 -0
  51. package/docs/superpowers/plans/2026-04-24-cc-env.md +1666 -0
  52. package/docs/superpowers/plans/2026-04-26-preset-create-interactive-refactor.md +1432 -0
  53. package/docs/superpowers/specs/2026-04-24-cc-env-design.md +438 -0
  54. package/docs/superpowers/specs/2026-04-24-cc-env-init-shell-migration-design.md +181 -0
  55. package/docs/superpowers/specs/2026-04-26-preset-create-interactive-refactor-design.md +78 -0
  56. package/package.json +55 -0
  57. package/src/cli.ts +337 -0
  58. package/src/commands/init.ts +139 -0
  59. package/src/commands/preset/create.ts +96 -0
  60. package/src/commands/preset/delete.ts +62 -0
  61. package/src/commands/preset/show.ts +51 -0
  62. package/src/commands/restore.ts +150 -0
  63. package/src/commands/run.ts +158 -0
  64. package/src/core/errors.ts +13 -0
  65. package/src/core/find-claude.ts +70 -0
  66. package/src/core/format.ts +29 -0
  67. package/src/core/fs.ts +18 -0
  68. package/src/core/gitignore.ts +26 -0
  69. package/src/core/logger.ts +11 -0
  70. package/src/core/mask.ts +17 -0
  71. package/src/core/paths.ts +41 -0
  72. package/src/core/process-env.ts +11 -0
  73. package/src/core/schema.ts +55 -0
  74. package/src/core/spawn.ts +36 -0
  75. package/src/flows/init-flow.ts +61 -0
  76. package/src/flows/preset-create-flow.ts +129 -0
  77. package/src/flows/restore-flow.ts +144 -0
  78. package/src/ink/init-app.tsx +110 -0
  79. package/src/ink/preset-create-app.tsx +451 -0
  80. package/src/ink/preset-delete-app.tsx +114 -0
  81. package/src/ink/preset-show-app.tsx +76 -0
  82. package/src/ink/restore-app.tsx +230 -0
  83. package/src/ink/run-preset-select-app.tsx +83 -0
  84. package/src/ink/summary.tsx +91 -0
  85. package/src/services/claude-settings-env-service.ts +72 -0
  86. package/src/services/history-service.ts +48 -0
  87. package/src/services/preset-service.ts +72 -0
  88. package/src/services/project-env-service.ts +128 -0
  89. package/src/services/project-state-service.ts +31 -0
  90. package/src/services/settings-env-service.ts +40 -0
  91. package/src/services/shell-env-service.ts +112 -0
  92. package/src/types.d.ts +19 -0
  93. package/tests/cli/help.test.ts +133 -0
  94. package/tests/cli/init.test.ts +76 -0
  95. package/tests/cli/restore.test.ts +172 -0
  96. package/tests/commands/create.test.ts +263 -0
  97. package/tests/commands/output.test.ts +119 -0
  98. package/tests/commands/run.test.ts +218 -0
  99. package/tests/core/gitignore.test.ts +98 -0
  100. package/tests/core/paths.test.ts +24 -0
  101. package/tests/core/schema-mask.test.ts +182 -0
  102. package/tests/core/spawn.test.ts +47 -0
  103. package/tests/flows/init-flow.test.ts +40 -0
  104. package/tests/flows/preset-create-flow.test.ts +225 -0
  105. package/tests/flows/restore-flow.test.ts +157 -0
  106. package/tests/integration/init-restore.test.ts +406 -0
  107. package/tests/services/claude-shell.test.ts +183 -0
  108. package/tests/services/storage.test.ts +143 -0
  109. package/tsconfig.build.json +9 -0
  110. package/tsconfig.json +22 -0
  111. package/vitest.config.ts +8 -0
@@ -0,0 +1,271 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { advancePresetCreateFlow, createPresetCreateFlowState, } from '../flows/preset-create-flow.js';
5
+ import { EnvSummary } from './summary.js';
6
+ function SourceStep({ cursor }) {
7
+ const options = [
8
+ { label: 'File import', value: 'file' },
9
+ { label: 'Manual input', value: 'manual' },
10
+ ];
11
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Select env source" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 enter confirm" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: options.map((opt, i) => (_jsxs(Box, { children: [_jsx(Text, { children: i === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(i === cursor ? { color: 'cyan' } : {}), children: opt.label })] }, opt.value))) })] }));
12
+ }
13
+ function FilePathStep({ value, error }) {
14
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Enter file path (.yaml/.yml/.json)" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Text, { dimColor: true, children: ['>', " "] }), _jsx(Text, { color: "cyan", children: value }), _jsx(Text, { dimColor: true, children: "\u2588" })] }), error ? _jsx(Text, { color: "red", children: error }) : null] }));
15
+ }
16
+ function KeysStep({ keys, selectedKeys, cursor, }) {
17
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Select env keys to import" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 space toggle \u00B7 enter confirm" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: keys.map((key, i) => {
18
+ const isSelected = selectedKeys.includes(key);
19
+ return (_jsxs(Box, { children: [_jsx(Text, { children: i === cursor ? '❯ ' : ' ' }), _jsx(Text, { color: isSelected ? 'green' : '', children: isSelected ? '[x]' : '[ ]' }), _jsxs(Text, { children: [" ", key] })] }, key));
20
+ }) }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { dimColor: true, children: [selectedKeys.length, " of ", keys.length, " selected"] }) })] }));
21
+ }
22
+ function ManualInputStep({ entries, value, error, }) {
23
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Enter KEY=VALUE pairs (press q when done)" }), entries.length > 0 ? (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: entries.map(([key, val]) => (_jsxs(Box, { children: [_jsx(Text, { color: "yellow", children: "\u2022 " }), _jsx(Text, { color: "magenta", children: key }), _jsx(Text, { dimColor: true, children: "=" }), _jsx(Text, { children: val })] }, key))) })) : null, _jsxs(Box, { children: [_jsxs(Text, { dimColor: true, children: ['>', " "] }), _jsx(Text, { color: "cyan", children: value }), _jsx(Text, { dimColor: true, children: "\u2588" })] }), error ? _jsx(Text, { color: "red", children: error }) : null] }));
24
+ }
25
+ function NameStep({ value }) {
26
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Enter preset name" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Text, { dimColor: true, children: ['>', " "] }), _jsx(Text, { color: "cyan", children: value }), _jsx(Text, { dimColor: true, children: "\u2588" })] })] }));
27
+ }
28
+ function DestinationStep({ cursor }) {
29
+ const options = [
30
+ { label: 'Global preset', value: 'global' },
31
+ { label: 'Project preset', value: 'project' },
32
+ ];
33
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Select save destination" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 enter confirm" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: options.map((opt, i) => (_jsxs(Box, { children: [_jsx(Text, { children: i === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(i === cursor ? { color: 'cyan' } : {}), children: opt.label })] }, opt.value))) })] }));
34
+ }
35
+ export function PresetCreateApp({ onSubmit, readFile, globalPresetPath, projectEnvPath, }) {
36
+ const { exit } = useApp();
37
+ const [state, setState] = useState(createPresetCreateFlowState);
38
+ const [textInput, setTextInput] = useState('');
39
+ const [listCursor, setListCursor] = useState(0);
40
+ const [allKeys, setAllKeys] = useState([]);
41
+ const [fileEnv, setFileEnv] = useState({});
42
+ useInput((input, key) => {
43
+ if (key.escape) {
44
+ exit();
45
+ return;
46
+ }
47
+ if (state.step === 'source') {
48
+ if (input === 'q') {
49
+ exit();
50
+ return;
51
+ }
52
+ if (key.upArrow || input === 'k') {
53
+ setListCursor((c) => Math.max(0, c - 1));
54
+ return;
55
+ }
56
+ if (key.downArrow || input === 'j') {
57
+ setListCursor((c) => Math.min(1, c + 1));
58
+ return;
59
+ }
60
+ if (key.return) {
61
+ const source = listCursor === 0 ? 'file' : 'manual';
62
+ setState((s) => advancePresetCreateFlow(s, { type: 'select-source', source }));
63
+ setListCursor(0);
64
+ setTextInput('');
65
+ return;
66
+ }
67
+ }
68
+ if (state.step === 'filePath') {
69
+ if (input === 'q') {
70
+ exit();
71
+ return;
72
+ }
73
+ if (key.backspace || key.delete) {
74
+ setTextInput((v) => v.slice(0, -1));
75
+ return;
76
+ }
77
+ if (key.return) {
78
+ void (async () => {
79
+ try {
80
+ const result = await readFile(textInput);
81
+ if (result.allKeys.length === 0) {
82
+ setState((s) => advancePresetCreateFlow(s, {
83
+ type: 'set-error',
84
+ error: 'No valid env keys found in file',
85
+ }));
86
+ return;
87
+ }
88
+ setAllKeys(result.allKeys);
89
+ setFileEnv(result.env);
90
+ setState((s) => advancePresetCreateFlow(s, {
91
+ type: 'set-file-path',
92
+ filePath: textInput,
93
+ }));
94
+ setListCursor(0);
95
+ }
96
+ catch (err) {
97
+ const message = err instanceof Error ? err.message : 'Failed to read file';
98
+ setState((s) => advancePresetCreateFlow(s, {
99
+ type: 'set-error',
100
+ error: message,
101
+ }));
102
+ }
103
+ })();
104
+ return;
105
+ }
106
+ if (input && !key.ctrl && !key.meta) {
107
+ setTextInput((v) => v + input);
108
+ return;
109
+ }
110
+ }
111
+ if (state.step === 'keys') {
112
+ if (input === 'q') {
113
+ exit();
114
+ return;
115
+ }
116
+ if (key.upArrow || input === 'k') {
117
+ setListCursor((c) => Math.max(0, c - 1));
118
+ return;
119
+ }
120
+ if (key.downArrow || input === 'j') {
121
+ setListCursor((c) => Math.min(allKeys.length - 1, c + 1));
122
+ return;
123
+ }
124
+ if (input === ' ') {
125
+ const targetKey = allKeys[listCursor];
126
+ if (targetKey) {
127
+ const newSelected = state.selectedKeys.includes(targetKey)
128
+ ? state.selectedKeys.filter((k) => k !== targetKey)
129
+ : [...state.selectedKeys, targetKey];
130
+ setState((s) => ({ ...s, selectedKeys: newSelected }));
131
+ }
132
+ return;
133
+ }
134
+ if (key.return && state.selectedKeys.length > 0) {
135
+ const selectedEnv = {};
136
+ for (const k of state.selectedKeys) {
137
+ selectedEnv[k] = fileEnv[k] ?? '';
138
+ }
139
+ setState((s) => advancePresetCreateFlow(s, {
140
+ type: 'select-keys',
141
+ keys: state.selectedKeys,
142
+ env: selectedEnv,
143
+ }));
144
+ setTextInput('');
145
+ return;
146
+ }
147
+ }
148
+ if (state.step === 'manualInput') {
149
+ if (input === 'q' && textInput === '') {
150
+ if (state.selectedKeys.length === 0) {
151
+ setState((s) => advancePresetCreateFlow(s, {
152
+ type: 'set-error',
153
+ error: 'Add at least one KEY=VALUE pair',
154
+ }));
155
+ return;
156
+ }
157
+ setState((s) => advancePresetCreateFlow(s, { type: 'finish-manual-input' }));
158
+ setTextInput('');
159
+ return;
160
+ }
161
+ if (key.backspace || key.delete) {
162
+ setTextInput((v) => v.slice(0, -1));
163
+ return;
164
+ }
165
+ if (key.return) {
166
+ const separatorIndex = textInput.indexOf('=');
167
+ if (separatorIndex <= 0) {
168
+ setState((s) => advancePresetCreateFlow(s, {
169
+ type: 'set-error',
170
+ error: 'Format must be KEY=VALUE',
171
+ }));
172
+ return;
173
+ }
174
+ const k = textInput.slice(0, separatorIndex);
175
+ const v = textInput.slice(separatorIndex + 1);
176
+ if (!/^[A-Z0-9_]+$/.test(k)) {
177
+ setState((s) => advancePresetCreateFlow(s, {
178
+ type: 'set-error',
179
+ error: 'Key must match [A-Z0-9_]+',
180
+ }));
181
+ return;
182
+ }
183
+ setState((s) => advancePresetCreateFlow(s, {
184
+ type: 'add-manual-pair',
185
+ key: k,
186
+ value: v,
187
+ }));
188
+ setTextInput('');
189
+ return;
190
+ }
191
+ if (input && !key.ctrl && !key.meta) {
192
+ setTextInput((v) => v + input);
193
+ return;
194
+ }
195
+ }
196
+ if (state.step === 'name') {
197
+ if (input === 'q') {
198
+ exit();
199
+ return;
200
+ }
201
+ if (key.backspace || key.delete) {
202
+ setTextInput((v) => v.slice(0, -1));
203
+ return;
204
+ }
205
+ if (key.return && textInput.trim().length > 0) {
206
+ setState((s) => advancePresetCreateFlow(s, {
207
+ type: 'set-name',
208
+ name: textInput.trim(),
209
+ }));
210
+ setListCursor(0);
211
+ return;
212
+ }
213
+ if (input && !key.ctrl && !key.meta) {
214
+ setTextInput((v) => v + input);
215
+ return;
216
+ }
217
+ }
218
+ if (state.step === 'destination') {
219
+ if (input === 'q') {
220
+ exit();
221
+ return;
222
+ }
223
+ if (key.upArrow || input === 'k') {
224
+ setListCursor((c) => Math.max(0, c - 1));
225
+ return;
226
+ }
227
+ if (key.downArrow || input === 'j') {
228
+ setListCursor((c) => Math.min(1, c + 1));
229
+ return;
230
+ }
231
+ if (key.return) {
232
+ const destination = listCursor === 0 ? 'global' : 'project';
233
+ setState((s) => advancePresetCreateFlow(s, {
234
+ type: 'select-destination',
235
+ destination,
236
+ }));
237
+ return;
238
+ }
239
+ }
240
+ if (state.step === 'confirm') {
241
+ if (input === 'q') {
242
+ exit();
243
+ return;
244
+ }
245
+ if (key.return && state.destination && state.presetName) {
246
+ const doneState = advancePresetCreateFlow(state, { type: 'confirm' });
247
+ setState(doneState);
248
+ void Promise.resolve(onSubmit({
249
+ source: state.source,
250
+ filePath: state.filePath,
251
+ env: state.env,
252
+ selectedKeys: state.selectedKeys,
253
+ presetName: state.presetName,
254
+ destination: state.destination,
255
+ })).finally(() => {
256
+ exit();
257
+ });
258
+ }
259
+ }
260
+ });
261
+ if (state.step === 'done') {
262
+ return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { color: "green", children: "Done" }) }));
263
+ }
264
+ return (_jsxs(Box, { flexDirection: "column", children: [state.step === 'source' && _jsx(SourceStep, { cursor: listCursor }), state.step === 'filePath' && (_jsx(FilePathStep, { value: textInput, ...(state.error ? { error: state.error } : {}) })), state.step === 'keys' && (_jsx(KeysStep, { keys: allKeys, selectedKeys: state.selectedKeys, cursor: listCursor })), state.step === 'manualInput' && (_jsx(ManualInputStep, { entries: state.selectedKeys.map((k) => [k, state.env[k] ?? '']), value: textInput, ...(state.error ? { error: state.error } : {}) })), state.step === 'name' && _jsx(NameStep, { value: textInput }), state.step === 'destination' && _jsx(DestinationStep, { cursor: listCursor }), state.step === 'confirm' && state.destination ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(EnvSummary, { title: `Preset: ${state.presetName}`, entries: Object.entries(state.env)
265
+ .filter(([k]) => state.selectedKeys.includes(k))
266
+ .sort(([a], [b]) => a.localeCompare(b)), mask: true, ...(state.filePath ? { fromFiles: [state.filePath] } : {}), toFiles: [
267
+ state.destination === 'global'
268
+ ? globalPresetPath(state.presetName)
269
+ : projectEnvPath,
270
+ ] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Press enter to confirm \u00B7 q to cancel" }) })] })) : null] }));
271
+ }
@@ -0,0 +1,47 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo, useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { EnvEntries } from './summary.js';
5
+ export function PresetDeleteApp({ presets, onSubmit, }) {
6
+ const { exit } = useApp();
7
+ const [cursor, setCursor] = useState(0);
8
+ const [step, setStep] = useState('browsing');
9
+ const activePreset = presets[cursor];
10
+ const entries = useMemo(() => activePreset
11
+ ? Object.entries(activePreset.env).sort(([a], [b]) => a.localeCompare(b))
12
+ : [], [activePreset]);
13
+ useInput((input, key) => {
14
+ if (step === 'browsing') {
15
+ if (key.escape || input.toLowerCase() === 'q') {
16
+ exit();
17
+ return;
18
+ }
19
+ if (key.upArrow || input === 'k') {
20
+ setCursor((c) => Math.max(0, c - 1));
21
+ return;
22
+ }
23
+ if (key.downArrow || input === 'j') {
24
+ setCursor((c) => Math.min(presets.length - 1, c + 1));
25
+ return;
26
+ }
27
+ if (key.return) {
28
+ setStep('confirming');
29
+ return;
30
+ }
31
+ }
32
+ if (step === 'confirming') {
33
+ if (input.toLowerCase() === 'y') {
34
+ onSubmit(activePreset);
35
+ exit();
36
+ return;
37
+ }
38
+ if (input.toLowerCase() === 'n' || key.escape) {
39
+ setStep('browsing');
40
+ return;
41
+ }
42
+ }
43
+ });
44
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: "Preset delete" }), _jsx(Text, { dimColor: true, children: step === 'browsing'
45
+ ? '↑/k ↓/j navigate · Enter select · q exit'
46
+ : 'y confirm · n cancel' }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Box, { flexDirection: "column", width: 28, marginRight: 2, children: [_jsx(Text, { bold: true, color: "cyan", children: "Presets" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: presets.map((preset, index) => (_jsxs(Box, { children: [_jsx(Text, { children: index === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(preset.source === 'project' ? { color: 'yellow' } : {}), children: preset.name }), _jsxs(Text, { dimColor: true, children: [" (", preset.source, ")"] })] }, `${preset.source}:${preset.name}`))) })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, borderStyle: "round", borderColor: "red", paddingX: 1, children: [_jsx(Text, { bold: true, color: "red", children: activePreset.name }), _jsx(Text, { dimColor: true, children: activePreset.source === 'project' ? 'Project preset' : 'Global preset' }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: _jsx(EnvEntries, { entries: entries }) })] })] }), step === 'confirming' && (_jsxs(Box, { marginTop: 1, children: [_jsx(Text, { color: "red", children: "Delete preset " }), _jsx(Text, { bold: true, children: activePreset.name }), _jsx(Text, { color: "red", children: "?" }), _jsx(Text, { dimColor: true, children: " y/n" })] }))] }));
47
+ }
@@ -0,0 +1,27 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo, useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { EnvEntries } from './summary.js';
5
+ export function PresetListApp({ presets, }) {
6
+ const { exit } = useApp();
7
+ const [cursor, setCursor] = useState(0);
8
+ const activePreset = presets[cursor];
9
+ const entries = useMemo(() => activePreset
10
+ ? Object.entries(activePreset.env).sort(([a], [b]) => a.localeCompare(b))
11
+ : [], [activePreset]);
12
+ useInput((input, key) => {
13
+ if (key.escape || input.toLowerCase() === 'q') {
14
+ exit();
15
+ return;
16
+ }
17
+ if (key.upArrow || input === 'k') {
18
+ setCursor((c) => Math.max(0, c - 1));
19
+ return;
20
+ }
21
+ if (key.downArrow || input === 'j') {
22
+ setCursor((c) => Math.min(presets.length - 1, c + 1));
23
+ return;
24
+ }
25
+ });
26
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: "Preset list" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 q exit" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Box, { flexDirection: "column", width: 28, marginRight: 2, children: [_jsx(Text, { bold: true, color: "cyan", children: "Presets" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: presets.map((preset, index) => (_jsxs(Box, { children: [_jsx(Text, { children: index === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(preset.source === 'project' ? { color: 'yellow' } : {}), children: preset.name }), _jsxs(Text, { dimColor: true, children: [" (", preset.source, ")"] })] }, `${preset.source}:${preset.name}`))) })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, borderStyle: "round", borderColor: "green", paddingX: 1, children: [_jsx(Text, { bold: true, color: "green", children: activePreset?.name ?? 'Preview' }), _jsx(Text, { dimColor: true, children: activePreset?.source === 'project' ? 'Project preset' : 'Global preset' }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: _jsx(EnvEntries, { entries: entries }) })] })] })] }));
27
+ }
@@ -0,0 +1,27 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo, useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { EnvEntries } from './summary.js';
5
+ export function PresetShowApp({ presets, }) {
6
+ const { exit } = useApp();
7
+ const [cursor, setCursor] = useState(0);
8
+ const activePreset = presets[cursor];
9
+ const entries = useMemo(() => activePreset
10
+ ? Object.entries(activePreset.env).sort(([a], [b]) => a.localeCompare(b))
11
+ : [], [activePreset]);
12
+ useInput((input, key) => {
13
+ if (key.escape || input.toLowerCase() === 'q') {
14
+ exit();
15
+ return;
16
+ }
17
+ if (key.upArrow || input === 'k') {
18
+ setCursor((c) => Math.max(0, c - 1));
19
+ return;
20
+ }
21
+ if (key.downArrow || input === 'j') {
22
+ setCursor((c) => Math.min(presets.length - 1, c + 1));
23
+ return;
24
+ }
25
+ });
26
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: "Preset show" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 q exit" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Box, { flexDirection: "column", width: 28, marginRight: 2, children: [_jsx(Text, { bold: true, color: "cyan", children: "Presets" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: presets.map((preset, index) => (_jsxs(Box, { children: [_jsx(Text, { children: index === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(preset.source === 'project' ? { color: 'yellow' } : {}), children: preset.name }), _jsxs(Text, { dimColor: true, children: [" (", preset.source, ")"] })] }, `${preset.source}:${preset.name}`))) })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, borderStyle: "round", borderColor: "green", paddingX: 1, children: [_jsx(Text, { bold: true, color: "green", children: activePreset?.name ?? 'Preview' }), _jsx(Text, { dimColor: true, children: activePreset?.source === 'project' ? 'Project preset' : 'Global preset' }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: _jsx(EnvEntries, { entries: entries }) })] })] })] }));
27
+ }
@@ -0,0 +1,102 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useEffect, useMemo, useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { advanceRestoreFlow } from '../flows/restore-flow.js';
5
+ import { EnvEntries, EnvSummary } from './summary.js';
6
+ export function RestoreApp({ state, onSubmit, }) {
7
+ const { exit } = useApp();
8
+ const [currentState, setCurrentState] = useState(state);
9
+ const [cursor, setCursor] = useState(0);
10
+ const recordAtCursor = currentState.records[cursor];
11
+ const selectedRecord = currentState.records.find((record) => record.timestamp === currentState.selectedTimestamp);
12
+ const activeRecord = currentState.step === 'record'
13
+ ? recordAtCursor
14
+ : selectedRecord ?? currentState.records[0];
15
+ const restoreEntries = useMemo(() => activeRecord
16
+ ? Object.entries(activeRecord.action === 'init'
17
+ ? Object.fromEntries(activeRecord.sources.flatMap((s) => Object.entries(s.backup)))
18
+ : activeRecord.backup).sort(([left], [right]) => left.localeCompare(right))
19
+ : [], [activeRecord]);
20
+ const fromFiles = useMemo(() => {
21
+ if (!activeRecord || activeRecord.action !== 'init') {
22
+ return [];
23
+ }
24
+ return activeRecord.shellWrites.map((sw) => sw.filePath);
25
+ }, [activeRecord]);
26
+ const toFiles = useMemo(() => {
27
+ if (!activeRecord || activeRecord.action !== 'init') {
28
+ return [];
29
+ }
30
+ return activeRecord.sources.map((s) => s.file);
31
+ }, [activeRecord]);
32
+ useEffect(() => {
33
+ setCurrentState(state);
34
+ setCursor(0);
35
+ }, [state]);
36
+ useEffect(() => {
37
+ if (!onSubmit || currentState.records.length !== 0) {
38
+ return;
39
+ }
40
+ onSubmit({
41
+ confirmed: false,
42
+ });
43
+ exit();
44
+ }, [currentState.records.length, exit, onSubmit]);
45
+ useInput((input, key) => {
46
+ if (!onSubmit) {
47
+ return;
48
+ }
49
+ if (key.escape || input.toLowerCase() === 'q') {
50
+ onSubmit({ confirmed: false });
51
+ exit();
52
+ return;
53
+ }
54
+ if (currentState.step === 'record') {
55
+ if (key.upArrow || input === 'k') {
56
+ setCursor((value) => Math.max(0, value - 1));
57
+ return;
58
+ }
59
+ if (key.downArrow || input === 'j') {
60
+ setCursor((value) => Math.min(currentState.records.length - 1, value + 1));
61
+ return;
62
+ }
63
+ }
64
+ if (!key.return || !activeRecord) {
65
+ return;
66
+ }
67
+ if (currentState.step === 'record') {
68
+ setCurrentState((previousState) => advanceRestoreFlow(previousState, {
69
+ type: 'select-record',
70
+ timestamp: activeRecord.timestamp,
71
+ }));
72
+ return;
73
+ }
74
+ if (currentState.step === 'target') {
75
+ if (activeRecord.action !== 'restore') {
76
+ return;
77
+ }
78
+ setCurrentState((previousState) => advanceRestoreFlow(previousState, {
79
+ type: 'select-target',
80
+ targetType: activeRecord.targetType,
81
+ ...(activeRecord.targetType === 'preset'
82
+ ? { targetName: activeRecord.targetName }
83
+ : {}),
84
+ }));
85
+ return;
86
+ }
87
+ if (currentState.step === 'confirm') {
88
+ onSubmit({
89
+ confirmed: true,
90
+ timestamp: activeRecord.timestamp,
91
+ ...(currentState.targetType ? { targetType: currentState.targetType } : {}),
92
+ ...(currentState.targetType === 'preset' && 'targetName' in currentState
93
+ ? { targetName: currentState.targetName }
94
+ : {}),
95
+ });
96
+ exit();
97
+ }
98
+ });
99
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { children: "Restore record" }), currentState.step === 'record' ? (_jsxs(_Fragment, { children: [_jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 enter confirm \u00B7 q cancel" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Box, { flexDirection: "column", width: 28, marginRight: 2, children: [_jsx(Text, { bold: true, color: "cyan", children: "History" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: currentState.records.map((record, index) => (_jsxs(Text, { children: [index === cursor ? '❯ ' : ' ', record.timestamp] }, record.timestamp))) })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, borderStyle: "round", borderColor: "green", paddingX: 1, children: [_jsx(Text, { bold: true, color: "green", children: "Preview" }), activeRecord?.action === 'init' ? (_jsxs(Box, { flexDirection: "column", children: [fromFiles.length > 0 ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "From:" }), fromFiles.map((file) => (_jsxs(Text, { color: "cyan", children: [" ", file] }, file)))] })) : null, toFiles.length > 0 ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "To:" }), toFiles.map((file) => (_jsxs(Text, { color: "cyan", children: [" ", file] }, file)))] })) : null] })) : (_jsxs(Text, { dimColor: true, children: ["Restore to ", activeRecord?.targetType === 'preset' ? `preset ${activeRecord.targetName}` : activeRecord?.targetType ?? 'settings'] })), _jsx(Box, { flexDirection: "column", marginTop: 1, children: _jsx(EnvEntries, { entries: restoreEntries }) })] })] })] })) : null, currentState.step === 'target' ? (_jsxs(Text, { children: ["Select target for ", selectedRecord?.timestamp ?? 'record', ": settings or preset"] })) : null, currentState.step === 'confirm' && selectedRecord?.action === 'init' ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: ["Confirm restore from ", _jsx(Text, { color: "cyan", children: selectedRecord.timestamp })] }), _jsx(EnvSummary, { title: "Will restore", entries: restoreEntries, ...(fromFiles.length > 0 ? { fromFiles } : {}), ...(toFiles.length > 0 ? { toFiles } : {}) })] })) : null, currentState.step === 'confirm' && selectedRecord?.action !== 'init' ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsxs(Text, { children: ["Confirm restore from ", _jsx(Text, { color: "cyan", children: selectedRecord?.timestamp ?? 'record' }), " to", ' ', _jsx(Text, { color: "green", children: currentState.targetType === 'preset'
100
+ ? `preset ${currentState.targetName}`
101
+ : currentState.targetType ?? 'settings' })] }), _jsx(EnvSummary, { title: "Will restore", entries: restoreEntries })] })) : null, currentState.step === 'done' ? (_jsxs(Text, { color: "green", children: ['\n', "Restore complete"] })) : null, currentState.step !== 'done' ? (_jsx(Text, { children: "Press Enter to confirm or q to cancel" })) : null] }));
102
+ }
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo, useState } from 'react';
3
+ import { Box, Text, useApp, useInput } from 'ink';
4
+ import { EnvEntries } from './summary.js';
5
+ export function RunPresetSelectApp({ presets, defaultIndex = 0, onSubmit }) {
6
+ const { exit } = useApp();
7
+ const [cursor, setCursor] = useState(defaultIndex);
8
+ const active = presets[cursor];
9
+ const entries = useMemo(() => active
10
+ ? Object.entries(active.env).sort(([a], [b]) => a.localeCompare(b))
11
+ : [], [active]);
12
+ useInput((input, key) => {
13
+ if (key.escape || input.toLowerCase() === 'q') {
14
+ exit();
15
+ return;
16
+ }
17
+ if (key.upArrow || input === 'k') {
18
+ setCursor((c) => Math.max(0, c - 1));
19
+ return;
20
+ }
21
+ if (key.downArrow || input === 'j') {
22
+ setCursor((c) => Math.min(presets.length - 1, c + 1));
23
+ return;
24
+ }
25
+ if (key.return && active) {
26
+ onSubmit(active);
27
+ exit();
28
+ }
29
+ });
30
+ return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, children: "Select a preset" }), _jsx(Text, { dimColor: true, children: "\u2191/k \u2193/j navigate \u00B7 enter select \u00B7 q cancel" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Box, { flexDirection: "column", width: 28, marginRight: 2, children: [_jsx(Text, { bold: true, color: "cyan", children: "Presets" }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: presets.map((p, i) => (_jsxs(Box, { children: [_jsx(Text, { children: i === cursor ? '❯ ' : ' ' }), _jsx(Text, { ...(p.source === 'project' ? { color: 'yellow' } : {}), children: p.name }), _jsxs(Text, { dimColor: true, children: [" (", p.source, ")"] })] }, `${p.source}:${p.name}`))) })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, borderStyle: "round", borderColor: "green", paddingX: 1, children: [_jsx(Text, { bold: true, color: "green", children: active?.name ?? 'Preview' }), _jsx(Text, { dimColor: true, children: active?.source === 'project' ? 'Project preset' : 'Global preset' }), _jsx(Box, { flexDirection: "column", marginTop: 1, children: _jsx(EnvEntries, { entries: entries }) })] })] })] }));
31
+ }
@@ -0,0 +1,28 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import React from 'react';
3
+ import { Box, Text, render } from 'ink';
4
+ const h = React.createElement;
5
+ import { maskValue } from '../core/mask.js';
6
+ export function EnvEntries({ entries, mask }) {
7
+ if (entries.length === 0) {
8
+ return _jsx(Text, { dimColor: true, children: "none" });
9
+ }
10
+ return _jsx(_Fragment, { children: entries.map(([key, value]) => (_jsxs(Box, { children: [_jsx(Text, { color: "yellow", children: "\u2022 " }), _jsx(Text, { color: "magenta", children: key }), _jsx(Text, { dimColor: true, children: "=" }), _jsx(Text, { color: "white", children: mask ? maskValue(key, value) : value })] }, key))) });
11
+ }
12
+ export function EnvSummary({ title, entries, description, fromFiles, toFiles, footer, mask, }) {
13
+ return (_jsxs(Box, { flexDirection: "column", children: [description ? _jsx(Text, { dimColor: true, children: description }) : null, fromFiles && fromFiles.length > 0 ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "From:" }), fromFiles.map((file) => (_jsxs(Text, { color: "cyan", children: [" ", file] }, file)))] })) : null, toFiles && toFiles.length > 0 ? (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "To:" }), toFiles.map((file) => (_jsxs(Text, { color: "cyan", children: [" ", file] }, file)))] })) : null, _jsxs(Box, { flexDirection: "column", marginTop: 1, borderStyle: "round", borderColor: "green", paddingX: 1, children: [_jsx(Text, { bold: true, color: "green", children: title }), _jsx(EnvEntries, { entries: entries, ...(mask ? { mask } : {}) })] }), footer ?? null] }));
14
+ }
15
+ export async function renderEnvSummary(props) {
16
+ const entries = Object.entries(props.env).sort(([a], [b]) => a.localeCompare(b));
17
+ const app = render(h(EnvSummary, {
18
+ title: props.title,
19
+ entries,
20
+ mask: true,
21
+ ...(props.description ? { description: props.description } : {}),
22
+ ...(props.fromFiles ? { fromFiles: props.fromFiles } : {}),
23
+ ...(props.toFiles ? { toFiles: props.toFiles } : {}),
24
+ ...(props.footer ? { footer: props.footer } : {}),
25
+ }));
26
+ app.unmount();
27
+ await app.waitUntilExit();
28
+ }
@@ -0,0 +1,55 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { atomicWriteFile } from '../core/fs.js';
3
+ import { resolveClaudeSettingsLocalPath, resolveClaudeSettingsPath, resolveProjectSettingsLocalPath, resolveProjectSettingsPath, } from '../core/paths.js';
4
+ import { envMapSchema } from '../core/schema.js';
5
+ export function createClaudeSettingsEnvService({ homeDir, cwd } = {}) {
6
+ const paths = [
7
+ resolveClaudeSettingsPath(homeDir),
8
+ resolveClaudeSettingsLocalPath(homeDir),
9
+ resolveProjectSettingsPath(cwd),
10
+ resolveProjectSettingsLocalPath(cwd),
11
+ ];
12
+ async function readOne(path) {
13
+ try {
14
+ const content = await readFile(path, 'utf8');
15
+ const json = JSON.parse(content);
16
+ return {
17
+ path,
18
+ exists: true,
19
+ env: envMapSchema.parse(json.env ?? {}),
20
+ };
21
+ }
22
+ catch (error) {
23
+ if (error.code === 'ENOENT') {
24
+ return {
25
+ path,
26
+ exists: false,
27
+ env: envMapSchema.parse({}),
28
+ };
29
+ }
30
+ throw error;
31
+ }
32
+ }
33
+ async function writeOne(path, env) {
34
+ let json = {};
35
+ try {
36
+ const content = await readFile(path, 'utf8');
37
+ json = JSON.parse(content);
38
+ }
39
+ catch (error) {
40
+ if (error.code !== 'ENOENT') {
41
+ throw error;
42
+ }
43
+ }
44
+ json.env = envMapSchema.parse(env);
45
+ await atomicWriteFile(path, `${JSON.stringify(json, null, 2)}\n`);
46
+ }
47
+ return {
48
+ read: () => Promise.all(paths.map(readOne)),
49
+ write: async (sources) => {
50
+ for (const { path, env } of sources) {
51
+ await writeOne(path, env);
52
+ }
53
+ },
54
+ };
55
+ }
@@ -0,0 +1,26 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { configSchema } from '../core/schema.js';
3
+ import { atomicWriteFile } from '../core/fs.js';
4
+ import { resolveConfigPath } from '../core/paths.js';
5
+ export function createConfigService(globalRoot) {
6
+ const filePath = resolveConfigPath(globalRoot);
7
+ return {
8
+ async read() {
9
+ try {
10
+ const content = await readFile(filePath, 'utf8');
11
+ return configSchema.parse(JSON.parse(content));
12
+ }
13
+ catch (error) {
14
+ if (error.code === 'ENOENT') {
15
+ return configSchema.parse({});
16
+ }
17
+ throw error;
18
+ }
19
+ },
20
+ async write(config) {
21
+ const parsed = configSchema.parse(config);
22
+ await atomicWriteFile(filePath, `${JSON.stringify(parsed, null, 2)}\n`);
23
+ return parsed;
24
+ },
25
+ };
26
+ }