import { existsSync, mkdirSync, writeFileSync, } from 'fs'; import { join } from 'path'; import { CONF_DIR, HOME_DIR } from '../constant/path'; import { print } from '../utils/print'; import { generateCsvConf } from './generate-csv-conf'; import { generateLogConf } from './generate-log-conf'; export interface CreatConf { type: 'csv' | 'log'; inputDir: string; outputDir: string; } export interface CommonGenerateConf { outputDirPath: string; inputDirPath: string; } /** * 获取文件全路径 * @param type * @returns */ export function getFullFileNameByType(type: string): string { return `${type}.conf.js`; } /** * 获取配置文件 * @param type * @returns */ export function getConfig(type: string): Promise { const path = join(HOME_DIR, CONF_DIR, getFullFileNameByType(type)); if (!existsSync(path)) { throw new Error(`配置文件不存在: ${path}, 请先使用 'tuc -g' 创建配置文件`); } return import(path); } /** * 生成配置文件 * @param conf */ export function createConf(conf: CreatConf): void { const dirPath = join(HOME_DIR, CONF_DIR); if (!existsSync(dirPath)) { mkdirSync(dirPath); } const { type, inputDir, outputDir } = conf; const confMap = { log: generateLogConf, csv: generateCsvConf, }; const filePath = join(dirPath, getFullFileNameByType(type)); confMap[type]({ inputDirPath: inputDir, outputDirPath: outputDir, }).then((data) => { writeFileSync(filePath, data); print(`配置文件创建成功,目录为: ${filePath}`); }); }