-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc.ts
More file actions
335 lines (317 loc) · 11.8 KB
/
rc.ts
File metadata and controls
335 lines (317 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @file Write a managed `export VAR='<value>'` block to the user's shell rc
* ("run commands") file. macOS only for now. `rc` is the historical Unix
* suffix for shell-startup files (`.bashrc`, `.zshrc`, `.cshrc`) — short for
* "run commands", a convention from MIT's CTSS (1965) carried through Multics
* into Unix. This module writes a managed block into that file so the value
* is exported into every subsequent shell session. Use case: `readSecret()`
* from `./keychain` round-trips through the OS credential store on every
* call. On macOS each call triggers a Keychain auth prompt unless the
* keychain item's ACL allows the calling process. That's the right shape for
* tools that read the secret once per process — but if you wire
* `readSecret()` into a shell rc file directly, the user gets an auth prompt
* on every new shell. Claude Code's Bash tool spawns a fresh shell per
* command, which means continuous prompt flood. Solution: write the literal
* value into ~/.zshenv (or equivalent) **once**, at install time, so every
* subsequent shell session picks up the env var without re-reading the
* keychain. The keychain is still the canonical store; this helper is just a
* cached materialization that lives in the rc file. API: write({ service,
* exports, notes?, legacySentinels? }) → { rcPath, outcome: 'inserted' |
* 'updated' | 'unchanged' } | undefined clear(service, legacySentinels?) →
* boolean (true when a block was found and removed) Block layout (idempotent
* — re-running with the same exports returns `outcome: 'unchanged'`):
*
* # BEGIN <service> env (managed)
*
* # Token persisted by <installer>.
*
* # Rotate via: <rotate-command>
*
* export VAR_1='<value>' export VAR_2='<value>'
*
* # END <service> env (managed)
*
* Target file by shell: zsh → ~/.zshenv (sourced by every zsh, including
* non-interactive) bash → ~/.bashrc (or ~/.bash_profile fallback) For zsh we
* deliberately pick .zshenv (not .zshrc) because tools that spawn
* non-interactive shells (Claude Code, IDE plugins, CI runners) skip .zshrc
* and would miss the export.
*/
import { chmodSync, existsSync, readFileSync, writeFileSync } from 'node:fs'
import { platform } from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { getHome } from '../env/home'
export function buildBlock(opts: WriteOptions): {
begin: string
end: string
body: string
full: string
} {
// Symmetric BEGIN/END sentinels — the `(managed)` suffix is on
// both so a `BEGIN → END` text-substitution swap produces the
// matching END string for migration regexes.
const begin = `# BEGIN ${opts.service} env (managed)`
const end = `# END ${opts.service} env (managed)`
const noteLines = (opts.notes ?? []).map(line => `# ${line}`)
const exportLines = Object.entries(opts.exports).map(
([name, value]) => `export ${name}=${shellSingleQuote(value)}`,
)
const body = [...noteLines, ...exportLines].join('\n')
return {
begin,
end,
body,
full: `${begin}\n${body}\n${end}`,
}
}
/**
* Remove the managed block from the user's shell rc file. Used by an uninstall
* / clear flow. Returns `true` when a block was found and removed, `false` when
* no block was present.
*/
export function clear(
service: string,
legacySentinels: readonly string[] = [],
): boolean {
if (platform() !== 'darwin') {
return false
}
const rcPath = pickRcFile()
if (!rcPath || !existsSync(rcPath)) {
return false
}
let existing = readFileSync(rcPath, 'utf8')
let removedAny = false
const sentinelsToStrip = [
`# BEGIN ${service} env (managed)`,
...legacySentinels,
]
for (const begin of sentinelsToStrip) {
const end = begin.replace(/\bBEGIN\b/, 'END')
const endStripped = end.replace(/\s*\(managed\)\s*$/, '')
const endAlt =
end === endStripped
? escapeRegExp(end)
: `(?:${escapeRegExp(end)}|${escapeRegExp(endStripped)})`
const re = new RegExp(
`\n*${escapeRegExp(begin)}[\\s\\S]*?${endAlt}\n?`,
'g',
)
const next = existing.replace(re, '\n')
if (next !== existing) {
removedAny = true
existing = next
}
}
if (removedAny) {
writeRcFile(rcPath, existing.replace(/\n{3,}/g, '\n\n'))
}
return removedAny
}
export function escapeRegExp(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
export interface WriteOptions {
/**
* Logical service name. Used to compose the BEGIN/END sentinels (`# BEGIN
* <service> env (managed)`) so multiple tools can manage independent blocks
* in the same rc file without stepping on each other.
*/
service: string
/**
* Map of env-var name → literal value to export. Each entry becomes a single
* POSIX `export NAME='value'` line.
*/
exports: Record<string, string>
/**
* Optional doc-comment lines added at the top of the managed block (e.g.
* "Rotate via: my-installer --rotate"). Each entry is prefixed with `# `
* automatically.
*/
notes?: readonly string[]
/**
* Legacy sentinel BEGIN strings to sweep before writing the new block. Used
* during a rename/migration so an older managed block is removed rather than
* ignored. Each entry should be the literal BEGIN line; the function
* tolerates any line endings up to the matching END (same prefix with `END`
* replacing `BEGIN`).
*/
legacySentinels?: readonly string[]
/**
* Override the auto-detected shell. By default the helper reads `$SHELL` and
* targets the matching rc file:
*
* - Zsh → `~/.zshenv`
* - Bash → `~/.bashrc` (or `~/.bash_profile` if `~/.bashrc` is absent)
* - Fish → `~/.config/fish/config.fish`
*
* Useful when an installer is running under a different shell than the user
* normally uses (e.g. invoked from a sudo /bin/sh but the user is a zsh
* user).
*/
shell?: 'zsh' | 'bash' | 'fish' | undefined
/**
* Override the auto-picked rc path entirely. Use this when the user has a
* non-standard layout (chezmoi, dotfile managers, a separate
* `~/.zshenv.local` they source from `~/.zshenv`, etc.). The file is created
* if missing.
*/
rcPath?: string | undefined
}
export type WriteResult =
| {
rcPath: string
outcome: 'inserted' | 'updated' | 'unchanged'
}
| {
rcPath: undefined
outcome: 'skipped'
reason: 'unsupported-platform' | 'unknown-shell'
}
export function pickRcFile(
shellOverride?: 'zsh' | 'bash' | 'fish',
): string | undefined {
const home = getHome()
if (!home) {
return undefined
}
const shellPath = process.env['SHELL'] ?? ''
const shell: 'zsh' | 'bash' | 'fish' | undefined =
shellOverride ??
(shellPath.endsWith('zsh')
? 'zsh'
: shellPath.endsWith('bash')
? 'bash'
: shellPath.endsWith('fish')
? 'fish'
: undefined)
if (shell === 'zsh') {
return path.join(home, '.zshenv')
}
if (shell === 'bash') {
const bashrc = path.join(home, '.bashrc')
if (existsSync(bashrc)) {
return bashrc
}
const bashProfile = path.join(home, '.bash_profile')
if (existsSync(bashProfile)) {
return bashProfile
}
return bashrc
}
if (shell === 'fish') {
return path.join(home, '.config', 'fish', 'config.fish')
}
return undefined
}
export function shellSingleQuote(value: string): string {
return `'${value.replace(/'/g, "'\\''")}'`
}
/**
* Insert or update the managed env-var block in the user's shell run-commands
* file (`~/.zshenv` for zsh, `~/.bashrc` / `~/.bash_profile` for bash,
* `~/.config/fish/config.fish` for fish). macOS only — Linux + Windows return
* `{outcome: 'skipped', reason: 'unsupported-platform'}` so callers can fall
* back to a copy- pasteable instruction without special-casing an `undefined`
* return.
*
* The block is matched by BEGIN/END sentinels, so it coexists with other
* managed blocks (homebrew, nvm, etc.). Idempotent: re-running with the same
* exports produces `outcome: 'unchanged'` and doesn't touch the file.
*
* `legacySentinels` lets a consumer migrate the block from an older BEGIN
* string. Each legacy block (matched by `BEGIN <legacy>` → `END <legacy>`) is
* stripped before the new block is written.
*
* `shell` and `rcPath` override the auto-detected target — useful for chezmoi /
* dotfile-manager users or installers running under a non-default shell.
*/
export function write(opts: WriteOptions): WriteResult {
if (platform() !== 'darwin') {
return {
rcPath: undefined,
outcome: 'skipped',
reason: 'unsupported-platform',
}
}
const rcPath = opts.rcPath ?? pickRcFile(opts.shell)
if (!rcPath) {
return { rcPath: undefined, outcome: 'skipped', reason: 'unknown-shell' }
}
const { begin, end, full: desiredBlock } = buildBlock(opts)
let onDisk = ''
if (existsSync(rcPath)) {
onDisk = readFileSync(rcPath, 'utf8')
}
let working = onDisk
// Sweep legacy sentinels first (migration support). The END line
// is derived by replacing the first `BEGIN` with `END`; we also
// accept the same string with a trailing ` (managed)` stripped,
// since older sentinels were asymmetric (BEGIN had the qualifier,
// END didn't).
for (const legacyBegin of opts.legacySentinels ?? []) {
const legacyEnd = legacyBegin.replace(/\bBEGIN\b/, 'END')
const legacyEndStripped = legacyEnd.replace(/\s*\(managed\)\s*$/, '')
const endAlt =
legacyEnd === legacyEndStripped
? escapeRegExp(legacyEnd)
: `(?:${escapeRegExp(legacyEnd)}|${escapeRegExp(legacyEndStripped)})`
const legacyRe = new RegExp(
`\n*${escapeRegExp(legacyBegin)}[\\s\\S]*?${endAlt}\n?`,
'g',
)
working = working.replace(legacyRe, '\n')
}
const blockRe = new RegExp(
`${escapeRegExp(begin)}[\\s\\S]*?${escapeRegExp(end)}`,
)
const match = blockRe.exec(working)
if (match) {
if (match[0] === desiredBlock && working === onDisk) {
// Existing block already canonical AND no legacy sweep happened.
return { rcPath, outcome: 'unchanged' }
}
const rewritten =
working.slice(0, match.index) +
desiredBlock +
working.slice(match.index + match[0].length)
writeRcFile(rcPath, rewritten.replace(/\n{3,}/g, '\n\n'))
return { rcPath, outcome: 'updated' }
}
// No existing canonical block. Append the new block to the
// (possibly legacy-scrubbed) working copy and write the whole
// thing back.
const needsLeadingNewline = working.length > 0 && !working.endsWith('\n\n')
const prefix = needsLeadingNewline
? working.endsWith('\n')
? '\n'
: '\n\n'
: ''
const next = `${working}${prefix}${desiredBlock}\n`.replace(/\n{3,}/g, '\n\n')
writeRcFile(rcPath, next)
// If we scrubbed a legacy block, the outcome is logically an
// "updated" (replaced the old shape) — but the API only has
// 'inserted' / 'updated' / 'unchanged', and the new BEGIN/END
// sentinel didn't exist on disk before, so 'inserted' is honest.
return { rcPath, outcome: 'inserted' }
}
/**
* Internal: write an rc file with 0o600 (owner-only). The rc file embeds a
* literal SOCKET_API_KEY value so the shell rc can `export` it on session start
* without re-prompting the keychain. The file must NEVER be readable by other
* local users — `writeFileSync` with no `mode:` lands at `0o644` on first
* create (default umask 022), exposing the token on multi-user macOS / Linux
* machines. We unconditionally chmod after write so existing files with looser
* permissions also get tightened.
*/
export function writeRcFile(rcPath: string, contents: string): void {
writeFileSync(rcPath, contents, { mode: 0o600 })
try {
chmodSync(rcPath, 0o600)
} catch {
// chmod may fail on a filesystem that doesn't support POSIX modes
// (FAT32-on-USB, some network mounts). The writeFileSync mode is
// already the best we can do there.
}
}