zy-gbs-cmd/bin/command.js

57 lines
1.6 KiB
JavaScript
Raw Permalink 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. 本文件解析 create、push、help、v 等用户参数。
* 3. 根据命令调用对应模块执行项目创建或构建推送。
*/
function printHelp() {
console.log(`zy-gbs-cmd <command>
使用:
zy-gbs-cmd help 查看帮助
zy-gbs-cmd v 查看版本
zy-gbs-cmd create 从空白模板创建项目
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 === 'create' && !argument) {
await require('../src/create')();
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;
});