Skip to content

Commit

Permalink
feat: support .ts suffx config file loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 1, 2023
1 parent 9a5a4b7 commit 3e30314
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
5 changes: 4 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ export default function autoConf<T>(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`,
]
```

Expand Down
27 changes: 19 additions & 8 deletions core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<T> = Record<string, (filepath: string, content: string) => T>;
Expand All @@ -12,22 +14,25 @@ export interface AutoConfOption<T> {
}

export default function autoConf<T>(namespace: string = 'autoconf', option: AutoConfOption<T> = {}) {
const { searchPlaces = [], cwd = process.cwd() } = option;
const { searchPlaces = [], defaluts = {}, cwd = process.cwd() } = option;
const loaders: Loader<T> = {
...(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]) {
Expand All @@ -36,12 +41,18 @@ export default function autoConf<T>(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<string, T>)[namespace];
const result = loaders['.json'](currentSearchPlaces, content);
resultData = (result as Record<string, T>)[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`);
}
}
3 changes: 3 additions & 0 deletions core/src/loader/jsonloader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function jsonLoader<T>(_: string, content: string): T {
return JSON.parse(content) as T;
}
3 changes: 3 additions & 0 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ 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`,
`.config/${moduleName}rc.yml`,
`.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);
Expand Down

0 comments on commit 3e30314

Please sign in to comment.