prepare refactor
This commit is contained in:
@@ -10,10 +10,16 @@ export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
smartcli,
|
||||
'start',
|
||||
async (argvArg: CliArguments) => {
|
||||
const script = argvArg._[1];
|
||||
if (!script) {
|
||||
console.error('Error: Please provide a script to run');
|
||||
console.log('Usage: tspm start <script> [options]');
|
||||
// Get all arguments after 'start' command
|
||||
const commandArgs = argvArg._.slice(1);
|
||||
if (commandArgs.length === 0) {
|
||||
console.error('Error: Please provide a command to run');
|
||||
console.log('Usage: tspm start <command> [options]');
|
||||
console.log('\nExamples:');
|
||||
console.log(' tspm start "npm run dev"');
|
||||
console.log(' tspm start pnpm start');
|
||||
console.log(' tspm start node server.js');
|
||||
console.log(' tspm start script.ts');
|
||||
console.log('\nOptions:');
|
||||
console.log(' --name <name> Name for the process');
|
||||
console.log(
|
||||
@@ -27,17 +33,25 @@ export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
console.log(' --autorestart Auto-restart on crash');
|
||||
return;
|
||||
}
|
||||
|
||||
// Join all command parts to form the full command
|
||||
const script = commandArgs.join(' ');
|
||||
|
||||
const memoryLimit = argvArg.memory
|
||||
? parseMemoryString(argvArg.memory)
|
||||
: 512 * 1024 * 1024;
|
||||
const projectDir = argvArg.cwd || process.cwd();
|
||||
|
||||
// Direct .ts support via tsx (bundled with TSPM)
|
||||
let actualCommand = script;
|
||||
let commandArgs: string[] | undefined = undefined;
|
||||
|
||||
if (script.endsWith('.ts')) {
|
||||
// Parse the command to determine if we need to handle .ts files
|
||||
let actualCommand: string;
|
||||
let processArgs: string[] | undefined = undefined;
|
||||
|
||||
// Split the script to check if it's a single .ts file or a full command
|
||||
const scriptParts = script.split(' ');
|
||||
const firstPart = scriptParts[0];
|
||||
|
||||
// Check if this is a direct .ts file execution (single argument ending in .ts)
|
||||
if (scriptParts.length === 1 && firstPart.endsWith('.ts')) {
|
||||
try {
|
||||
const tsxPath = await (async () => {
|
||||
const { createRequire } = await import('module');
|
||||
@@ -45,15 +59,20 @@ export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
return require.resolve('tsx/dist/cli.mjs');
|
||||
})();
|
||||
|
||||
const scriptPath = plugins.path.isAbsolute(script)
|
||||
? script
|
||||
: plugins.path.join(projectDir, script);
|
||||
const scriptPath = plugins.path.isAbsolute(firstPart)
|
||||
? firstPart
|
||||
: plugins.path.join(projectDir, firstPart);
|
||||
actualCommand = tsxPath;
|
||||
commandArgs = [scriptPath];
|
||||
processArgs = [scriptPath];
|
||||
} catch {
|
||||
actualCommand = 'tsx';
|
||||
commandArgs = [script];
|
||||
processArgs = [firstPart];
|
||||
}
|
||||
} else {
|
||||
// For multi-word commands, use the entire script as the command
|
||||
// This handles cases like "pnpm start", "npm run dev", etc.
|
||||
actualCommand = script;
|
||||
processArgs = undefined;
|
||||
}
|
||||
|
||||
const name = argvArg.name || script;
|
||||
@@ -69,7 +88,7 @@ export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
id: name.replace(/[^a-zA-Z0-9-_]/g, '_'),
|
||||
name,
|
||||
command: actualCommand,
|
||||
args: commandArgs,
|
||||
args: processArgs,
|
||||
projectDir,
|
||||
memoryLimitBytes: memoryLimit,
|
||||
autorestart,
|
||||
@@ -79,7 +98,7 @@ export function registerStartCommand(smartcli: plugins.smartcli.Smartcli) {
|
||||
|
||||
console.log(`Starting process: ${name}`);
|
||||
console.log(
|
||||
` Command: ${script}${script.endsWith('.ts') ? ' (via tsx)' : ''}`,
|
||||
` Command: ${script}${scriptParts.length === 1 && firstPart.endsWith('.ts') ? ' (via tsx)' : ''}`,
|
||||
);
|
||||
console.log(` Directory: ${projectDir}`);
|
||||
console.log(` Memory limit: ${formatMemory(memoryLimit)}`);
|
||||
|
Reference in New Issue
Block a user