zy-gbs-cmd/bin/command.js

52 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env node
const { version } = require('../package.json');
/*
* 命令启动入口:
* 1. package.json 的 bin 配置把全局命令 zy-gbs-cmd 指向本文件。
* 2. 用户执行 zy-gbs-cmd push java production。
* 3. 本文件从 process.argv 取出 push、java、production。
* 4. 确认是 push java 后,调用 src/push.js 开始真正的构建和推送。
*/
function printHelp() {
console.log(`zy-gbs-cmd <command>
使用:
zy-gbs-cmd help 查看帮助
zy-gbs-cmd v 查看版本
zy-gbs-cmd push java [environment] 构建并推送到 Java Git 仓库
`);
}
async function main() {
// 第 1 步解析命令。process.argv 前两项是 Node.js 路径和本文件路径,从第三项开始才是用户参数。
const [, , command, argument, environment] = process.argv;
if (!command || command === 'help') {
printHelp();
return;
}
if (command === 'v') {
console.log(`当前版本: v${version}`);
return;
}
if (command === 'push' && argument === 'java') {
// 第 2 步进入推送主流程。production 会作为 environment 传给 pushJava。
await require('../src/push')({ environment });
return;
}
console.error('无效命令,请执行 zy-gbs-cmd help 查看用法。');
process.exitCode = 1;
}
main().catch(error => {
// 统一设置非零退出码,便于终端和 CI 判断命令执行失败。
console.error(`推送失败: ${error.message}`);
process.exitCode = 1;
});