@lorion-org/runtime-config-node 1.0.0-beta.0

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/index.cjs ADDED
@@ -0,0 +1,586 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ collectRuntimeConfigFragmentFiles: () => collectRuntimeConfigFragmentFiles,
34
+ ensureParentDir: () => ensureParentDir,
35
+ getRuntimeConfigScopeView: () => getRuntimeConfigScopeView,
36
+ getRuntimeConfigValue: () => getRuntimeConfigValue,
37
+ listRuntimeConfigFragments: () => listRuntimeConfigFragments,
38
+ listRuntimeConfigScopeFiles: () => listRuntimeConfigScopeFiles,
39
+ loadRuntimeConfigEnvVars: () => loadRuntimeConfigEnvVars,
40
+ loadRuntimeConfigFragment: () => loadRuntimeConfigFragment,
41
+ loadRuntimeConfigShellAssignments: () => loadRuntimeConfigShellAssignments,
42
+ loadRuntimeConfigSourceTree: () => loadRuntimeConfigSourceTree,
43
+ loadRuntimeConfigTree: () => loadRuntimeConfigTree,
44
+ parseRuntimeConfigFragmentFiles: () => parseRuntimeConfigFragmentFiles,
45
+ projectRuntimeConfigTree: () => projectRuntimeConfigTree,
46
+ readJsonFile: () => readJsonFile,
47
+ readRequiredJsonFile: () => readRequiredJsonFile,
48
+ readRuntimeConfigScopeJson: () => readRuntimeConfigScopeJson,
49
+ readTextFile: () => readTextFile,
50
+ resolveRuntimeConfigFilePath: () => resolveRuntimeConfigFilePath,
51
+ resolveRuntimeConfigPaths: () => resolveRuntimeConfigPaths,
52
+ resolveRuntimeConfigPublicFilePath: () => resolveRuntimeConfigPublicFilePath,
53
+ resolveRuntimeConfigPublicRootPath: () => resolveRuntimeConfigPublicRootPath,
54
+ resolveRuntimeConfigScopeFilePath: () => resolveRuntimeConfigScopeFilePath,
55
+ resolveRuntimeConfigSource: () => resolveRuntimeConfigSource,
56
+ resolveRuntimeConfigSourceFiles: () => resolveRuntimeConfigSourceFiles,
57
+ resolveRuntimeConfigSourcePublicRootPath: () => resolveRuntimeConfigSourcePublicRootPath,
58
+ showRuntimeConfigFragment: () => showRuntimeConfigFragment,
59
+ validateRuntimeConfigSchemaTargets: () => validateRuntimeConfigSchemaTargets,
60
+ validateRuntimeConfigScopes: () => validateRuntimeConfigScopes,
61
+ validateRuntimeConfigSourceScopes: () => validateRuntimeConfigSourceScopes,
62
+ writeJsonFile: () => writeJsonFile,
63
+ writeRuntimeConfigFragment: () => writeRuntimeConfigFragment,
64
+ writeRuntimeConfigScopeJson: () => writeRuntimeConfigScopeJson,
65
+ writeTextFile: () => writeTextFile
66
+ });
67
+ module.exports = __toCommonJS(index_exports);
68
+ var import_node_fs = require("fs");
69
+ var import_node_path = __toESM(require("path"), 1);
70
+ var import_ajv = __toESM(require("ajv"), 1);
71
+ var import_runtime_config = require("@lorion-org/runtime-config");
72
+ var defaultRuntimeConfigFileName = "runtime.config.json";
73
+ var defaultRuntimeConfigDirName = "runtime-config";
74
+ var defaultRuntimeConfigSchemaFileName = "runtime-config.schema.json";
75
+ function toPosixPath(filePath) {
76
+ return filePath.split(import_node_path.default.sep).join("/");
77
+ }
78
+ function escapeRegex(value) {
79
+ return value.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
80
+ }
81
+ function countWildcards(pattern) {
82
+ return (pattern.match(/\*/g) ?? []).length;
83
+ }
84
+ function assertSingleWildcardPattern(pattern) {
85
+ const wildcardCount = countWildcards(pattern);
86
+ if (wildcardCount !== 1) {
87
+ throw new Error(
88
+ `RuntimeConfig path pattern must contain exactly one "*" wildcard: "${pattern}"`
89
+ );
90
+ }
91
+ }
92
+ function createGlobSegmentRegex(segment) {
93
+ return new RegExp(`^${segment.split("*").map(escapeRegex).join("[^/\\\\]*")}$`);
94
+ }
95
+ function splitAbsolutePattern(pattern) {
96
+ const absolutePattern = import_node_path.default.resolve(pattern);
97
+ const parsed = import_node_path.default.parse(absolutePattern);
98
+ const relativePattern = import_node_path.default.relative(parsed.root, absolutePattern);
99
+ return {
100
+ root: parsed.root,
101
+ segments: relativePattern.split(/[\\/]+/).filter(Boolean)
102
+ };
103
+ }
104
+ function expandRuntimeConfigPattern(pattern) {
105
+ assertSingleWildcardPattern(pattern);
106
+ const { root, segments } = splitAbsolutePattern(pattern);
107
+ const visit = (currentDir, index) => {
108
+ const segment = segments[index];
109
+ if (!segment) return [];
110
+ const isLast = index === segments.length - 1;
111
+ if (!segment.includes("*")) {
112
+ const nextPath = import_node_path.default.join(currentDir, segment);
113
+ if (isLast) return (0, import_node_fs.existsSync)(nextPath) ? [nextPath] : [];
114
+ if (!(0, import_node_fs.existsSync)(nextPath)) return [];
115
+ return visit(nextPath, index + 1);
116
+ }
117
+ if (!(0, import_node_fs.existsSync)(currentDir)) return [];
118
+ const matcher = createGlobSegmentRegex(segment);
119
+ return (0, import_node_fs.readdirSync)(currentDir, { withFileTypes: true }).filter((entry) => entry.name !== "node_modules" && matcher.test(entry.name)).flatMap((entry) => {
120
+ const nextPath = import_node_path.default.join(currentDir, entry.name);
121
+ if (isLast) return entry.isFile() ? [nextPath] : [];
122
+ return entry.isDirectory() ? visit(nextPath, index + 1) : [];
123
+ });
124
+ };
125
+ return visit(root, 0);
126
+ }
127
+ function getRuntimeConfigScopeIdFromPattern(input) {
128
+ assertSingleWildcardPattern(input.pattern);
129
+ const resolvedPattern = import_node_path.default.resolve(input.pattern);
130
+ const wildcardIndex = resolvedPattern.indexOf("*");
131
+ const beforeWildcard = resolvedPattern.slice(0, wildcardIndex);
132
+ const afterWildcard = resolvedPattern.slice(wildcardIndex + 1);
133
+ const configPath = import_node_path.default.resolve(input.configPath);
134
+ if (!configPath.startsWith(beforeWildcard) || !configPath.endsWith(afterWildcard)) {
135
+ throw new Error(
136
+ `RuntimeConfig file "${input.configPath}" does not match pattern "${input.pattern}"`
137
+ );
138
+ }
139
+ return toPosixPath(
140
+ configPath.slice(beforeWildcard.length, configPath.length - afterWildcard.length)
141
+ ).replace(/^\/+|\/+$/g, "");
142
+ }
143
+ function getRuntimeConfigPatternPublicRoot(pattern) {
144
+ assertSingleWildcardPattern(pattern);
145
+ const resolvedPattern = import_node_path.default.resolve(pattern);
146
+ const beforeWildcard = resolvedPattern.slice(0, resolvedPattern.indexOf("*"));
147
+ const wildcardParent = beforeWildcard.replace(/[\\/]+$/, "");
148
+ return import_node_path.default.resolve(wildcardParent, "public");
149
+ }
150
+ function getFileName(options) {
151
+ return options.fileName ?? defaultRuntimeConfigFileName;
152
+ }
153
+ function getRuntimeConfigDirName(options) {
154
+ return options.runtimeConfigDirName ?? defaultRuntimeConfigDirName;
155
+ }
156
+ function getRuntimeConfigPathOptions(source) {
157
+ return {
158
+ ...source.fileName ? { fileName: source.fileName } : {},
159
+ ...source.runtimeConfigDirName ? { runtimeConfigDirName: source.runtimeConfigDirName } : {}
160
+ };
161
+ }
162
+ function getSchemaFileName(options) {
163
+ return options.schemaFileName ?? defaultRuntimeConfigSchemaFileName;
164
+ }
165
+ function stripJsonBom(text) {
166
+ return text.charCodeAt(0) === 65279 ? text.slice(1) : text;
167
+ }
168
+ function ensureParentDir(filePath) {
169
+ const dirPath = import_node_path.default.dirname(filePath);
170
+ if (!(0, import_node_fs.existsSync)(dirPath)) (0, import_node_fs.mkdirSync)(dirPath, { recursive: true });
171
+ }
172
+ function readTextFile(filePath) {
173
+ return (0, import_node_fs.existsSync)(filePath) ? (0, import_node_fs.readFileSync)(filePath, "utf8") : void 0;
174
+ }
175
+ function writeTextFile(filePath, data, options = {}) {
176
+ if (options.createDir !== false) ensureParentDir(filePath);
177
+ (0, import_node_fs.writeFileSync)(filePath, data, "utf8");
178
+ }
179
+ function readJsonFile(filePath, options = {}) {
180
+ const text = readTextFile(filePath);
181
+ if (text === void 0) return void 0;
182
+ try {
183
+ return JSON.parse(stripJsonBom(text));
184
+ } catch (error) {
185
+ options.onParseError?.(error, filePath);
186
+ return options.defaultValue;
187
+ }
188
+ }
189
+ function readRequiredJsonFile(filePath) {
190
+ const text = readTextFile(filePath);
191
+ if (text === void 0) {
192
+ throw new Error(`RuntimeConfig JSON file not found: "${filePath}"`);
193
+ }
194
+ try {
195
+ return JSON.parse(stripJsonBom(text));
196
+ } catch (error) {
197
+ throw new Error(`RuntimeConfig JSON parse error in "${filePath}": ${String(error)}`);
198
+ }
199
+ }
200
+ function writeJsonFile(filePath, data, options = {}) {
201
+ const spacing = options.pretty === false ? void 0 : 2;
202
+ writeTextFile(filePath, `${JSON.stringify(data, void 0, spacing)}
203
+ `, options);
204
+ }
205
+ function resolveRuntimeConfigPaths(varDir, scopeId, options = {}) {
206
+ const runtimeConfigDir = import_node_path.default.join(varDir, getRuntimeConfigDirName(options));
207
+ const scopeDir = import_node_path.default.join(runtimeConfigDir, scopeId);
208
+ const filePath = import_node_path.default.join(scopeDir, getFileName(options));
209
+ return {
210
+ varDir,
211
+ runtimeConfigDir,
212
+ scopeDir,
213
+ filePath
214
+ };
215
+ }
216
+ function resolveRuntimeConfigSource(options = {}) {
217
+ const envKey = options.envKey ?? "RUNTIME_CONFIG_VAR_DIR";
218
+ const varDir = String(
219
+ options.varDir ?? options.env?.[envKey] ?? options.defaultVarDir ?? ""
220
+ ).trim();
221
+ if (!varDir) {
222
+ throw new Error(`RuntimeConfig varDir not found. Pass varDir or set ${envKey}.`);
223
+ }
224
+ return {
225
+ varDir,
226
+ ...options.fileName ? { fileName: options.fileName } : {},
227
+ ...options.runtimeConfigDirName ? { runtimeConfigDirName: options.runtimeConfigDirName } : {}
228
+ };
229
+ }
230
+ function resolveRuntimeConfigFilePath(varDir, scopeId, filename, options = {}) {
231
+ const { scopeDir } = resolveRuntimeConfigPaths(varDir, scopeId, options);
232
+ return import_node_path.default.join(scopeDir, filename);
233
+ }
234
+ function resolveRuntimeConfigScopeFilePath(source, scopeId, fileName) {
235
+ return resolveRuntimeConfigFilePath(
236
+ source.varDir,
237
+ scopeId,
238
+ fileName,
239
+ getRuntimeConfigPathOptions(source)
240
+ );
241
+ }
242
+ function readRuntimeConfigScopeJson(source, scopeId, fileName, options = {}) {
243
+ return readJsonFile(resolveRuntimeConfigScopeFilePath(source, scopeId, fileName), options);
244
+ }
245
+ function writeRuntimeConfigScopeJson(source, scopeId, fileName, data, options = {}) {
246
+ writeJsonFile(resolveRuntimeConfigScopeFilePath(source, scopeId, fileName), data, options);
247
+ }
248
+ function resolveRuntimeConfigPublicRootPath(source) {
249
+ return import_node_path.default.resolve(
250
+ source.varDir,
251
+ getRuntimeConfigDirName(getRuntimeConfigPathOptions(source)),
252
+ "public"
253
+ );
254
+ }
255
+ function resolveRuntimeConfigPublicFilePath(source, relativePath) {
256
+ return import_node_path.default.resolve(resolveRuntimeConfigPublicRootPath(source), relativePath);
257
+ }
258
+ function resolveRuntimeConfigSourceFiles(source) {
259
+ return source.paths.flatMap(
260
+ (pattern) => expandRuntimeConfigPattern(pattern).map((configPath) => ({
261
+ configPath,
262
+ pattern,
263
+ scopeId: getRuntimeConfigScopeIdFromPattern({ configPath, pattern })
264
+ }))
265
+ ).filter((entry, index, entries) => {
266
+ return entries.findIndex((candidate) => candidate.configPath === entry.configPath) === index;
267
+ }).sort((left, right) => left.configPath.localeCompare(right.configPath));
268
+ }
269
+ function loadRuntimeConfigSourceTree(source) {
270
+ const fragments = /* @__PURE__ */ new Map();
271
+ for (const entry of resolveRuntimeConfigSourceFiles(source)) {
272
+ const config = readJsonFile(entry.configPath);
273
+ if (config) fragments.set(entry.scopeId, config);
274
+ }
275
+ return fragments;
276
+ }
277
+ function resolveRuntimeConfigSourcePublicRootPath(source) {
278
+ const pattern = source.paths[0];
279
+ return pattern ? getRuntimeConfigPatternPublicRoot(pattern) : void 0;
280
+ }
281
+ function listRuntimeConfigScopeFiles(varDir, scopeId, subdir, options = {}) {
282
+ const dirPath = resolveRuntimeConfigFilePath(varDir, scopeId, subdir, options);
283
+ if (!(0, import_node_fs.existsSync)(dirPath)) return [];
284
+ return (0, import_node_fs.readdirSync)(dirPath, { withFileTypes: true }).filter((entry) => entry.isFile()).map((entry) => entry.name).filter((name) => options.extension ? name.endsWith(options.extension) : true);
285
+ }
286
+ function collectRuntimeConfigFragmentFiles(runtimeConfigDir, options = {}) {
287
+ const fileName = getFileName(options);
288
+ const files = [];
289
+ if (!(0, import_node_fs.existsSync)(runtimeConfigDir)) return files;
290
+ const walk = (currentPath) => {
291
+ for (const entry of (0, import_node_fs.readdirSync)(currentPath, { withFileTypes: true })) {
292
+ const absolutePath = import_node_path.default.resolve(currentPath, entry.name);
293
+ if (entry.isDirectory()) {
294
+ walk(absolutePath);
295
+ continue;
296
+ }
297
+ if (entry.isFile() && entry.name === fileName) {
298
+ files.push(absolutePath);
299
+ }
300
+ }
301
+ };
302
+ walk(runtimeConfigDir);
303
+ return files.sort((left, right) => left.localeCompare(right));
304
+ }
305
+ function parseRuntimeConfigFragmentFiles(filePaths, runtimeConfigDir, options = {}) {
306
+ const fileName = getFileName(options);
307
+ const fragments = /* @__PURE__ */ new Map();
308
+ for (const filePath of filePaths) {
309
+ const relativePath = toPosixPath(import_node_path.default.relative(runtimeConfigDir, filePath));
310
+ const scopeId = relativePath.replace(
311
+ new RegExp(`/${fileName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`),
312
+ ""
313
+ );
314
+ const config = readJsonFile(filePath);
315
+ if (config) fragments.set(scopeId, config);
316
+ }
317
+ return fragments;
318
+ }
319
+ function loadRuntimeConfigFragment(varDir, scopeId, options = {}) {
320
+ const { filePath } = resolveRuntimeConfigPaths(varDir, scopeId, options);
321
+ return readJsonFile(filePath);
322
+ }
323
+ function writeRuntimeConfigFragment(varDir, scopeId, config, options = {}) {
324
+ const { filePath } = resolveRuntimeConfigPaths(varDir, scopeId, options);
325
+ writeJsonFile(filePath, config, options);
326
+ }
327
+ function formatRuntimeConfigSchemaValidationError(target, validationError) {
328
+ const jsonPath = validationError.instancePath || "/";
329
+ const ajvError = `${validationError.keyword}${validationError.message ? `: ${validationError.message}` : ""}`;
330
+ return new Error(
331
+ [
332
+ "RuntimeConfig schema validation failed.",
333
+ `Scope: ${target.scopeId}`,
334
+ `File: ${target.configPath}`,
335
+ `JSON path: ${jsonPath}`,
336
+ `Schema error: ${ajvError}`
337
+ ].join("\n")
338
+ );
339
+ }
340
+ function validateRuntimeConfigSchemaTargets(targets, options = {}) {
341
+ const ajv = new import_ajv.default({
342
+ strict: false,
343
+ allErrors: false,
344
+ ...options.ajvOptions
345
+ });
346
+ const formatError = options.formatError ?? formatRuntimeConfigSchemaValidationError;
347
+ for (const target of targets) {
348
+ if (!(0, import_node_fs.existsSync)(target.schemaPath) || !(0, import_node_fs.existsSync)(target.configPath)) {
349
+ continue;
350
+ }
351
+ const schema = readRequiredJsonFile(target.schemaPath);
352
+ const runtimeConfig = readRequiredJsonFile(target.configPath);
353
+ const validate = ajv.compile(schema);
354
+ const isValid = validate(runtimeConfig);
355
+ if (!isValid) {
356
+ const validationError = validate.errors?.[0];
357
+ if (validationError) {
358
+ throw formatError(target, validationError);
359
+ }
360
+ throw new Error(
361
+ `RuntimeConfig schema validation failed for "${target.scopeId}" (${target.configPath})`
362
+ );
363
+ }
364
+ }
365
+ }
366
+ function loadRuntimeConfigTree(varDir, options = {}) {
367
+ const runtimeConfigDir = import_node_path.default.join(varDir, getRuntimeConfigDirName(options));
368
+ const filePaths = collectRuntimeConfigFragmentFiles(runtimeConfigDir, options);
369
+ return parseRuntimeConfigFragmentFiles(filePaths, runtimeConfigDir, options);
370
+ }
371
+ function loadRuntimeConfigEnvVars(varDir, options = {}) {
372
+ const { fileName, runtimeConfigDirName, ...envOptions } = options;
373
+ return (0, import_runtime_config.projectRuntimeConfigEnvVars)(
374
+ loadRuntimeConfigTree(varDir, {
375
+ ...fileName ? { fileName } : {},
376
+ ...runtimeConfigDirName ? { runtimeConfigDirName } : {}
377
+ }),
378
+ envOptions
379
+ );
380
+ }
381
+ function loadRuntimeConfigShellAssignments(varDir, options = {}) {
382
+ return (0, import_runtime_config.runtimeEnvVarsToShellAssignments)(loadRuntimeConfigEnvVars(varDir, options));
383
+ }
384
+ function listRuntimeConfigFragments(varDir, options = {}) {
385
+ const { contextInputKey, ...pathOptions } = options;
386
+ const fragments = loadRuntimeConfigTree(varDir, pathOptions);
387
+ const scopes = Array.from(fragments.entries()).map(([scopeId, config]) => {
388
+ const fragment = contextInputKey ? (0, import_runtime_config.toRuntimeConfigFragment)(config, { contextInputKey }) : config;
389
+ return {
390
+ contextIds: Object.keys(fragment.contexts ?? {}).sort(),
391
+ path: resolveRuntimeConfigPaths(varDir, scopeId, pathOptions).filePath,
392
+ privateKeys: Object.keys(config.private ?? {}).sort(),
393
+ publicKeys: Object.keys(config.public ?? {}).sort(),
394
+ scopeId
395
+ };
396
+ }).sort((left, right) => left.scopeId.localeCompare(right.scopeId));
397
+ return {
398
+ fileName: getFileName(pathOptions),
399
+ scopes,
400
+ varDir
401
+ };
402
+ }
403
+ function showRuntimeConfigFragment(varDir, scopeId, options = {}) {
404
+ const normalizedScopeId = scopeId.trim();
405
+ const config = normalizedScopeId ? loadRuntimeConfigFragment(varDir, normalizedScopeId, options) : void 0;
406
+ return {
407
+ fileName: getFileName(options),
408
+ path: resolveRuntimeConfigPaths(varDir, normalizedScopeId, options).filePath,
409
+ scopeId: normalizedScopeId,
410
+ varDir,
411
+ exists: Boolean(config),
412
+ ...config ? { config } : {}
413
+ };
414
+ }
415
+ function projectRuntimeConfigTree(varDir, options = {}) {
416
+ const fragments = loadRuntimeConfigTree(varDir, options);
417
+ const scopeIds = options.scopeIds?.length ? options.scopeIds : Array.from(fragments.keys()).sort();
418
+ return {
419
+ fileName: getFileName(options),
420
+ runtimeConfig: (0, import_runtime_config.projectSectionedRuntimeConfig)(fragments, {
421
+ ...options.contextInputKey ? { contextInputKey: options.contextInputKey } : {},
422
+ ...options.contextOutputKey ? { contextOutputKey: options.contextOutputKey } : {},
423
+ scopeIds
424
+ }),
425
+ scopeIds,
426
+ varDir
427
+ };
428
+ }
429
+ function getRuntimeConfigValue(varDir, scopeId, key, options = {}) {
430
+ const normalizedScopeId = scopeId.trim();
431
+ const normalizedKey = key.trim();
432
+ const visibility = options.visibility ?? "public";
433
+ const runtimeConfig = projectRuntimeConfigTree(varDir, {
434
+ ...options,
435
+ scopeIds: normalizedScopeId ? [normalizedScopeId] : []
436
+ }).runtimeConfig;
437
+ const value = (0, import_runtime_config.resolveRuntimeConfigValueFromRuntimeConfig)(
438
+ runtimeConfig,
439
+ normalizedScopeId,
440
+ normalizedKey,
441
+ {
442
+ ...options.contextId ? { contextId: options.contextId } : {},
443
+ ...options.contextOutputKey ? { contextOutputKey: options.contextOutputKey } : {},
444
+ visibility
445
+ }
446
+ );
447
+ return {
448
+ fileName: getFileName(options),
449
+ key: normalizedKey,
450
+ scopeId: normalizedScopeId,
451
+ varDir,
452
+ visibility,
453
+ ...options.contextId ? { contextId: options.contextId } : {},
454
+ exists: value !== void 0,
455
+ ...value !== void 0 ? { value } : {}
456
+ };
457
+ }
458
+ function getRuntimeConfigScopeView(varDir, scopeId, options = {}) {
459
+ const normalizedScopeId = scopeId.trim();
460
+ const visibility = options.visibility ?? "public";
461
+ const runtimeConfig = projectRuntimeConfigTree(varDir, {
462
+ ...options,
463
+ scopeIds: normalizedScopeId ? [normalizedScopeId] : []
464
+ }).runtimeConfig;
465
+ const scopeOptions = {
466
+ ...options.contextId ? { contextId: options.contextId } : {},
467
+ ...options.contextOutputKey ? { contextOutputKey: options.contextOutputKey } : {},
468
+ visibility
469
+ };
470
+ return {
471
+ config: (0, import_runtime_config.getRuntimeConfigScope)(runtimeConfig, normalizedScopeId, scopeOptions),
472
+ fileName: getFileName(options),
473
+ scopeId: normalizedScopeId,
474
+ varDir,
475
+ visibility,
476
+ ...options.contextId ? { contextId: options.contextId } : {}
477
+ };
478
+ }
479
+ function validateRuntimeConfigScopes(varDir, targets, options = {}) {
480
+ const schemaFileName = getSchemaFileName(options);
481
+ const result = {
482
+ fileName: getFileName(options),
483
+ skipped: [],
484
+ validated: [],
485
+ varDir
486
+ };
487
+ for (const target of targets) {
488
+ const scopeId = target.scopeId.trim();
489
+ if (!scopeId) continue;
490
+ const configPath = resolveRuntimeConfigPaths(varDir, scopeId, options).filePath;
491
+ const schemaPath = target.cwd ? import_node_path.default.resolve(target.cwd, schemaFileName) : void 0;
492
+ if (!target.cwd || !schemaPath) {
493
+ result.skipped.push({ configPath, reason: "missing-scope-dir", scopeId });
494
+ continue;
495
+ }
496
+ if (!(0, import_node_fs.existsSync)(configPath)) {
497
+ result.skipped.push({ configPath, reason: "missing-config", schemaPath, scopeId });
498
+ continue;
499
+ }
500
+ if (!(0, import_node_fs.existsSync)(schemaPath)) {
501
+ result.skipped.push({ configPath, reason: "missing-schema", schemaPath, scopeId });
502
+ continue;
503
+ }
504
+ result.validated.push({ configPath, schemaPath, scopeId });
505
+ }
506
+ validateRuntimeConfigSchemaTargets(result.validated, {
507
+ ...options.formatError ? { formatError: options.formatError } : {}
508
+ });
509
+ return result;
510
+ }
511
+ function validateRuntimeConfigSourceScopes(source, targets, options = {}) {
512
+ const schemaFileName = getSchemaFileName(options);
513
+ const filesByScope = /* @__PURE__ */ new Map();
514
+ const result = {
515
+ fileName: source.paths[0] ?? "",
516
+ skipped: [],
517
+ validated: [],
518
+ varDir: ""
519
+ };
520
+ for (const file of resolveRuntimeConfigSourceFiles(source)) {
521
+ filesByScope.set(file.scopeId, file.configPath);
522
+ }
523
+ for (const target of targets) {
524
+ const scopeId = target.scopeId.trim();
525
+ if (!scopeId) continue;
526
+ const configPath = filesByScope.get(scopeId);
527
+ const schemaPath = target.cwd ? import_node_path.default.resolve(target.cwd, schemaFileName) : void 0;
528
+ if (!target.cwd || !schemaPath) {
529
+ result.skipped.push({
530
+ ...configPath ? { configPath } : {},
531
+ reason: "missing-scope-dir",
532
+ scopeId
533
+ });
534
+ continue;
535
+ }
536
+ if (!configPath) {
537
+ result.skipped.push({ reason: "missing-config", schemaPath, scopeId });
538
+ continue;
539
+ }
540
+ if (!(0, import_node_fs.existsSync)(schemaPath)) {
541
+ result.skipped.push({ configPath, reason: "missing-schema", schemaPath, scopeId });
542
+ continue;
543
+ }
544
+ result.validated.push({ configPath, schemaPath, scopeId });
545
+ }
546
+ validateRuntimeConfigSchemaTargets(result.validated, {
547
+ ...options.formatError ? { formatError: options.formatError } : {}
548
+ });
549
+ return result;
550
+ }
551
+ // Annotate the CommonJS export names for ESM import in node:
552
+ 0 && (module.exports = {
553
+ collectRuntimeConfigFragmentFiles,
554
+ ensureParentDir,
555
+ getRuntimeConfigScopeView,
556
+ getRuntimeConfigValue,
557
+ listRuntimeConfigFragments,
558
+ listRuntimeConfigScopeFiles,
559
+ loadRuntimeConfigEnvVars,
560
+ loadRuntimeConfigFragment,
561
+ loadRuntimeConfigShellAssignments,
562
+ loadRuntimeConfigSourceTree,
563
+ loadRuntimeConfigTree,
564
+ parseRuntimeConfigFragmentFiles,
565
+ projectRuntimeConfigTree,
566
+ readJsonFile,
567
+ readRequiredJsonFile,
568
+ readRuntimeConfigScopeJson,
569
+ readTextFile,
570
+ resolveRuntimeConfigFilePath,
571
+ resolveRuntimeConfigPaths,
572
+ resolveRuntimeConfigPublicFilePath,
573
+ resolveRuntimeConfigPublicRootPath,
574
+ resolveRuntimeConfigScopeFilePath,
575
+ resolveRuntimeConfigSource,
576
+ resolveRuntimeConfigSourceFiles,
577
+ resolveRuntimeConfigSourcePublicRootPath,
578
+ showRuntimeConfigFragment,
579
+ validateRuntimeConfigSchemaTargets,
580
+ validateRuntimeConfigScopes,
581
+ validateRuntimeConfigSourceScopes,
582
+ writeJsonFile,
583
+ writeRuntimeConfigFragment,
584
+ writeRuntimeConfigScopeJson,
585
+ writeTextFile
586
+ });