Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support .config dir #136

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.
## βœ… Features

- `.js`, `.ts`, `.cjs`, `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
- `.json`, `.json5` and `.jsonc` config support.
- `.json`, `.json5` and `.jsonc` config support
- `.config/` directory support following [config dir proposal](https://github.com/pi0/config-dir)
- `.rc` config support with [unjs/rc9](https://github.com/unjs/rc9)
- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
Expand Down
24 changes: 16 additions & 8 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,16 @@ async function resolveConfig<
source = cloned.dir;
}

// Try resolving as npm package
if (NPM_PACKAGE_RE.test(source)) {
// Util to try resolving a module
const tryResolve = (id: string) => {
try {
source = options.jiti!.resolve(source, { paths: [options.cwd!] });
return options.jiti!.resolve(id, { paths: [options.cwd!] });
} catch {}
};

// Try resolving as npm package
if (NPM_PACKAGE_RE.test(source)) {
source = tryResolve(source) || source;
}

// Import from local fs
Expand All @@ -314,19 +319,22 @@ async function resolveConfig<
}
const res: ResolvedConfig<T, MT> = {
config: undefined as unknown as T,
configFile: undefined,
cwd,
source,
sourceOptions,
};
try {
res.configFile = options.jiti!.resolve(resolve(cwd, source), {
paths: [cwd],
});
} catch {}

res.configFile =
tryResolve(resolve(cwd, source)) ||
tryResolve(resolve(cwd, ".config", source.replace(/\.config$/, ""))) ||
tryResolve(resolve(cwd, ".config", source)) ||
source;

if (!existsSync(res.configFile!)) {
return res;
}

if (res.configFile!.endsWith(".jsonc")) {
const { parse } = await import("jsonc-parser");
res.config = parse(await readFile(res.configFile!, "utf8"));
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe("c12", () => {
"secondary": "theme_secondary",
},
},
"configFile": "<path>/fixture/theme/test.config.json5",
"configFile": "<path>/fixture/theme/.config/test.config.json5",
"cwd": "<path>/fixture/theme",
"meta": {},
"source": "test.config",
Expand Down
Loading