@powerlines/plugin-env 0.16.234 → 0.16.235
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.
- package/dist/babel/plugin.cjs +23 -71
- package/dist/babel/plugin.d.cts.map +1 -1
- package/dist/babel/plugin.d.mts.map +1 -1
- package/dist/babel/plugin.mjs +23 -71
- package/dist/babel/plugin.mjs.map +1 -1
- package/dist/components/docs.cjs +14 -14
- package/dist/components/docs.d.cts +0 -2
- package/dist/components/docs.d.cts.map +1 -1
- package/dist/components/docs.d.mts +0 -2
- package/dist/components/docs.d.mts.map +1 -1
- package/dist/components/docs.mjs +14 -14
- package/dist/components/docs.mjs.map +1 -1
- package/dist/components/env-builtin.cjs +233 -426
- package/dist/components/env-builtin.d.cts +1 -4
- package/dist/components/env-builtin.d.cts.map +1 -1
- package/dist/components/env-builtin.d.mts +1 -4
- package/dist/components/env-builtin.d.mts.map +1 -1
- package/dist/components/env-builtin.mjs +236 -428
- package/dist/components/env-builtin.mjs.map +1 -1
- package/dist/helpers/index.cjs +9 -26
- package/dist/helpers/index.d.cts +3 -5
- package/dist/helpers/index.d.mts +3 -5
- package/dist/helpers/index.mjs +3 -5
- package/dist/helpers/load.cjs +8 -7
- package/dist/helpers/load.d.cts +3 -3
- package/dist/helpers/load.d.cts.map +1 -1
- package/dist/helpers/load.d.mts +3 -3
- package/dist/helpers/load.d.mts.map +1 -1
- package/dist/helpers/load.mjs +7 -6
- package/dist/helpers/load.mjs.map +1 -1
- package/dist/helpers/schema.cjs +206 -0
- package/dist/helpers/schema.d.cts +53 -0
- package/dist/helpers/schema.d.cts.map +1 -0
- package/dist/helpers/schema.d.mts +53 -0
- package/dist/helpers/schema.d.mts.map +1 -0
- package/dist/helpers/schema.mjs +199 -0
- package/dist/helpers/schema.mjs.map +1 -0
- package/dist/index.cjs +23 -201
- package/dist/index.d.cts +2 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +25 -203
- package/dist/index.mjs.map +1 -1
- package/dist/types/index.cjs +1 -0
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.mts +2 -2
- package/dist/types/index.mjs +2 -2
- package/dist/types/plugin.cjs +16 -24
- package/dist/types/plugin.d.cts +23 -57
- package/dist/types/plugin.d.cts.map +1 -1
- package/dist/types/plugin.d.mts +23 -57
- package/dist/types/plugin.d.mts.map +1 -1
- package/dist/types/plugin.mjs +16 -25
- package/dist/types/plugin.mjs.map +1 -1
- package/package.json +12 -26
- package/dist/helpers/create-reflection-resource.cjs +0 -23
- package/dist/helpers/create-reflection-resource.d.cts +0 -13
- package/dist/helpers/create-reflection-resource.d.cts.map +0 -1
- package/dist/helpers/create-reflection-resource.d.mts +0 -13
- package/dist/helpers/create-reflection-resource.d.mts.map +0 -1
- package/dist/helpers/create-reflection-resource.mjs +0 -23
- package/dist/helpers/create-reflection-resource.mjs.map +0 -1
- package/dist/helpers/persistence.cjs +0 -309
- package/dist/helpers/persistence.d.cts +0 -84
- package/dist/helpers/persistence.d.cts.map +0 -1
- package/dist/helpers/persistence.d.mts +0 -84
- package/dist/helpers/persistence.d.mts.map +0 -1
- package/dist/helpers/persistence.mjs +0 -297
- package/dist/helpers/persistence.mjs.map +0 -1
- package/dist/helpers/reflect.cjs +0 -290
- package/dist/helpers/reflect.d.cts +0 -68
- package/dist/helpers/reflect.d.cts.map +0 -1
- package/dist/helpers/reflect.d.mts +0 -68
- package/dist/helpers/reflect.d.mts.map +0 -1
- package/dist/helpers/reflect.mjs +0 -281
- package/dist/helpers/reflect.mjs.map +0 -1
- package/dist/helpers/template-helpers.cjs +0 -70
- package/dist/helpers/template-helpers.d.cts +0 -15
- package/dist/helpers/template-helpers.d.cts.map +0 -1
- package/dist/helpers/template-helpers.d.mts +0 -15
- package/dist/helpers/template-helpers.d.mts.map +0 -1
- package/dist/helpers/template-helpers.mjs +0 -69
- package/dist/helpers/template-helpers.mjs.map +0 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
const require_types_plugin = require('../types/plugin.cjs');
|
|
4
|
+
const require_helpers_load = require('./load.cjs');
|
|
5
|
+
let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string");
|
|
6
|
+
let _powerlines_schema_extract = require("@powerlines/schema/extract");
|
|
7
|
+
let _powerlines_schema_helpers = require("@powerlines/schema/helpers");
|
|
8
|
+
let _stryke_path_join = require("@stryke/path/join");
|
|
9
|
+
let _stryke_type_checks_is_set_array = require("@stryke/type-checks/is-set-array");
|
|
10
|
+
let _stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
|
|
11
|
+
let defu = require("defu");
|
|
12
|
+
defu = require_runtime.__toESM(defu, 1);
|
|
13
|
+
|
|
14
|
+
//#region src/helpers/schema.ts
|
|
15
|
+
function __assignType(fn, args) {
|
|
16
|
+
fn.__type = args;
|
|
17
|
+
return fn;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Resolves the runtime type definition file for the environment variables.
|
|
21
|
+
*
|
|
22
|
+
* @param context - The plugin context.
|
|
23
|
+
* @returns The runtime type definition file for the environment variables.
|
|
24
|
+
*/
|
|
25
|
+
async function resolveRuntimeTypeFile(context) {
|
|
26
|
+
const resolved = await context.fs.resolve("@powerlines/plugin-env/types/env");
|
|
27
|
+
if (!resolved) throw new Error(`Failed to resolve the runtime type definition file for the environment variables. Please ensure that the "@powerlines/plugin-env" package is installed.`);
|
|
28
|
+
return resolved;
|
|
29
|
+
}
|
|
30
|
+
resolveRuntimeTypeFile.__type = [
|
|
31
|
+
"context",
|
|
32
|
+
"resolveRuntimeTypeFile",
|
|
33
|
+
"Resolves the runtime type definition file for the environment variables.",
|
|
34
|
+
"P\"2!&`/\"?#"
|
|
35
|
+
];
|
|
36
|
+
/**
|
|
37
|
+
* Gets the default type definition for the environment variables.
|
|
38
|
+
*
|
|
39
|
+
* @param context - The plugin context.
|
|
40
|
+
* @returns The default type definition for the environment variables.
|
|
41
|
+
*/
|
|
42
|
+
async function getDefaultVarsTypeDefinition(context) {
|
|
43
|
+
return {
|
|
44
|
+
file: await resolveRuntimeTypeFile(context),
|
|
45
|
+
name: "EnvInterface"
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
getDefaultVarsTypeDefinition.__type = [
|
|
49
|
+
"context",
|
|
50
|
+
"getDefaultVarsTypeDefinition",
|
|
51
|
+
"Gets the default type definition for the environment variables.",
|
|
52
|
+
"P\"2!!`/\"?#"
|
|
53
|
+
];
|
|
54
|
+
/** Gets the default type definition for the environment secrets.
|
|
55
|
+
*
|
|
56
|
+
* @param context - The plugin context.
|
|
57
|
+
* @returns The default type definition for the environment secrets.
|
|
58
|
+
*/
|
|
59
|
+
async function getDefaultSecretsTypeDefinition(context) {
|
|
60
|
+
return {
|
|
61
|
+
file: await resolveRuntimeTypeFile(context),
|
|
62
|
+
name: "SecretsInterface"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
getDefaultSecretsTypeDefinition.__type = [
|
|
66
|
+
"context",
|
|
67
|
+
"getDefaultSecretsTypeDefinition",
|
|
68
|
+
"P\"2!!`/\""
|
|
69
|
+
];
|
|
70
|
+
/**
|
|
71
|
+
* 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.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* 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.
|
|
75
|
+
*
|
|
76
|
+
* @param context - The plugin context
|
|
77
|
+
* @param options - The plugin options containing the environment variables and secrets type definitions to extract the schema from. If not provided, the default type definitions will be used.
|
|
78
|
+
* @returns A promise that resolves when the schema has been extracted and stored in the plugin context.
|
|
79
|
+
*/
|
|
80
|
+
async function extractEnvSchema(context, options = {}) {
|
|
81
|
+
const defaultVarsTypeDefinition = await getDefaultVarsTypeDefinition(context);
|
|
82
|
+
const defaultSecretsTypeDefinition = await getDefaultSecretsTypeDefinition(context);
|
|
83
|
+
const vars = await (0, _powerlines_schema_extract.extract)(context, context.config.env.vars);
|
|
84
|
+
if ((0, _stryke_type_checks_is_string.isString)(context.config.env.vars) && context.config.env.vars !== `${defaultVarsTypeDefinition.file}#${defaultVarsTypeDefinition.name}` || (0, _stryke_type_checks_is_set_object.isSetObject)(context.config.env.vars) && (context.config.env.vars.file !== defaultVarsTypeDefinition.file || context.config.env.vars.name !== defaultVarsTypeDefinition.name)) vars.schema = (0, _powerlines_schema_helpers.mergeSchemas)(vars, await (0, _powerlines_schema_extract.extract)(context, defaultVarsTypeDefinition));
|
|
85
|
+
const secrets = await (0, _powerlines_schema_extract.extract)(context, context.config.env.secrets);
|
|
86
|
+
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_helpers.mergeSchemas)(secrets, await (0, _powerlines_schema_extract.extract)(context, defaultSecretsTypeDefinition));
|
|
87
|
+
context.env = (0, defu.default)({
|
|
88
|
+
vars,
|
|
89
|
+
secrets,
|
|
90
|
+
parsed: await require_helpers_load.loadEnv(context, context.config.env)
|
|
91
|
+
}, context.env ?? {}, {
|
|
92
|
+
parsed: {},
|
|
93
|
+
injected: []
|
|
94
|
+
});
|
|
95
|
+
const properties = (0, _powerlines_schema_helpers.getProperties)(context.env.vars);
|
|
96
|
+
context.info({
|
|
97
|
+
meta: { category: "env" },
|
|
98
|
+
message: `Environment Variables configuration: ${options.vars ? "" : "Defaulted "}${context.env.vars.variant === "reflection" ? "Deepkit type definition" : context.env.vars.variant === "json-schema" ? "JSON Schema" : context.env.vars.variant === "standard-schema" ? "Standard Schema" : context.env.vars.variant === "zod3" ? "Zod v3 schema" : "Typescript exported type"}${options.vars ? " from plugin options" : ""} provided ${Object.keys(properties).length} parameters\nEnvironment Secret configuration: ${options.secrets ? "" : "Defaulted "}${context.env.secrets.variant === "reflection" ? "Deepkit type definition" : context.env.secrets.variant === "json-schema" ? "JSON Schema" : context.env.secrets.variant === "standard-schema" ? "Standard Schema" : context.env.secrets.variant === "zod3" ? "Zod v3 schema" : "Typescript exported type"}${options.secrets ? " from plugin options" : ""} provided ${context.env.secrets?.schema ? Object.keys((0, _powerlines_schema_helpers.getProperties)(context.env.secrets)).length : "0"} parameters\nEnvironment variable Prefixes: ${context.config.env.prefix.join(", ")}\nShould inject values: ${context.config.env.inject ? "Yes" : "No"}\nShould validate configuration: ${context.config.env.validate ? "Yes" : "No"}`
|
|
99
|
+
});
|
|
100
|
+
const aliases = Object.fromEntries(Object.entries(properties).flatMap(__assignType(([key, prop]) => (0, _stryke_type_checks_is_set_array.isSetArray)(prop.metadata?.alias) ? prop.metadata?.alias?.map(__assignType((alias) => [alias, {
|
|
101
|
+
...prop,
|
|
102
|
+
metadata: {
|
|
103
|
+
...prop.metadata,
|
|
104
|
+
alias: [key]
|
|
105
|
+
}
|
|
106
|
+
}], [
|
|
107
|
+
"alias",
|
|
108
|
+
"",
|
|
109
|
+
"P\"2!\"/\""
|
|
110
|
+
])) : [], [
|
|
111
|
+
"param0",
|
|
112
|
+
"",
|
|
113
|
+
"P\"2!\"/\""
|
|
114
|
+
])));
|
|
115
|
+
for (const [key, value] of Object.entries(await require_helpers_load.loadEnv(context, context.config.env))) {
|
|
116
|
+
const unprefixedKey = context.config.env.prefix.reduce(__assignType((ret, prefix) => {
|
|
117
|
+
if (key.replace(/_$/g, "").startsWith(prefix)) return key.replace(/_$/g, "").slice(prefix.length);
|
|
118
|
+
return ret;
|
|
119
|
+
}, [
|
|
120
|
+
"ret",
|
|
121
|
+
"prefix",
|
|
122
|
+
"",
|
|
123
|
+
"P\"2!\"2\"\"/#"
|
|
124
|
+
]), key);
|
|
125
|
+
if (properties[unprefixedKey]) {
|
|
126
|
+
if (!properties[unprefixedKey].metadata?.isRuntime) {
|
|
127
|
+
if (properties[unprefixedKey].optional && context.env.vars.schema.optionalProperties?.[unprefixedKey]) {
|
|
128
|
+
context.env.vars.schema.optionalProperties[unprefixedKey].metadata ??= {};
|
|
129
|
+
context.env.vars.schema.optionalProperties[unprefixedKey].metadata.default = value;
|
|
130
|
+
} else if (context.env.vars.schema.properties?.[unprefixedKey]) {
|
|
131
|
+
context.env.vars.schema.properties[unprefixedKey].metadata ??= {};
|
|
132
|
+
context.env.vars.schema.properties[unprefixedKey].metadata.default = value;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
} else if (aliases[unprefixedKey]) {
|
|
136
|
+
if (!aliases[unprefixedKey].metadata?.isRuntime) {
|
|
137
|
+
const alias = aliases[unprefixedKey].metadata?.alias?.[0] ?? unprefixedKey;
|
|
138
|
+
if (aliases[unprefixedKey].optional && context.env.vars.schema.optionalProperties?.[alias]) {
|
|
139
|
+
context.env.vars.schema.optionalProperties[alias].metadata ??= {};
|
|
140
|
+
context.env.vars.schema.optionalProperties[alias].metadata.default = value;
|
|
141
|
+
} else if (context.env.vars.schema.properties?.[alias]) {
|
|
142
|
+
context.env.vars.schema.properties[alias].metadata ??= {};
|
|
143
|
+
context.env.vars.schema.properties[alias].metadata.default = value;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
extractEnvSchema.__type = [
|
|
150
|
+
"context",
|
|
151
|
+
() => require_types_plugin.__ΩEnvPluginOptions,
|
|
152
|
+
"options",
|
|
153
|
+
() => ({}),
|
|
154
|
+
"extractEnvSchema",
|
|
155
|
+
"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.",
|
|
156
|
+
"P\"2!n\"2#>$$`/%?&"
|
|
157
|
+
];
|
|
158
|
+
/**
|
|
159
|
+
* Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.
|
|
160
|
+
*
|
|
161
|
+
* @param context - The plugin context
|
|
162
|
+
* @returns A promise that resolves when the active environment variables and secrets have been read and stored in the plugin context.
|
|
163
|
+
*/
|
|
164
|
+
async function readActiveEnv(context) {
|
|
165
|
+
context.env.vars.schema.metadata ??= {};
|
|
166
|
+
context.env.vars.schema.metadata.active ??= [];
|
|
167
|
+
if (context.fs.existsSync((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "vars.json"))) {
|
|
168
|
+
const content = await context.fs.read((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "vars.json"));
|
|
169
|
+
if (content) context.env.vars.schema.metadata.active = JSON.parse(content)?.elements ?? [];
|
|
170
|
+
}
|
|
171
|
+
context.env.secrets.schema.metadata ??= {};
|
|
172
|
+
context.env.secrets.schema.metadata.active ??= [];
|
|
173
|
+
if (context.fs.existsSync((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "secrets.json"))) {
|
|
174
|
+
const content = await context.fs.read((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "secrets.json"));
|
|
175
|
+
if (content) context.env.secrets.schema.metadata.active = JSON.parse(content)?.elements ?? [];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
readActiveEnv.__type = [
|
|
179
|
+
"context",
|
|
180
|
+
"readActiveEnv",
|
|
181
|
+
"Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.",
|
|
182
|
+
"P\"2!\"/\"?#"
|
|
183
|
+
];
|
|
184
|
+
/**
|
|
185
|
+
* Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.
|
|
186
|
+
*
|
|
187
|
+
* @param context - The plugin context
|
|
188
|
+
* @returns A promise that resolves when the active environment variables and secrets have been written to the plugin context's cache.
|
|
189
|
+
*/
|
|
190
|
+
async function writeActiveEnv(context) {
|
|
191
|
+
return Promise.all([(0, _stryke_type_checks_is_set_array.isSetArray)(context.env.vars.schema.metadata?.active) ? context.fs.write((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "vars.json"), JSON.stringify({ elements: context.env.vars.schema.metadata.active })) : void 0, (0, _stryke_type_checks_is_set_array.isSetArray)(context.env.secrets.schema.metadata?.active) ? context.fs.write((0, _stryke_path_join.joinPaths)(context.cachePath, "env", "secrets.json"), JSON.stringify({ elements: context.env.secrets.schema.metadata.active })) : void 0].filter(Boolean));
|
|
192
|
+
}
|
|
193
|
+
writeActiveEnv.__type = [
|
|
194
|
+
"context",
|
|
195
|
+
"writeActiveEnv",
|
|
196
|
+
"Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.",
|
|
197
|
+
"P\"2!\"/\"?#"
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
exports.extractEnvSchema = extractEnvSchema;
|
|
202
|
+
exports.getDefaultSecretsTypeDefinition = getDefaultSecretsTypeDefinition;
|
|
203
|
+
exports.getDefaultVarsTypeDefinition = getDefaultVarsTypeDefinition;
|
|
204
|
+
exports.readActiveEnv = readActiveEnv;
|
|
205
|
+
exports.resolveRuntimeTypeFile = resolveRuntimeTypeFile;
|
|
206
|
+
exports.writeActiveEnv = writeActiveEnv;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { EnvPluginContext, EnvPluginOptions } from "../types/plugin.cjs";
|
|
2
|
+
import { UnresolvedContext } from "powerlines";
|
|
3
|
+
import { TypeDefinition } from "@stryke/types/configuration";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers/schema.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the runtime type definition file for the environment variables.
|
|
8
|
+
*
|
|
9
|
+
* @param context - The plugin context.
|
|
10
|
+
* @returns The runtime type definition file for the environment variables.
|
|
11
|
+
*/
|
|
12
|
+
declare function resolveRuntimeTypeFile<TContext extends UnresolvedContext>(context: TContext): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Gets the default type definition for the environment variables.
|
|
15
|
+
*
|
|
16
|
+
* @param context - The plugin context.
|
|
17
|
+
* @returns The default type definition for the environment variables.
|
|
18
|
+
*/
|
|
19
|
+
declare function getDefaultVarsTypeDefinition<TContext extends UnresolvedContext>(context: TContext): Promise<TypeDefinition>;
|
|
20
|
+
/** Gets the default type definition for the environment secrets.
|
|
21
|
+
*
|
|
22
|
+
* @param context - The plugin context.
|
|
23
|
+
* @returns The default type definition for the environment secrets.
|
|
24
|
+
*/
|
|
25
|
+
declare function getDefaultSecretsTypeDefinition<TContext extends UnresolvedContext>(context: TContext): Promise<TypeDefinition>;
|
|
26
|
+
/**
|
|
27
|
+
* 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.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* 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.
|
|
31
|
+
*
|
|
32
|
+
* @param context - The plugin context
|
|
33
|
+
* @param options - The plugin options containing the environment variables and secrets type definitions to extract the schema from. If not provided, the default type definitions will be used.
|
|
34
|
+
* @returns A promise that resolves when the schema has been extracted and stored in the plugin context.
|
|
35
|
+
*/
|
|
36
|
+
declare function extractEnvSchema<TContext extends EnvPluginContext>(context: TContext, options?: EnvPluginOptions): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.
|
|
39
|
+
*
|
|
40
|
+
* @param context - The plugin context
|
|
41
|
+
* @returns A promise that resolves when the active environment variables and secrets have been read and stored in the plugin context.
|
|
42
|
+
*/
|
|
43
|
+
declare function readActiveEnv<TContext extends EnvPluginContext>(context: TContext): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.
|
|
46
|
+
*
|
|
47
|
+
* @param context - The plugin context
|
|
48
|
+
* @returns A promise that resolves when the active environment variables and secrets have been written to the plugin context's cache.
|
|
49
|
+
*/
|
|
50
|
+
declare function writeActiveEnv<TContext extends EnvPluginContext>(context: TContext): Promise<void[]>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { extractEnvSchema, getDefaultSecretsTypeDefinition, getDefaultVarsTypeDefinition, readActiveEnv, resolveRuntimeTypeFile, writeActiveEnv };
|
|
53
|
+
//# sourceMappingURL=schema.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.cts","names":[],"sources":["../../src/helpers/schema.ts"],"mappings":";;;;;;;AAyCA;;;;iBAAsB,sBAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA;;;;;;;iBAiBA,4BAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA,CAAQ,cAAA;;;AAnBD;AAiB7B;;iBAcsB,+BAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA,CAAQ,cAAA;;;;;;;;;;;iBAiBR,gBAAA,kBAAkC,gBAAA,CAAA,CACtD,OAAA,EAAS,QAAA,EACT,OAAA,GAAS,gBAAA,GACR,OAAA;;;AAlCyC;AAY5C;;;iBAqMsB,aAAA,kBAA+B,gBAAA,CAAA,CACnD,OAAA,EAAS,QAAA,GAAQ,OAAA;;;;;;;iBAmCG,cAAA,kBAAgC,gBAAA,CAAA,CACpD,OAAA,EAAS,QAAA,GAAQ,OAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { EnvPluginContext, EnvPluginOptions } from "../types/plugin.mjs";
|
|
2
|
+
import { UnresolvedContext } from "powerlines";
|
|
3
|
+
import { TypeDefinition } from "@stryke/types/configuration";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers/schema.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the runtime type definition file for the environment variables.
|
|
8
|
+
*
|
|
9
|
+
* @param context - The plugin context.
|
|
10
|
+
* @returns The runtime type definition file for the environment variables.
|
|
11
|
+
*/
|
|
12
|
+
declare function resolveRuntimeTypeFile<TContext extends UnresolvedContext>(context: TContext): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Gets the default type definition for the environment variables.
|
|
15
|
+
*
|
|
16
|
+
* @param context - The plugin context.
|
|
17
|
+
* @returns The default type definition for the environment variables.
|
|
18
|
+
*/
|
|
19
|
+
declare function getDefaultVarsTypeDefinition<TContext extends UnresolvedContext>(context: TContext): Promise<TypeDefinition>;
|
|
20
|
+
/** Gets the default type definition for the environment secrets.
|
|
21
|
+
*
|
|
22
|
+
* @param context - The plugin context.
|
|
23
|
+
* @returns The default type definition for the environment secrets.
|
|
24
|
+
*/
|
|
25
|
+
declare function getDefaultSecretsTypeDefinition<TContext extends UnresolvedContext>(context: TContext): Promise<TypeDefinition>;
|
|
26
|
+
/**
|
|
27
|
+
* 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.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* 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.
|
|
31
|
+
*
|
|
32
|
+
* @param context - The plugin context
|
|
33
|
+
* @param options - The plugin options containing the environment variables and secrets type definitions to extract the schema from. If not provided, the default type definitions will be used.
|
|
34
|
+
* @returns A promise that resolves when the schema has been extracted and stored in the plugin context.
|
|
35
|
+
*/
|
|
36
|
+
declare function extractEnvSchema<TContext extends EnvPluginContext>(context: TContext, options?: EnvPluginOptions): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.
|
|
39
|
+
*
|
|
40
|
+
* @param context - The plugin context
|
|
41
|
+
* @returns A promise that resolves when the active environment variables and secrets have been read and stored in the plugin context.
|
|
42
|
+
*/
|
|
43
|
+
declare function readActiveEnv<TContext extends EnvPluginContext>(context: TContext): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.
|
|
46
|
+
*
|
|
47
|
+
* @param context - The plugin context
|
|
48
|
+
* @returns A promise that resolves when the active environment variables and secrets have been written to the plugin context's cache.
|
|
49
|
+
*/
|
|
50
|
+
declare function writeActiveEnv<TContext extends EnvPluginContext>(context: TContext): Promise<void[]>;
|
|
51
|
+
//#endregion
|
|
52
|
+
export { extractEnvSchema, getDefaultSecretsTypeDefinition, getDefaultVarsTypeDefinition, readActiveEnv, resolveRuntimeTypeFile, writeActiveEnv };
|
|
53
|
+
//# sourceMappingURL=schema.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.mts","names":[],"sources":["../../src/helpers/schema.ts"],"mappings":";;;;;;;AAyCA;;;;iBAAsB,sBAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA;;;;;;;iBAiBA,4BAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA,CAAQ,cAAA;;;AAnBD;AAiB7B;;iBAcsB,+BAAA,kBACH,iBAAA,CAAA,CACjB,OAAA,EAAS,QAAA,GAAW,OAAA,CAAQ,cAAA;;;;;;;;;;;iBAiBR,gBAAA,kBAAkC,gBAAA,CAAA,CACtD,OAAA,EAAS,QAAA,EACT,OAAA,GAAS,gBAAA,GACR,OAAA;;;AAlCyC;AAY5C;;;iBAqMsB,aAAA,kBAA+B,gBAAA,CAAA,CACnD,OAAA,EAAS,QAAA,GAAQ,OAAA;;;;;;;iBAmCG,cAAA,kBAAgC,gBAAA,CAAA,CACpD,OAAA,EAAS,QAAA,GAAQ,OAAA"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { __ΩEnvPluginOptions } from "../types/plugin.mjs";
|
|
2
|
+
import { loadEnv } from "./load.mjs";
|
|
3
|
+
import { isString } from "@stryke/type-checks/is-string";
|
|
4
|
+
import { extract } from "@powerlines/schema/extract";
|
|
5
|
+
import { getProperties, mergeSchemas } from "@powerlines/schema/helpers";
|
|
6
|
+
import { joinPaths } from "@stryke/path/join";
|
|
7
|
+
import { isSetArray } from "@stryke/type-checks/is-set-array";
|
|
8
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
9
|
+
import defu from "defu";
|
|
10
|
+
|
|
11
|
+
//#region src/helpers/schema.ts
|
|
12
|
+
function __assignType(fn, args) {
|
|
13
|
+
fn.__type = args;
|
|
14
|
+
return fn;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Resolves the runtime type definition file for the environment variables.
|
|
18
|
+
*
|
|
19
|
+
* @param context - The plugin context.
|
|
20
|
+
* @returns The runtime type definition file for the environment variables.
|
|
21
|
+
*/
|
|
22
|
+
async function resolveRuntimeTypeFile(context) {
|
|
23
|
+
const resolved = await context.fs.resolve("@powerlines/plugin-env/types/env");
|
|
24
|
+
if (!resolved) throw new Error(`Failed to resolve the runtime type definition file for the environment variables. Please ensure that the "@powerlines/plugin-env" package is installed.`);
|
|
25
|
+
return resolved;
|
|
26
|
+
}
|
|
27
|
+
resolveRuntimeTypeFile.__type = [
|
|
28
|
+
"context",
|
|
29
|
+
"resolveRuntimeTypeFile",
|
|
30
|
+
"Resolves the runtime type definition file for the environment variables.",
|
|
31
|
+
"P\"2!&`/\"?#"
|
|
32
|
+
];
|
|
33
|
+
/**
|
|
34
|
+
* Gets the default type definition for the environment variables.
|
|
35
|
+
*
|
|
36
|
+
* @param context - The plugin context.
|
|
37
|
+
* @returns The default type definition for the environment variables.
|
|
38
|
+
*/
|
|
39
|
+
async function getDefaultVarsTypeDefinition(context) {
|
|
40
|
+
return {
|
|
41
|
+
file: await resolveRuntimeTypeFile(context),
|
|
42
|
+
name: "EnvInterface"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
getDefaultVarsTypeDefinition.__type = [
|
|
46
|
+
"context",
|
|
47
|
+
"getDefaultVarsTypeDefinition",
|
|
48
|
+
"Gets the default type definition for the environment variables.",
|
|
49
|
+
"P\"2!!`/\"?#"
|
|
50
|
+
];
|
|
51
|
+
/** Gets the default type definition for the environment secrets.
|
|
52
|
+
*
|
|
53
|
+
* @param context - The plugin context.
|
|
54
|
+
* @returns The default type definition for the environment secrets.
|
|
55
|
+
*/
|
|
56
|
+
async function getDefaultSecretsTypeDefinition(context) {
|
|
57
|
+
return {
|
|
58
|
+
file: await resolveRuntimeTypeFile(context),
|
|
59
|
+
name: "SecretsInterface"
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
getDefaultSecretsTypeDefinition.__type = [
|
|
63
|
+
"context",
|
|
64
|
+
"getDefaultSecretsTypeDefinition",
|
|
65
|
+
"P\"2!!`/\""
|
|
66
|
+
];
|
|
67
|
+
/**
|
|
68
|
+
* 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.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* 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.
|
|
72
|
+
*
|
|
73
|
+
* @param context - The plugin context
|
|
74
|
+
* @param options - The plugin options containing the environment variables and secrets type definitions to extract the schema from. If not provided, the default type definitions will be used.
|
|
75
|
+
* @returns A promise that resolves when the schema has been extracted and stored in the plugin context.
|
|
76
|
+
*/
|
|
77
|
+
async function extractEnvSchema(context, options = {}) {
|
|
78
|
+
const defaultVarsTypeDefinition = await getDefaultVarsTypeDefinition(context);
|
|
79
|
+
const defaultSecretsTypeDefinition = await getDefaultSecretsTypeDefinition(context);
|
|
80
|
+
const vars = await extract(context, context.config.env.vars);
|
|
81
|
+
if (isString(context.config.env.vars) && context.config.env.vars !== `${defaultVarsTypeDefinition.file}#${defaultVarsTypeDefinition.name}` || isSetObject(context.config.env.vars) && (context.config.env.vars.file !== defaultVarsTypeDefinition.file || context.config.env.vars.name !== defaultVarsTypeDefinition.name)) vars.schema = mergeSchemas(vars, await extract(context, defaultVarsTypeDefinition));
|
|
82
|
+
const secrets = await extract(context, context.config.env.secrets);
|
|
83
|
+
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));
|
|
84
|
+
context.env = defu({
|
|
85
|
+
vars,
|
|
86
|
+
secrets,
|
|
87
|
+
parsed: await loadEnv(context, context.config.env)
|
|
88
|
+
}, context.env ?? {}, {
|
|
89
|
+
parsed: {},
|
|
90
|
+
injected: []
|
|
91
|
+
});
|
|
92
|
+
const properties = getProperties(context.env.vars);
|
|
93
|
+
context.info({
|
|
94
|
+
meta: { category: "env" },
|
|
95
|
+
message: `Environment Variables configuration: ${options.vars ? "" : "Defaulted "}${context.env.vars.variant === "reflection" ? "Deepkit type definition" : context.env.vars.variant === "json-schema" ? "JSON Schema" : context.env.vars.variant === "standard-schema" ? "Standard Schema" : context.env.vars.variant === "zod3" ? "Zod v3 schema" : "Typescript exported type"}${options.vars ? " from plugin options" : ""} provided ${Object.keys(properties).length} parameters\nEnvironment Secret configuration: ${options.secrets ? "" : "Defaulted "}${context.env.secrets.variant === "reflection" ? "Deepkit type definition" : context.env.secrets.variant === "json-schema" ? "JSON Schema" : context.env.secrets.variant === "standard-schema" ? "Standard Schema" : context.env.secrets.variant === "zod3" ? "Zod v3 schema" : "Typescript exported type"}${options.secrets ? " from plugin options" : ""} provided ${context.env.secrets?.schema ? Object.keys(getProperties(context.env.secrets)).length : "0"} parameters\nEnvironment variable Prefixes: ${context.config.env.prefix.join(", ")}\nShould inject values: ${context.config.env.inject ? "Yes" : "No"}\nShould validate configuration: ${context.config.env.validate ? "Yes" : "No"}`
|
|
96
|
+
});
|
|
97
|
+
const aliases = Object.fromEntries(Object.entries(properties).flatMap(__assignType(([key, prop]) => isSetArray(prop.metadata?.alias) ? prop.metadata?.alias?.map(__assignType((alias) => [alias, {
|
|
98
|
+
...prop,
|
|
99
|
+
metadata: {
|
|
100
|
+
...prop.metadata,
|
|
101
|
+
alias: [key]
|
|
102
|
+
}
|
|
103
|
+
}], [
|
|
104
|
+
"alias",
|
|
105
|
+
"",
|
|
106
|
+
"P\"2!\"/\""
|
|
107
|
+
])) : [], [
|
|
108
|
+
"param0",
|
|
109
|
+
"",
|
|
110
|
+
"P\"2!\"/\""
|
|
111
|
+
])));
|
|
112
|
+
for (const [key, value] of Object.entries(await loadEnv(context, context.config.env))) {
|
|
113
|
+
const unprefixedKey = context.config.env.prefix.reduce(__assignType((ret, prefix) => {
|
|
114
|
+
if (key.replace(/_$/g, "").startsWith(prefix)) return key.replace(/_$/g, "").slice(prefix.length);
|
|
115
|
+
return ret;
|
|
116
|
+
}, [
|
|
117
|
+
"ret",
|
|
118
|
+
"prefix",
|
|
119
|
+
"",
|
|
120
|
+
"P\"2!\"2\"\"/#"
|
|
121
|
+
]), key);
|
|
122
|
+
if (properties[unprefixedKey]) {
|
|
123
|
+
if (!properties[unprefixedKey].metadata?.isRuntime) {
|
|
124
|
+
if (properties[unprefixedKey].optional && context.env.vars.schema.optionalProperties?.[unprefixedKey]) {
|
|
125
|
+
context.env.vars.schema.optionalProperties[unprefixedKey].metadata ??= {};
|
|
126
|
+
context.env.vars.schema.optionalProperties[unprefixedKey].metadata.default = value;
|
|
127
|
+
} else if (context.env.vars.schema.properties?.[unprefixedKey]) {
|
|
128
|
+
context.env.vars.schema.properties[unprefixedKey].metadata ??= {};
|
|
129
|
+
context.env.vars.schema.properties[unprefixedKey].metadata.default = value;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else if (aliases[unprefixedKey]) {
|
|
133
|
+
if (!aliases[unprefixedKey].metadata?.isRuntime) {
|
|
134
|
+
const alias = aliases[unprefixedKey].metadata?.alias?.[0] ?? unprefixedKey;
|
|
135
|
+
if (aliases[unprefixedKey].optional && context.env.vars.schema.optionalProperties?.[alias]) {
|
|
136
|
+
context.env.vars.schema.optionalProperties[alias].metadata ??= {};
|
|
137
|
+
context.env.vars.schema.optionalProperties[alias].metadata.default = value;
|
|
138
|
+
} else if (context.env.vars.schema.properties?.[alias]) {
|
|
139
|
+
context.env.vars.schema.properties[alias].metadata ??= {};
|
|
140
|
+
context.env.vars.schema.properties[alias].metadata.default = value;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
extractEnvSchema.__type = [
|
|
147
|
+
"context",
|
|
148
|
+
() => __ΩEnvPluginOptions,
|
|
149
|
+
"options",
|
|
150
|
+
() => ({}),
|
|
151
|
+
"extractEnvSchema",
|
|
152
|
+
"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.",
|
|
153
|
+
"P\"2!n\"2#>$$`/%?&"
|
|
154
|
+
];
|
|
155
|
+
/**
|
|
156
|
+
* Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.
|
|
157
|
+
*
|
|
158
|
+
* @param context - The plugin context
|
|
159
|
+
* @returns A promise that resolves when the active environment variables and secrets have been read and stored in the plugin context.
|
|
160
|
+
*/
|
|
161
|
+
async function readActiveEnv(context) {
|
|
162
|
+
context.env.vars.schema.metadata ??= {};
|
|
163
|
+
context.env.vars.schema.metadata.active ??= [];
|
|
164
|
+
if (context.fs.existsSync(joinPaths(context.cachePath, "env", "vars.json"))) {
|
|
165
|
+
const content = await context.fs.read(joinPaths(context.cachePath, "env", "vars.json"));
|
|
166
|
+
if (content) context.env.vars.schema.metadata.active = JSON.parse(content)?.elements ?? [];
|
|
167
|
+
}
|
|
168
|
+
context.env.secrets.schema.metadata ??= {};
|
|
169
|
+
context.env.secrets.schema.metadata.active ??= [];
|
|
170
|
+
if (context.fs.existsSync(joinPaths(context.cachePath, "env", "secrets.json"))) {
|
|
171
|
+
const content = await context.fs.read(joinPaths(context.cachePath, "env", "secrets.json"));
|
|
172
|
+
if (content) context.env.secrets.schema.metadata.active = JSON.parse(content)?.elements ?? [];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
readActiveEnv.__type = [
|
|
176
|
+
"context",
|
|
177
|
+
"readActiveEnv",
|
|
178
|
+
"Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.",
|
|
179
|
+
"P\"2!\"/\"?#"
|
|
180
|
+
];
|
|
181
|
+
/**
|
|
182
|
+
* Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.
|
|
183
|
+
*
|
|
184
|
+
* @param context - The plugin context
|
|
185
|
+
* @returns A promise that resolves when the active environment variables and secrets have been written to the plugin context's cache.
|
|
186
|
+
*/
|
|
187
|
+
async function writeActiveEnv(context) {
|
|
188
|
+
return Promise.all([isSetArray(context.env.vars.schema.metadata?.active) ? context.fs.write(joinPaths(context.cachePath, "env", "vars.json"), JSON.stringify({ elements: context.env.vars.schema.metadata.active })) : void 0, isSetArray(context.env.secrets.schema.metadata?.active) ? context.fs.write(joinPaths(context.cachePath, "env", "secrets.json"), JSON.stringify({ elements: context.env.secrets.schema.metadata.active })) : void 0].filter(Boolean));
|
|
189
|
+
}
|
|
190
|
+
writeActiveEnv.__type = [
|
|
191
|
+
"context",
|
|
192
|
+
"writeActiveEnv",
|
|
193
|
+
"Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.",
|
|
194
|
+
"P\"2!\"/\"?#"
|
|
195
|
+
];
|
|
196
|
+
|
|
197
|
+
//#endregion
|
|
198
|
+
export { extractEnvSchema, getDefaultSecretsTypeDefinition, getDefaultVarsTypeDefinition, readActiveEnv, resolveRuntimeTypeFile, writeActiveEnv };
|
|
199
|
+
//# sourceMappingURL=schema.mjs.map
|
|
@@ -0,0 +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 { ObjectSchema } from \"@powerlines/schema\";\nimport { extract } from \"@powerlines/schema/extract\";\nimport { getProperties, mergeSchemas } from \"@powerlines/schema/helpers\";\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 defu from \"defu\";\nimport { UnresolvedContext } from \"powerlines\";\nimport {\n EnvPluginContext,\n EnvPluginOptions,\n EnvSchemaMetadata\n} 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 * 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 * @param options - The plugin options containing the environment variables and secrets type definitions to extract the schema from. If not provided, the default type definitions will be used.\n * @returns A promise that resolves when the schema has been extracted and stored in the plugin context.\n */\nexport async function extractEnvSchema<TContext extends EnvPluginContext>(\n context: TContext,\n options: EnvPluginOptions = {}\n): Promise<void> {\n const defaultVarsTypeDefinition = await getDefaultVarsTypeDefinition(context);\n const defaultSecretsTypeDefinition =\n await getDefaultSecretsTypeDefinition(context);\n\n const vars = (await extract(\n context,\n context.config.env.vars\n )) as ObjectSchema<EnvSchemaMetadata>;\n if (\n (isString(context.config.env.vars) &&\n context.config.env.vars !==\n `${defaultVarsTypeDefinition.file}#${defaultVarsTypeDefinition.name}`) ||\n (isSetObject(context.config.env.vars) &&\n ((context.config.env.vars as TypeDefinition).file !==\n defaultVarsTypeDefinition.file ||\n (context.config.env.vars as TypeDefinition).name !==\n defaultVarsTypeDefinition.name))\n ) {\n vars.schema = mergeSchemas(\n vars,\n (await extract(\n context,\n defaultVarsTypeDefinition\n )) as ObjectSchema<EnvSchemaMetadata>\n );\n }\n\n const secrets = (await extract(\n context,\n context.config.env.secrets\n )) as ObjectSchema<EnvSchemaMetadata>;\n if (\n (isString(context.config.env.secrets) &&\n context.config.env.secrets !==\n `${defaultSecretsTypeDefinition.file}#${defaultSecretsTypeDefinition.name}`) ||\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(\n context,\n defaultSecretsTypeDefinition\n )) as ObjectSchema<EnvSchemaMetadata>\n );\n }\n\n context.env = defu(\n {\n vars,\n secrets,\n parsed: await loadEnv(context, context.config.env)\n },\n context.env ?? {},\n {\n parsed: {},\n injected: []\n }\n );\n\n const properties = getProperties(context.env.vars);\n context.info({\n meta: {\n category: \"env\"\n },\n message: `Environment Variables configuration: ${\n options.vars ? \"\" : \"Defaulted \"\n }${\n context.env.vars.variant === \"reflection\"\n ? \"Deepkit type definition\"\n : context.env.vars.variant === \"json-schema\"\n ? \"JSON Schema\"\n : context.env.vars.variant === \"standard-schema\"\n ? \"Standard Schema\"\n : context.env.vars.variant === \"zod3\"\n ? \"Zod v3 schema\"\n : \"Typescript exported type\"\n }${options.vars ? \" from plugin options\" : \"\"} provided ${\n Object.keys(properties).length\n } parameters\\nEnvironment Secret configuration: ${\n options.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 }${options.secrets ? \" from plugin options\" : \"\"} provided ${\n context.env.secrets?.schema\n ? Object.keys(getProperties(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.metadata?.alias)\n ? prop.metadata?.alias?.map(alias => [\n alias,\n { ...prop, metadata: { ...prop.metadata, alias: [key] } }\n ])\n : []) as [string, typeof prop][]\n )\n );\n\n for (const [key, value] of Object.entries(\n await loadEnv(context, context.config.env)\n )) {\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].metadata?.isRuntime) {\n if (\n properties[unprefixedKey].optional &&\n context.env.vars.schema.optionalProperties?.[unprefixedKey]\n ) {\n context.env.vars.schema.optionalProperties[unprefixedKey].metadata ??=\n {} as EnvSchemaMetadata;\n context.env.vars.schema.optionalProperties[\n unprefixedKey\n ].metadata.default = value;\n } else if (context.env.vars.schema.properties?.[unprefixedKey]) {\n context.env.vars.schema.properties[unprefixedKey].metadata ??=\n {} as EnvSchemaMetadata;\n context.env.vars.schema.properties[unprefixedKey].metadata.default =\n value;\n }\n }\n } else if (aliases[unprefixedKey]) {\n if (!aliases[unprefixedKey].metadata?.isRuntime) {\n const alias =\n aliases[unprefixedKey].metadata?.alias?.[0] ?? unprefixedKey;\n if (\n aliases[unprefixedKey].optional &&\n context.env.vars.schema.optionalProperties?.[alias]\n ) {\n context.env.vars.schema.optionalProperties[alias].metadata ??=\n {} as EnvSchemaMetadata;\n context.env.vars.schema.optionalProperties[alias].metadata.default =\n value;\n } else if (context.env.vars.schema.properties?.[alias]) {\n context.env.vars.schema.properties[alias].metadata ??=\n {} as EnvSchemaMetadata;\n context.env.vars.schema.properties[alias].metadata.default = value;\n }\n }\n }\n }\n}\n\n/**\n * Reads the active environment variables and secrets from the plugin context's cache and stores them in the plugin context for use during the build process. This function should be called during the plugin's `buildStart` hook to ensure that the active environment variables and secrets are available before the build process begins.\n *\n * @param context - The plugin context\n * @returns A promise that resolves when the active environment variables and secrets have been read and stored in the plugin context.\n */\nexport async function readActiveEnv<TContext extends EnvPluginContext>(\n context: TContext\n) {\n context.env.vars.schema.metadata ??= {} as EnvSchemaMetadata;\n context.env.vars.schema.metadata.active ??= [];\n if (context.fs.existsSync(joinPaths(context.cachePath, \"env\", \"vars.json\"))) {\n const content = await context.fs.read(\n joinPaths(context.cachePath, \"env\", \"vars.json\")\n );\n if (content) {\n context.env.vars.schema.metadata.active =\n JSON.parse(content)?.elements ?? [];\n }\n }\n\n context.env.secrets.schema.metadata ??= {} as EnvSchemaMetadata;\n context.env.secrets.schema.metadata.active ??= [];\n if (\n context.fs.existsSync(joinPaths(context.cachePath, \"env\", \"secrets.json\"))\n ) {\n const content = await context.fs.read(\n joinPaths(context.cachePath, \"env\", \"secrets.json\")\n );\n if (content) {\n context.env.secrets.schema.metadata.active =\n JSON.parse(content)?.elements ?? [];\n }\n }\n}\n\n/**\n * Writes the active environment variables and secrets from the plugin context to the plugin context's cache for use during the build process. This function should be called whenever the active environment variables and secrets are updated in the plugin context to ensure that the latest values are available during the build process.\n *\n * @param context - The plugin context\n * @returns A promise that resolves when the active environment variables and secrets have been written to the plugin context's cache.\n */\nexport async function writeActiveEnv<TContext extends EnvPluginContext>(\n context: TContext\n) {\n return Promise.all(\n [\n isSetArray(context.env.vars.schema.metadata?.active)\n ? context.fs.write(\n joinPaths(context.cachePath, \"env\", \"vars.json\"),\n JSON.stringify({\n elements: context.env.vars.schema.metadata!.active\n })\n )\n : undefined,\n isSetArray(context.env.secrets.schema.metadata?.active)\n ? context.fs.write(\n joinPaths(context.cachePath, \"env\", \"secrets.json\"),\n JSON.stringify({\n elements: context.env.secrets.schema.metadata!.active\n })\n )\n : undefined\n ].filter(Boolean) as Promise<void>[]\n );\n}\n"],"mappings":";;;;;;;;;;;AAEA,SAAS,aAAY,IAAK,MAAC;;CAE1B,OAAK;AACN;;;;;;;AAeA,eAAsB,uBAAuB,SAAC;CAC9C,MAAQ,WAAW,MAAM,QAAG,GAAO,QAAS,kCAAC;CAC7C,IAAM,CAAC,UACP,MAAS,IAAA,MAAW,yJAA2C;CAE/D,OAAO;AACP;AACA,uBAAS,SAAoB;CAAI;CAAE;CAAW;CAAA;AAAA;;;;;;;;CAQ5C,OAAA;EACC,MAAA,MAAa,uBAAuB,OAAM;EAC5C,MAAA;CACC;AACF;AACA,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