31
32
interface ShellConfig {
33
readonly shell: Shell;
34
- readonly rcFile: string;
34
+ readonly pathFile: string;
35
+ readonly completionFile: string;
36
readonly pathBlock: string;
37
readonly completionBlock: string | undefined;
38
}
109
shell: Shell,
110
rcFile: string,
111
installDir: string,
111
- completionScript: string | undefined
112
+ completionScript: string | undefined,
113
+ homedir: string
114
): ShellConfig => ({
115
shell,
114
- rcFile,
116
+ pathFile: rcFile,
117
+ completionFile:
118
+ shell === 'fish'
119
+ ? path.join(homedir, '.config', 'fish', 'completions', 'composio.fish')
120
+ : rcFile,
121
pathBlock: pathBlockForShell(shell, installDir),
122
completionBlock: completionScript ? `${COMPLETIONS_MARKER}\n${completionScript}` : undefined,
123
});
129
const tildify = (p: string, homedir: string): string =>
130
p.startsWith(homedir + '/') ? `~/${p.slice(homedir.length + 1)}` : p;
131
132
+const readMaybeMissingFile = (
133
+ filePath: string,
134
+ fs: FileSystem.FileSystem
135
+): Effect.Effect<string, PlatformError> =>
136
+ fs
137
+ .readFileString(filePath)
138
+ .pipe(
139
+ Effect.catchAll(e =>
140
+ Effect.logDebug('File does not exist yet, will create:', e).pipe(Effect.as(''))
141
+ )
142
+ );
143
+
144
// ---------------------------------------------------------------------------
145
// Exported logic (reusable from install.sh post-install delegation)
146
// ---------------------------------------------------------------------------
205
}
206
207
const rcFile = yield* resolveRcFile(rcFileCandidates(shell, os.homedir), fs);
190
- const config = buildShellConfig(shell, rcFile, installDir, completionScript);
191
-
192
- // Read existing rc file (or empty if it doesn't exist yet)
193
- const rcPath = config.rcFile;
194
- const existing = yield* fs
195
- .readFileString(rcPath)
196
- .pipe(
197
- Effect.catchAll(e =>
198
- Effect.logDebug('RC file does not exist yet, will create:', e).pipe(Effect.as(''))
199
- )
200
- );
208
+ const config = buildShellConfig(shell, rcFile, installDir, completionScript, os.homedir);
209
202
- // Build blocks to append (idempotently)
203
- const blocks: string[] = [];
210
+ const uniqueTargetFiles = [...new Set([config.pathFile, config.completionFile])];
211
+ const existingByFile = new Map<string, string>();
212
+ for (const filePath of uniqueTargetFiles) {
213
+ existingByFile.set(filePath, yield* readMaybeMissingFile(filePath, fs));
214
+ }
215
205
- if (!fileContains(existing, MARKER)) {
206
- blocks.push(config.pathBlock);
216
+ const blocksByFile = new Map<string, Array<string>>();
217
+ const pushBlock = (filePath: string, block: string) => {
218
+ const blocks = blocksByFile.get(filePath);
219
+ if (blocks) {
220
+ blocks.push(block);
221
+ } else {
222
+ blocksByFile.set(filePath, [block]);
223
+ }
224
+ };
225
+
226
+ const existingPathFile = existingByFile.get(config.pathFile) ?? '';
227
+ if (!fileContains(existingPathFile, MARKER)) {
228
+ pushBlock(config.pathFile, config.pathBlock);
229
yield* ui.log.step(`PATH: will add ${tildify(installDir, os.homedir)} to $PATH`);
230
} else {
231
yield* ui.log.step('PATH: already configured');
235
yield* ui.log.step('Completions: skipped for zsh');
236
} else if (!params.completions) {
237
yield* ui.log.step('Completions: skipped by default (pass --completions to enable)');
216
- } else if (config.completionBlock && !fileContains(existing, COMPLETIONS_MARKER)) {
217
- blocks.push(config.completionBlock);
218
- yield* ui.log.step('Completions: will install shell completions');
238
} else if (!config.completionBlock) {
239
yield* ui.log.step('Completions: not available for this shell');
240
} else {
222
- yield* ui.log.step('Completions: already configured');
241
+ const existingCompletionFile = existingByFile.get(config.completionFile) ?? '';
242
+ if (!fileContains(existingCompletionFile, COMPLETIONS_MARKER)) {
243
+ pushBlock(config.completionFile, config.completionBlock);
244
+ yield* ui.log.step(
245
+ config.shell === 'fish'
246
+ ? `Completions: will install fish completions to ${tildify(config.completionFile, os.homedir)}`
247
+ : 'Completions: will install shell completions'
248
+ );
249
+ } else {
250
+ yield* ui.log.step('Completions: already configured');
251
+ }
252
}
253
225
- if (blocks.length > 0) {
226
- // Ensure parent directory exists (for fish config)
227
- yield* fs
228
- .makeDirectory(path.dirname(rcPath), { recursive: true })
229
- .pipe(
230
- Effect.catchAll(e =>
231
- Effect.logDebug('Could not create parent directory (may already exist):', e)
232
- )
233
- );
254
+ if (blocksByFile.size > 0) {
255
+ for (const [filePath, blocks] of blocksByFile.entries()) {
256
+ const existingContents = existingByFile.get(filePath) ?? '';
257
235
- const appendContent = '\n' + blocks.join('\n\n') + '\n';
258
+ yield* fs
259
+ .makeDirectory(path.dirname(filePath), { recursive: true })
260
+ .pipe(
261
+ Effect.catchAll(e =>
262
+ Effect.logDebug('Could not create parent directory (may already exist):', e)
263
+ )
264
+ );
265
237
- // Atomic write: write to a temp file then rename, so a crash mid-write
238
- // cannot leave the user's rc file truncated/corrupted.
239
- const tmpPath = `${rcPath}.composio-tmp`;
240
- yield* fs.writeFileString(tmpPath, existing + appendContent);
241
- yield* fs.rename(tmpPath, rcPath);
266
+ const appendContent = '\n' + blocks.join('\n\n') + '\n';
267
+ const tmpPath = `${filePath}.composio-tmp`;
268
243
- yield* ui.log.success(`Updated ${tildify(rcPath, os.homedir)}`);
269
+ yield* fs.writeFileString(tmpPath, existingContents + appendContent);
270
+ yield* fs.rename(tmpPath, filePath);
271
+
272
+ yield* ui.log.success(`Updated ${tildify(filePath, os.homedir)}`);
273
+ }
274
+ } else {
275
+ yield* ui.log.success('Shell integration already configured — nothing to do.');
276
+ }
277
+
278
+ if (blocksByFile.size > 0) {
279
yield* ui.note(
245
- shell === 'fish' || shell === 'zsh'
246
- ? `source ${tildify(rcPath, os.homedir)}`
247
- : 'exec $SHELL',
280
+ shell === 'fish'
281
+ ? 'exec fish'
282
+ : shell === 'zsh'
283
+ ? `source ${tildify(rcFile, os.homedir)}`
284
+ : 'exec $SHELL',
285
'Restart your shell to apply changes'
286
);
250
- } else {
251
- yield* ui.log.success('Shell integration already configured — nothing to do.');
287
}
288
289
yield* ui.outro('Done');