This commit is contained in:
2025-08-28 15:47:59 +00:00
parent 1c2310c185
commit 33fb02733d
30 changed files with 1061 additions and 1070 deletions

16
ts/cli/helpers/argv.ts Normal file
View File

@@ -0,0 +1,16 @@
import type { CliArguments } from '../types.js';
// Argument parsing helpers
export const getBool = (argv: CliArguments, ...keys: string[]) =>
keys.some(k => Boolean((argv as any)[k]));
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) => {
const v = (argv as any)[key];
return typeof v === 'string' ? v : fallback;
};

14
ts/cli/helpers/errors.ts Normal file
View File

@@ -0,0 +1,14 @@
// 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')) {
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)');
} else {
console.error(`Error ${action}:`, error.message);
}
process.exit(1);
}

View File

@@ -0,0 +1,17 @@
// Helper function for padding strings
export function pad(str: string, length: number): string {
return str.length > length
? str.substring(0, length - 3) + '...'
: str.padEnd(length);
}
// Helper for unknown errors
export const unknownError = (err: any) =>
(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]';
return `${timestamp} ${prefix} ${log.message}`;
}

View File

@@ -0,0 +1,22 @@
// Streaming lifecycle helper
export function withStreamingLifecycle(
setup: () => Promise<void>,
teardown: () => Promise<void>,
) {
let isCleaningUp = false;
const cleanup = async () => {
if (isCleaningUp) return;
isCleaningUp = true;
try {
await teardown();
} finally {
process.exit(0);
}
};
process.once('SIGINT', cleanup);
process.once('SIGTERM', cleanup);
return (async () => {
await setup();
await new Promise(() => {}); // keep alive
})();
}

33
ts/cli/helpers/memory.ts Normal file
View File

@@ -0,0 +1,33 @@
// Helper function to parse memory strings (e.g., "512MB", "2GB")
export function parseMemoryString(memStr: string): number {
const units = {
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024,
};
const match = memStr.toUpperCase().match(/^(\d+(?:\.\d+)?)\s*(KB|MB|GB)?$/);
if (!match) {
throw new Error(
`Invalid memory format: ${memStr}. Use format like "512MB" or "2GB"`,
);
}
const value = parseFloat(match[1]);
const unit = (match[2] || 'MB') as keyof typeof units;
return Math.floor(value * units[unit]);
}
// Helper function to format memory for display
export function formatMemory(bytes: number): string {
if (bytes >= 1024 * 1024 * 1024) {
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
} else if (bytes >= 1024 * 1024) {
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
} else if (bytes >= 1024) {
return `${(bytes / 1024).toFixed(1)} KB`;
} else {
return `${bytes} B`;
}
}