@whyuds/ask-user
Version:
A cross-platform command-line tool and MCP server to show dialog windows and get user input for AI code editors
64 lines (52 loc) • 1.53 kB
JavaScript
import { spawn } from 'child_process';
import path from 'path';
import { program } from 'commander';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 配置命令行参数
program
.name('ask')
.description('Show a dialog window to get user input')
.version('1.0.0')
.option('-t, --title <title>', 'Dialog title')
.option('-m, --message <message>', 'Dialog message')
.parse();
const options = program.opts();
// 主函数
async function main() {
try {
// 构建Electron命令参数
const mainScript = path.join(__dirname, '..', 'electron-main.js');
const args = ['electron', mainScript];
if (options.title) {
args.push('--title', options.title);
}
if (options.message) {
args.push('--message', options.message);
}
// 启动Electron进程
const electronProcess = spawn('npx', args, {
stdio: ['inherit', 'pipe', 'inherit'],
shell: true
});
// 监听输出
electronProcess.stdout.on('data', (data) => {
process.stdout.write(data);
});
// 监听进程退出
electronProcess.on('close', (code) => {
process.exit(code || 0);
});
electronProcess.on('error', (error) => {
console.error('Error starting Electron:', error.message);
process.exit(1);
});
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
// 启动应用
main();