import { app } from '../app.ts'; import { z } from 'zod'; import { checkFileExists, getConfig, writeConfig } from '@/module/index.ts'; import path from 'path'; import fs from 'fs'; import { chalk } from '@/module/chalk.ts'; import { confirm, input } from '@inquirer/prompts'; const setWorkdir = async (workdir: string) => { const execPath = process.cwd(); let flag = false; let config = getConfig(); let finalPath: string; if (workdir.startsWith('/')) { finalPath = workdir; } else { finalPath = path.join(execPath, workdir); } if (!checkFileExists(finalPath)) { console.log('路径不存在'); fs.mkdirSync(finalPath, { recursive: true }); } const confirmed = await confirm({ message: `Are you sure you want to set the workdir to: ${finalPath}?`, default: false, }); if (confirmed) { flag = true; config.workdir = finalPath; console.log(chalk.green(`set workdir success:`, finalPath)); } else { console.log('Cancel set workdir'); } if (flag) { writeConfig(config); } }; app.route({ path: 'config', key: 'main', description: 'config 命令', metadata: { middleware: ['auth'], args: { dev: z.string().optional().describe('Specify dev'), set: z.string().optional().describe('set config'), get: z.string().optional().describe('get config'), remove: z.string().optional().describe('remove config'), value: z.string().optional().describe('value'), workdir: z.string().optional().describe('web config'), } } }).define(async (ctx) => { const { dev, workdir, set, get: getKey, remove, value } = ctx.args; let config = getConfig(); let flag = false; const execPath = process.cwd(); if (dev === 'true' || dev === 'false') { flag = true; if (dev === 'true') { config.dev = true; } else { config.dev = false; } } if (workdir) { let finalPath: string; if (workdir.startsWith('/')) { finalPath = workdir; } else { finalPath = path.join(execPath, workdir); } if (!checkFileExists(finalPath)) { console.log('路径不存在'); fs.mkdirSync(finalPath, { recursive: true }); } const confirmed = await confirm({ message: `Are you sure you want to set the workdir to: ${finalPath}?`, default: false, }); if (confirmed) { flag = true; config.workdir = finalPath; console.log(chalk.green(`set workdir success:`, finalPath)); } else { console.log('Cancel set workdir'); } } if (set) { let val = value; if (!val) { val = await input({ message: `Enter your ${set}:(current: ${config[set as keyof typeof config]})`, }); } if (set && val) { flag = true; config[set] = val; } } if (remove) { delete config[remove]; flag = true; } if (flag) { writeConfig(config); } }).addTo(app) app.route({ path: 'config', key: 'set', description: 'set config', metadata: { middleware: ['auth'], args: { key: z.string().describe('配置键名'), value: z.string().optional().describe('配置值'), } } }).define(async (ctx) => { const { key, value } = ctx.args; const config = getConfig(); if (!key) { console.log('key is empty'); return; } let flag = false; let val = value || 'not_input'; if (val === 'not_input') { val = await input({ message: `Enter your ${key}:(current: ${config[key as keyof typeof config]})`, }); } if (key === 'workdir') { await setWorkdir(val); return; } const transformValue = (val: string) => { if (val === 'true') { return true; } if (val === 'false') { return false; } if (!isNaN(Number(val))) { return Number(val); } return val; }; const newValue = transformValue(val); if (key && val) { flag = true; if (key === 'dev') { if (val === 'true') { config.dev = true; } else { config.dev = false; } } else { config[key] = val; } console.log(chalk.green(`set ${key} success:`, config.key)); } if (flag) { writeConfig(config); } }).addTo(app) app.route({ path: 'config', key: 'get', description: 'get config', metadata: { middleware: ['auth'], args: { key: z.string().optional().describe('配置键名'), } } }).define(async (ctx) => { const { key } = ctx.args; const config = getConfig(); const keys = Object.keys(config); let selectedKey = key; if (!selectedKey) { selectedKey = await input({ message: `Enter your key:(keys: ${JSON.stringify(keys)})`, }); } if (config[selectedKey as keyof typeof config]) { console.log(chalk.green(`get ${selectedKey}:`)); console.log(config[selectedKey as keyof typeof config]); } else { console.log(chalk.red(`not found ${selectedKey}`)); } }).addTo(app) app.route({ path: 'config', key: 'remove', description: 'remove config', metadata: { middleware: ['auth'], args: { key: z.string().describe('配置键名'), } } }).define(async (ctx) => { const { key } = ctx.args; const config = getConfig(); if (key) { delete config[key]; writeConfig(config); console.log(chalk.green(`remove ${key} success`)); } }).addTo(app) app.route({ path: 'config', key: 'list', description: 'list config', metadata: { middleware: ['auth'], args: {} } }).define(async () => { const config = getConfig(); console.log(chalk.green('config', JSON.stringify(config, null, 2))); }).addTo(app)