Skip to content

Commit

Permalink
feat: add getConfigPath util.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 3, 2023
1 parent 2c6902b commit 0d11cd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 16 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ export declare const merge: {
export declare function findConfigFile(moduleName: string, root: string, searchPlaces?: string[]): string;
```

### getConfigPath

```ts
export declare const getConfigPath: () => string;
```

Example:

```ts
import { autoConf, getConfigPath } from 'auto-config-loader';

const data = autoConf<Config>('idoc');
const configPath = getConfigPath();
// => /.autoconfrc.js
```

## Related

- [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig) Find and load configuration from a package.json property, rc file, or CommonJS module
Expand Down
20 changes: 12 additions & 8 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface AutoConfOption<T> {
ignoreLog?: boolean;
}

let configPath = '';

export const getConfigPath = () => configPath;

/**
* Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module.
* @param namespace {string} Configuration base name. The default is `autoconf`.
Expand All @@ -54,29 +58,29 @@ export function autoConf<T>(namespace: string = 'autoconf', option: AutoConfOpti
...(option.loaders || {}),
};
const pkgPath = path.resolve(cwd, 'package.json');
const currentSearchPlaces = findConfigFile(namespace, cwd, searchPlaces);
configPath = findConfigFile(namespace, cwd, searchPlaces);
let content = '';
let resultData: T;
let loaderFunc: LoaderFunc<T>;
try {
if (currentSearchPlaces) {
const extname = path.extname(currentSearchPlaces);
const basename = path.basename(currentSearchPlaces);
if (configPath) {
const extname = path.extname(configPath);
const basename = path.basename(configPath);
if (new RegExp(`^(.?${namespace}rc)$`).test(basename)) {
content = fs.readFileSync(currentSearchPlaces, 'utf-8');
content = fs.readFileSync(configPath, 'utf-8');
loaderFunc = loaders['.json'];
} else if (loaders[extname]) {
content = fs.readFileSync(currentSearchPlaces, 'utf-8');
content = fs.readFileSync(configPath, 'utf-8');
loaderFunc = loaders[extname];
}
} else if (fs.existsSync(pkgPath)) {
content = fs.readFileSync(pkgPath, 'utf-8');
const result = loaders['.json'](currentSearchPlaces, content);
const result = loaders['.json'](configPath, content);
resultData = (result as Record<string, T>)[namespace];
}

if (content && loaderFunc) {
resultData = loaderFunc(currentSearchPlaces, content, jsOption);
resultData = loaderFunc(configPath, content, jsOption);
if (typeof resultData === 'function') {
return merge(defaultValue, resultData, { default: resultData });
}
Expand Down

0 comments on commit 0d11cd6

Please sign in to comment.