Files
tspm/ts/cli/helpers/interactive-edit.ts

164 lines
4.9 KiB
TypeScript

import * as plugins from '../plugins.js';
import { tspmIpcClient } from '../../client/tspm.ipcclient.js';
import { formatMemory, parseMemoryString } from './memory.js';
export async function interactiveEditProcess(processId: number): Promise<void> {
// Load current config
const { config } = await tspmIpcClient.request('describe', { id: processId as any });
// Create interactive prompts for editing
const smartInteract = new plugins.smartinteract.SmartInteract([
{
name: 'name',
type: 'input',
message: 'Process name:',
default: config.name,
validate: (input: string) => {
return input && input.trim() !== '';
}
},
{
name: 'command',
type: 'input',
message: 'Command to execute:',
default: config.command,
validate: (input: string) => {
return input && input.trim() !== '';
}
},
{
name: 'projectDir',
type: 'input',
message: 'Working directory:',
default: config.projectDir,
validate: (input: string) => {
return input && input.trim() !== '';
}
},
{
name: 'memoryLimit',
type: 'input',
message: 'Memory limit (e.g., 512M, 1G):',
default: formatMemory(config.memoryLimitBytes),
validate: (input: string) => {
const parsed = parseMemoryString(input);
return parsed !== null;
}
},
{
name: 'autorestart',
type: 'confirm',
message: 'Enable auto-restart on failure?',
default: config.autorestart
},
{
name: 'watch',
type: 'confirm',
message: 'Enable file watching for auto-restart?',
default: config.watch || false
},
{
name: 'updateEnv',
type: 'confirm',
message: 'Update environment variables to current environment?',
default: true
}
]);
console.log('\n📝 Edit Process Configuration');
console.log(` Process ID: ${processId}`);
console.log(' (Press Enter to keep current values)\n');
// Run the interactive prompts
const answerBucket = await smartInteract.runQueue();
// Get answers from the bucket
const name = answerBucket.getAnswerFor('name');
const command = answerBucket.getAnswerFor('command');
const projectDir = answerBucket.getAnswerFor('projectDir');
const memoryLimit = answerBucket.getAnswerFor('memoryLimit');
const autorestart = answerBucket.getAnswerFor('autorestart');
const watch = answerBucket.getAnswerFor('watch');
const updateEnv = answerBucket.getAnswerFor('updateEnv');
// Prepare updates object
const updates: any = {};
// Check what has changed
if (name !== config.name) {
updates.name = name;
}
if (command !== config.command) {
updates.command = command;
}
if (projectDir !== config.projectDir) {
updates.projectDir = projectDir;
}
const newMemoryBytes = parseMemoryString(memoryLimit);
if (newMemoryBytes !== config.memoryLimitBytes) {
updates.memoryLimitBytes = newMemoryBytes;
}
if (autorestart !== config.autorestart) {
updates.autorestart = autorestart;
}
if (watch !== config.watch) {
updates.watch = watch;
}
// Handle environment variables update if requested
if (updateEnv) {
const essentialEnvVars: NodeJS.ProcessEnv = {
PATH: process.env.PATH || '',
HOME: process.env.HOME,
USER: process.env.USER,
SHELL: process.env.SHELL,
LANG: process.env.LANG,
LC_ALL: process.env.LC_ALL,
// Node.js specific
NODE_ENV: process.env.NODE_ENV,
NODE_PATH: process.env.NODE_PATH,
// npm/pnpm/yarn paths
npm_config_prefix: process.env.npm_config_prefix,
// Include any TSPM_ prefixed vars
...Object.fromEntries(
Object.entries(process.env).filter(([key]) => key.startsWith('TSPM_'))
),
};
// Remove undefined values
Object.keys(essentialEnvVars).forEach(key => {
if (essentialEnvVars[key] === undefined) {
delete essentialEnvVars[key];
}
});
updates.env = { ...(config.env || {}), ...essentialEnvVars };
}
// Only update if there are changes
if (Object.keys(updates).length === 0) {
console.log('\n✓ No changes made');
return;
}
// Send updates to daemon
await tspmIpcClient.request('update', {
id: processId as any,
updates,
});
// Display what was updated
console.log('\n✓ Process configuration updated successfully');
if (updates.name) console.log(` Name: ${updates.name}`);
if (updates.command) console.log(` Command: ${updates.command}`);
if (updates.projectDir) console.log(` Directory: ${updates.projectDir}`);
if (updates.memoryLimitBytes) console.log(` Memory limit: ${formatMemory(updates.memoryLimitBytes)}`);
if (updates.autorestart !== undefined) console.log(` Auto-restart: ${updates.autorestart}`);
if (updates.watch !== undefined) console.log(` Watch: ${updates.watch ? 'enabled' : 'disabled'}`);
if (updateEnv) console.log(' Environment variables: updated');
}