Skip to content

Commit

Permalink
feat: support .jsonc/json5 config.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 2, 2023
1 parent 0396a53 commit d5f4c9a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 3 deletions.
27 changes: 26 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Auto Config Loader

Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module. It has smart defaults based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.

## 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.
- Reads config from the nearest `package.json` file

## Install

```bash
Expand All @@ -18,6 +23,22 @@ $ npm i auto-config-loader
```js
import load from 'auto-config-loader';

// will look for:
// process.cwd() + '.namespacerc'
// process.cwd() + '.namespacerc.js'
// process.cwd() + '.namespacerc.ts'
// process.cwd() + '.namespacerc.mjs'
// process.cwd() + '.namespacerc.cjs'
// process.cwd() + '.namespacerc.json'
// process.cwd() + '.namespacerc.json5'
// process.cwd() + '.namespacerc.jsonc'
// process.cwd() + '.namespacerc.yaml'
// process.cwd() + '.namespacerc.yml'
// process.cwd() + '.namespacerc.toml'
// process.cwd() + 'namespace.config.mjs'
// process.cwd() + 'namespace.config.cjs'
// process.cwd() + 'namespace.config.js'
// ........
const data = load('namespace', {
defaults: {
testItem2: 'some value'
Expand All @@ -30,7 +51,7 @@ const data = load('namespace', {
Load the JS file and return the result, support `.js`, `.cjs`, `.mjs`, `.ts`.

```js
// => app.config.js
// => ./app/app.config.js
export default {
name: 'app'
}
Expand Down Expand Up @@ -81,6 +102,8 @@ Discover configurations in the specified directory order. When configuring a too
'package.json',
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.json5`,
`.${moduleName}rc.jsonc`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.toml`,
Expand All @@ -90,6 +113,8 @@ Discover configurations in the specified directory order. When configuring a too
`.${moduleName}rc.mjs`,
`.config/${moduleName}rc`,
`.config/${moduleName}rc.json`,
`.config/${moduleName}rc.json5`,
`.config/${moduleName}rc.jsonc`,
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.toml`,
Expand Down
3 changes: 3 additions & 0 deletions core/config-example/ext-json/.autoconfrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"projectName": "ext-json"
}
14 changes: 14 additions & 0 deletions core/config-example/ext-json5/.autoconfrc.json5
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/ext-jsonc/.autoconfrc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// 注释
"projectName": "ext-jsonc"
}
18 changes: 18 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@
"coverage": "tsbb test --coverage --bail"
},
"keywords": [
"rc",
"config",
"loader",
"load",
"configuration",
"cjs",
"commonjs",
"esm",
"es module",
"yml-config",
"js-config",
"ts-config",
"toml-config",
"yml",
"js",
"ts",
"toml",
"typescript",
"config-loader",
"auto-config-loader"
],
Expand All @@ -44,6 +61,7 @@
"jiti": "^1.18.2",
"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"
},
Expand Down
4 changes: 3 additions & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export interface AutoConfOption<T> {
export default function autoConf<T>(namespace: string = 'autoconf', option: AutoConfOption<T> = {}) {
const { searchPlaces = [], defaluts = {}, cwd = process.cwd(), jsOption } = option;
const loaders: Loader<T> = {
'.json': jsonLoader,
'.yml': yamlLoader,
'.yaml': yamlLoader,
'.toml': tomlLoader,
'.json': jsonLoader,
'.json5': jsonLoader,
'.jsonc': jsonLoader,
'.js': jsLoader,
'.ts': jsLoader,
'.cjs': jsLoader,
Expand Down
6 changes: 5 additions & 1 deletion core/src/loader/jsonloader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { AST } from 'jsonc-eslint-parser';
import { parseJSON, getStaticJSONValue } from 'jsonc-eslint-parser';

export function jsonLoader<T>(_: string, content: string): T {
return JSON.parse(content) as T;
const ast: AST.JSONProgram = parseJSON(content);
return getStaticJSONValue(ast) as T;
}
4 changes: 4 additions & 0 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s
...searchPlaces,
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.json5`,
`.${moduleName}rc.jsonc`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.toml`,
Expand All @@ -15,6 +17,8 @@ export function findConfigFile(moduleName: string, root: string, searchPlaces: s
`.${moduleName}rc.mjs`,
`.config/${moduleName}rc`,
`.config/${moduleName}rc.json`,
`.config/${moduleName}rc.json5`,
`.config/${moduleName}rc.jsonc`,
`.config/${moduleName}rc.yaml`,
`.config/${moduleName}rc.yml`,
`.config/${moduleName}rc.toml`,
Expand Down
26 changes: 26 additions & 0 deletions core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ test('Loader .autoconfrc.json', () => {
expect(data?.name).toBe('auto-conf');
});


test('Loader .autoconfrc.json', () => {
const data = 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<any>(undefined, {
cwd: path.resolve(__dirname, '../config-example/ext-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 .autoconfrc.jsonc', () => {
const data = 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, {
cwd: path.resolve(__dirname, '../config-example/js-esm'),
Expand Down

0 comments on commit d5f4c9a

Please sign in to comment.