Compare commits

...

6 Commits

13 changed files with 2508 additions and 160 deletions

@ -1,5 +1,27 @@
# Changelog
## 2025-03-10 - 1.5.1 - fix(core)
Improve error handling, logging, and test suite; update dependency versions
- Updated devDependencies versions in package.json (@git.zone/tsbuild, @push.rocks/tapbundle, and @push.rocks/smartdaemon)
- Refactored error handling and enhanced logging in ProcessMonitor and ProcessWrapper modules
- Improved test structure by adding clear module import tests and usage examples in test files
## 2025-03-04 - 1.5.0 - feat(cli)
Enhance CLI with new process management commands
- Added comprehensive CLI commands for process management including start, stop, restart, list, describe and logs.
- Implemented memory string parsing for process memory limits.
- Enhanced CLI output with formatted table listings for active processes.
## 2025-03-03 - 1.4.0 - feat(core)
Introduced process management features using ProcessWrapper and enhanced configuration.
- Added ProcessWrapper for wrapping and managing child processes.
- Refactored process monitoring logic using ProcessWrapper.
- Introduced TspmConfig for configuration handling.
- Enhanced CLI to support new process management commands like 'startAsDaemon'.
## 2025-03-01 - 1.3.1 - fix(test)
Update test script to fix type references and remove private method call

@ -1,6 +1,6 @@
{
"name": "@git.zone/tspm",
"version": "1.3.1",
"version": "1.5.1",
"private": false,
"description": "a no fuzz process manager",
"main": "dist_ts/index.js",
@ -18,16 +18,18 @@
"tspm": "./cli.js"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.25",
"@git.zone/tsbuild": "^2.2.6",
"@git.zone/tsbundle": "^2.0.5",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.44",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^22.13.8"
"@push.rocks/tapbundle": "^5.5.9",
"@types/node": "^22.13.10"
},
"dependencies": {
"@push.rocks/npmextra": "^5.1.2",
"@push.rocks/projectinfo": "^5.0.2",
"@push.rocks/smartcli": "^4.0.11",
"@push.rocks/smartdaemon": "^2.0.8",
"@push.rocks/smartpath": "^5.0.18",
"pidusage": "^4.0.0",
"ps-tree": "^1.2.0"

1086
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

@ -1,27 +1,124 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as tspm from '../ts/index.js';
import { join } from 'path';
tap.test('first test', async () => {
console.log(tspm);
// Basic module import test
tap.test('module import test', async () => {
console.log('Imported modules:', Object.keys(tspm));
expect(tspm.ProcessMonitor).toBeTypeOf('function');
expect(tspm.Tspm).toBeTypeOf('function');
});
// ProcessMonitor test
tap.test('ProcessMonitor test', async () => {
const config: tspm.IMonitorConfig = {
name: 'Test Monitor',
projectDir: process.cwd(),
command: 'echo "Test process running"',
memoryLimitBytes: 50 * 1024 * 1024, // 50MB
monitorIntervalMs: 1000,
};
const monitor = new tspm.ProcessMonitor(config);
// Test monitor creation
expect(monitor).toBeInstanceOf(tspm.ProcessMonitor);
// We won't actually start it in tests to avoid side effects
// but we can test the API
expect(monitor.start).toBeInstanceOf('function');
expect(monitor.stop).toBeInstanceOf('function');
expect(monitor.getLogs).toBeInstanceOf('function');
});
// Tspm class test
tap.test('Tspm class test', async () => {
const tspmInstance = new tspm.Tspm();
expect(tspmInstance).toBeInstanceOf(tspm.Tspm);
expect(tspmInstance.start).toBeInstanceOf('function');
expect(tspmInstance.stop).toBeInstanceOf('function');
expect(tspmInstance.restart).toBeInstanceOf('function');
expect(tspmInstance.list).toBeInstanceOf('function');
expect(tspmInstance.describe).toBeInstanceOf('function');
expect(tspmInstance.getLogs).toBeInstanceOf('function');
});
tap.start();
// Example usage:
const config: tspm.IMonitorConfig = {
name: 'Project XYZ Monitor', // Identifier for the instance
projectDir: '/path/to/your/project', // Set the project directory here
command: 'npm run xyz', // Full command string (no need for args)
memoryLimitBytes: 500 * 1024 * 1024, // 500 MB memory limit
monitorIntervalMs: 5000, // Check memory usage every 5 seconds
};
// ====================================================
// Example usage (this part is not executed in tests)
// ====================================================
const monitor = new tspm.ProcessMonitor(config);
monitor.start();
// Example 1: Using ProcessMonitor directly
function exampleUsingProcessMonitor() {
const config: tspm.IMonitorConfig = {
name: 'Project XYZ Monitor',
projectDir: '/path/to/your/project',
command: 'npm run xyz',
memoryLimitBytes: 500 * 1024 * 1024, // 500 MB memory limit
monitorIntervalMs: 5000, // Check memory usage every 5 seconds
logBufferSize: 200, // Keep last 200 log lines
};
// Ensure that on process exit (e.g. Ctrl+C) we clean up the child process and prevent respawns.
process.on('SIGINT', () => {
console.log('Received SIGINT, stopping monitor...');
monitor.stop();
process.exit();
});
const monitor = new tspm.ProcessMonitor(config);
monitor.start();
// Ensure that on process exit (e.g. Ctrl+C) we clean up the child process and prevent respawns.
process.on('SIGINT', () => {
console.log('Received SIGINT, stopping monitor...');
monitor.stop();
process.exit();
});
// Get logs example
setTimeout(() => {
const logs = monitor.getLogs(10); // Get last 10 log lines
console.log('Latest logs:', logs);
}, 10000);
}
// Example 2: Using Tspm (higher-level process manager)
async function exampleUsingTspm() {
const tspmInstance = new tspm.Tspm();
// Start a process
await tspmInstance.start({
id: 'web-server',
name: 'Web Server',
projectDir: '/path/to/web/project',
command: 'npm run serve',
memoryLimitBytes: 300 * 1024 * 1024, // 300 MB
autorestart: true,
watch: true,
monitorIntervalMs: 10000,
});
// Start another process
await tspmInstance.start({
id: 'api-server',
name: 'API Server',
projectDir: '/path/to/api/project',
command: 'npm run api',
memoryLimitBytes: 400 * 1024 * 1024, // 400 MB
autorestart: true,
});
// List all processes
const processes = tspmInstance.list();
console.log('Running processes:', processes);
// Get logs from a process
const logs = tspmInstance.getLogs('web-server', 20);
console.log('Web server logs:', logs);
// Stop a process
await tspmInstance.stop('api-server');
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.log('Shutting down all processes...');
await tspmInstance.stopAll();
process.exit();
});
}

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tspm',
version: '1.3.1',
version: '1.5.1',
description: 'a no fuzz process manager'
}

