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: layer meta and source options #62

Merged
merged 8 commits into from
Mar 13, 2023
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
48 changes: 41 additions & 7 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@ import { DotenvOptions, setupDotenv } from "./dotenv";

export type UserInputConfig = Record<string, any>;

export interface ConfigLayerMeta {
name?: string;
[key: string]: any;
}

export interface C12InputConfig {
extends?: string | string[];
$envName?: string;
$test?: UserInputConfig;
$development?: UserInputConfig;
$production?: UserInputConfig;
$env?: Record<string, UserInputConfig>;
$meta?: ConfigLayerMeta;
}

export interface InputConfig extends C12InputConfig, UserInputConfig {}

export interface SourceOptions {
meta?: ConfigLayerMeta;
pi0 marked this conversation as resolved.
Show resolved Hide resolved
overrides?: UserInputConfig;
[key: string]: any;
}

export interface ConfigLayer<T extends InputConfig = InputConfig> {
config: T | null;
source?: string;
sourceOptions?: SourceOptions;
meta?: ConfigLayerMeta;
cwd?: string;
configFile?: string;
}
Expand Down Expand Up @@ -215,18 +229,28 @@ async function extendConfig(config, options: LoadConfigOptions) {
);
delete config[key];
}
for (const extendSource of extendSources) {
for (let extendSource of extendSources) {
const originalExtendSource = extendSource;
let sourceOptions = {};
if (extendSource.source) {
sourceOptions = extendSource.options || {};
extendSource = extendSource.source;
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(extendSource)) {
sourceOptions = extendSource[1] || {};
extendSource = extendSource[0];
}
if (typeof extendSource !== "string") {
// TODO: Use error in next major versions
// eslint-disable-next-line no-console
console.warn(
`Cannot extend config from \`${JSON.stringify(
extendSource
)}\` (which should be a string) in ${options.cwd}`
originalExtendSource
)}\` in ${options.cwd}`
);
continue;
}
const _config = await resolveConfig(extendSource, options);
const _config = await resolveConfig(extendSource, options, sourceOptions);
if (!_config.config) {
// TODO: Use error in next major versions
// eslint-disable-next-line no-console
Expand All @@ -252,7 +276,8 @@ const NPM_PACKAGE_RE =

async function resolveConfig(
source: string,
options: LoadConfigOptions
options: LoadConfigOptions,
sourceOptions: SourceOptions = {}
): Promise<ResolvedConfig> {
// Custom user resolver
if (options.resolve) {
Expand Down Expand Up @@ -292,7 +317,7 @@ async function resolveConfig(
if (isDir) {
source = options.configFile;
}
const res: ResolvedConfig = { config: undefined, cwd };
const res: ResolvedConfig = { config: undefined, cwd, source, sourceOptions };
try {
res.configFile = options.jiti.resolve(resolve(cwd, source), {
paths: [cwd],
Expand All @@ -316,5 +341,14 @@ async function resolveConfig(
res.config = defu(envConfig, res.config);
}

// Meta
res.meta = defu(res.sourceOptions.meta, res.config.$meta);
pi0 marked this conversation as resolved.
Show resolved Hide resolved
delete res.config.$meta;

// Overrides
if (res.sourceOptions.overrides) {
res.config = defu(res.sourceOptions.overrides, res.config);
}

return res;
}
4 changes: 4 additions & 0 deletions test/fixture/base/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default {
$meta: {
name: "base",
version: "1.0.0",
},
baseConfig: true,
colors: {
primary: "base_primary",
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
theme: "./theme",
extends: ["c12-npm-test"],
extends: [["c12-npm-test", { userMeta: 123 }]],
$test: {
extends: ["./config.dev"],
envConfig: true,
Expand Down
24 changes: 23 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ describe("c12", () => {
"envConfig": true,
"extends": [
"./config.dev",
"c12-npm-test",
[
"c12-npm-test",
{
"userMeta": 123,
},
],
],
"overriden": false,
"theme": "./theme",
Expand Down Expand Up @@ -126,6 +131,9 @@ describe("c12", () => {
},
"configFile": "<path>/fixture/theme/config.ts",
"cwd": "<path>/fixture/theme",
"meta": {},
"source": "config",
"sourceOptions": {},
},
{
"config": {
Expand All @@ -146,20 +154,34 @@ describe("c12", () => {
},
"configFile": "<path>/fixture/base/config.ts",
"cwd": "<path>/fixture/base",
"meta": {
"name": "base",
"version": "1.0.0",
},
"source": "config",
"sourceOptions": {},
},
{
"config": {
"devConfig": true,
},
"configFile": "<path>/fixture/config.dev.ts",
"cwd": "<path>/fixture",
"meta": {},
"source": "./config.dev",
"sourceOptions": {},
},
{
"config": {
"npmConfig": true,
},
"configFile": "<path>/fixture/node_modules/c12-npm-test/config.ts",
"cwd": "<path>/fixture/node_modules/c12-npm-test",
"meta": {},
"source": "<path>/fixture/node_modules/c12-npm-test/config.ts",
"sourceOptions": {
"userMeta": 123,
},
},
{
"config": {
Expand Down