108 lines
3.0 KiB
JavaScript
108 lines
3.0 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* API代码生成脚本
|
|
* 功能等同于gen_api.ps1
|
|
*/
|
|
|
|
const { execSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// 配置信息
|
|
const config = {
|
|
// API定义文件路径
|
|
apiFile: './app/user/cmd/api/desc/main.api',
|
|
// 输出目录
|
|
outputDir: './app/user/cmd/api',
|
|
// 模板目录
|
|
templateDir: './deploy/template'
|
|
};
|
|
|
|
/**
|
|
* 生成API代码
|
|
* @param {object} options 选项
|
|
* @param {string} options.apiFile API定义文件路径
|
|
* @param {string} options.outputDir 输出目录
|
|
* @param {string} options.templateDir 模板目录
|
|
* @param {boolean} options.useStyle 是否使用样式
|
|
*/
|
|
function generateApi({ apiFile, outputDir, templateDir, useStyle = false }) {
|
|
try {
|
|
console.log('=========================================');
|
|
console.log('Generating API code...');
|
|
|
|
// 构建命令
|
|
let command = `goctl api go --api ${apiFile} --dir ${outputDir} --home ${templateDir}`;
|
|
|
|
// 如果需要使用样式
|
|
if (useStyle) {
|
|
command += ' --style=goZero';
|
|
console.log('Using goZero style');
|
|
}
|
|
|
|
console.log(`Running command: ${command}`);
|
|
|
|
// 执行命令
|
|
execSync(command, { stdio: 'inherit' });
|
|
|
|
console.log('API code generation completed!');
|
|
console.log('=========================================');
|
|
} catch (error) {
|
|
console.error('ERROR in API generation:');
|
|
console.error(error.message);
|
|
console.error(error.stack);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 主函数
|
|
*/
|
|
function main() {
|
|
// 解析命令行参数
|
|
const args = process.argv.slice(2);
|
|
const options = {
|
|
apiFile: config.apiFile,
|
|
outputDir: config.outputDir,
|
|
templateDir: config.templateDir,
|
|
useStyle: false
|
|
};
|
|
|
|
// 处理命令行参数
|
|
for (let i = 0; i < args.length; i++) {
|
|
const arg = args[i];
|
|
|
|
if (arg === '--api' && i + 1 < args.length) {
|
|
options.apiFile = args[++i];
|
|
} else if (arg === '--dir' && i + 1 < args.length) {
|
|
options.outputDir = args[++i];
|
|
} else if (arg === '--template' && i + 1 < args.length) {
|
|
options.templateDir = args[++i];
|
|
} else if (arg === '--style') {
|
|
options.useStyle = true;
|
|
} else if (arg === '--help') {
|
|
console.log('Usage: node gen_api.js [options]');
|
|
console.log('Options:');
|
|
console.log(' --api <file> API definition file path');
|
|
console.log(' --dir <dir> Output directory');
|
|
console.log(' --template <dir> Template directory');
|
|
console.log(' --style Use goZero style');
|
|
console.log(' --help Show this help message');
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
// 生成API代码
|
|
generateApi(options);
|
|
}
|
|
|
|
// 执行主函数
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
// 导出函数,以便其他脚本可以调用
|
|
module.exports = {
|
|
generateApi
|
|
};
|