BREAKING CHANGE(daemon): Refactor daemon and service management: remove IPC auto-spawn, add TspmServiceManager, tighten IPC/client/CLI behavior and tests

This commit is contained in:
2025-08-28 15:52:29 +00:00
parent 8e3cfb624b
commit e73f4acd63
38 changed files with 810 additions and 580 deletions

View File

@@ -2,15 +2,23 @@ import type { CliArguments } from '../types.js';
// Argument parsing helpers
export const getBool = (argv: CliArguments, ...keys: string[]) =>
keys.some(k => Boolean((argv as any)[k]));
keys.some((k) => Boolean((argv as any)[k]));
export const getNumber = (argv: CliArguments, key: string, fallback: number) => {
export const getNumber = (
argv: CliArguments,
key: string,
fallback: number,
) => {
const v = (argv as any)[key];
const n = typeof v === 'string' ? Number(v) : v;
return Number.isFinite(n) ? n : fallback;
};
export const getString = (argv: CliArguments, key: string, fallback?: string) => {
export const getString = (
argv: CliArguments,
key: string,
fallback?: string,
) => {
const v = (argv as any)[key];
return typeof v === 'string' ? v : fallback;
};
};

View File

@@ -1,14 +1,18 @@
// Helper function to handle daemon connection errors
export function handleDaemonError(error: any, action: string): void {
if (error.message?.includes('daemon is not running') ||
error.message?.includes('Not connected') ||
error.message?.includes('ECONNREFUSED')) {
if (
error.message?.includes('daemon is not running') ||
error.message?.includes('Not connected') ||
error.message?.includes('ECONNREFUSED')
) {
console.error(`Error: Cannot ${action} - TSPM daemon is not running.`);
console.log('\nTo start the daemon, run one of:');
console.log(' tspm daemon start - Start for this session only');
console.log(' tspm enable - Enable as system service (recommended)');
console.log(
' tspm enable - Enable as system service (recommended)',
);
} else {
console.error(`Error ${action}:`, error.message);
}
process.exit(1);
}
}

View File

@@ -7,11 +7,12 @@ export function pad(str: string, length: number): string {
// Helper for unknown errors
export const unknownError = (err: any) =>
(err?.message && typeof err.message === 'string') ? err.message : String(err);
err?.message && typeof err.message === 'string' ? err.message : String(err);
// Helper function to format log entries
export function formatLog(log: any): string {
const timestamp = new Date(log.timestamp).toLocaleTimeString();
const prefix = log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
const prefix =
log.type === 'stdout' ? '[OUT]' : log.type === 'stderr' ? '[ERR]' : '[SYS]';
return `${timestamp} ${prefix} ${log.message}`;
}
}

View File

@@ -19,4 +19,4 @@ export function withStreamingLifecycle(
await setup();
await new Promise(() => {}); // keep alive
})();
}
}

View File

@@ -30,4 +30,4 @@ export function formatMemory(bytes: number): string {
} else {
return `${bytes} B`;
}
}
}