import fs from 'fs-extra' import path from 'path' import chalk from 'chalk' import log from '../utils/log' export type LegoOptions = { root: string, port: number, namespaceRoot: string, customFormMatch: string | string[] queryFormMatch: string | string[] } function getLegoConfigPath(): string { let currentCwd: string = process.cwd() let configPath: string = '' while (true) { const possiblePath: string = path.resolve(currentCwd, 'lego.config.js') if (fs.pathExistsSync(possiblePath)) { configPath = possiblePath break; } if (currentCwd === path.resolve(currentCwd, '..')) { break; } currentCwd = path.resolve(currentCwd, '..') } return configPath } export function getLegoConfig(): LegoOptions { const configPath = getLegoConfigPath() const defaultOptions = { root: configPath ? path.dirname(configPath) : process.cwd(), namespaceRoot: path.resolve(configPath ? path.dirname(configPath) : process.cwd(), '.lego_amespace'), customFormMatch: [ '**/*.lego.js' ], queryFormMatch: [ '**/*lego' ], port: 3000 } if (!configPath) { log.warn('没有找到lego.config.js配置文件, 可使用`lego init`初始化一个配置文件') log.info(`将使用默认配置:`) console.log(chalk.cyan(JSON.stringify(defaultOptions, null, 2))) } let userOptions: LegoOptions = configPath ? require(configPath) : {} userOptions = Object.assign(defaultOptions, userOptions) fs.ensureDirSync(userOptions.namespaceRoot) return userOptions }