20
ts/classes.config.ts Normal file

@ -0,0 +1,20 @@
import * as plugins from './plugins.js';
export class TspmConfig {
public npmextraInstance = new plugins.npmextra.KeyValueStore({
identityArg: '@git.zone__tspm',
typeArg: 'userHomeDir',
})
public async readKey(keyArg: string): Promise<string> {
return await this.npmextraInstance.readKey(keyArg);
}
public async writeKey(keyArg: string, value: string): Promise<void> {
return await this.npmextraInstance.writeKey(keyArg, value);
}
public async deleteKey(keyArg: string): Promise<void> {
return await this.npmextraInstance.deleteKey(keyArg);
}
}

@ -1,4 +1,6 @@
import * as plugins from './plugins.js';
import { ProcessWrapper, type IProcessLog } from './classes.processwrapper.js';
import { Logger, ProcessError, handleError } from './utils.errorhandler.js';
export interface IMonitorConfig {
name?: string; // Optional name to identify the instance
@ -7,96 +9,150 @@ export interface IMonitorConfig {
args?: string[]; // Optional: arguments for the command
memoryLimitBytes: number; // Maximum allowed memory (in bytes) for the process group
monitorIntervalMs?: number; // Interval (in ms) at which memory is checked (default: 5000)
env?: NodeJS.ProcessEnv; // Optional: custom environment variables
logBufferSize?: number; // Optional: number of log lines to keep (default: 100)
}
export class ProcessMonitor {
private child: plugins.childProcess.ChildProcess | null = null;
private processWrapper: ProcessWrapper | null = null;
private config: IMonitorConfig;
private intervalId: NodeJS.Timeout | null = null;
private stopped: boolean = true; // Initially stopped until start() is called
private restartCount: number = 0;
private logger: Logger;
constructor(config: IMonitorConfig) {
this.config = config;
this.logger = new Logger(`ProcessMonitor:${config.name || 'unnamed'}`);
}
public start(): void {
// Reset the stopped flag so that new processes can spawn.
this.stopped = false;
this.log(`Starting process monitor.`);
this.spawnChild();
this.spawnProcess();
// Set the monitoring interval.
const interval = this.config.monitorIntervalMs || 5000;
this.intervalId = setInterval(() => {
if (this.child && this.child.pid) {
this.monitorProcessGroup(this.child.pid, this.config.memoryLimitBytes);
this.intervalId = setInterval((): void => {
if (this.processWrapper && this.processWrapper.getPid()) {
this.monitorProcessGroup(this.processWrapper.getPid()!, this.config.memoryLimitBytes);
}
}, interval);
}
private spawnChild(): void {
private spawnProcess(): void {
// Don't spawn if the monitor has been stopped.
if (this.stopped) return;
if (this.config.args && this.config.args.length > 0) {
this.log(
`Spawning command "${this.config.command}" with args [${this.config.args.join(
', '
)}] in directory: ${this.config.projectDir}`
);
this.child = plugins.childProcess.spawn(this.config.command, this.config.args, {
cwd: this.config.projectDir,
detached: true,
stdio: 'inherit',
});
} else {
this.log(
`Spawning command "${this.config.command}" in directory: ${this.config.projectDir}`
);
// Use shell mode to allow a full command string.
this.child = plugins.childProcess.spawn(this.config.command, {
cwd: this.config.projectDir,
detached: true,
stdio: 'inherit',
shell: true,
});
if (this.stopped) {
this.logger.debug('Not spawning process because monitor is stopped');
return;
}
this.log(`Spawned process with PID ${this.child.pid}`);
this.logger.info(`Spawning process: ${this.config.command}`);
// When the child process exits, restart it if the monitor isn't stopped.
this.child.on('exit', (code, signal) => {
this.log(`Child process exited with code ${code}, signal ${signal}.`);
if (!this.stopped) {
this.log('Restarting process...');
this.spawnChild();
// Create a new process wrapper
this.processWrapper = new ProcessWrapper({
name: this.config.name || 'unnamed-process',
command: this.config.command,
args: this.config.args,
cwd: this.config.projectDir,
env: this.config.env,
logBuffer: this.config.logBufferSize,
});
// Set up event handlers
this.processWrapper.on('log', (log: IProcessLog): void => {
// Here we could add handlers to send logs somewhere
// For now, we just log system messages to the console
if (log.type === 'system') {
this.log(log.message);
}
});
this.processWrapper.on('exit', (code: number | null, signal: string | null): void => {
const exitMsg = `Process exited with code ${code}, signal ${signal}.`;
this.logger.info(exitMsg);
this.log(exitMsg);
if (!this.stopped) {
this.logger.info('Restarting process...');
this.log('Restarting process...');
this.restartCount++;
this.spawnProcess();
} else {
this.logger.debug('Not restarting process because monitor is stopped');
}
});
this.processWrapper.on('error', (error: Error | ProcessError): void => {
const errorMsg = error instanceof ProcessError
? `Process error: ${error.toString()}`
: `Process error: ${error.message}`;
this.logger.error(error);
this.log(errorMsg);
if (!this.stopped) {
this.logger.info('Restarting process due to error...');
this.log('Restarting process due to error...');
this.restartCount++;
this.spawnProcess();
} else {
this.logger.debug('Not restarting process because monitor is stopped');
}
});
// Start the process
try {
this.processWrapper.start();
} catch (error: Error | unknown) {
// The process wrapper will handle logging the error
// Just prevent it from bubbling up further
this.logger.error(`Failed to start process: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Monitor the process groups memory usage. If the total memory exceeds the limit,
* Monitor the process group's memory usage. If the total memory exceeds the limit,
* kill the process group so that the 'exit' handler can restart it.
*/
private async monitorProcessGroup(pid: number, memoryLimit: number): Promise<void> {
try {
const memoryUsage = await this.getProcessGroupMemory(pid);
this.logger.debug(
`Memory usage for PID ${pid}: ${this.humanReadableBytes(memoryUsage)} (${memoryUsage} bytes)`
);
// Only log to the process log at longer intervals to avoid spamming
this.log(
`Current memory usage for process group (PID ${pid}): ${this.humanReadableBytes(
memoryUsage
)} (${memoryUsage} bytes)`
);
if (memoryUsage > memoryLimit) {
this.log(
`Memory usage ${this.humanReadableBytes(
memoryUsage
)} exceeds limit of ${this.humanReadableBytes(memoryLimit)}. Restarting process.`
);
// Kill the entire process group by sending a signal to -PID.
process.kill(-pid, 'SIGKILL');
const memoryLimitMsg = `Memory usage ${this.humanReadableBytes(
memoryUsage
)} exceeds limit of ${this.humanReadableBytes(memoryLimit)}. Restarting process.`;
this.logger.warn(memoryLimitMsg);
this.log(memoryLimitMsg);
// Stop the process wrapper, which will trigger the exit handler and restart
if (this.processWrapper) {
this.processWrapper.stop();
}
}
} catch (error) {
this.log('Error monitoring process group: ' + error);
} catch (error: Error | unknown) {
const processError = new ProcessError(
error instanceof Error ? error.message : String(error),
'ERR_MEMORY_MONITORING_FAILED',
{ pid }
);
this.logger.error(processError);
this.log(`Error monitoring process group: ${processError.toString()}`);
}
}
@ -105,16 +161,40 @@ export class ProcessMonitor {
*/
private getProcessGroupMemory(pid: number): Promise<number> {
return new Promise((resolve, reject) => {
plugins.psTree(pid, (err, children) => {
if (err) return reject(err);
this.logger.debug(`Getting memory usage for process group with PID ${pid}`);
plugins.psTree(pid, (err: Error | null, children: Array<{ PID: string }>) => {
if (err) {
const processError = new ProcessError(
`Failed to get process tree: ${err.message}`,
'ERR_PSTREE_FAILED',
{ pid }
);
this.logger.debug(`psTree error: ${err.message}`);
return reject(processError);
}
// Include the main process and its children.
const pids: number[] = [pid, ...children.map(child => Number(child.PID))];
plugins.pidusage(pids, (err, stats) => {
if (err) return reject(err);
this.logger.debug(`Found ${pids.length} processes in group with parent PID ${pid}`);
plugins.pidusage(pids, (err: Error | null, stats: Record<string, { memory: number }>) => {
if (err) {
const processError = new ProcessError(
`Failed to get process usage stats: ${err.message}`,
'ERR_PIDUSAGE_FAILED',
{ pids }
);
this.logger.debug(`pidusage error: ${err.message}`);
return reject(processError);
}
let totalMemory = 0;
for (const key in stats) {
totalMemory += stats[key].memory;
}
this.logger.debug(`Total memory for process group: ${this.humanReadableBytes(totalMemory)}`);
resolve(totalMemory);
});
});
@ -142,10 +222,48 @@ export class ProcessMonitor {
if (this.intervalId) {
clearInterval(this.intervalId);
}
if (this.child && this.child.pid) {
process.kill(-this.child.pid, 'SIGKILL');
if (this.processWrapper) {
this.processWrapper.stop();
}
}
/**
* Get the current logs from the process
*/
public getLogs(limit?: number): IProcessLog[] {
if (!this.processWrapper) {
return [];
}
return this.processWrapper.getLogs(limit);
}
/**
* Get the number of times the process has been restarted
*/
public getRestartCount(): number {
return this.restartCount;
}
/**
* Get the process ID if running
*/
public getPid(): number | null {
return this.processWrapper?.getPid() || null;
}
/**
* Get process uptime in milliseconds
*/
public getUptime(): number {
return this.processWrapper?.getUptime() || 0;
}
/**
* Check if the process is currently running
*/
public isRunning(): boolean {
return this.processWrapper?.isRunning() || false;
}
/**
* Helper method for logging messages with the instance name.
@ -154,4 +272,4 @@ export class ProcessMonitor {
const prefix = this.config.name ? `[${this.config.name}] ` : '';
console.log(prefix + message);
}
}
}

@ -0,0 +1,243 @@
import * as plugins from './plugins.js';
import { EventEmitter } from 'events';
import { Logger, ProcessError, handleError } from './utils.errorhandler.js';
export interface IProcessWrapperOptions {
command: string;
args?: string[];
cwd: string;
env?: NodeJS.ProcessEnv;
name: string;
logBuffer?: number; // Number of log lines to keep in memory (default: 100)
}
export interface IProcessLog {
timestamp: Date;
type: 'stdout' | 'stderr' | 'system';
message: string;
}
export class ProcessWrapper extends EventEmitter {
private process: plugins.childProcess.ChildProcess | null = null;
private options: IProcessWrapperOptions;
private logs: IProcessLog[] = [];
private logBufferSize: number;
private startTime: Date | null = null;
private logger: Logger;
constructor(options: IProcessWrapperOptions) {
super();
this.options = options;
this.logBufferSize = options.logBuffer || 100;
this.logger = new Logger(`ProcessWrapper:${options.name}`);
}
/**
* Start the wrapped process
*/
public start(): void {
this.addSystemLog('Starting process...');
try {
this.logger.debug(`Starting process: ${this.options.command}`);
if (this.options.args && this.options.args.length > 0) {
this.process = plugins.childProcess.spawn(this.options.command, this.options.args, {
cwd: this.options.cwd,
env: this.options.env || process.env,
stdio: ['ignore', 'pipe', 'pipe'], // We need to pipe stdout and stderr
});
} else {
// Use shell mode to allow a full command string
this.process = plugins.childProcess.spawn(this.options.command, {
cwd: this.options.cwd,
env: this.options.env || process.env,
stdio: ['ignore', 'pipe', 'pipe'], // We need to pipe stdout and stderr
shell: true,
});
}
this.startTime = new Date();
// Handle process exit
this.process.on('exit', (code, signal) => {
const exitMessage = `Process exited with code ${code}, signal ${signal}`;
this.logger.info(exitMessage);
this.addSystemLog(exitMessage);
this.emit('exit', code, signal);
});
// Handle errors
this.process.on('error', (error) => {
const processError = new ProcessError(
error.message,
'ERR_PROCESS_EXECUTION',
{ command: this.options.command, pid: this.process?.pid }
);
this.logger.error(processError);
this.addSystemLog(`Process error: ${processError.toString()}`);
this.emit('error', processError);
});
// Capture stdout
if (this.process.stdout) {
this.process.stdout.on('data', (data) => {
const lines = data.toString().split('\n');
for (const line of lines) {
if (line.trim()) {
this.addLog('stdout', line);
}
}
});
}
// Capture stderr
if (this.process.stderr) {
this.process.stderr.on('data', (data) => {
const lines = data.toString().split('\n');
for (const line of lines) {
if (line.trim()) {
this.addLog('stderr', line);
}
}
});
}
this.addSystemLog(`Process started with PID ${this.process.pid}`);
this.logger.info(`Process started with PID ${this.process.pid}`);
this.emit('start', this.process.pid);
} catch (error: Error | unknown) {
const processError = error instanceof ProcessError
? error
: new ProcessError(
error instanceof Error ? error.message : String(error),
'ERR_PROCESS_START_FAILED',
{ command: this.options.command }
);
this.logger.error(processError);
this.addSystemLog(`Failed to start process: ${processError.toString()}`);
this.emit('error', processError);
throw processError;
}
}
/**
* Stop the wrapped process
*/
public stop(): void {
if (!this.process) {
this.logger.debug('Stop called but no process is running');
this.addSystemLog('No process running');
return;
}
this.logger.info('Stopping process...');
this.addSystemLog('Stopping process...');
// First try SIGTERM for graceful shutdown
if (this.process.pid) {
try {
this.logger.debug(`Sending SIGTERM to process ${this.process.pid}`);
process.kill(this.process.pid, 'SIGTERM');
// Give it 5 seconds to shut down gracefully
setTimeout((): void => {
if (this.process && this.process.pid) {
this.logger.warn(`Process ${this.process.pid} did not exit gracefully, force killing...`);
this.addSystemLog('Process did not exit gracefully, force killing...');
try {
process.kill(this.process.pid, 'SIGKILL');
} catch (error: Error | unknown) {
// Process might have exited between checks
this.logger.debug(`Failed to send SIGKILL, process probably already exited: ${
error instanceof Error ? error.message : String(error)
}`);
}
}
}, 5000);
} catch (error: Error | unknown) {
const processError = new ProcessError(
error instanceof Error ? error.message : String(error),
'ERR_PROCESS_STOP_FAILED',
{ pid: this.process.pid }
);
this.logger.error(processError);
this.addSystemLog(`Error stopping process: ${processError.toString()}`);
}
}
}
/**
* Get the process ID if running
*/
public getPid(): number | null {
return this.process?.pid || null;
}
/**
* Get the current logs
*/
public getLogs(limit: number = this.logBufferSize): IProcessLog[] {
// Return the most recent logs up to the limit
return this.logs.slice(-limit);
}
/**
* Get uptime in milliseconds
*/
public getUptime(): number {
if (!this.startTime) return 0;
return Date.now() - this.startTime.getTime();
}
/**
* Check if the process is currently running
*/
public isRunning(): boolean {
return this.process !== null && typeof this.process.exitCode !== 'number';
}
/**
* Add a log entry from stdout or stderr
*/
private addLog(type: 'stdout' | 'stderr', message: string): void {
const log: IProcessLog = {
timestamp: new Date(),
type,
message,
};
this.logs.push(log);
// Trim logs if they exceed buffer size
if (this.logs.length > this.logBufferSize) {
this.logs = this.logs.slice(-this.logBufferSize);
}
// Emit log event for potential handlers
this.emit('log', log);
}
/**
* Add a system log entry (not from the process itself)
*/
private addSystemLog(message: string): void {
const log: IProcessLog = {
timestamp: new Date(),
type: 'system',
message,
};
this.logs.push(log);
// Trim logs if they exceed buffer size
if (this.logs.length > this.logBufferSize) {
this.logs = this.logs.slice(-this.logBufferSize);
}
// Emit log event for potential handlers
this.emit('log', log);
}
}

@ -1,6 +1,419 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { ProcessMonitor, type IMonitorConfig } from './classes.processmonitor.js';
import { TspmConfig } from './classes.config.js';
import {
Logger,
ProcessError,
ConfigError,
ValidationError,
handleError
} from './utils.errorhandler.js';
export interface IProcessConfig extends IMonitorConfig {
id: string; // Unique identifier for the process
autorestart: boolean; // Whether to restart the process automatically on crash
watch?: boolean; // Whether to watch for file changes and restart
watchPaths?: string[]; // Paths to watch for changes
}
export interface IProcessInfo {
id: string;
pid?: number;
status: 'online' | 'stopped' | 'errored';
memory: number;
cpu?: number;
uptime?: number;
restarts: number;
}
export interface IProcessLog {
timestamp: Date;
type: 'stdout' | 'stderr' | 'system';
message: string;
}
export class Tspm {
private processes: Map<string, ProcessMonitor> = new Map();
private processConfigs: Map<string, IProcessConfig> = new Map();
private processInfo: Map<string, IProcessInfo> = new Map();
private config: TspmConfig;
private configStorageKey = 'processes';
private logger: Logger;
constructor() {
this.logger = new Logger('Tspm');
this.config = new TspmConfig();
this.loadProcessConfigs();
}
/**
* Start a new process with the given configuration
*/
public async start(config: IProcessConfig): Promise<void> {
this.logger.info(`Starting process with id '${config.id}'`);
// Validate config
if (!config.id || !config.command || !config.projectDir) {
throw new ValidationError(
'Invalid process configuration: missing required fields',
'ERR_INVALID_CONFIG',
{ config }
);
}
// Check if process with this id already exists
if (this.processes.has(config.id)) {
throw new ValidationError(
`Process with id '${config.id}' already exists`,
'ERR_DUPLICATE_PROCESS'
);
}
try {
// Create and store process config
this.processConfigs.set(config.id, config);
// Initialize process info
this.processInfo.set(config.id, {
id: config.id,
status: 'stopped',
memory: 0,
restarts: 0
});
// Create and start process monitor
const monitor = new ProcessMonitor({
name: config.name || config.id,
projectDir: config.projectDir,
command: config.command,
args: config.args,
memoryLimitBytes: config.memoryLimitBytes,
monitorIntervalMs: config.monitorIntervalMs,
env: config.env,
logBufferSize: config.logBufferSize
});
this.processes.set(config.id, monitor);
monitor.start();
// Update process info
this.updateProcessInfo(config.id, { status: 'online' });
// Save updated configs
await this.saveProcessConfigs();
this.logger.info(`Successfully started process with id '${config.id}'`);
} catch (error: Error | unknown) {
// Clean up in case of error
this.processConfigs.delete(config.id);
this.processInfo.delete(config.id);
this.processes.delete(config.id);
if (error instanceof Error) {
this.logger.error(error);
throw new ProcessError(
`Failed to start process: ${error.message}`,
'ERR_PROCESS_START_FAILED',
{ id: config.id, command: config.command }
);
} else {
const genericError = new ProcessError(
`Failed to start process: ${String(error)}`,
'ERR_PROCESS_START_FAILED',
{ id: config.id }
);
this.logger.error(genericError);
throw genericError;
}
}
}
/**
* Stop a process by id
*/
public async stop(id: string): Promise<void> {
this.logger.info(`Stopping process with id '${id}'`);
const monitor = this.processes.get(id);
if (!monitor) {
const error = new ValidationError(
`Process with id '${id}' not found`,
'ERR_PROCESS_NOT_FOUND'
);
this.logger.error(error);
throw error;
}
try {
monitor.stop();
this.updateProcessInfo(id, { status: 'stopped' });
this.logger.info(`Successfully stopped process with id '${id}'`);
} catch (error: Error | unknown) {
const processError = new ProcessError(
`Failed to stop process: ${error instanceof Error ? error.message : String(error)}`,
'ERR_PROCESS_STOP_FAILED',
{ id }
);
this.logger.error(processError);
throw processError;
}
// Don't remove from the maps, just mark as stopped
// This allows it to be restarted later
}
/**
* Restart a process by id
*/
public async restart(id: string): Promise<void> {
this.logger.info(`Restarting process with id '${id}'`);
const monitor = this.processes.get(id);
const config = this.processConfigs.get(id);
if (!monitor || !config) {
const error = new ValidationError(
`Process with id '${id}' not found`,
'ERR_PROCESS_NOT_FOUND'
);
this.logger.error(error);
throw error;
}
try {
// Stop and then start the process
monitor.stop();
// Create a new monitor instance
const newMonitor = new ProcessMonitor({
name: config.name || config.id,
projectDir: config.projectDir,
command: config.command,
args: config.args,
memoryLimitBytes: config.memoryLimitBytes,
monitorIntervalMs: config.monitorIntervalMs,
env: config.env,
logBufferSize: config.logBufferSize
});
this.processes.set(id, newMonitor);
newMonitor.start();
// Update restart count
const info = this.processInfo.get(id);
if (info) {
this.updateProcessInfo(id, {
status: 'online',
restarts: info.restarts + 1
});
}
this.logger.info(`Successfully restarted process with id '${id}'`);
} catch (error: Error | unknown) {
const processError = new ProcessError(
`Failed to restart process: ${error instanceof Error ? error.message : String(error)}`,
'ERR_PROCESS_RESTART_FAILED',
{ id }
);
this.logger.error(processError);
throw processError;
}
}
/**
* Delete a process by id
*/
public async delete(id: string): Promise<void> {
this.logger.info(`Deleting process with id '${id}'`);
// Check if process exists
if (!this.processConfigs.has(id)) {
const error = new ValidationError(
`Process with id '${id}' not found`,
'ERR_PROCESS_NOT_FOUND'
);
this.logger.error(error);
throw error;
}
// Stop the process if it's running
try {
if (this.processes.has(id)) {
await this.stop(id);
}
// Remove from all maps
this.processes.delete(id);
this.processConfigs.delete(id);
this.processInfo.delete(id);
// Save updated configs
await this.saveProcessConfigs();
this.logger.info(`Successfully deleted process with id '${id}'`);
} catch (error: Error | unknown) {
// Even if stop fails, we should still try to delete the configuration
try {
this.processes.delete(id);
this.processConfigs.delete(id);
this.processInfo.delete(id);
await this.saveProcessConfigs();
this.logger.info(`Successfully deleted process with id '${id}' after stopping failure`);
} catch (deleteError: Error | unknown) {
const configError = new ConfigError(
`Failed to delete process configuration: ${deleteError instanceof Error ? deleteError.message : String(deleteError)}`,
'ERR_CONFIG_DELETE_FAILED',
{ id }
);
this.logger.error(configError);
throw configError;
}
}
}
/**
* Get a list of all process infos
*/
public list(): IProcessInfo[] {
return Array.from(this.processInfo.values());
}
/**
* Get detailed info for a specific process
*/
public describe(id: string): { config: IProcessConfig; info: IProcessInfo } | null {
const config = this.processConfigs.get(id);
const info = this.processInfo.get(id);
if (!config || !info) {
return null;
}
return { config, info };
}
/**
* Get process logs
*/
public getLogs(id: string, limit?: number): IProcessLog[] {
const monitor = this.processes.get(id);
if (!monitor) {
return [];
}
return monitor.getLogs(limit);
}
/**
* Start all saved processes
*/
public async startAll(): Promise<void> {
for (const [id, config] of this.processConfigs.entries()) {
if (!this.processes.has(id)) {
await this.start(config);
}
}
}
/**
* Stop all running processes
*/
public async stopAll(): Promise<void> {
for (const id of this.processes.keys()) {
await this.stop(id);
}
}
/**
* Restart all processes
*/
public async restartAll(): Promise<void> {
for (const id of this.processes.keys()) {
await this.restart(id);
}
}
/**
* Update the info for a process
*/
private updateProcessInfo(id: string, update: Partial<IProcessInfo>): void {
const info = this.processInfo.get(id);
if (info) {
this.processInfo.set(id, { ...info, ...update });
}
}
/**
* Save all process configurations to config storage
*/
private async saveProcessConfigs(): Promise<void> {
this.logger.debug('Saving process configurations to storage');
try {
const configs = Array.from(this.processConfigs.values());
await this.config.writeKey(this.configStorageKey, JSON.stringify(configs));
this.logger.debug(`Saved ${configs.length} process configurations`);
} catch (error: Error | unknown) {
const configError = new ConfigError(
`Failed to save process configurations: ${error instanceof Error ? error.message : String(error)}`,
'ERR_CONFIG_SAVE_FAILED'
);
this.logger.error(configError);
throw configError;
}
}
/**
* Load process configurations from config storage
*/
private async loadProcessConfigs(): Promise<void> {
this.logger.debug('Loading process configurations from storage');
try {
const configsJson = await this.config.readKey(this.configStorageKey);
if (configsJson) {
try {
const configs = JSON.parse(configsJson) as IProcessConfig[];
this.logger.debug(`Loaded ${configs.length} process configurations`);
for (const config of configs) {
// Validate config
if (!config.id || !config.command || !config.projectDir) {
this.logger.warn(`Skipping invalid process config for id '${config.id || 'unknown'}'`);
continue;
}
this.processConfigs.set(config.id, config);
// Initialize process info
this.processInfo.set(config.id, {
id: config.id,
status: 'stopped',
memory: 0,
restarts: 0
});
}
} catch (parseError: Error | unknown) {
const configError = new ConfigError(
`Failed to parse process configurations: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
'ERR_CONFIG_PARSE_FAILED'
);
this.logger.error(configError);
throw configError;
}
} else {
this.logger.info('No saved process configurations found');
}
} catch (error: Error | unknown) {
// Only throw if it's not the "no configs found" case
if (error instanceof ConfigError) {
throw error;
}
// If no configs found or error reading, just continue with empty configs
this.logger.info('No saved process configurations found or error reading them');
}
}
}

358
ts/cli.ts

@ -1,21 +1,371 @@
import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { Tspm, type IProcessConfig } from './classes.tspm.js';
import {
Logger,
LogLevel,
handleError,
TspmError,
ProcessError,
ConfigError,
ValidationError
} from './utils.errorhandler.js';
export const run = async () => {
// Define interface for CLI arguments
interface CliArguments {
_: (string | number)[];
[key: string]: any;
}
export const run = async (): Promise<void> => {
const cliLogger = new Logger('CLI');
const tspmProjectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir);
const tspm = new Tspm();
// Check if debug mode is enabled
const debugMode = process.env.TSPM_DEBUG === 'true';
if (debugMode) {
cliLogger.setLevel(LogLevel.DEBUG);
cliLogger.debug('Debug mode enabled');
}
const smartcliInstance = new plugins.smartcli.Smartcli();
smartcliInstance.addVersion(tspmProjectinfo.npm.version);
// Default command - show help and list processes
smartcliInstance.standardCommand().subscribe({
next: (argvArg) => {
console.log(`Please specify a command.`)
next: async (argvArg: CliArguments) => {
console.log(`TSPM - TypeScript Process Manager v${tspmProjectinfo.npm.version}`);
console.log('Usage: tspm [command] [options]');
console.log('\nCommands:');
console.log(' start <script> Start a process');
console.log(' startAsDaemon <script> Start a process in daemon mode');
console.log(' list List all processes');
console.log(' stop <id> Stop a process');
console.log(' restart <id> Restart a process');
console.log(' delete <id> Delete a process');
console.log(' describe <id> Show details for a process');
console.log('\nUse tspm [command] --help for more information about a command.');
// Show current process list
console.log('\nProcess List:');
const processes = tspm.list();
if (processes.length === 0) {
console.log(' No processes running. Use "tspm start" to start a process.');
} else {
console.log('┌─────────┬─────────────┬───────────┬───────────┬──────────┐');
console.log('│ ID │ Name │ Status │ Memory │ Restarts │');
console.log('├─────────┼─────────────┼───────────┼───────────┼──────────┤');
for (const proc of processes) {
console.log(`${pad(proc.id, 8)}${pad(proc.id, 12)}${pad(proc.status, 10)}${pad(formatMemory(proc.memory), 10)}${pad(String(proc.restarts), 9)}`);
}
console.log('└─────────┴─────────────┴───────────┴───────────┴──────────┘');
}
},
});
// Start command - start a new process
smartcliInstance.addCommand('start').subscribe({
next: async (argvArg: CliArguments) => {
const script = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!script) {
console.error('Error: Missing script argument. Usage: tspm start <script>');
return;
}
// Parse additional options
const name = argvArg.name || script;
const cwd = argvArg.cwd || process.cwd();
const memLimit = parseMemoryString(argvArg.memory || '500MB');
try {
cliLogger.debug(`Starting process with script: ${script}`);
const processConfig: IProcessConfig = {
id: argvArg.id || name.replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase(),
name: name,
projectDir: cwd,
command: script,
args: argvArg.args ? String(argvArg.args).split(' ') : undefined,
memoryLimitBytes: memLimit,
monitorIntervalMs: Number(argvArg.interval) || 5000,
autorestart: argvArg.autorestart !== 'false',
watch: Boolean(argvArg.watch)
};
cliLogger.debug(`Created process config: ${JSON.stringify(processConfig)}`);
await tspm.start(processConfig);
console.log(`Process ${processConfig.id} started successfully.`);
} catch (error: Error | unknown) {
const tspmError = handleError(error);
if (tspmError instanceof ValidationError) {
console.error(`Validation error: ${tspmError.message}`);
} else if (tspmError instanceof ProcessError) {
console.error(`Process error: ${tspmError.message}`);
if (debugMode) {
console.error(`Error details: ${JSON.stringify(tspmError.details)}`);
}
} else {
console.error(`Error starting process: ${tspmError.message}`);
}
cliLogger.error(tspmError);
}
},
});
// Start as daemon command
smartcliInstance.addCommand('startAsDaemon').subscribe({
next: async (argvArg: CliArguments) => {
const script = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!script) {
console.error('Error: Missing script argument. Usage: tspm startAsDaemon <script>');
return;
}
// For daemon mode, we'll detach from the console
const daemonProcess = plugins.childProcess.spawn(
process.execPath,
[
...process.execArgv,
process.argv[1], // The tspm script path
'start',
script,
...process.argv.slice(3) // Pass other arguments
],
{
detached: true,
stdio: 'ignore',
cwd: process.cwd()
}
);
// Unref to allow parent to exit
daemonProcess.unref();
console.log(`Started process ${script} as daemon.`);
}
});
// Stop command
smartcliInstance.addCommand('stop').subscribe({
next: async (argvArg: CliArguments) => {
const id = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!id) {
console.error('Error: Missing process ID. Usage: tspm stop <id>');
return;
}
try {
cliLogger.debug(`Stopping process: ${id}`);
await tspm.stop(id);
console.log(`Process ${id} stopped.`);
} catch (error: Error | unknown) {
const tspmError = handleError(error);
if (tspmError instanceof ValidationError) {
console.error(`Validation error: ${tspmError.message}`);
} else {
console.error(`Error stopping process: ${tspmError.message}`);
}
cliLogger.error(tspmError);
}
}
});
// Restart command
smartcliInstance.addCommand('restart').subscribe({
next: async (argvArg: CliArguments) => {
const id = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!id) {
console.error('Error: Missing process ID. Usage: tspm restart <id>');
return;
}
try {
cliLogger.debug(`Restarting process: ${id}`);
await tspm.restart(id);
console.log(`Process ${id} restarted.`);
} catch (error: Error | unknown) {
const tspmError = handleError(error);
if (tspmError instanceof ValidationError) {
console.error(`Validation error: ${tspmError.message}`);
} else if (tspmError instanceof ProcessError) {
console.error(`Process error: ${tspmError.message}`);
} else {
console.error(`Error restarting process: ${tspmError.message}`);
}
cliLogger.error(tspmError);
}
}
});
})
// Delete command
smartcliInstance.addCommand('delete').subscribe({
next: async (argvArg: CliArguments) => {
const id = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!id) {
console.error('Error: Missing process ID. Usage: tspm delete <id>');
return;
}
try {
cliLogger.debug(`Deleting process: ${id}`);
await tspm.delete(id);
console.log(`Process ${id} deleted.`);
} catch (error: Error | unknown) {
const tspmError = handleError(error);
if (tspmError instanceof ValidationError) {
console.error(`Validation error: ${tspmError.message}`);
} else if (tspmError instanceof ConfigError) {
console.error(`Configuration error: ${tspmError.message}`);
} else {
console.error(`Error deleting process: ${tspmError.message}`);
}
cliLogger.error(tspmError);
}
}
});
// List command
smartcliInstance.addCommand('list').subscribe({
next: async (argvArg: CliArguments) => {
const processes = tspm.list();
if (processes.length === 0) {
console.log('No processes running.');
return;
}
console.log('┌─────────┬─────────────┬───────────┬───────────┬──────────┐');
console.log('│ ID │ Name │ Status │ Memory │ Restarts │');
console.log('├─────────┼─────────────┼───────────┼───────────┼──────────┤');
for (const proc of processes) {
console.log(`${pad(proc.id, 8)}${pad(proc.id, 12)}${pad(proc.status, 10)}${pad(formatMemory(proc.memory), 10)}${pad(String(proc.restarts), 9)}`);
}
console.log('└─────────┴─────────────┴───────────┴───────────┴──────────┘');
}
});
// Describe command
smartcliInstance.addCommand('describe').subscribe({
next: async (argvArg: CliArguments) => {
const id = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!id) {
console.error('Error: Missing process ID. Usage: tspm describe <id>');
return;
}
const details = tspm.describe(id);
if (!details) {
console.error(`Process with ID '${id}' not found.`);
return;
}
console.log(`Details for process '${id}':`);
console.log(` Status: ${details.info.status}`);
console.log(` Memory: ${formatMemory(details.info.memory)}`);
console.log(` Restarts: ${details.info.restarts}`);
console.log(` Command: ${details.config.command}`);
console.log(` Directory: ${details.config.projectDir}`);
console.log(` Memory limit: ${formatMemory(details.config.memoryLimitBytes)}`);
if (details.config.args && details.config.args.length > 0) {
console.log(` Arguments: ${details.config.args.join(' ')}`);
}
}
});
// Logs command
smartcliInstance.addCommand('logs').subscribe({
next: async (argvArg: CliArguments) => {
const id = argvArg._.length > 1 ? String(argvArg._[1]) : '';
if (!id) {
console.error('Error: Missing process ID. Usage: tspm logs <id>');
return;
}
const lines = Number(argvArg.lines || argvArg.n) || 20;
const logs = tspm.getLogs(id, lines);
if (logs.length === 0) {
console.log(`No logs found for process '${id}'.`);
return;
}
// Display logs with colors for different log types
for (const log of logs) {
const timestamp = log.timestamp.toISOString();
const prefix = `[${timestamp}] `;
switch (log.type) {
case 'stdout':
console.log(`${prefix}${log.message}`);
break;
case 'stderr':
console.error(`${prefix}${log.message}`);
break;
case 'system':
console.log(`${prefix}[SYSTEM] ${log.message}`);
break;
}
}
}
});
// Start parsing
smartcliInstance.startParse();
};
// Helper function to format memory usage
function formatMemory(bytes: number): string {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// Helper function to parse memory strings like "500MB"
function parseMemoryString(memString: string): number {
const units = {
'B': 1,
'KB': 1024,
'MB': 1024 * 1024,
'GB': 1024 * 1024 * 1024,
'TB': 1024 * 1024 * 1024 * 1024
};
const match = memString.match(/^(\d+(?:\.\d+)?)\s*([KMGT]?B)$/i);
if (!match) {
throw new Error(`Invalid memory format: ${memString}. Use format like 500MB`);
}
const value = parseFloat(match[1]);
const unit = match[2].toUpperCase();
return value * units[unit];
}
// Helper function to pad strings for table display
function pad(str: string, length: number): string {
return str.padEnd(length);
}

@ -1,4 +1,4 @@
import * as plugins from './plugins.js';
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '..');
export const cwd = process.cwd();
export const packageDir: string = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '..');
export const cwd: string = process.cwd();

