@baseplate-dev/sync
Version:
Library for syncing Baseplate descriptions
58 lines • 2.42 kB
JavaScript
import { createGeneratorTask } from '#src/generators/generators.js';
import { createProviderType, createReadOnlyProviderType, } from '#src/providers/providers.js';
import { createConfigFieldMap } from './create-config-field-map.js';
/**
* Creates a configuration provider task that exposes both a modifiable config map and its resolved values
*
* This helper creates a task with two providers:
* 1. A standard provider exposing the field map with modifiable containers
* 2. An output provider exposing the resolved configuration values (read-only)
*
* The field map contains containers for each field that can be modified by calling
* methods like set(), push(), or merge() depending on the container type.
*
* @param schemaBuilder - The schema builder for defining the field map structure
* @param options - The options for the configuration task
* @returns A tuple containing the generator task and both provider types
*
* @example
* ```typescript
* const [nodeConfigTask, nodeConfigProvider, nodeConfigValuesProvider] = createConfigProviderTask(
* t => ({
* port: t.number().default(3000),
* host: t.string().default('localhost')
* }),
* { prefix: 'node-server' }
* );
*
* // In a consuming task:
* // 1. Modify the field map containers:
* deps.nodeConfigProvider.port.set(4000, 'environment-override');
*
* // 2. Read the resolved values:
* const config = deps.nodeConfigValues;
* console.log(config.port); // 4000
* ```
*/
export function createConfigProviderTask(schemaBuilder, { prefix, taskName = 'config', configScope, configValuesScope, }) {
const configProvider = createProviderType(`${prefix}-${taskName}-config`);
const configValuesProvider = createReadOnlyProviderType(`${prefix}-${taskName}-config-values`);
return [
createGeneratorTask({
exports: { config: configProvider.export(configScope) },
outputs: { configValues: configValuesProvider.export(configValuesScope) },
run() {
const config = createConfigFieldMap(schemaBuilder);
return {
providers: { config },
build() {
return { configValues: config.getValues() };
},
};
},
}),
configProvider,
configValuesProvider,
];
}
//# sourceMappingURL=create-config-provider-task.js.map