BREAKING CHANGE(cli): Add persistent process registration (tspm add), alias remove, and change start to use saved process IDs (breaking CLI behavior)

This commit is contained in:
2025-08-29 09:43:54 +00:00
parent 0427d38c7d
commit 4db128edaf
10 changed files with 263 additions and 142 deletions

View File

@@ -17,53 +17,56 @@ import { ensureDaemonOrHint } from './daemon-check.js';
*/
export function registerIpcCommand(
smartcli: plugins.smartcli.Smartcli,
name: string,
name: string | string[],
action: CommandAction,
opts: IpcCommandOptions = {},
) {
const { actionLabel = name, keepAlive = false, requireDaemon = true } = opts;
const names = Array.isArray(name) ? name : [name];
for (const singleName of names) {
const { actionLabel = singleName, keepAlive = false, requireDaemon = true } = opts;
smartcli.addCommand(name).subscribe({
next: async (argv: CliArguments) => {
// Early preflight for better UX
const ok = await ensureDaemonOrHint(requireDaemon, actionLabel);
if (!ok) {
process.exit(1);
return;
}
// Evaluate keepAlive - can be boolean or function
const shouldKeepAlive =
typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
if (shouldKeepAlive) {
// Let action manage its own connection/cleanup lifecycle
try {
await action(argv);
} catch (error) {
handleDaemonError(error, actionLabel);
smartcli.addCommand(singleName).subscribe({
next: async (argv: CliArguments) => {
// Early preflight for better UX
const ok = await ensureDaemonOrHint(requireDaemon, actionLabel);
if (!ok) {
process.exit(1);
return;
}
} else {
// Auto-disconnect pattern for one-shot IPC commands
await runIpcCommand(async () => {
// Evaluate keepAlive - can be boolean or function
const shouldKeepAlive =
typeof keepAlive === 'function' ? keepAlive(argv) : keepAlive;
if (shouldKeepAlive) {
// Let action manage its own connection/cleanup lifecycle
try {
await action(argv);
} catch (error) {
handleDaemonError(error, actionLabel);
}
});
}
},
error: (err) => {
// Fallback error path (should be rare with try/catch in next)
console.error(
`Unexpected error in command "${name}":`,
unknownError(err),
);
process.exit(1);
},
complete: () => {},
});
} else {
// Auto-disconnect pattern for one-shot IPC commands
await runIpcCommand(async () => {
try {
await action(argv);
} catch (error) {
handleDaemonError(error, actionLabel);
}
});
}
},
error: (err) => {
// Fallback error path (should be rare with try/catch in next)
console.error(
`Unexpected error in command "${singleName}":`,
unknownError(err),
);
process.exit(1);
},
complete: () => {},
});
}
}
/**