@ -2,26 +2,33 @@
import * as childProcess from 'child_process';
import * as path from 'node:path';
// Export with explicit module types
export {
childProcess,
path,
}
// @push.rocks scope
import * as npmextra from '@push.rocks/npmextra';
import * as projectinfo from '@push.rocks/projectinfo';
import * as smartpath from '@push.rocks/smartpath';
import * as smartcli from '@push.rocks/smartcli';
import * as smartdaemon from '@push.rocks/smartdaemon';
import * as smartpath from '@push.rocks/smartpath';
// Export with explicit module types
export {
npmextra,
projectinfo,
smartpath,
smartcli,
smartdaemon,
smartpath,
}
// third-party scope
import psTree from 'ps-tree';
import pidusage from 'pidusage';
// Add explicit types for third-party exports
export {
psTree,
pidusage,

138
ts/utils.errorhandler.ts Normal file

@ -0,0 +1,138 @@
/**
* Centralized error handling utility for TSPM
*/
// Define error types
export enum ErrorType {
CONFIG = 'ConfigError',
PROCESS = 'ProcessError',
RUNTIME = 'RuntimeError',
VALIDATION = 'ValidationError',
UNKNOWN = 'UnknownError'
}
// Base error class with type and code support
export class TspmError extends Error {
type: ErrorType;
code: string;
details?: Record<string, any>;
constructor(
message: string,
type: ErrorType = ErrorType.UNKNOWN,
code: string = 'ERR_UNKNOWN',
details?: Record<string, any>
) {
super(message);
this.name = type;
this.type = type;
this.code = code;
this.details = details;
// Preserve proper stack trace
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
toString(): string {
return `[${this.type}:${this.code}] ${this.message}`;
}
}
// Specific error classes
export class ConfigError extends TspmError {
constructor(message: string, code: string = 'ERR_CONFIG', details?: Record<string, any>) {
super(message, ErrorType.CONFIG, code, details);
}
}
export class ProcessError extends TspmError {
constructor(message: string, code: string = 'ERR_PROCESS', details?: Record<string, any>) {
super(message, ErrorType.PROCESS, code, details);
}
}
export class ValidationError extends TspmError {
constructor(message: string, code: string = 'ERR_VALIDATION', details?: Record<string, any>) {
super(message, ErrorType.VALIDATION, code, details);
}
}
// Utility for handling any error type
export const handleError = (error: Error | unknown): TspmError => {
if (error instanceof TspmError) {
return error;
}
if (error instanceof Error) {
return new TspmError(error.message, ErrorType.UNKNOWN, 'ERR_UNKNOWN', { originalError: error });
}
return new TspmError(String(error), ErrorType.UNKNOWN, 'ERR_UNKNOWN');
};
// Logger with different log levels
export enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
NONE = 4
}
export class Logger {
private static instance: Logger;
private level: LogLevel = LogLevel.INFO;
private componentName: string;
constructor(componentName: string) {
this.componentName = componentName;
}
static getInstance(componentName: string): Logger {
if (!Logger.instance) {
Logger.instance = new Logger(componentName);
}
return Logger.instance;
}
setLevel(level: LogLevel): void {
this.level = level;
}
private formatMessage(message: string): string {
const timestamp = new Date().toISOString();
return `[${timestamp}] [${this.componentName}] ${message}`;
}
debug(message: string): void {
if (this.level <= LogLevel.DEBUG) {
console.log(this.formatMessage(`DEBUG: ${message}`));
}
}
info(message: string): void {
if (this.level <= LogLevel.INFO) {
console.log(this.formatMessage(message));
}
}
warn(message: string): void {
if (this.level <= LogLevel.WARN) {
console.warn(this.formatMessage(`WARNING: ${message}`));
}
}
error(error: Error | unknown): void {
if (this.level <= LogLevel.ERROR) {
const tspmError = handleError(error);
console.error(this.formatMessage(`ERROR: ${tspmError.toString()}`));
// In debug mode, also log stack trace
if (this.level === LogLevel.DEBUG && tspmError.stack) {
console.error(tspmError.stack);
}
}
}
}