Files
tspm/ts/cli/commands/process/add.ts

92 lines
3.3 KiB
TypeScript

import * as plugins from '../../../plugins.js';
import { tspmIpcClient } from '../../../client/tspm.ipcclient.js';
import type { CliArguments } from '../../types.js';
import { parseMemoryString, formatMemory } from '../../helpers/memory.js';
import { registerIpcCommand } from '../../registration/index.js';
export function registerAddCommand(smartcli: plugins.smartcli.Smartcli) {
registerIpcCommand(
smartcli,
'add',
async (argvArg: CliArguments) => {
const args = argvArg._.slice(1);
if (args.length === 0) {
console.error('Error: Please provide a command or .ts file');
console.log('Usage: tspm add <command|file.ts> [options]');
console.log('\nOptions:');
console.log(' --name <name> Optional name');
console.log(' --memory <size> Memory limit (e.g., 512MB, 2GB)');
console.log(' --cwd <path> Working directory');
console.log(' --watch Watch for file changes');
console.log(' --watch-paths <paths> Comma-separated paths');
console.log(' --autorestart Auto-restart on crash (default true)');
return;
}
const script = args.join(' ');
const projectDir = argvArg.cwd || process.cwd();
const memoryLimit = argvArg.memory
? parseMemoryString(argvArg.memory)
: 512 * 1024 * 1024;
// Resolve .ts single-file execution via tsx if needed
const parts = script.split(' ');
const first = parts[0];
let command = script;
let cmdArgs: string[] | undefined;
if (parts.length === 1 && first.endsWith('.ts')) {
try {
const { createRequire } = await import('module');
const require = createRequire(import.meta.url);
const tsxPath = require.resolve('tsx/dist/cli.mjs');
const filePath = plugins.path.isAbsolute(first)
? first
: plugins.path.join(projectDir, first);
command = tsxPath;
cmdArgs = [filePath];
} catch {
command = 'tsx';
cmdArgs = [first];
}
}
const name = argvArg.name || script;
const watch = argvArg.watch || false;
const autorestart = argvArg.autorestart !== false;
const watchPaths = argvArg.watchPaths
? typeof argvArg.watchPaths === 'string'
? (argvArg.watchPaths as string).split(',')
: argvArg.watchPaths
: undefined;
console.log('Adding process configuration:');
console.log(` Command: ${script}${parts.length === 1 && first.endsWith('.ts') ? ' (via tsx)' : ''}`);
console.log(` Directory: ${projectDir}`);
console.log(` Memory limit: ${formatMemory(memoryLimit)}`);
console.log(` Auto-restart: ${autorestart}`);
if (watch) {
console.log(` Watch: enabled`);
if (watchPaths) console.log(` Watch paths: ${watchPaths.join(',')}`);
}
const response = await tspmIpcClient.request('add', {
config: {
name,
command,
args: cmdArgs,
projectDir,
memoryLimitBytes: memoryLimit,
autorestart,
watch,
watchPaths,
},
});
console.log('✓ Added');
console.log(` Assigned ID: ${response.id}`);
},
{ actionLabel: 'add process config' },
);
}