Skip to content

Commit 232cf4b

Browse files
author
Raul Melo
committed
Remove configFolder option and replace it with absoluteConfigFolderPath for improved configuration handling
1 parent 7baff86 commit 232cf4b

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

.changeset/yellow-crews-fall.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@layerfig/config": patch
3+
---
4+
5+
Remove configFolder from type

docs/src/content/docs/migrate-to-v3.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ These are useful if you want to:
5858

5959
## Breaking Changes
6060

61+
### Remove `configFolder`
62+
63+
Before, if you wanted to point out a different config folder to Layerfig search for files, you would use the `configFolder` option:
64+
65+
```ts
66+
import path from "node:path";
67+
68+
const config = new ConfigBuilder({
69+
validate: (finalConfig) => schema.parse(finalConfig),
70+
configFolder: "./path/to/config-folder",
71+
})
72+
.addSource(new FileSource("base.json"))
73+
.build();
74+
```
75+
76+
The problem was that internally, Layerfig would use `process.cwd()` and resolve to the folder you've specified but in some cases this caused issues.
77+
78+
Now, if you want to change the default folder (or have a different one based on the environment), you can use the `absoluteConfigFolderPath` option:
79+
80+
```ts
81+
import path from "node:path";
82+
83+
const config = new ConfigBuilder({
84+
validate: (finalConfig) => schema.parse(finalConfig),
85+
absoluteConfigFolderPath: path.resolve(
86+
process.cwd(),
87+
"./path/to/config-folder"
88+
),
89+
})
90+
.addSource(new FileSource("base.json"))
91+
.build();
92+
```
93+
94+
Please notice that as the name states, this option expects an **absolute** path to the folder where Layerfig should look for configuration files. If not it will throw an error.
95+
6196
### Custom Parser API
6297

6398
In v2, creating a custom configuration parser looked like this:

docs/src/content/docs/setup/configuration.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,33 @@ export const config = new ConfigBuilder({
4343
.build();
4444
```
4545

46-
## options.configFolder
46+
## options.absoluteConfigFolderPath
4747

48-
> Default: `'./config'`
48+
> Default: `'<process.cwd()>/config'`
4949
50-
By default, file-based sources added via `addSource` are loaded from a `config` folder in the project's root. You can customize this path using the `configFolder` option:
50+
By default, file-based sources added via `addSource` are loaded from a `config` folder in the project's root. You can customize this path using the `absoluteConfigFolderPath` option:
5151

5252
```ts
53+
import path from "node:path";
54+
5355
const config = new ConfigBuilder({
5456
validate: (finalConfig) => schema.parse(finalConfig),
55-
configFolder: "./path/to/config-folder",
57+
absoluteConfigFolderPath: path.resolve(
58+
process.cwd(),
59+
"./path/to/config-folder"
60+
),
5661
})
5762
.addSource(new FileSource("base.json"))
5863
.build();
5964
```
6065

6166
In this example, the library will look for `<root>/path/to/config-folder/base.json`.
6267

68+
<Aside type="danger">
69+
`absoluteConfigFolderPath` must be an absolute path, otherwise it'll throw an
70+
error.
71+
</Aside>
72+
6373
## options.parser
6474

6575
> Default: simple json parser

docs/src/content/docs/setup/sources.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const config = new ConfigBuilder({
2121
.build();
2222
```
2323

24-
In this case, `base.json` is a file located in the `<root-project-dir>/config` directory (or the one specified via `configFolder`).
24+
In this case, `base.json` is a file located in the `<root-project-dir>/config` directory (or the one specified via `absoluteConfigFolderPath`).
2525

2626
<Aside>Without a specified parser, you can only load `.json`.</Aside>
2727

packages/config/src/server/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const ServerConfigBuilderOptionsSchema: z.ZodMiniType<ValidatedServerConf
2020
),
2121
path.resolve(process.cwd(), "./config"),
2222
),
23-
configFolder: z._default(z.string(), "./config"),
2423
runtimeEnv: z._default(RuntimeEnv, process.env),
2524
parser: z._default(z.custom<ConfigParser>(), basicJsonParser),
2625
validate: z.custom<ServerConfigBuilderOptions["validate"]>(),

0 commit comments

Comments
 (0)