From 3e30314db7ec4a57a29033bbc4778fdb4ce34236 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 1 Jun 2023 14:10:20 +0800 Subject: [PATCH] feat: support .ts suffx config file loading. --- core/README.md | 5 ++++- core/src/index.ts | 27 +++++++++++++++++++-------- core/src/loader/jsonloader.ts | 3 +++ core/src/utils.ts | 3 +++ 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 core/src/loader/jsonloader.ts diff --git a/core/README.md b/core/README.md index 98805c9..f29b9d7 100644 --- a/core/README.md +++ b/core/README.md @@ -50,16 +50,19 @@ export default function autoConf(namespace?: string, option?: AutoConfOption< `.${moduleName}rc.js`, `.${moduleName}rc.ts`, `.${moduleName}rc.cjs`, + `.${moduleName}rc.mjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, - `.config/${moduleName}rc.jts`, + `.config/${moduleName}rc.ts`, `.config/${moduleName}rc.cjs`, + `.config/${moduleName}rc.mjs`, `${moduleName}.config.js`, `${moduleName}.config.ts`, `${moduleName}.config.cjs`, + `${moduleName}.config.mjs`, ] ``` diff --git a/core/src/index.ts b/core/src/index.ts index 5813e68..e91d8bf 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -1,6 +1,8 @@ import fs from 'fs'; import path from 'path'; +import merge from 'lodash.merge'; import { importDefault } from './loader/jsloader'; +import { jsonLoader } from './loader/jsonloader'; import { findConfigFile } from './utils'; export type Loader = Record T>; @@ -12,22 +14,25 @@ export interface AutoConfOption { } export default function autoConf(namespace: string = 'autoconf', option: AutoConfOption = {}) { - const { searchPlaces = [], cwd = process.cwd() } = option; + const { searchPlaces = [], defaluts = {}, cwd = process.cwd() } = option; const loaders: Loader = { ...(option.loaders || {}), - '.json': (_, content: string) => JSON.parse(content) as T, + '.json': jsonLoader, '.js': importDefault, + '.ts': importDefault, '.cjs': importDefault, '.mjs': importDefault, }; const pkgPath = path.resolve(cwd, 'package.json'); const currentSearchPlaces = findConfigFile(namespace, cwd, searchPlaces); let content = ''; + let resultData: T; let loaderFunc: (filepath: string, content: string) => T; try { if (currentSearchPlaces) { const extname = path.extname(currentSearchPlaces); - if (currentSearchPlaces.endsWith(`.${namespace}rc`) || currentSearchPlaces.endsWith('.json')) { + const basename = path.basename(currentSearchPlaces); + if (new RegExp(`^(.?${namespace}rc)$`).test(basename)) { content = fs.readFileSync(currentSearchPlaces, 'utf-8'); loaderFunc = loaders['.json']; } else if (loaders[extname]) { @@ -36,12 +41,18 @@ export default function autoConf(namespace: string = 'autoconf', option: Auto } } else if (fs.existsSync(pkgPath)) { content = fs.readFileSync(pkgPath, 'utf-8'); - loaderFunc = loaders['.json']; - const result = loaderFunc(currentSearchPlaces, content); - return (result as Record)[namespace]; + const result = loaders['.json'](currentSearchPlaces, content); + resultData = (result as Record)[namespace]; } - return loaderFunc(currentSearchPlaces, content); + + if (content && loaderFunc) { + resultData = loaderFunc(currentSearchPlaces, content); + } + if (resultData) { + return merge(defaluts, resultData); + } + throw new Error(`Can't find config file`); } catch (error) { - console.log(`AUTO_CONF:ERROR: \x1b[31;1m${error}\x1b[0m`); + console.log(`AUTO_CONF:ERROR: \x1b[31;1m${error.message}\x1b[0m`); } } diff --git a/core/src/loader/jsonloader.ts b/core/src/loader/jsonloader.ts new file mode 100644 index 0000000..4f5e9da --- /dev/null +++ b/core/src/loader/jsonloader.ts @@ -0,0 +1,3 @@ +export function jsonLoader(_: string, content: string): T { + return JSON.parse(content) as T; +} diff --git a/core/src/utils.ts b/core/src/utils.ts index c7589c9..d7efd80 100644 --- a/core/src/utils.ts +++ b/core/src/utils.ts @@ -11,6 +11,7 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s `.${moduleName}rc.js`, `.${moduleName}rc.ts`, `.${moduleName}rc.cjs`, + `.${moduleName}rc.mjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, @@ -18,9 +19,11 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s `.config/${moduleName}rc.js`, `.config/${moduleName}rc.ts`, `.config/${moduleName}rc.cjs`, + `.config/${moduleName}rc.mjs`, `${moduleName}.config.js`, `${moduleName}.config.ts`, `${moduleName}.config.cjs`, + `${moduleName}.config.mjs`, ]; for (const file of data) { const filePath = path.resolve(root, file);