monorepo-config 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -18
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/profile.d.ts +5 -6
- package/dist/profile.d.ts.map +1 -1
- package/dist/profile.js +21 -12
- package/dist/profile.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,44 +35,39 @@ Actually setting the config happens in yet another place. Using `setConfig(confi
|
|
|
35
35
|
|
|
36
36
|
## Profile-based usage
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
**Still being worked on! Typescript imports may lead to dependency cycles, which breaks some tools.**
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
export const configDef = makeConfig({
|
|
44
|
-
package: 'your-package',
|
|
45
|
-
schema: z.object({
|
|
46
|
-
foobar: z.string(),
|
|
47
|
-
}),
|
|
48
|
-
depedencies: [ otherConfigDef ],
|
|
49
|
-
profileDir: import.meta.dirname,
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
You can then load the chosen configuration based on a single environment variable like so:
|
|
40
|
+
This allows you to replace the mess of untyped, unchecked env vars across packages with something more manageable (inspired by [Django](https://docs.djangoproject.com/en/6.0/topics/settings/)). You can load the chosen configuration based on a single environment variable like so:
|
|
54
41
|
|
|
55
42
|
```ts
|
|
56
43
|
import { loadConfigProfile } from "monorepo-config";
|
|
57
44
|
import { configDef } from "./config-def.ts";
|
|
58
45
|
|
|
59
|
-
export const
|
|
46
|
+
export const loader = loadProfileByEnvVar('FOOBAR_CONFIG', (profile) => import(`./${profile}.js`).then(x => x.default))
|
|
47
|
+
|
|
48
|
+
export const FOOBAR_CONFIG = await loadConfigProfile(configDef, loader);
|
|
60
49
|
```
|
|
61
50
|
|
|
62
51
|
Profiles are just typescript files like this:
|
|
63
52
|
|
|
64
53
|
```ts
|
|
54
|
+
import type { MergeConfigTypes } from "monorepo-config";
|
|
65
55
|
import type { configDef } from "./config-def.ts";
|
|
56
|
+
import type { otherConfigDef } from "other-package/config-def.ts";
|
|
57
|
+
|
|
58
|
+
type Config = MergeConfigTypes<[
|
|
59
|
+
typeof configDef, typeof otherConfig, ...
|
|
60
|
+
]>;
|
|
66
61
|
|
|
67
62
|
export default {
|
|
68
63
|
'your-package': {
|
|
69
64
|
foobar: 'quuz'
|
|
70
65
|
},
|
|
71
66
|
'other-config': { ... }
|
|
72
|
-
} satisfies
|
|
67
|
+
} satisfies Config;
|
|
73
68
|
```
|
|
74
69
|
|
|
75
|
-
You can choose, like the example, to use the same name the configuration object as the environment variable. The idea is to use the same environment variable for all packages in your monorepo (this mirrors `DJANGO_SETTINGS_MODULE`).
|
|
70
|
+
You can split up the above as you wish. You can choose, like the example, to use the same name for the configuration object as the environment variable. The idea is to use the same environment variable for all packages in your monorepo (this mirrors `DJANGO_SETTINGS_MODULE`).
|
|
76
71
|
|
|
77
72
|
|
|
78
73
|
## Which to use?
|
|
@@ -80,6 +75,13 @@ You can choose, like the example, to use the same name the configuration object
|
|
|
80
75
|
You can mix both in your monorepo! Packages representing reusable libraries probably want to stick with the basic usage and let other packages define their config. Packages that represent apps probably want to define their own config using profiles.
|
|
81
76
|
|
|
82
77
|
|
|
78
|
+
## Common errors
|
|
79
|
+
|
|
80
|
+
### Package name '...' is used for two different config definitions
|
|
81
|
+
|
|
82
|
+
Your package manager may have included two different copies of the same code. Run `pnpm dedupe`, or manually inspect your lockfile to verify this. If not, add `console.trace()` calls to the `defineConfig` function to figure out exactly where the configs are defined.
|
|
83
|
+
|
|
84
|
+
|
|
83
85
|
## TODO
|
|
84
86
|
|
|
85
87
|
- [ ] Vite plugin
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { defineConfig, setConfig, getConfig } from "./basic.js";
|
|
2
2
|
export type { MergeConfigTypes } from "./basic.js";
|
|
3
|
-
export { loadConfigProfile } from "./profile.js";
|
|
3
|
+
export { loadConfigProfile, loadProfileByEnvVar } from "./profile.js";
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAChE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAChE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEhE,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEhE,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/profile.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { output, ZodObject } from "zod";
|
|
2
2
|
import { ConfigDefinition } from "./basic.js";
|
|
3
|
+
export type ProfileLoader = (profile: string) => Promise<object>;
|
|
4
|
+
export declare function loadProfileByEnvVar(envVar: string, importer: ProfileLoader): () => Promise<object>;
|
|
3
5
|
export declare function loadConfigProfile<PackageName extends string, ConfigSchema extends ZodObject, SubSchemas extends Record<string, ZodObject>>(config: ConfigDefinition<PackageName, ConfigSchema, SubSchemas> & {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export declare function loadConfigProfile<PackageName extends string, ConfigSchema extends ZodObject, SubSchemas extends Record<string, ZodObject>>(config: ConfigDefinition<PackageName, ConfigSchema, SubSchemas> & {
|
|
8
|
-
profileSuffix?: string;
|
|
9
|
-
}, envVar: string, profileDir: string): Promise<output<ConfigSchema>>;
|
|
6
|
+
loader: () => Promise<object>;
|
|
7
|
+
}, loader?: () => Promise<object>): Promise<output<ConfigSchema>>;
|
|
8
|
+
export declare function loadConfigProfile<PackageName extends string, ConfigSchema extends ZodObject, SubSchemas extends Record<string, ZodObject>>(config: ConfigDefinition<PackageName, ConfigSchema, SubSchemas>, loader: () => Promise<object>): Promise<output<ConfigSchema>>;
|
|
10
9
|
//# sourceMappingURL=profile.d.ts.map
|
package/dist/profile.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAC7C,OAAO,EAAe,gBAAgB,EAAa,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAC7C,OAAO,EAAe,gBAAgB,EAAa,MAAM,YAAY,CAAC;AAEtE,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAIjE,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,CAWlG;AAED,wBAAsB,iBAAiB,CACrC,WAAW,SAAS,MAAM,EAC1B,YAAY,SAAS,SAAS,EAC9B,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAE5C,MAAM,EAAE,gBAAgB,CAAC,WAAW,EAAE,YAAY,EAAE,UAAU,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;CAAE,EACnG,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GAC7B,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;AAEhC,wBAAsB,iBAAiB,CACrC,WAAW,SAAS,MAAM,EAC1B,YAAY,SAAS,SAAS,EAC9B,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAE5C,MAAM,EAAE,gBAAgB,CAAC,WAAW,EAAE,YAAY,EAAE,UAAU,CAAC,EAC/D,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,GAC5B,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA"}
|
package/dist/profile.js
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import { ConfigError, setConfig } from "./basic.js";
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
const ProfileName = Symbol('monorepo-config:ProfileName');
|
|
3
|
+
export function loadProfileByEnvVar(envVar, importer) {
|
|
4
|
+
return async () => {
|
|
5
|
+
const profileName = process.env[envVar];
|
|
6
|
+
if (profileName === undefined) {
|
|
7
|
+
throw new ConfigError(`Please set the environment variable ${envVar}`);
|
|
8
|
+
}
|
|
9
|
+
const res = await importer(profileName);
|
|
10
|
+
res[ProfileName] = profileName;
|
|
11
|
+
return res;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export async function loadConfigProfile(config, loader) {
|
|
15
|
+
loader = loader ?? config.loader;
|
|
16
|
+
if (loader === undefined) {
|
|
17
|
+
throw new ConfigError(`Either give loader as an argument or set loader in the config metadata`);
|
|
11
18
|
}
|
|
12
|
-
|
|
13
|
-
const
|
|
19
|
+
console.debug("LOADER IS", loader);
|
|
20
|
+
//const path = join(loader, `${configName}.${config.profileSuffix ?? 'js'}`);
|
|
21
|
+
const data = await loader();
|
|
14
22
|
setConfig(config, data);
|
|
15
23
|
if (!config.isLoaded) {
|
|
16
|
-
|
|
24
|
+
const profileName = data[ProfileName];
|
|
25
|
+
console.warn(`WARNING: Imported config (profile ${profileName}) for ${config.package} but config was still not marked as set`);
|
|
17
26
|
}
|
|
18
27
|
return config.output;
|
|
19
28
|
}
|
package/dist/profile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"profile.js","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAoB,SAAS,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"profile.js","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAoB,SAAS,EAAE,MAAM,YAAY,CAAC;AAItE,MAAM,WAAW,GAAkB,MAAM,CAAC,6BAA6B,CAAC,CAAC;AAEzE,MAAM,UAAU,mBAAmB,CAAC,MAAc,EAAE,QAAuB;IACzE,OAAO,KAAK,IAAI,EAAE;QAChB,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxC,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;YAC9B,MAAM,IAAI,WAA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