@powerlines/plugin-env 0.16.275 → 0.16.276

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.
@@ -136,9 +136,9 @@ async function extractEnv(context) {
136
136
  context.env.config = await (0, _powerlines_schema.extract)(context, context.config.env.config);
137
137
  context.env.config.active = await readActive(context, "config");
138
138
  if ((0, _stryke_type_checks_is_string.isString)(context.config.env.config) && context.config.env.config !== `${defaultVarsTypeDefinition.file}#${defaultVarsTypeDefinition.name}` || (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.env.config) && (context.config.env.config.file !== defaultVarsTypeDefinition.file || context.config.env.config.name !== defaultVarsTypeDefinition.name)) context.env.config.schema = (0, _powerlines_schema.mergeSchemas)(context.env.config, await (0, _powerlines_schema.extract)(context, defaultVarsTypeDefinition));
139
- const secrets = await (0, _powerlines_schema.extract)(context, context.config.env.secrets);
140
- secrets.active = await readActive(context, "secrets");
141
- if ((0, _stryke_type_checks_is_string.isString)(context.config.env.secrets) && context.config.env.secrets !== `${defaultSecretsTypeDefinition.file}#${defaultSecretsTypeDefinition.name}` || (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.env.secrets) && (context.config.env.secrets.file !== defaultSecretsTypeDefinition.file || context.config.env.secrets.name !== defaultSecretsTypeDefinition.name)) secrets.schema = (0, _powerlines_schema.mergeSchemas)(secrets, await (0, _powerlines_schema.extract)(context, defaultSecretsTypeDefinition));
139
+ context.env.secrets = await (0, _powerlines_schema.extract)(context, context.config.env.secrets);
140
+ context.env.secrets.active = await readActive(context, "secrets");
141
+ if ((0, _stryke_type_checks_is_string.isString)(context.config.env.secrets) && context.config.env.secrets !== `${defaultSecretsTypeDefinition.file}#${defaultSecretsTypeDefinition.name}` || (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.env.secrets) && (context.config.env.secrets.file !== defaultSecretsTypeDefinition.file || context.config.env.secrets.name !== defaultSecretsTypeDefinition.name)) context.env.secrets.schema = (0, _powerlines_schema.mergeSchemas)(context.env.secrets, await (0, _powerlines_schema.extract)(context, defaultSecretsTypeDefinition));
142
142
  const properties = (0, _powerlines_schema.getProperties)(context.env.config);
143
143
  context.info({
144
144
  meta: { category: "env" },
@@ -135,9 +135,9 @@ async function extractEnv(context) {
135
135
  context.env.config = await extract(context, context.config.env.config);
136
136
  context.env.config.active = await readActive(context, "config");
137
137
  if (isString(context.config.env.config) && context.config.env.config !== `${defaultVarsTypeDefinition.file}#${defaultVarsTypeDefinition.name}` || isSetObject(context.config.env.config) && (context.config.env.config.file !== defaultVarsTypeDefinition.file || context.config.env.config.name !== defaultVarsTypeDefinition.name)) context.env.config.schema = mergeSchemas(context.env.config, await extract(context, defaultVarsTypeDefinition));
138
- const secrets = await extract(context, context.config.env.secrets);
139
- secrets.active = await readActive(context, "secrets");
140
- if (isString(context.config.env.secrets) && context.config.env.secrets !== `${defaultSecretsTypeDefinition.file}#${defaultSecretsTypeDefinition.name}` || isSetObject(context.config.env.secrets) && (context.config.env.secrets.file !== defaultSecretsTypeDefinition.file || context.config.env.secrets.name !== defaultSecretsTypeDefinition.name)) secrets.schema = mergeSchemas(secrets, await extract(context, defaultSecretsTypeDefinition));
138
+ context.env.secrets = await extract(context, context.config.env.secrets);
139
+ context.env.secrets.active = await readActive(context, "secrets");
140
+ if (isString(context.config.env.secrets) && context.config.env.secrets !== `${defaultSecretsTypeDefinition.file}#${defaultSecretsTypeDefinition.name}` || isSetObject(context.config.env.secrets) && (context.config.env.secrets.file !== defaultSecretsTypeDefinition.file || context.config.env.secrets.name !== defaultSecretsTypeDefinition.name)) context.env.secrets.schema = mergeSchemas(context.env.secrets, await extract(context, defaultSecretsTypeDefinition));
141
141
  const properties = getProperties(context.env.config);
142
142
  context.info({
143
143
  meta: { category: "env" },
@@ -1 +1 @@
1
- {"version":3,"file":"schema.mjs","names":[],"sources":["../../src/helpers/schema.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n extract,\n getProperties,\n getPropertiesList,\n isSchema,\n JsonSchemaObject,\n mergeSchemas,\n Schema,\n writeSchema\n} from \"@powerlines/schema\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { isSetArray } from \"@stryke/type-checks/is-set-array\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { TypeDefinition } from \"@stryke/types/configuration\";\nimport { UnresolvedContext } from \"powerlines\";\nimport { EnvPluginContext, EnvSchema } from \"../types/plugin\";\nimport { loadEnv } from \"./load\";\n\n/**\n * Resolves the runtime type definition file for the environment variables.\n *\n * @param context - The plugin context.\n * @returns The runtime type definition file for the environment variables.\n */\nexport async function resolveRuntimeTypeFile<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<string> {\n const resolved = await context.fs.resolve(\"@powerlines/plugin-env/types/env\");\n if (!resolved) {\n throw new Error(\n `Failed to resolve the runtime type definition file for the environment variables. Please ensure that the \"@powerlines/plugin-env\" package is installed.`\n );\n }\n\n return resolved;\n}\n\n/**\n * Gets the default type definition for the environment variables.\n *\n * @param context - The plugin context.\n * @returns The default type definition for the environment variables.\n */\nexport async function getDefaultVarsTypeDefinition<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<TypeDefinition> {\n return {\n file: await resolveRuntimeTypeFile(context),\n name: \"EnvInterface\"\n };\n}\n\n/** Gets the default type definition for the environment secrets.\n *\n * @param context - The plugin context.\n * @returns The default type definition for the environment secrets.\n */\nexport async function getDefaultSecretsTypeDefinition<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<TypeDefinition> {\n return {\n file: await resolveRuntimeTypeFile(context),\n name: \"SecretsInterface\"\n };\n}\n\n/**\n * A helper function to get the cache directory path for storing schemas. This function takes a context object as input and returns the path to the cache directory where schemas are stored. The cache directory is constructed by joining the `cachePath` property from the context with a subdirectory named \"schemas\". This function is useful for centralizing the logic for determining where schema files should be cached, ensuring that all schema-related file operations use a consistent location for storing and retrieving cached schemas.\n *\n * @param context - The context object providing access to the cache path.\n * @returns The path to the cache directory for storing schemas, constructed by joining the context's `cachePath` with the \"schemas\" subdirectory.\n */\nexport function getCacheDirectory<TContext extends EnvPluginContext>(\n context: TContext\n): string {\n return joinPaths(context.cachePath, \"env\");\n}\n\nexport function getCacheFilePath<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\"\n): string {\n return joinPaths(getCacheDirectory(context), `${variant}.json`);\n}\n\nasync function writeActive<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\",\n schema: EnvSchema\n) {\n if (!isSchema(schema)) {\n throw new Error(\n `The provided input is not a valid env schema. A valid schema must have a \"variant\" property indicating the type of the input and a \"schema\" property containing the parsed JSON Schema object.`\n );\n }\n\n await context.fs.write(\n getCacheFilePath(context, variant),\n JSON.stringify(schema.schema)\n );\n}\n\nasync function readActive<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\"\n): Promise<string[]> {\n if (!context.fs.existsSync(getCacheFilePath(context, variant))) {\n return [];\n }\n\n const data = await context.fs.read(getCacheFilePath(context, variant));\n if (!data) {\n return [];\n }\n\n return JSON.parse(data);\n}\n\n/**\n * Extracts the environment variables and secrets schema from the provided type definitions in the plugin options, merges them with the default environment variables and secrets schema, and stores the resulting schema in the plugin context for later use during the build process.\n *\n * @remarks\n * This function should be called during the plugin's `config` hook to ensure that the environment variables and secrets schema is available in the plugin context before the build process begins. The resulting schema will be used to validate the loaded environment variables and secrets, as well as to provide type information for the injected environment variables and secrets during the build process.\n *\n * @param context - The plugin context\n * @returns A promise that resolves when the schema has been extracted and stored in the plugin context.\n */\nexport async function extractEnv<TContext extends EnvPluginContext>(\n context: TContext\n): Promise<void> {\n const defaultVarsTypeDefinition = await getDefaultVarsTypeDefinition(context);\n const defaultSecretsTypeDefinition =\n await getDefaultSecretsTypeDefinition(context);\n\n context.env ??= {} as EnvPluginContext[\"env\"];\n context.env.parsed ??= {};\n context.env.injected ??= [];\n\n context.env.config = (await extract(\n context,\n context.config.env.config\n )) as EnvSchema;\n context.env.config.active = await readActive(context, \"config\");\n\n if (\n (isString(context.config.env.config) &&\n context.config.env.config !==\n `${defaultVarsTypeDefinition.file}#${\n defaultVarsTypeDefinition.name\n }`) ||\n (isSetObject(context.config.env.config) &&\n ((context.config.env.config as TypeDefinition).file !==\n defaultVarsTypeDefinition.file ||\n (context.config.env.config as TypeDefinition).name !==\n defaultVarsTypeDefinition.name))\n ) {\n context.env.config.schema = mergeSchemas(\n context.env.config,\n await extract(context, defaultVarsTypeDefinition)\n ) as JsonSchemaObject;\n }\n\n const secrets = (await extract(\n context,\n context.config.env.secrets\n )) as EnvSchema;\n secrets.active = await readActive(context, \"secrets\");\n\n if (\n (isString(context.config.env.secrets) &&\n context.config.env.secrets !==\n `${defaultSecretsTypeDefinition.file}#${\n defaultSecretsTypeDefinition.name\n }`) ||\n (isSetObject(context.config.env.secrets) &&\n ((context.config.env.secrets as TypeDefinition).file !==\n defaultSecretsTypeDefinition.file ||\n (context.config.env.secrets as TypeDefinition).name !==\n defaultSecretsTypeDefinition.name))\n ) {\n secrets.schema = mergeSchemas(\n secrets,\n await extract(context, defaultSecretsTypeDefinition)\n ) as JsonSchemaObject;\n }\n\n const properties = getProperties(context.env.config);\n context.info({\n meta: {\n category: \"env\"\n },\n message: `Environment Variables configuration: ${\n context.config.env.config ? \"\" : \"Defaulted \"\n }${\n context.env.config.variant === \"reflection\"\n ? \"Deepkit type definition\"\n : context.env.config.variant === \"json-schema\"\n ? \"JSON Schema\"\n : context.env.config.variant === \"standard-schema\"\n ? \"Standard Schema\"\n : context.env.config.variant === \"zod3\"\n ? \"Zod v3 schema\"\n : \"Typescript exported type\"\n }${context.config.env.config ? \" from plugin options\" : \"\"} provided ${\n Object.keys(properties).length\n } parameters\\nEnvironment Secret configuration: ${\n context.config.env.secrets ? \"\" : \"Defaulted \"\n }${\n context.env.secrets.variant === \"reflection\"\n ? \"Deepkit type definition\"\n : context.env.secrets.variant === \"json-schema\"\n ? \"JSON Schema\"\n : context.env.secrets.variant === \"standard-schema\"\n ? \"Standard Schema\"\n : context.env.secrets.variant === \"zod3\"\n ? \"Zod v3 schema\"\n : \"Typescript exported type\"\n }${context.config.env.secrets ? \" from plugin options\" : \"\"} provided ${\n context.env.secrets?.schema\n ? getPropertiesList(context.env.secrets).length\n : \"0\"\n } parameters\\nEnvironment variable Prefixes: ${context.config.env.prefix.join(\n \", \"\n )}\\nShould inject values: ${\n context.config.env.inject ? \"Yes\" : \"No\"\n }\\nShould validate configuration: ${\n context.config.env.validate ? \"Yes\" : \"No\"\n }`\n });\n\n const aliases = Object.fromEntries(\n Object.entries(properties).flatMap(\n ([key, prop]) =>\n (isSetArray(prop.alias)\n ? prop.alias?.map(alias => [\n alias,\n {\n ...prop,\n name: alias,\n alias: [...(prop.alias?.filter(a => a !== alias) ?? []), key]\n }\n ])\n : []) as [string, typeof prop][]\n )\n );\n\n context.env.parsed = await loadEnv(context);\n for (const [key, value] of Object.entries(context.env.parsed)) {\n const unprefixedKey = context.config.env.prefix.reduce((ret, prefix) => {\n if (key.replace(/_$/g, \"\").startsWith(prefix)) {\n return key.replace(/_$/g, \"\").slice(prefix.length);\n }\n return ret;\n }, key);\n if (properties[unprefixedKey]) {\n if (!properties[unprefixedKey]?.runtime) {\n const propertySchema = getProperties(context.env.config.schema)?.[\n unprefixedKey\n ];\n if (propertySchema) {\n propertySchema.default = value;\n }\n }\n } else if (aliases[unprefixedKey]) {\n if (!aliases[unprefixedKey]?.runtime) {\n const alias = aliases[unprefixedKey]?.alias?.[0] ?? unprefixedKey;\n const aliasSchema = getProperties(context.env.config.schema)?.[alias];\n if (aliasSchema) {\n aliasSchema.default = value;\n }\n }\n }\n }\n}\n\n/**\n * Writes the environment variables and secrets schema stored in the plugin context to the cache directory for later retrieval during the build process. This function should be called during the plugin's `build` hook after the schema has been extracted and stored in the plugin context to ensure that the active environment variables and secrets are persisted across builds and can be accessed during the build process for validation and injection purposes.\n *\n * @param context - The plugin context containing the environment variables and secrets schema to be written to the cache directory.\n * @returns A promise that resolves when the schema has been successfully written to the cache directory.\n */\nexport async function writeEnv<TContext extends EnvPluginContext>(\n context: TContext\n): Promise<void[]> {\n return Promise.all([\n writeSchema(context, omit(context.env.config, [\"active\"]) as Schema),\n writeSchema(context, omit(context.env.secrets, [\"active\"]) as Schema),\n writeActive(context, \"config\", context.env.config),\n writeActive(context, \"secrets\", context.env.secrets)\n ]);\n}\n"],"mappings":";;;;;;;;;;AAEA,SAAS,aAAY,IAAK,MAAC;;CAE1B,OAAK;AACN;;;;;;;AA+BA,eAAmB,uBAAa,SAAA;;CAE9B,IAAA,CAAA,UACC,MAAA,IAAS,MAAI,yJAA0D;CAExE,OAAO;AACT;AACA,uBAAE,SAAA;CAAA;CAAA;CAAA;CAAA;AAAA;;;;;;;AAOF,eAAiB,6BAAyB,SAAgB;CACxD,OAAG;EACH,MAAA,MAAA,uBAAA,OAAA;;CAEA;AACF;;;;;;;;;;;;AAOA,eAAE,gCAAA,SAAA;CACF,OAAO;EACL,MAAQ,MAAC,uBAAQ,OAAA;EACjB,MAAQ;CACR;AACF;AACA,gCAAuB,SAAA;CAAA;CAAA;CAAA;AAAA;;;;;;;AAOvB,SAAgB,kBAAa,SAAc;CACzC,OAAA,UAAA,QAAA,WAAA,KAAA;AACF;AACA,kBAAkB,SAAC;CAAA;CAAA;CAAA;CAAA;AAAA;AACnB,SAAW,iBAAmB,SAAA,SAAgB;CAC5C,OAAO,UAAA,kBAAA,OAA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,IAAA,OAAA;CAAA,CAAA;AACxC;AACA,SAAS,SAAC;CAAA;CAAA;CAA+B;CAAA;AAAA"}
1
+ {"version":3,"file":"schema.mjs","names":[],"sources":["../../src/helpers/schema.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n extract,\n getProperties,\n getPropertiesList,\n isSchema,\n JsonSchemaObject,\n mergeSchemas,\n Schema,\n writeSchema\n} from \"@powerlines/schema\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { joinPaths } from \"@stryke/path/join\";\nimport { isSetArray } from \"@stryke/type-checks/is-set-array\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport type { TypeDefinition } from \"@stryke/types/configuration\";\nimport { UnresolvedContext } from \"powerlines\";\nimport { EnvPluginContext, EnvSchema } from \"../types/plugin\";\nimport { loadEnv } from \"./load\";\n\n/**\n * Resolves the runtime type definition file for the environment variables.\n *\n * @param context - The plugin context.\n * @returns The runtime type definition file for the environment variables.\n */\nexport async function resolveRuntimeTypeFile<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<string> {\n const resolved = await context.fs.resolve(\"@powerlines/plugin-env/types/env\");\n if (!resolved) {\n throw new Error(\n `Failed to resolve the runtime type definition file for the environment variables. Please ensure that the \"@powerlines/plugin-env\" package is installed.`\n );\n }\n\n return resolved;\n}\n\n/**\n * Gets the default type definition for the environment variables.\n *\n * @param context - The plugin context.\n * @returns The default type definition for the environment variables.\n */\nexport async function getDefaultVarsTypeDefinition<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<TypeDefinition> {\n return {\n file: await resolveRuntimeTypeFile(context),\n name: \"EnvInterface\"\n };\n}\n\n/** Gets the default type definition for the environment secrets.\n *\n * @param context - The plugin context.\n * @returns The default type definition for the environment secrets.\n */\nexport async function getDefaultSecretsTypeDefinition<\n TContext extends UnresolvedContext\n>(context: TContext): Promise<TypeDefinition> {\n return {\n file: await resolveRuntimeTypeFile(context),\n name: \"SecretsInterface\"\n };\n}\n\n/**\n * A helper function to get the cache directory path for storing schemas. This function takes a context object as input and returns the path to the cache directory where schemas are stored. The cache directory is constructed by joining the `cachePath` property from the context with a subdirectory named \"schemas\". This function is useful for centralizing the logic for determining where schema files should be cached, ensuring that all schema-related file operations use a consistent location for storing and retrieving cached schemas.\n *\n * @param context - The context object providing access to the cache path.\n * @returns The path to the cache directory for storing schemas, constructed by joining the context's `cachePath` with the \"schemas\" subdirectory.\n */\nexport function getCacheDirectory<TContext extends EnvPluginContext>(\n context: TContext\n): string {\n return joinPaths(context.cachePath, \"env\");\n}\n\nexport function getCacheFilePath<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\"\n): string {\n return joinPaths(getCacheDirectory(context), `${variant}.json`);\n}\n\nasync function writeActive<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\",\n schema: EnvSchema\n) {\n if (!isSchema(schema)) {\n throw new Error(\n `The provided input is not a valid env schema. A valid schema must have a \"variant\" property indicating the type of the input and a \"schema\" property containing the parsed JSON Schema object.`\n );\n }\n\n await context.fs.write(\n getCacheFilePath(context, variant),\n JSON.stringify(schema.schema)\n );\n}\n\nasync function readActive<TContext extends EnvPluginContext>(\n context: TContext,\n variant: \"config\" | \"secrets\"\n): Promise<string[]> {\n if (!context.fs.existsSync(getCacheFilePath(context, variant))) {\n return [];\n }\n\n const data = await context.fs.read(getCacheFilePath(context, variant));\n if (!data) {\n return [];\n }\n\n return JSON.parse(data);\n}\n\n/**\n * Extracts the environment variables and secrets schema from the provided type definitions in the plugin options, merges them with the default environment variables and secrets schema, and stores the resulting schema in the plugin context for later use during the build process.\n *\n * @remarks\n * This function should be called during the plugin's `config` hook to ensure that the environment variables and secrets schema is available in the plugin context before the build process begins. The resulting schema will be used to validate the loaded environment variables and secrets, as well as to provide type information for the injected environment variables and secrets during the build process.\n *\n * @param context - The plugin context\n * @returns A promise that resolves when the schema has been extracted and stored in the plugin context.\n */\nexport async function extractEnv<TContext extends EnvPluginContext>(\n context: TContext\n): Promise<void> {\n const defaultVarsTypeDefinition = await getDefaultVarsTypeDefinition(context);\n const defaultSecretsTypeDefinition =\n await getDefaultSecretsTypeDefinition(context);\n\n context.env ??= {} as EnvPluginContext[\"env\"];\n context.env.parsed ??= {};\n context.env.injected ??= [];\n\n context.env.config = (await extract(\n context,\n context.config.env.config\n )) as EnvSchema;\n context.env.config.active = await readActive(context, \"config\");\n\n if (\n (isString(context.config.env.config) &&\n context.config.env.config !==\n `${defaultVarsTypeDefinition.file}#${\n defaultVarsTypeDefinition.name\n }`) ||\n (isSetObject(context.config.env.config) &&\n ((context.config.env.config as TypeDefinition).file !==\n defaultVarsTypeDefinition.file ||\n (context.config.env.config as TypeDefinition).name !==\n defaultVarsTypeDefinition.name))\n ) {\n context.env.config.schema = mergeSchemas(\n context.env.config,\n await extract(context, defaultVarsTypeDefinition)\n ) as JsonSchemaObject;\n }\n\n context.env.secrets = (await extract(\n context,\n context.config.env.secrets\n )) as EnvSchema;\n context.env.secrets.active = await readActive(context, \"secrets\");\n\n if (\n (isString(context.config.env.secrets) &&\n context.config.env.secrets !==\n `${defaultSecretsTypeDefinition.file}#${\n defaultSecretsTypeDefinition.name\n }`) ||\n (isSetObject(context.config.env.secrets) &&\n ((context.config.env.secrets as TypeDefinition).file !==\n defaultSecretsTypeDefinition.file ||\n (context.config.env.secrets as TypeDefinition).name !==\n defaultSecretsTypeDefinition.name))\n ) {\n context.env.secrets.schema = mergeSchemas(\n context.env.secrets,\n await extract(context, defaultSecretsTypeDefinition)\n ) as JsonSchemaObject;\n }\n\n const properties = getProperties(context.env.config);\n context.info({\n meta: {\n category: \"env\"\n },\n message: `Environment Variables configuration: ${\n context.config.env.config ? \"\" : \"Defaulted \"\n }${\n context.env.config.variant === \"reflection\"\n ? \"Deepkit type definition\"\n : context.env.config.variant === \"json-schema\"\n ? \"JSON Schema\"\n : context.env.config.variant === \"standard-schema\"\n ? \"Standard Schema\"\n : context.env.config.variant === \"zod3\"\n ? \"Zod v3 schema\"\n : \"Typescript exported type\"\n }${context.config.env.config ? \" from plugin options\" : \"\"} provided ${\n Object.keys(properties).length\n } parameters\\nEnvironment Secret configuration: ${\n context.config.env.secrets ? \"\" : \"Defaulted \"\n }${\n context.env.secrets.variant === \"reflection\"\n ? \"Deepkit type definition\"\n : context.env.secrets.variant === \"json-schema\"\n ? \"JSON Schema\"\n : context.env.secrets.variant === \"standard-schema\"\n ? \"Standard Schema\"\n : context.env.secrets.variant === \"zod3\"\n ? \"Zod v3 schema\"\n : \"Typescript exported type\"\n }${context.config.env.secrets ? \" from plugin options\" : \"\"} provided ${\n context.env.secrets?.schema\n ? getPropertiesList(context.env.secrets).length\n : \"0\"\n } parameters\\nEnvironment variable Prefixes: ${context.config.env.prefix.join(\n \", \"\n )}\\nShould inject values: ${\n context.config.env.inject ? \"Yes\" : \"No\"\n }\\nShould validate configuration: ${\n context.config.env.validate ? \"Yes\" : \"No\"\n }`\n });\n\n const aliases = Object.fromEntries(\n Object.entries(properties).flatMap(\n ([key, prop]) =>\n (isSetArray(prop.alias)\n ? prop.alias?.map(alias => [\n alias,\n {\n ...prop,\n name: alias,\n alias: [...(prop.alias?.filter(a => a !== alias) ?? []), key]\n }\n ])\n : []) as [string, typeof prop][]\n )\n );\n\n context.env.parsed = await loadEnv(context);\n for (const [key, value] of Object.entries(context.env.parsed)) {\n const unprefixedKey = context.config.env.prefix.reduce((ret, prefix) => {\n if (key.replace(/_$/g, \"\").startsWith(prefix)) {\n return key.replace(/_$/g, \"\").slice(prefix.length);\n }\n return ret;\n }, key);\n if (properties[unprefixedKey]) {\n if (!properties[unprefixedKey]?.runtime) {\n const propertySchema = getProperties(context.env.config.schema)?.[\n unprefixedKey\n ];\n if (propertySchema) {\n propertySchema.default = value;\n }\n }\n } else if (aliases[unprefixedKey]) {\n if (!aliases[unprefixedKey]?.runtime) {\n const alias = aliases[unprefixedKey]?.alias?.[0] ?? unprefixedKey;\n const aliasSchema = getProperties(context.env.config.schema)?.[alias];\n if (aliasSchema) {\n aliasSchema.default = value;\n }\n }\n }\n }\n}\n\n/**\n * Writes the environment variables and secrets schema stored in the plugin context to the cache directory for later retrieval during the build process. This function should be called during the plugin's `build` hook after the schema has been extracted and stored in the plugin context to ensure that the active environment variables and secrets are persisted across builds and can be accessed during the build process for validation and injection purposes.\n *\n * @param context - The plugin context containing the environment variables and secrets schema to be written to the cache directory.\n * @returns A promise that resolves when the schema has been successfully written to the cache directory.\n */\nexport async function writeEnv<TContext extends EnvPluginContext>(\n context: TContext\n): Promise<void[]> {\n return Promise.all([\n writeSchema(context, omit(context.env.config, [\"active\"]) as Schema),\n writeSchema(context, omit(context.env.secrets, [\"active\"]) as Schema),\n writeActive(context, \"config\", context.env.config),\n writeActive(context, \"secrets\", context.env.secrets)\n ]);\n}\n"],"mappings":";;;;;;;;;;AAEA,SAAS,aAAY,IAAK,MAAC;;CAE1B,OAAK;AACN;;;;;;;AA+BA,eAAmB,uBAAa,SAAA;;CAE9B,IAAA,CAAA,UACC,MAAA,IAAS,MAAI,yJAA0D;CAExE,OAAO;AACT;AACA,uBAAE,SAAA;CAAA;CAAA;CAAA;CAAA;AAAA;;;;;;;AAOF,eAAiB,6BAAyB,SAAgB;CACxD,OAAG;EACH,MAAA,MAAA,uBAAA,OAAA;;CAEA;AACF;;;;;;;;;;;;AAOA,eAAE,gCAAA,SAAA;CACF,OAAO;EACL,MAAQ,MAAC,uBAAQ,OAAA;EACjB,MAAQ;CACR;AACF;AACA,gCAA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;;;;;;;AAOL,eAAsB,SAAK,SAAU;CACnC,OAAM,QAAA,IAAA;EAAA,YAA0B,SAAM,KAAA,QAAA,IAAA,QAAA,CAAA,QAAA,CAAA,CAAA;EAAA,YAAA,SAAA,KAAA,QAAA,IAAA,SAAA,CAAA,QAAA,CAAA,CAAA;EAAA,YAAA,SAAA,UAAA,QAAA,IAAA,MAAA;EAAA,YAAA,SAAA,WAAA,QAAA,IAAA,OAAA;CAAA,CAAA;AACxC;AACA,SAAS,SAAC;CAAA;CAAA;CAA+B;CAAA;AAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powerlines/plugin-env",
3
- "version": "0.16.275",
3
+ "version": "0.16.276",
4
4
  "private": false,
5
5
  "description": "A package containing a Powerlines plugin for injecting static .env configuration values to the code so that they're accessible at runtime.",
6
6
  "keywords": ["dotenv", "powerlines", "storm-software", "powerlines-plugin"],
@@ -108,7 +108,7 @@
108
108
  "@babel/core": "8.0.0-rc.6",
109
109
  "@babel/types": "8.0.0-rc.6",
110
110
  "@powerlines/core": "^0.48.18",
111
- "@powerlines/plugin-alloy": "0.26.165",
111
+ "@powerlines/plugin-alloy": "^0.26.165",
112
112
  "@powerlines/plugin-automd": "^0.1.536",
113
113
  "@powerlines/plugin-babel": "^0.13.71",
114
114
  "@powerlines/plugin-plugin": "^0.12.487",
@@ -131,5 +131,5 @@
131
131
  },
132
132
  "devDependencies": { "@types/node": "^25.9.1", "vite": "^8.0.14" },
133
133
  "publishConfig": { "access": "public" },
134
- "gitHead": "c4bcff0c82d498fe06faf6040e22fc77b4499154"
134
+ "gitHead": "ad0194a01faa44b4f7c1a2a8987eb99c9723344f"
135
135
  }