geoapify-mcp-server
Version:
Geoapify API MCP Server for location-based services - 一键部署的地理位置服务
175 lines (147 loc) • 5.02 kB
JavaScript
/**
* 简化的NPM发布脚本
* 用于Windows PowerShell环境
*/
const { spawn, exec } = require('child_process');
const fs = require('fs');
const path = require('path');
// 读取package.json
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
console.log('🚀 NPM发布助手');
console.log('================');
console.log(`包名: ${packageJson.name}`);
console.log(`版本: ${packageJson.version}`);
console.log('');
// 执行命令的辅助函数
function runCommand(command, args = []) {
return new Promise((resolve, reject) => {
console.log(`执行: ${command} ${args.join(' ')}`);
const process = spawn(command, args, {
stdio: 'inherit',
shell: true
});
process.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`命令失败,退出码: ${code}`));
}
});
process.on('error', (error) => {
reject(error);
});
});
}
// 检查命令
function checkCommand(command) {
return new Promise((resolve) => {
exec(`${command} --version`, (error) => {
resolve(!error);
});
});
}
// 主发布流程
async function publish() {
try {
console.log('📋 发布前检查...');
// 1. 检查npm是否可用
const npmAvailable = await checkCommand('npm');
if (!npmAvailable) {
throw new Error('NPM不可用,请确保已安装Node.js');
}
console.log('✅ NPM可用');
// 2. 检查registry
console.log('\n🔍 检查NPM registry...');
await runCommand('npm', ['config', 'get', 'registry']);
// 3. 设置官方registry
console.log('\n🔧 设置官方registry...');
await runCommand('npm', ['config', 'set', 'registry', 'https://registry.npmjs.org/']);
// 4. 检查登录状态
console.log('\n👤 检查登录状态...');
try {
await runCommand('npm', ['whoami']);
console.log('✅ 已登录NPM');
} catch (error) {
console.log('❌ 未登录NPM');
console.log('\n🔑 请手动登录NPM:');
console.log(' npm login');
console.log('\n登录后重新运行此脚本');
return;
}
// 5. 检查包是否存在
console.log('\n📦 检查包是否已存在...');
try {
await runCommand('npm', ['view', packageJson.name]);
console.log('⚠️ 包已存在,检查版本...');
try {
await runCommand('npm', ['view', `${packageJson.name}@${packageJson.version}`]);
console.log('❌ 当前版本已存在,请更新版本号');
console.log(' npm version patch # 补丁版本');
console.log(' npm version minor # 次要版本');
console.log(' npm version major # 主要版本');
return;
} catch (versionError) {
console.log('✅ 当前版本可用');
}
} catch (error) {
console.log('✅ 包名可用');
}
// 6. 预览包内容
console.log('\n📋 预览包内容...');
await runCommand('npm', ['pack', '--dry-run']);
// 7. 确认发布
console.log('\n❓ 确认发布?');
console.log(' 如果确认,请手动运行: npm publish');
console.log(' 或者继续运行自动发布...');
// 等待用户确认
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const answer = await new Promise((resolve) => {
rl.question('是否继续自动发布?(y/N): ', (answer) => {
rl.close();
resolve(answer.toLowerCase());
});
});
if (answer === 'y' || answer === 'yes') {
// 8. 发布
console.log('\n🚀 发布包...');
await runCommand('npm', ['publish']);
// 9. 验证
console.log('\n✅ 发布成功!');
console.log('\n📦 包信息:');
console.log(` 名称: ${packageJson.name}`);
console.log(` 版本: ${packageJson.version}`);
console.log(` 链接: https://www.npmjs.com/package/${packageJson.name}`);
console.log('\n🎯 使用方法:');
console.log(` npx ${packageJson.name}`);
console.log('\n🔧 MCP配置:');
console.log(`{
"mcpServers": {
"geoapify-maps": {
"command": "npx",
"args": ["-y", "${packageJson.name}"],
"env": {
"GEOAPIFY_API_KEY": "3234bd7e35264a8aa29fc15e89f8f76f"
}
}
}
}`);
} else {
console.log('\n⏸️ 发布已取消');
console.log(' 手动发布命令: npm publish');
}
} catch (error) {
console.error('\n❌ 发布失败:', error.message);
console.log('\n🔧 可能的解决方案:');
console.log(' 1. 检查网络连接');
console.log(' 2. 确认NPM登录: npm whoami');
console.log(' 3. 检查包名是否可用');
console.log(' 4. 更新版本号: npm version patch');
}
}
// 运行发布流程
publish();