refactor(install): 优化安装脚本配置逻辑
- 提取 Nginx 检测为独立函数 isNginxInstalled - 为 select 函数添加 defaultIndex 参数支持默认选项 - 数据库配置保留用户上次输入值作为默认值 - 运行环境和部署方式选择支持记忆上次配置
This commit is contained in:
+7
-5
@@ -15,10 +15,12 @@ async function configureDatabase(cmdArgs) {
|
||||
config.dbType = 'sqlite';
|
||||
log.info('使用默认配置: 数据库类型 = sqlite');
|
||||
} else {
|
||||
const defaultDbType = config.dbType || 'sqlite';
|
||||
const defaultIndex = defaultDbType === 'mysql' ? 2 : 1;
|
||||
config.dbType = await select('选择数据库类型:', [
|
||||
{ label: 'SQLite(零配置,适合开发/小规模)', value: 'sqlite' },
|
||||
{ label: 'MySQL(生产环境推荐)', value: 'mysql' }
|
||||
]);
|
||||
], defaultIndex);
|
||||
}
|
||||
|
||||
if (config.dbType === 'mysql') {
|
||||
@@ -74,11 +76,11 @@ async function configureExistingMySQL() {
|
||||
console.log('\n' + colors.yellow + '请确保 MySQL 服务已启动并创建了数据库' + colors.reset);
|
||||
console.log(colors.gray + '创建数据库命令: CREATE DATABASE idc_management CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' + colors.reset + '\n');
|
||||
|
||||
config.dbConfig.host = await ask('MySQL 主机地址', 'localhost');
|
||||
config.dbConfig.port = await ask('MySQL 端口', '3306');
|
||||
config.dbConfig.username = await ask('MySQL 用户名', 'root');
|
||||
config.dbConfig.host = await ask('MySQL 主机地址', config.dbConfig.host || 'localhost');
|
||||
config.dbConfig.port = await ask('MySQL 端口', config.dbConfig.port || '3306');
|
||||
config.dbConfig.username = await ask('MySQL 用户名', config.dbConfig.username || 'root');
|
||||
config.dbConfig.password = await askPassword('MySQL 密码');
|
||||
config.dbConfig.database = await ask('数据库名称', 'idc_management');
|
||||
config.dbConfig.database = await ask('数据库名称', config.dbConfig.database || 'idc_management');
|
||||
}
|
||||
|
||||
async function installMySQL() {
|
||||
|
||||
@@ -403,7 +403,7 @@ async function checkEnvironment() {
|
||||
}
|
||||
}
|
||||
|
||||
if (commandExists('nginx') || commandExists('nginx.exe')) {
|
||||
if (isNginxInstalled()) {
|
||||
log.success('Nginx 已安装');
|
||||
} else {
|
||||
log.warning('未检测到 Nginx(可选,仅在使用Nginx部署时需要)');
|
||||
|
||||
+10
-8
@@ -311,17 +311,18 @@ async function configureServices(cmdArgs) {
|
||||
config.backendPort = '8000';
|
||||
log.info('使用默认配置: 后端端口 = 8000');
|
||||
} else {
|
||||
config.backendPort = await ask('后端服务端口', '8000');
|
||||
config.backendPort = await ask('后端服务端口', config.backendPort || '8000');
|
||||
}
|
||||
|
||||
if (cmdArgs?.nonInteractive) {
|
||||
config.nodeEnv = 'production';
|
||||
log.info('使用默认配置: 运行环境 = production');
|
||||
} else {
|
||||
const defaultEnvIndex = (config.nodeEnv || 'production') === 'development' ? 2 : 1;
|
||||
config.nodeEnv = await select('选择运行环境:', [
|
||||
{ label: 'production(生产模式,性能优化,推荐正式部署)', value: 'production' },
|
||||
{ label: 'development(开发模式,详细日志,便于调试)', value: 'development' }
|
||||
]);
|
||||
], defaultEnvIndex);
|
||||
}
|
||||
|
||||
if (cmdArgs?.nonInteractive || cmdArgs?.skipNginx) {
|
||||
@@ -329,10 +330,11 @@ async function configureServices(cmdArgs) {
|
||||
config.frontendPort = '3000';
|
||||
log.info('使用默认配置: 前端部署 = PM2 serve (端口 3000)');
|
||||
} else {
|
||||
const defaultDeployIndex = (config.frontendDeploy || 'nginx') === 'pm2' ? 2 : 1;
|
||||
config.frontendDeploy = await select('前端部署方式:', [
|
||||
{ label: 'Nginx(性能最优,推荐生产环境)', value: 'nginx' },
|
||||
{ label: 'PM2 serve(简单快捷,无需额外安装)', value: 'pm2' }
|
||||
]);
|
||||
], defaultDeployIndex);
|
||||
}
|
||||
|
||||
if (config.frontendDeploy === 'nginx') {
|
||||
@@ -380,15 +382,15 @@ async function configureServices(cmdArgs) {
|
||||
}
|
||||
|
||||
if (!cmdArgs?.nonInteractive) {
|
||||
config.frontendPort = await ask('Nginx 监听端口', '80');
|
||||
config.domain = await ask('域名(没有则填localhost)', 'localhost');
|
||||
config.frontendPort = await ask('Nginx 监听端口', config.frontendPort || '80');
|
||||
config.domain = await ask('域名(没有则填localhost)', config.domain || 'localhost');
|
||||
} else {
|
||||
config.frontendPort = '80';
|
||||
config.domain = 'localhost';
|
||||
config.frontendPort = config.frontendPort || '80';
|
||||
config.domain = config.domain || 'localhost';
|
||||
}
|
||||
} else {
|
||||
if (!cmdArgs?.nonInteractive) {
|
||||
config.frontendPort = await ask('前端服务端口', '3000');
|
||||
config.frontendPort = await ask('前端服务端口', config.frontendPort || '3000');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -77,14 +77,14 @@ function askPassword(question) {
|
||||
});
|
||||
}
|
||||
|
||||
async function select(question, options) {
|
||||
async function select(question, options, defaultIndex = 1) {
|
||||
const { colors } = require('./logger');
|
||||
console.log(`\n${question}`);
|
||||
options.forEach((opt, idx) => {
|
||||
console.log(` ${colors.cyan}${idx + 1}.${colors.reset} ${opt.label}`);
|
||||
});
|
||||
|
||||
const answer = await ask('请选择', '1');
|
||||
const answer = await ask('请选择', String(defaultIndex));
|
||||
const index = parseInt(answer) - 1;
|
||||
|
||||
return options[index]?.value || options[0].value;
|
||||
|
||||
Reference in New Issue
Block a user