diff --git a/package.json b/package.json index 97d7215..b53a810 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.24.8", + "version": "0.24.9", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick", diff --git a/src/common/schema/env.spec.ts b/src/common/schema/env.spec.ts index 56f42ea..8845c07 100644 --- a/src/common/schema/env.spec.ts +++ b/src/common/schema/env.spec.ts @@ -1,4 +1,4 @@ -import { parseSchemaEnv } from './env' +import { parseSchemaEnv, stringFromSchemaEnv } from './env' import { boolean, number, object, string } from './schema' describe('env.spec', () => { @@ -45,5 +45,11 @@ describe('env.spec', () => { "servicePort": 9999, } `) + + expect(stringFromSchemaEnv(schema, 'APP_', true)).toMatchInlineSnapshot(` + "# APP_SERVICE_NAME=generic + # APP_SERVICE_PORT=80 + # APP_SERVICE_FLAG=true" + `) }) }) diff --git a/src/common/schema/env.ts b/src/common/schema/env.ts index c4c9e12..1d06203 100644 --- a/src/common/schema/env.ts +++ b/src/common/schema/env.ts @@ -1,6 +1,6 @@ import { assert } from '../assert' import { objectFilter, objectMap, valueToBoolean, valueToBooleanNotFalse, valueToInteger } from '../data' -import { toCamelCase } from '../data/camelcase' +import { fromCamelCase, toCamelCase } from '../data/camelcase' import type { Type } from './schema' import { isSchemaObjectFlat } from './utils' @@ -35,3 +35,15 @@ export function parseSchemaEnv(schema: Type, env: any = process?.env ?? {} return schema.parse(value) }) as T } + +export function stringFromSchemaEnv(schema: Type, prefix = '', commentOut = false): string { + assert(isSchemaObjectFlat(schema), 'schema should be a flat object') + + const lines: string[] = [] + + objectMap(schema._object!, (key, schema) => { + lines.push(`${commentOut ? '# ' : ''}${prefix + fromCamelCase(key, '_').toUpperCase()}=${schema._default ?? ''}`) + }) as T + + return lines.join('\n') +}