UNPKG

@baseplate-dev/sync

Version:

Library for syncing Baseplate descriptions

60 lines 2.54 kB
import { createFieldMap } from '@baseplate-dev/utils'; import { createGeneratorTask } from '#src/generators/generators.js'; import { createProviderType, createReadOnlyProviderType, } from '#src/providers/providers.js'; /** * Creates a configuration provider task that exposes both a modifiable config and its resolved values, * augmented with additional information extracted from the descriptor * * This helper creates a task with two providers: * 1. A standard provider exposing the field map with modifiable containers, plus additional info * 2. An output provider exposing the resolved configuration values (read-only), plus additional info * * @param schemaBuilder - The schema builder for defining the field map structure * @param options - The options for the configuration task * @returns A tuple containing a task factory function and both provider types * * @example * ```typescript * const [createNodeConfigTask, nodeConfigProvider, nodeConfigValuesProvider] = createConfigProviderTaskWithInfo( * t => ({ * port: t.number().default(3000), * host: t.string().default('localhost') * }), * { * prefix: 'node-server', * infoFromDescriptor: (descriptor) => ({ * appName: descriptor.name, * environment: descriptor.env * }) * } * ); * * // Usage: * const nodeConfigTask = createNodeConfigTask({ name: 'api', env: 'production' }); * ``` */ export function createConfigProviderTaskWithInfo(schemaBuilder, { prefix, taskName = 'config', configScope, configValuesScope, infoFromDescriptor, }) { const configProvider = createProviderType(`${prefix}-${taskName}-config`); const configValuesProvider = createReadOnlyProviderType(`${prefix}-${taskName}-config-values`); return [ (descriptor) => createGeneratorTask({ exports: { config: configProvider.export(configScope) }, outputs: { configValues: configValuesProvider.export(configValuesScope), }, run() { const config = createFieldMap(schemaBuilder); const info = infoFromDescriptor(descriptor); return { providers: { config: { ...config, ...info } }, build() { return { configValues: { ...config.getValues(), ...info } }; }, }; }, }), configProvider, configValuesProvider, ]; } //# sourceMappingURL=create-config-provider-task-with-info.js.map