56 lines
2.9 KiB
JavaScript
56 lines
2.9 KiB
JavaScript
import { z } from 'zod';
|
|
import { config } from '../../config.js';
|
|
import { detectProject, resolvePackage } from '../../lib/frameworks/detect.js';
|
|
import { resolveTarget } from '../../lib/frameworks/resolve.js';
|
|
import { detectJsPackageManager, runScriptCommandFor } from '../../lib/frameworks/packageManager.js';
|
|
import { runCommand } from '../../lib/runCommand.js';
|
|
import { processManager } from '../../lib/processManager.js';
|
|
import { errorResult, jsonResult, packageSchema, targetSchema, toErrorMessage } from '../shared.js';
|
|
export function registerRunScriptTool(server) {
|
|
server.registerTool('run_script', {
|
|
title: 'Run package.json Script',
|
|
description: 'Run an npm/pnpm/yarn/bun script from package.json. Runs in the foreground and waits for completion by default; set background: true for long-running scripts (watchers, servers) and manage them via get_process_logs/stop_process. Optional `package` scopes to a monorepo package.',
|
|
inputSchema: {
|
|
script: z.string().describe('The package.json script name to run, e.g. "build" or "dev".'),
|
|
target: targetSchema,
|
|
package: packageSchema,
|
|
background: z
|
|
.boolean()
|
|
.optional()
|
|
.default(false)
|
|
.describe('Run as a tracked background process instead of waiting for it to finish.'),
|
|
extraArgs: z.array(z.string()).optional().describe('Extra arguments forwarded to the script.'),
|
|
},
|
|
}, async ({ script, target, package: packageName, background, extraArgs }) => {
|
|
try {
|
|
const scopeRoot = packageName
|
|
? resolvePackage(config.workspaceRoot, packageName).pkg.absolutePath
|
|
: config.workspaceRoot;
|
|
const detected = detectProject(scopeRoot);
|
|
const { adapter, target: resolvedTarget } = resolveTarget(detected, target, 'frontend');
|
|
const root = adapter.root;
|
|
const pm = detectJsPackageManager(root);
|
|
const { command, args } = runScriptCommandFor(pm, script, extraArgs ?? []);
|
|
if (background) {
|
|
const info = processManager.start({ label: `run_script:${script}`, command, args, cwd: root });
|
|
return jsonResult({ started: true, target: resolvedTarget, package: packageName ?? null, ...info });
|
|
}
|
|
const result = await runCommand(command, args, { cwd: root });
|
|
return jsonResult({
|
|
target: resolvedTarget,
|
|
package: packageName ?? null,
|
|
exitCode: result.exitCode,
|
|
timedOut: result.timedOut,
|
|
failed: result.failed,
|
|
output: result.output,
|
|
cwd: result.cwd,
|
|
command: result.command,
|
|
args: result.args,
|
|
});
|
|
}
|
|
catch (error) {
|
|
return errorResult(toErrorMessage(error));
|
|
}
|
|
});
|
|
}
|
|
//# sourceMappingURL=runScript.js.map
|