Skip to content

Commit

Permalink
rework mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Moeglich committed Nov 13, 2024
1 parent 881a836 commit 0456b90
Show file tree
Hide file tree
Showing 14 changed files with 817 additions and 153 deletions.
1 change: 1 addition & 0 deletions .prototools
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rust = "1.82.0"
54 changes: 53 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "weaveconfig"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
description = "A unified configuration tool for monorepos"
readme = "README.md"
Expand All @@ -16,6 +16,8 @@ biome_js_syntax = "0.5.7"
clap = { version = "4.5.18", features = ["cargo", "derive"] }
fjson = "0.3.1"
futures = "0.3.30"
hashlink = "0.9.1"
lazy_static = "1.5.0"
regex = "1.11.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
Expand Down
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,56 @@

Weaveconfig is a configuration tool for monorepos. It allows you to manage all configuration in a single directory in the root of your project.

To use it just run `weaveconfig` in the root of your project, to create the initial configuration run `weaveconfig init`.
To use it just run `weaveconfig gen` in the root of your project, to create the initial configuration run `weaveconfig init`.

The weaveconfig contains 3 kinds of files:

- `_space.jsonc` - This file contains the configuration for the space. A space typically is an app / package within your monorepo.
- `_env.jsonc` - This file contains the configuration / variables for the space.
- other files - These files will be copied into each space inlined with variables from the space.

## \_space.jsonc

The `_space.jsonc` file defines a configuration space and supports the following fields:

- `name` (required): A unique identifier for the space, used for dependency references. Must be unique across all spaces.

- `dependencies` (optional): An array of other space names that this space depends on. The referenced spaces must exist within the weaveconfig directory. Circular dependencies are not allowed. If the environment names of the dependency don't match they will be remapped based on the equvalent in the root space.

- `environments` (optional): An array of environment names supported by this space (e.g. "development", "staging", "production"). These names are used in mappings and must be unique within the space.
- `space_to_parent_mapping` (optional): Maps environments in this space to environments in the parent space. For root spaces (those without a parent), this maps to the ENV variable values. For non-root spaces, this maps to environments in the closest parent space (nearest ancestor directory with \_space.jsonc). If omitted, environments are inherited as-is from the parent.

Example: `{"prod": ["prod1", "prod2"], "dev": ["dev"]}`

- `generate` (optional): Controls configuration generation options:
- Can be a boolean to toggle all generation
- Or an object with:
- `typescript`: Boolean to toggle TypeScript binding generation

When generation is enabled, it creates:

- `gen/config.json`: Contains the resolved configuration
- `gen/binding.ts`: Provides type-safe access to the configuration
- `gen/.gitignore`: Ignores the generated files from the git index, it's recommended to ignore the whole gen folder rather than just individual files.

## \_env.jsonc

The `_env.jsonc` file contains the actual configuration variables for a space. It supports:

- Shared variables in as JSON, these are available in all environments
- Environment-specific variables using `_<env>.env.jsonc` files (e.g. `_prod.env.jsonc`)
- Variables are merged hierarchically from parent spaces to child spaces
- JSON/JSONC format is supported for both file types

The variables defined in these files will be:

1. Merged according to the space hierarchy
2. Made available in the generated config.json
3. Accessible via the TypeScript bindings when enabled
4. Used to substitute values in other files that are copied to the space from the weaveconfig directory

## Runtime

weaveconfig runs purely at build time generating a config that contains variables for all environments at the same time.
This means that the runtime of your application must choose which environment to use at runtime.
The recommended way to do this is to use the `ENV` environment variable, this is also what the TypeScript binding expects.
34 changes: 12 additions & 22 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,18 @@
},
"uniqueItems": true
},
"mapping": {
"type": "array",
"description": "Defines how environments from the parent and dependency spaces map to environments in this space.\n\nFor root spaces (those without a parent space), this maps environment names from the ENV variable and dependency spaces to the space's environments.\nFor non-root spaces, this maps environment names from the closest parent space (nearest ancestor directory with _space.jsonc) and dependency spaces to the space's environments.\n\nMappings are optional:\n- If omitted, all parent/dependency environments are inherited as-is\n- If provided, you can map all or a subset of environments. Unmapped environments are inherited from the parent\n\nExample: [{\"parent\": \"prod\", \"this\": \"production\"}, {\"parent\": \"dev\", \"this\": \"development\"}]",
"items": {
"type": "object",
"properties": {
"from": {
"type": "string",
"description": "Environment name from the parent space (or ENV variable for root spaces) or dependency space.",
"minLength": 1
},
"this": {
"type": "string",
"description": "Corresponding environment name in this space. Must exist in this space's environments array.",
"minLength": 1
}
"space_to_parent_mapping": {
"type": "object",
"description": "Maps environments in this space to environments in the parent space. For root spaces (those without a parent), this maps to the ENV variable values. For non-root spaces, this maps to environments in the closest parent space (nearest ancestor directory with _space.jsonc).\n\nIf omitted, environments are inherited as-is from the parent.\n\nExample: {\"production\": [\"prod\", \"prod-dr\"], \"development\": [\"dev\"]}",
"additionalProperties": {
"type": "array",
"items": {
"type": "string",
"minLength": 1
},
"required": ["parent", "this"],
"additionalProperties": false
},
"uniqueItems": true
"uniqueItems": true,
"minItems": 1
}
},
"environments": {
"type": "array",
Expand All @@ -52,8 +43,7 @@
"type": "string",
"minLength": 1
},
"uniqueItems": true,
"minItems": 1
"uniqueItems": true
},
"generate": {
"description": "Configuration generation options for this space. When enabled, generates:\n- gen/config.json: Contains the resolved configuration\n- gen/binding.ts: Provides type-safe access to the configuration",
Expand Down
Loading

0 comments on commit 0456b90

Please sign in to comment.