103 lines
3.6 KiB
JavaScript
103 lines
3.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { execSync } = require('child_process');
|
|
|
|
const args = process.argv.slice(2);
|
|
const environment = args[0]; // 'dev' 或 'prod'
|
|
const forcePublish = args.includes('--force');
|
|
|
|
if (!environment || !['dev', 'prod'].includes(environment)) {
|
|
console.error('请指定发布环境: dev 或 prod');
|
|
console.error('使用方法: npm run publish:dev 或 npm run publish:prod');
|
|
process.exit(1);
|
|
}
|
|
|
|
const projectRoot = path.resolve(__dirname, '..');
|
|
const npmrcSource = path.join(projectRoot, `.npmrc.${environment}`);
|
|
const npmrcTarget = path.join(projectRoot, '.npmrc');
|
|
|
|
try {
|
|
// 检查源配置文件是否存在
|
|
if (!fs.existsSync(npmrcSource)) {
|
|
throw new Error(`配置文件 .npmrc.${environment} 不存在`);
|
|
}
|
|
|
|
// 检查必要的环境变量
|
|
const requiredEnvVars = ['NPM_USERNAME', 'NPM_PASSWORD', 'NPM_EMAIL'];
|
|
const missingVars = requiredEnvVars.filter(varName => !process.env[varName]);
|
|
|
|
if (missingVars.length > 0) {
|
|
console.error('❌ 缺少必要的环境变量:');
|
|
missingVars.forEach(varName => {
|
|
console.error(` - ${varName}`);
|
|
});
|
|
console.error('\n请设置环境变量:');
|
|
console.error('export NPM_USERNAME="your-username"');
|
|
console.error('export NPM_PASSWORD="your-password"');
|
|
console.error('export NPM_EMAIL="your-email"');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 检查 Git 状态(除非强制发布)
|
|
if (!forcePublish) {
|
|
try {
|
|
execSync('git diff-index --quiet HEAD --', { cwd: projectRoot });
|
|
} catch (err) {
|
|
console.error('❌ Git 工作目录不干净,请先提交更改或使用 --force 参数');
|
|
console.error('提示: git add . && git commit -m "发布准备"');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// 复制对应的 .npmrc 配置
|
|
fs.copyFileSync(npmrcSource, npmrcTarget);
|
|
console.log(`✅ 已切换到 ${environment} 环境配置`);
|
|
|
|
// 读取 package.json 获取版本信息
|
|
const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'));
|
|
const currentVersion = packageJson.version;
|
|
|
|
console.log(`📦 当前版本: ${currentVersion}`);
|
|
console.log(`🚀 开始发布到 ${environment} 仓库...`);
|
|
|
|
// 验证认证配置
|
|
try {
|
|
const registryUrl = environment === 'dev'
|
|
? 'https://nexus.yangjie.link/repository/npm-snapshots/'
|
|
: 'https://nexus.yangjie.link/repository/npm-releases/';
|
|
|
|
execSync(`npm whoami --registry=${registryUrl}`, {
|
|
stdio: 'pipe',
|
|
cwd: projectRoot,
|
|
env: { ...process.env }
|
|
});
|
|
console.log('✅ 认证验证成功');
|
|
} catch (err) {
|
|
console.error('❌ 认证验证失败');
|
|
console.error('请检查用户名、密码和邮箱是否正确');
|
|
console.error('或者尝试手动登录: npm login');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 构建发布命令
|
|
const publishCmd = forcePublish ? 'npm publish --force' : 'npm publish';
|
|
|
|
// 执行发布
|
|
execSync(publishCmd, {
|
|
stdio: 'inherit',
|
|
cwd: projectRoot,
|
|
env: { ...process.env }
|
|
});
|
|
|
|
console.log(`✅ 发布成功! 版本 ${currentVersion} 已发布到 ${environment} 仓库`);
|
|
|
|
} catch (err) {
|
|
console.error(`❌ 发布失败: ${err.message}`);
|
|
process.exit(1);
|
|
} finally {
|
|
// 清理临时 .npmrc 文件
|
|
if (fs.existsSync(npmrcTarget)) {
|
|
fs.unlinkSync(npmrcTarget);
|
|
console.log('🧹 已清理临时配置文件');
|
|
}
|
|
} |