Skip to content

Commit

Permalink
feat: support .ini config.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 2, 2023
1 parent d5f4c9a commit 67bb12d
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Find and load configuration from a `package.json` property, `rc` file, or `Commo

## Features

- Support [JSON](https://www.json.org), [JSONC](https://github.com/microsoft/node-jsonc-parser), [JSON5](), [YAML](https://yaml.org/), [TOML](https://toml.io), CJS, [Typescript](https://www.typescriptlang.org/), and ESM config load.
- 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.
- Reads config from the nearest `package.json` file

## Install
Expand Down Expand Up @@ -107,6 +107,7 @@ Discover configurations in the specified directory order. When configuring a too
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.toml`,
`.${moduleName}rc.ini`,
`.${moduleName}rc.js`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.cjs`,
Expand All @@ -118,6 +119,7 @@ Discover configurations in the specified directory order. When configuring a too
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.toml`,
`.config/${moduleName}rc.ini`,
`.config/${moduleName}rc.js`,
`.config/${moduleName}rc.ts`,
`.config/${moduleName}rc.cjs`,
Expand Down
13 changes: 13 additions & 0 deletions core/config-example/config-dir-ini/.config/autoconfrc.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; this comment is being ignored
scope = global

[database]
user = dbuser
password = dbpassword
database = use_this_database

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
14 changes: 14 additions & 0 deletions core/config-example/config-dir-json5/.config/autoconfrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// 注释
"projectName": "ext-json",
"name": "my-app",
// 允许使用十六进制和八进制数值
"hex": 0x1a,
"octal": 0o755,
// 允许使用 NaN、Infinity 和 -Infinity
"max": Infinity,
"min": -Infinity,
"notANumber": NaN,
// 允许使用尾随逗号
"react-dom": "^16.13.1"
}
4 changes: 4 additions & 0 deletions core/config-example/config-dir-jsonc/.config/autoconfrc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// 注释
"projectName": "ext-jsonc"
}
13 changes: 13 additions & 0 deletions core/config-example/ext-ini/.autoconfrc.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; this comment is being ignored
scope = global

[database]
user = dbuser
password = dbpassword
database = use_this_database

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
4 changes: 3 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@
"node": ">=16.0.0"
},
"dependencies": {
"ini": "^4.1.1",
"jiti": "^1.18.2",
"jsonc-eslint-parser": "^2.3.0",
"lodash.merge": "^4.6.2",
"sucrase": "^3.32.0",
"jsonc-eslint-parser": "^2.3.0",
"toml-eslint-parser": "^0.6.0",
"yaml-eslint-parser": "^1.2.2"
},
"devDependencies": {
"@types/ini": "^1.3.31",
"@types/lodash.merge": "^4.6.7"
}
}
3 changes: 3 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { jsLoader, LoadConfOption } from './loader/jsloader';
import { jsonLoader } from './loader/jsonloader';
import { yamlLoader } from './loader/yamlloader';
import { tomlLoader } from './loader/tomlloader';
import { iniLoader } from './loader/ini';
import { findConfigFile } from './utils';

export * from './loader/jsloader';
export * from './loader/jsonloader';
export * from './loader/yamlloader';
export * from './loader/tomlloader';
export * from './loader/ini';

export type LoaderFunc<T> = (filepath: string, content: string, jsOption?: LoadConfOption) => T;
export type Loader<T> = Record<string, LoaderFunc<T>>;
Expand All @@ -36,6 +38,7 @@ export default function autoConf<T>(namespace: string = 'autoconf', option: Auto
const loaders: Loader<T> = {
'.yml': yamlLoader,
'.yaml': yamlLoader,
'.ini': iniLoader,
'.toml': tomlLoader,
'.json': jsonLoader,
'.json5': jsonLoader,
Expand Down
5 changes: 5 additions & 0 deletions core/src/loader/ini.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ini from 'ini';

export function iniLoader<T>(_: string, content: string): T {
return ini.parse(content) as T;
}
2 changes: 2 additions & 0 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.toml`,
`.${moduleName}rc.ini`,
`.${moduleName}rc.js`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.cjs`,
Expand All @@ -22,6 +23,7 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.toml`,
`.config/${moduleName}rc.ini`,
`.config/${moduleName}rc.js`,
`.config/${moduleName}rc.ts`,
`.config/${moduleName}rc.cjs`,
Expand Down
53 changes: 52 additions & 1 deletion core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ test('Loader .autoconfrc.json5', () => {
expect(data?.notANumber).toBe(NaN);
});

test('Loader .config/autoconfrc.json5', () => {
const data = autoConf<any>(undefined, {
cwd: path.resolve(__dirname, '../config-example/config-dir-json5'),
});
expect(data?.projectName).toBe('ext-json');
expect(data?.name).toBe('my-app');
expect(data?.max).toBe(Infinity);
expect(data?.min).toBe(-Infinity);
expect(data?.notANumber).toBe(NaN);
});

test('Loader .config/autoconfrc.jsonc', () => {
const data = autoConf<any>(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, {
cwd: path.resolve(__dirname, '../config-example/ext-jsonc'),
Expand Down Expand Up @@ -267,6 +285,39 @@ test('Loader .autoconfrc.ts', () => {
expect(data?.projectName).toEqual('ext-ts');
});

const iniData = {
"database": {
"database": "use_this_database",
"password": "dbpassword",
"user": "dbuser",
},
"paths": {
"default": {
"array": [
"first value",
"second value",
"third value",
],
"datadir": "/var/lib/data",
},
},
"scope": "global",
}

test('Loader .autoconfrc.ini', () => {
const data = autoConf<any>(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, {
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, {
cwd: path.resolve(__dirname, '../config-example/ext-ts'),
Expand All @@ -284,4 +335,4 @@ test('Loader cwd = undefined', () => {
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`);
});
});

0 comments on commit 67bb12d

Please sign in to comment.