diff --git a/core/README.md b/core/README.md index 1736fc2..f8f8eb4 100644 --- a/core/README.md +++ b/core/README.md @@ -9,6 +9,8 @@ Auto Config Loader Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want. +[V1 To V2 Migration](#v1-to-v2-migration) + ## Features - Support [JSON](https://www.json.org), [JSONC](https://github.com/microsoft/node-jsonc-parser), [JSON5](https://json5.org/), [YAML](https://yaml.org/), [TOML](https://toml.io), [INI](https://en.wikipedia.org/wiki/INI_file), [CJS](http://www.commonjs.org), [Typescript](https://www.typescriptlang.org/), and ESM config load. @@ -43,7 +45,7 @@ import { autoConf } from 'auto-config-loader'; // process.cwd() + 'namespace.config.cjs' // process.cwd() + 'namespace.config.js' // ........ -const data = autoConf('namespace', { +const data = await autoConf('namespace', { default: { testItem2: 'some value' } @@ -68,7 +70,7 @@ interface Config { name: string; } -const result = loadConf('./app/app.config.js'); +const result = await loadConf('./app/app.config.js'); // => { name: 'app' } ``` @@ -76,14 +78,14 @@ const result = loadConf('./app/app.config.js'); ```ts import { LoadConfOption } from 'auto-config-loader'; -export type LoaderFunc = (filepath: string, content: string, jsOption?: LoadConfOption) => T; +export type LoaderFunc = (filepath: string, content: string, jsOption?: LoadConfOption) => T | Promise; export type Loader = Record>; export interface AutoConfOption { searchPlaces?: string[]; /** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */ loaders?: Loader; /** Specify default configuration. It has the lowest priority and is applied after extending config. */ - defaluts?: T; + default?: T; /** Resolve configuration from this working directory. The default is `process.cwd()` */ cwd?: string; /** Default transform js configuration */ @@ -92,12 +94,14 @@ export interface AutoConfOption { ignoreLog?: boolean; mustExist?: boolean; } +export declare const getConfigPath: () => string; /** * Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. * @param namespace {string} Configuration base name. The default is `autoconf`. - * @param option + * @param option */ -export default function autoConf(namespace?: string, option?: AutoConfOption): {} & T; +export declare function autoConf(namespace?: string, option?: AutoConfOption): Promise<{} & T>; +export default autoConf; ``` Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where `${moduleName}` represents the name of the tool: @@ -187,7 +191,7 @@ function loadJS(filepath, content) { }); } -const data = load('namespace', { +const data = await load('namespace', { loaders: { '.js': loadJS, '.ts': loadJS, @@ -276,7 +280,7 @@ function loadYaml(filepath, content) { return yaml.parse(content); } -const data = load('namespace', { +const data = await load('namespace', { searchPlaces: [ '.namespacerc.yaml', '.namespacerc.yml', @@ -298,9 +302,9 @@ const data = load('namespace', { ```ts export declare const merge: { (object: TObject, source: TSource): TObject & TSource; - (object: TObject_1, source1: TSource1, source2: TSource2): TObject_1 & TSource1 & TSource2; - (object: TObject_2, source1: TSource1_1, source2: TSource2_1, source3: TSource3): TObject_2 & TSource1_1 & TSource2_1 & TSource3; - (object: TObject_3, source1: TSource1_2, source2: TSource2_2, source3: TSource3_1, source4: TSource4): TObject_3 & TSource1_2 & TSource2_2 & TSource3_1 & TSource4; + (object: TObject, source1: TSource1, source2: TSource2): TObject & TSource1 & TSource2; + (object: TObject, source1: TSource1, source2: TSource2, source3: TSource3): TObject & TSource1 & TSource2 & TSource3; + (object: TObject, source1: TSource1, source2: TSource2, source3: TSource3, source4: TSource4): TObject & TSource1 & TSource2 & TSource3 & TSource4; (object: any, ...otherArgs: any[]): any; }; ``` @@ -327,6 +331,59 @@ const configPath = getConfigPath(); // => /.autoconfrc.js ``` +## V1 To V2 Migration + +This guide provides the steps to migrate to the latest version of the configuration loader API. + +### Key Changes + +1. **Loader Functions Support Async** + - `LoaderFunc` now supports returning `T` or `Promise`. + - Update custom loaders to handle asynchronous operations if needed. + + **Example:** + ```ts + export type LoaderFunc = (filepath: string, content: string, jsOption?: LoadConfOption) => T | Promise; + ``` + +2. **`autoConf` Returns a Promise** + - The `autoConf` function now returns a `Promise` instead of a synchronous result. + - Update your code to handle asynchronous calls. + + **Example:** + ```ts + export declare function autoConf(namespace?: string, option?: AutoConfOption): Promise<{} & T>; + ``` + +### Migration Steps + +#### 1. Update Custom Loader Functions + +If you have custom loaders, update their return types to support asynchronous operations: + +**Example:** + +```ts +const jsonLoader: LoaderFunc = async (filepath, content) => JSON.parse(content); +``` + +#### 2. Handle Asynchronous `autoConf` Calls + +Update all calls to `autoConf` to use `await` or `.then` to handle Promises: + +**Example Using `await`:** +```ts +const config = await autoConf('myNamespace', options); +console.log(config); +``` + +**Example Using `.then`:** +```ts +autoConf('myNamespace', options).then(config => { + console.log(config); +}); +``` + ## Related - [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig) Find and load configuration from a package.json property, rc file, or CommonJS module diff --git a/core/package.json b/core/package.json index c1a7612..d3a2136 100644 --- a/core/package.json +++ b/core/package.json @@ -60,10 +60,10 @@ }, "dependencies": { "ini": "^5.0.0", - "jiti": "^1.18.2", + "jiti": "^2.4.1", "jsonc-eslint-parser": "^2.3.0", "lodash.merge": "^4.6.2", - "sucrase": "^3.32.0", + "sucrase": "^3.35.0", "toml-eslint-parser": "^0.10.0", "yaml-eslint-parser": "^1.2.2" }, diff --git a/core/src/index.ts b/core/src/index.ts index fb5cf01..94dffdb 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -17,7 +17,7 @@ export * from './loader/ini'; export const merge = mergeFun; -export type LoaderFunc = (filepath: string, content: string, jsOption?: LoadConfOption) => T; +export type LoaderFunc = (filepath: string, content: string, jsOption?: LoadConfOption) => T | Promise; export type Loader = Record>; export interface AutoConfOption { searchPlaces?: string[]; @@ -43,7 +43,7 @@ export const getConfigPath = () => configPath; * @param namespace {string} Configuration base name. The default is `autoconf`. * @param option */ -export function autoConf(namespace: string = 'autoconf', option: AutoConfOption = {}) { +export async function autoConf(namespace: string = 'autoconf', option: AutoConfOption = {}) { const { searchPlaces = [], default: defaultValue = {}, @@ -89,7 +89,7 @@ export function autoConf(namespace: string = 'autoconf', option: AutoConfOpti } if (content && loaderFunc) { - resultData = loaderFunc(configPath, content, jsOption); + resultData = await loaderFunc(configPath, content, jsOption); if (typeof resultData === 'function') { return merge(defaultValue, resultData, { default: resultData }); } diff --git a/core/src/loader/js.ts b/core/src/loader/js.ts index 300e806..a871baa 100644 --- a/core/src/loader/js.ts +++ b/core/src/loader/js.ts @@ -1,12 +1,15 @@ -import jitiFactory from 'jiti'; -import type { JITIOptions } from 'jiti/dist/types'; +import { createJiti } from 'jiti'; +import type jiti from 'jiti'; import { transform, Options } from 'sucrase'; -let jiti: ReturnType | null = null; +type Jiti = ReturnType; +type JITIOptions = Jiti['options']; + +let jitiInstance: ReturnType | null = null; function lazyJiti(option: JITIOptions = {}, transformOpt = {} as Options) { return ( - jiti ?? - (jiti = jitiFactory(__filename, { + jitiInstance ?? + (jitiInstance = createJiti(__filename, { interopDefault: true, ...option, transform: (opts) => { @@ -25,22 +28,28 @@ export interface LoadConfOption { transformOption?: Options; } -export function loadConf(path: string, option: LoadConfOption = {}): T { +export async function loadConf(path: string, option: LoadConfOption = {}): Promise { const { jiti = true, jitiOptions, transformOption } = option; - let config = (function () { + let config = await (async function () { try { if (jiti) { - return path ? lazyJiti(jitiOptions, transformOption)(path) : {}; + return path ? await lazyJiti(jitiOptions, transformOption).import(path) : {}; } else { return path ? require(path) : {}; } } catch { - return lazyJiti(jitiOptions, transformOption)(path); + return await lazyJiti(jitiOptions, transformOption).import(path); } })(); - return config.default ?? config; + + // Ensure both default export and named exports are handled + if (config.default) { + config = { ...config.default, ...config }; + } + + return config; } -export function jsLoader(filepath: string, content: string, option: LoadConfOption = {}): T { - return loadConf(filepath, option); +export async function jsLoader(filepath: string, content: string, option: LoadConfOption = {}): Promise { + return await loadConf(filepath, option); } diff --git a/core/test/config.test.ts b/core/test/config.test.ts index 5c6d5c6..6a39d57 100644 --- a/core/test/config.test.ts +++ b/core/test/config.test.ts @@ -2,37 +2,37 @@ import path from 'path'; import autoConf from '../src'; import { autoConf as confLoad } from '../src'; -test('Loader .autoconfrc', () => { - const data = autoConf<{ one: number; }>(undefined, { +test('Loader .autoconfrc', async () => { + const data = await autoConf<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example'), }); expect(data?.one).toBe(123); }); -test('Loader .autoconfrc', () => { - const data = confLoad<{ one: number; }>(undefined, { +test('Loader .autoconfrc', async () => { + const data = await confLoad<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example'), }); expect(data?.one).toBe(123); }); -test('Loader .autoconfrc.json', () => { - const data = autoConf<{ name: string; }>(undefined, { +test('Loader .autoconfrc.json', async () => { + const data = await autoConf<{ name: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/autoconfrcjson'), }); expect(data?.name).toBe('auto-conf'); }); -test('Loader .autoconfrc.json', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.json', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-json'), }); expect(data?.projectName).toBe('ext-json'); }); -test('Loader .autoconfrc.json5', () => { - const data = autoConf(undefined, { +test('Loader .autoconfrc.json5', async () => { + const data = await autoConf(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-json5'), }); expect(data?.projectName).toBe('ext-json'); @@ -42,8 +42,8 @@ test('Loader .autoconfrc.json5', () => { expect(data?.notANumber).toBe(NaN); }); -test('Loader .config/autoconfrc.json5', () => { - const data = autoConf(undefined, { +test('Loader .config/autoconfrc.json5', async () => { + const data = await autoConf(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-json5'), }); expect(data?.projectName).toBe('ext-json'); @@ -53,100 +53,100 @@ test('Loader .config/autoconfrc.json5', () => { expect(data?.notANumber).toBe(NaN); }); -test('Loader .config/autoconfrc.jsonc', () => { - const data = autoConf(undefined, { +test('Loader .config/autoconfrc.jsonc', async () => { + const data = await autoConf(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-jsonc'), }); expect(data?.projectName).toBe('ext-jsonc'); }); -test('Loader .autoconfrc.jsonc', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.jsonc', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-jsonc'), }); expect(data?.projectName).toBe('ext-jsonc'); }); -test('Loader .autoconfrc.js ESM', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.js ESM', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/js-esm'), }); expect(data?.projectName).toBe('js-esm'); }); -test('Loader .autoconfrc.mjs EXT ESM', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.mjs EXT ESM', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-mjs'), }); expect(data?.projectName).toBe('ext-mjs'); }); -test('Loader .autoconfrc.js CJS', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.js CJS', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/js-cjs'), }); expect(data?.projectName).toBe('js-cjs'); }); -test('Loader .autoconfrc.cjs EXT CJS', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.cjs EXT CJS', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/js-cjs'), }); expect(data?.projectName).toBe('js-cjs'); }); -test('Loader .autoconfrc.ts TypeScript', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader .autoconfrc.ts TypeScript', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-cjs'), }); expect(data?.projectName).toBe('ext-cjs'); }); -test('Loader pkg/package.json', () => { - const data = autoConf<{ name: string; }>(undefined, { +test('Loader pkg/package.json', async () => { + const data = await autoConf<{ name: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/pkg'), }); expect(data?.name).toBe('autoconf'); }); -test('Loader autoconf.config.js CJS', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader autoconf.config.js CJS', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-config-js'), }); expect(data?.projectName).toBe('ext-config-js'); }); -test('Loader autoconf.config.mjs', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader autoconf.config.mjs', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-config-mjs'), }); expect(data?.projectName).toBe('ext-config-mjs'); }); -test('Loader autoconf.config.cjs', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader autoconf.config.cjs', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-config-cjs'), }); expect(data?.projectName).toBe('ext-config-cjs'); }); -test('Loader autoconf.config.ts', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('Loader autoconf.config.ts', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-config-ts'), }); expect(data?.projectName).toBe('ext-config-ts'); }); -test('Loader .config/autoconfrc', () => { - const data = autoConf<{ one: number; }>(undefined, { +test('Loader .config/autoconfrc', async () => { + const data = await autoConf<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-rc'), }); expect(data?.one).toBe(123); }); -test('Loader .config/autoconfrc.json', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.json', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { default: { projectName: 'name' }, @@ -158,56 +158,68 @@ test('Loader .config/autoconfrc.json', () => { }); }); -test('Loader .config/autoconfrc.mjs', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.mjs', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { default: { projectName: 'name' }, cwd: path.resolve(__dirname, '../../config-example/config-dir-mjs'), }); expect(data).toEqual({ + default: { + projectName: 'ext-config-mjs' + }, projectName: 'ext-config-mjs' }); }); -test('Loader .config/autoconfrc.cjs', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.cjs', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { default: { projectName: 'name' }, cwd: path.resolve(__dirname, '../../config-example/config-dir-cjs'), }); expect(data).toEqual({ + default: { + projectName: 'ext-config-cjs' + }, projectName: 'ext-config-cjs' }); }); -test('Loader .config/autoconfrc.js', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.js', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { default: { projectName: 'name' }, cwd: path.resolve(__dirname, '../../config-example/config-dir-js'), }); expect(data).toEqual({ + default: { + projectName: 'ext-config-js' + }, projectName: 'ext-config-js' }); }); -test('Loader .config/autoconfrc.ts', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.ts', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { default: { projectName: 'name' }, cwd: path.resolve(__dirname, '../../config-example/config-dir-ts'), }); expect(data).toEqual({ + default: { + projectName: 'ext-config-ts' + }, projectName: 'ext-config-ts' }); }); -test('Loader .autoconfrc.yaml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .autoconfrc.yaml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-yaml'), }); expect(data).toEqual({ @@ -216,8 +228,8 @@ test('Loader .autoconfrc.yaml', () => { }); }); -test('Loader .autoconfrc.yml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .autoconfrc.yml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-yml'), }); expect(data).toEqual({ @@ -226,8 +238,8 @@ test('Loader .autoconfrc.yml', () => { }); }); -test('Loader .config/autoconfrc.yml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.yml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-yml'), }); expect(data).toEqual({ @@ -236,8 +248,8 @@ test('Loader .config/autoconfrc.yml', () => { }); }); -test('Loader .config/autoconfrc.yaml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.yaml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-yaml'), }); expect(data).toEqual({ @@ -270,25 +282,25 @@ const tomlData = { } } -test('Loader .autoconfrc.toml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .autoconfrc.toml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-toml'), }); expect(data).toEqual(tomlData); }); -test('Loader .config/autoconfrc.toml', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.toml', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-toml'), }); expect(data).toEqual(tomlData); }); -test('Loader .autoconfrc.ts', () => { - const data = autoConf<{ default?: Function; projectName?: string; }>(undefined, { +test('Loader .autoconfrc.ts', async () => { + const data = await autoConf<{ default?: Function; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-ts'), }); - expect(data).toHaveProperty(['default', 'projectName']); + expect(data).toHaveProperty(['default']); expect(data?.projectName).toEqual('ext-ts'); }); @@ -311,42 +323,42 @@ const iniData = { "scope": "global", } -test('Loader .autoconfrc.ini', () => { - const data = autoConf(undefined, { +test('Loader .autoconfrc.ini', async () => { + const data = await autoConf(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-ini'), }); expect(data).toEqual(iniData); }); -test('Loader .config/autoconfrc.ini', () => { - const data = autoConf<{ one?: number; projectName?: string; }>(undefined, { +test('Loader .config/autoconfrc.ini', async () => { + const data = await autoConf<{ one?: number; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/config-dir-ini'), }); expect(data).toEqual(iniData); }); -test('Loader .autoconfrc.ts (option={ jiti: false })', () => { - const data = autoConf<{ default?: Function; projectName?: string; }>(undefined, { +test('Loader .autoconfrc.ts (option={ jiti: false })', async () => { + const data = await autoConf<{ default?: Function; projectName?: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-ts'), jsOption: { jiti: false, } }); expect(data).toHaveProperty(['default']); - expect(data?.projectName).toBeUndefined(); + expect(data?.projectName).toEqual('ext-ts'); }); -test('Loader cwd = undefined', () => { +test('Loader cwd = undefined', async () => { console.log = jest.fn(); - const data = autoConf(); + const data = await autoConf(); expect(data).toBeUndefined(); // @ts-ignore expect(console.log.mock.calls[0][0]).toBe(`AUTO_CONF:ERROR: \x1b[31;1mCan't find config file\x1b[0m`); }); -test('Loader ignoreLog = true', () => { +test('Loader ignoreLog = true', async () => { console.log = jest.fn(); - const data = autoConf(undefined, { ignoreLog: true }); + const data = await autoConf(undefined, { ignoreLog: true }); expect(data).toBeNull(); // @ts-ignore expect(console.log.mock.calls).toHaveLength(0); diff --git a/core/test/error.test.ts b/core/test/error.test.ts index 3733cb7..929e25f 100644 --- a/core/test/error.test.ts +++ b/core/test/error.test.ts @@ -1,9 +1,9 @@ import path from 'path'; import autoConf from '../src'; -test('loadConf test case', () => { +test('loadConf test case', async () => { console.log = jest.fn(); - const data = autoConf<{ one: number; }>(undefined, { + const data = await autoConf<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/12312312'), }); expect(data).toBeUndefined(); @@ -11,9 +11,9 @@ test('loadConf test case', () => { expect(console.log.mock.calls[0][0]).toBe(`AUTO_CONF:ERROR: \x1b[31;1mCan't find config file\x1b[0m`); }); -test('loadConf error-config test case, wrong configuration loaded', () => { +test('loadConf error-config test case, wrong configuration loaded', async () => { console.log = jest.fn(); - const data = autoConf<{ one: number; }>(undefined, { + const data = await autoConf<{ one: number; }>(undefined, { mustExist: true, cwd: path.resolve(__dirname, '../../config-example/error-config/mjs'), }); @@ -22,9 +22,9 @@ test('loadConf error-config test case, wrong configuration loaded', () => { expect(console.log.mock.calls[0][0]).toBe(`AUTO_CONF:CATCH:ERROR: \x1b[31;1mReferenceError: path is not defined\x1b[0m`); }); -test('loadConf options mustExist=true test case', () => { +test('loadConf options mustExist=true test case', async () => { console.log = jest.fn(); - const data = autoConf<{ one: number; }>(undefined, { + const data = await autoConf<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/12312312'), mustExist: true, }); @@ -34,9 +34,9 @@ test('loadConf options mustExist=true test case', () => { }); -test('loadConf options mustExist=false test case', () => { +test('loadConf options mustExist=false test case', async () => { console.log = jest.fn(); - const data = autoConf<{ one: number; }>(undefined, { + const data = await autoConf<{ one: number; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/12312312'), mustExist: false, }); diff --git a/core/test/loader.test.ts b/core/test/loader.test.ts index 90d87b9..f035a23 100644 --- a/core/test/loader.test.ts +++ b/core/test/loader.test.ts @@ -3,16 +3,16 @@ import { loadConf } from "../src/loader/js"; const tsPath = path.resolve(__dirname, '../../config-example/ext-ts/.autoconfrc.ts'); -test('loadConf test case', () => { +test('loadConf test case', async () => { // @ts-ignore - expect(loadConf()).toEqual({}); - const result = loadConf(tsPath); + expect(loadConf()).resolves.toEqual({}); + const result = await loadConf(tsPath); expect(result.projectName).toEqual('ext-ts'); - expect(result('good')).toEqual('good123'); + expect(result.default('good')).toEqual('good123'); }); -test('loadConf option=jiti=false test case', () => { - const result = loadConf(tsPath, { jiti: false }); - expect(result.projectName).toBeUndefined(); - expect(result('good')).toEqual('good123'); +test('loadConf option=jiti=false test case', async () => { + const result = await loadConf(tsPath, { jiti: false }); + expect(result.projectName).toEqual('ext-ts'); + expect(result.default('good')).toEqual('good123'); }); diff --git a/core/test/utils.test.ts b/core/test/utils.test.ts index e8953ca..99dd48b 100644 --- a/core/test/utils.test.ts +++ b/core/test/utils.test.ts @@ -5,8 +5,8 @@ test('merge test case', () => { expect(merge({ a: 1}, { b: 2, a: 3})).toEqual({ a: 3, b: 2}); }); -test('getConfigPath test case', () => { - const data = autoConf<{ projectName: string; }>(undefined, { +test('getConfigPath test case', async () => { + const data = await autoConf<{ projectName: string; }>(undefined, { cwd: path.resolve(__dirname, '../../config-example/ext-json'), }); expect(data?.projectName).toBe('ext-json'); diff --git a/package.json b/package.json index 49741af..83fca7c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "private": true, "scripts": { "prepare": "husky", - "start": "npm run start --workspace=core", + "start": "npm run build --workspace=core", "watch": "npm run watch --workspace=core", "version": "lerna version --exact --force-publish --no-push --no-git-tag-version", "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",