138
/**
139
* Render a path for embedding in a shell rc file. `~` never expands inside
140
* double quotes, so home-relative paths use a literal `$HOME` prefix instead.
141
+ * Must stay in lockstep with install.sh's render_bin_dir: the installer's
142
+ * delegated_setup_verified compares its own rendering against the line this
143
+ * command wrote byte for byte, so any disagreement makes every delegated
144
+ * install look stale and forces a needless inline rewrite.
145
*/
142
-const renderWithHome = (dir: string, homedir: string): string =>
146
+export const renderWithHome = (dir: string, homedir: string): string =>
147
dir === homedir
148
? '$HOME'
149
: dir.startsWith(`${homedir}/`)
150
? `$HOME/${dir.slice(homedir.length + 1)}`
151
: dir;
152
149
-const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {
153
+export const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {
154
const rendered = renderWithHome(binDir, homedir);
155
return shell === 'fish'
156
? [MARKER, `set --export PATH "${rendered}" $PATH`].join('\n')
246
completionBlock: completionScript ? `${COMPLETIONS_MARKER}\n${completionScript}` : undefined,
247
});
248
245
-/** Check whether a file already contains a given marker line. */
249
+/**
250
+ * Check whether a file already contains a given marker line. Byte-exact, like
251
+ * install.sh's awk `$0 == "# Composio CLI"` match: a marker mangled by CRLF
252
+ * endings or stray whitespace is not ours and is left alone on both sides.
253
+ */
254
const fileContains = (contents: string, marker: string): boolean =>
247
- contents.split('\n').some(line => line.trim() === marker.trim());
255
+ contents.split('\n').some(line => line === marker);
256
257
/** Outcome of reconciling a startup file's managed PATH block against the resolved bin dir. */
258
type ManagedPathBlockState =
274
);
275
};
276
277
+/**
278
+ * Append managed blocks after existing content, separated by one blank line
279
+ * and ending with a newline. Content lacking a final newline gains one first,
280
+ * exactly as install.sh's line-based awk rewrite normalizes it.
281
+ */
282
+export const appendManagedBlocks = (contents: string, blocks: readonly string[]): string => {
283
+ const base = contents === '' || contents.endsWith('\n') ? contents : `${contents}\n`;
284
+ return `${base}\n${blocks.join('\n\n')}\n`;
285
+};
286
+
287
/**
288
* Reconcile the managed PATH block — the `MARKER` line plus the PATH
289
* assignment right below it — with the currently resolved bin directory.
272
- * A well-formed block recording a stale bin dir is replaced in place,
273
- * duplicate managed PATH blocks collapse into a single current block, and
274
- * unmanaged content (including the completions block) is left byte-for-byte
275
- * untouched.
290
*
277
- * A marker whose next line is not a recognizable managed assignment (a user
278
- * annotation, a blank line, another marker) is malformed: only the marker
279
- * line itself is removed, and the fresh block is appended after all remaining
280
- * content instead of replacing in place. That guarantees no user content is
281
- * ever deleted, and — because these prepend-style assignments make the
282
- * last-sourced line win — the new bin dir still takes PATH precedence over
283
- * any orphaned stale assignment left behind.
291
+ * This mirrors install.sh's write_path_block awk program step for step, and
292
+ * the shared fixtures under test/managed-block-fixtures pin both
293
+ * implementations to byte-identical outputs — any behavioral edit here must
294
+ * land in install.sh too. A file whose single byte-exact marker is directly
295
+ * followed by the expected assignment is current and left untouched.
296
+ * Otherwise every managed line — each marker, plus the next line only when it
297
+ * is a recognizable managed PATH assignment; anything else (a user
298
+ * annotation, a blank line, another marker) is not ours to delete — is
299
+ * removed and one fresh block is appended after the remaining content. No
300
+ * user content is ever deleted, and because these prepend-style assignments
301
+ * make the last-sourced line win, the new bin dir takes PATH precedence over
302
+ * any orphaned stale assignment that had to be preserved.
303
*/
285
-const reconcileManagedPathBlock = (contents: string, pathBlock: string): ManagedPathBlockState => {
304
+export const reconcileManagedPathBlock = (
305
+ contents: string,
306
+ pathBlock: string
307
+): ManagedPathBlockState => {
308
const lines = contents.split('\n');
287
- const markerIndexes = lines.flatMap((line, index) => (line.trim() === MARKER ? [index] : []));
309
+ const markerIndexes = lines.flatMap((line, index) => (line === MARKER ? [index] : []));
310
const firstMarkerIndex = markerIndexes[0];
311
if (firstMarkerIndex === undefined) return { state: 'absent' };
312
313
const blockLines = pathBlock.split('\n');
292
- const expectedAssignment = (blockLines[1] ?? '').trim();
293
- if (
294
- markerIndexes.length === 1 &&
295
- (lines[firstMarkerIndex + 1] ?? '').trim() === expectedAssignment
296
- ) {
314
+ if (markerIndexes.length === 1 && lines[firstMarkerIndex + 1] === blockLines[1]) {
315
return { state: 'current' };
316
}
317
325
: [markerIndex];
326
})
327
);
310
- const hasMalformedBlock = markerIndexes.some(
311
- markerIndex => !isManagedPathAssignment(lines[markerIndex + 1] ?? '')
312
- );
328
314
- if (!hasMalformedBlock) {
315
- const reconciledLines = lines.flatMap((line, index) => {
316
- if (index === firstMarkerIndex) return blockLines;
317
- return managedLineIndexes.has(index) ? [] : [line];
318
- });
319
- return { state: 'stale', reconciled: reconciledLines.join('\n') };
320
- }
321
-
322
- const keptLines = lines.flatMap((line, index) => (managedLineIndexes.has(index) ? [] : [line]));
323
- const endsWithNewline = keptLines[keptLines.length - 1] === '';
324
- const reconciledLines = endsWithNewline
325
- ? [...keptLines.slice(0, -1), ...blockLines, '']
326
- : [...keptLines, ...blockLines];
327
- return { state: 'stale', reconciled: reconciledLines.join('\n') };
329
+ // Like the awk rewrite, kept content is a sequence of newline-terminated
330
+ // lines: drop the split's trailing sentinel and re-terminate the last line.
331
+ const keptLines = lines.filter((_, index) => !managedLineIndexes.has(index));
332
+ const contentLines = keptLines[keptLines.length - 1] === '' ? keptLines.slice(0, -1) : keptLines;
333
+ const kept = contentLines.length === 0 ? '' : contentLines.join('\n') + '\n';
334
+ return { state: 'stale', reconciled: appendManagedBlocks(kept, [pathBlock]) };
335
};
336
337
interface ManagedBlockReplacement {
432
)
433
);
434
428
- const appendContent = '\n' + blocks.join('\n\n') + '\n';
429
- yield* replaceFilePreservingMode(fs, writeTarget, existingContents + appendContent);
435
+ yield* replaceFilePreservingMode(
436
+ fs,
437
+ writeTarget,
438
+ appendManagedBlocks(existingContents, blocks)
439
+ );
440
yield* params.report(`Updated ${tildify(filePath, params.homedir)}`);
441
}
442
});