@cloudcannon/configuration-loader 0.0.7 → 0.0.8
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/browser.d.ts +0 -1
- package/{src/browser.ts → dist/browser.js} +1 -13
- package/dist/index.d.ts +0 -1
- package/{src/index.ts → dist/index.js} +2 -15
- package/dist/load.d.ts +0 -1
- package/dist/load.js +103 -0
- package/dist/loader.d.ts +0 -1
- package/dist/loader.js +349 -0
- package/dist/types.d.ts +0 -1
- package/dist/types.js +1 -0
- package/package.json +7 -9
- package/dist/browser.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/load.d.ts.map +0 -1
- package/dist/loader.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/src/load.ts +0 -125
- package/src/loader.test.ts +0 -528
- package/src/loader.ts +0 -525
- package/src/types.ts +0 -101
package/dist/browser.d.ts
CHANGED
|
@@ -17,4 +17,3 @@
|
|
|
17
17
|
export type { Configuration, StructureValue } from '@cloudcannon/configuration-types';
|
|
18
18
|
export { mergeConfiguration } from './loader.ts';
|
|
19
19
|
export type { GlobLoaderWarnings, GlobResult, GlobTypeKey, MergeConfigurationOptions, } from './types.ts';
|
|
20
|
-
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -14,17 +14,5 @@
|
|
|
14
14
|
* });
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
|
-
// Re-export useful types from configuration-types for convenience
|
|
19
|
-
export type { Configuration, StructureValue } from '@cloudcannon/configuration-types';
|
|
20
|
-
|
|
21
17
|
// Export the core merge function (no Node.js dependencies)
|
|
22
|
-
export { mergeConfiguration } from
|
|
23
|
-
|
|
24
|
-
// Export types from dedicated types file
|
|
25
|
-
export type {
|
|
26
|
-
GlobLoaderWarnings,
|
|
27
|
-
GlobResult,
|
|
28
|
-
GlobTypeKey,
|
|
29
|
-
MergeConfigurationOptions,
|
|
30
|
-
} from './types.ts';
|
|
18
|
+
export { mergeConfiguration } from "./loader.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -21,4 +21,3 @@ export type { Configuration, StructureValue } from '@cloudcannon/configuration-t
|
|
|
21
21
|
export { type LoadConfigurationOptions, loadConfiguration } from './load.ts';
|
|
22
22
|
export { mergeConfiguration } from './loader.ts';
|
|
23
23
|
export type { GlobLoaderWarnings, GlobResult, GlobTypeKey, MergeConfigurationOptions, } from './types.ts';
|
|
24
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -17,20 +17,7 @@
|
|
|
17
17
|
* console.log(result.config);
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
// Re-export useful types from configuration-types for convenience
|
|
22
|
-
export type { Configuration, StructureValue } from '@cloudcannon/configuration-types';
|
|
23
|
-
|
|
24
20
|
// Export the simple high-level API
|
|
25
|
-
export {
|
|
26
|
-
|
|
21
|
+
export { loadConfiguration } from "./load.js";
|
|
27
22
|
// Export the lower-level API for custom implementations
|
|
28
|
-
export { mergeConfiguration } from
|
|
29
|
-
|
|
30
|
-
// Export types from dedicated types file
|
|
31
|
-
export type {
|
|
32
|
-
GlobLoaderWarnings,
|
|
33
|
-
GlobResult,
|
|
34
|
-
GlobTypeKey,
|
|
35
|
-
MergeConfigurationOptions,
|
|
36
|
-
} from './types.ts';
|
|
23
|
+
export { mergeConfiguration } from "./loader.js";
|
package/dist/load.d.ts
CHANGED
package/dist/load.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { globSync } from 'node:fs';
|
|
10
|
+
import { readFile } from 'node:fs/promises';
|
|
11
|
+
import { dirname, resolve } from 'node:path';
|
|
12
|
+
import { mergeConfiguration } from "./loader.js";
|
|
13
|
+
/**
|
|
14
|
+
* Attempts to load the js-yaml package dynamically.
|
|
15
|
+
* Returns the load function if available, otherwise returns undefined.
|
|
16
|
+
*/
|
|
17
|
+
async function tryLoadYamlParser() {
|
|
18
|
+
try {
|
|
19
|
+
// Use a variable to prevent TypeScript from trying to resolve the module statically
|
|
20
|
+
const jsYamlModuleName = 'js-yaml';
|
|
21
|
+
const jsYaml = (await import(__rewriteRelativeImportExtension(jsYamlModuleName)));
|
|
22
|
+
return jsYaml.load;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a file parser that handles JSON and YAML files.
|
|
30
|
+
*/
|
|
31
|
+
async function createDefaultParser() {
|
|
32
|
+
const yamlParse = await tryLoadYamlParser();
|
|
33
|
+
return (contents, filePath) => {
|
|
34
|
+
if (filePath.endsWith('.json')) {
|
|
35
|
+
return JSON.parse(contents);
|
|
36
|
+
}
|
|
37
|
+
if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) {
|
|
38
|
+
if (!yamlParse) {
|
|
39
|
+
throw new Error(`Cannot parse YAML file "${filePath}". Install js-yaml: npm install js-yaml`);
|
|
40
|
+
}
|
|
41
|
+
return yamlParse(contents);
|
|
42
|
+
}
|
|
43
|
+
// Try JSON first, then YAML if available
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(contents);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
if (yamlParse) {
|
|
49
|
+
return yamlParse(contents);
|
|
50
|
+
}
|
|
51
|
+
throw new Error(`Cannot parse file "${filePath}". Install js-yaml for YAML support: npm install js-yaml`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Loads and merges a CloudCannon configuration file with all its split configuration files.
|
|
57
|
+
*
|
|
58
|
+
* This is the simplest way to load CloudCannon configuration. It handles:
|
|
59
|
+
* - Reading the configuration file from disk
|
|
60
|
+
* - Parsing YAML and JSON files
|
|
61
|
+
* - Finding files matching glob patterns
|
|
62
|
+
* - Merging all configuration into a single object
|
|
63
|
+
*
|
|
64
|
+
* @param configPath - Path to the CloudCannon configuration file (e.g., 'cloudcannon.config.yml')
|
|
65
|
+
* @param options - Optional configuration options
|
|
66
|
+
* @returns A promise resolving to the merged configuration with metadata
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { loadConfiguration } from '@cloudcannon/configuration-loader';
|
|
71
|
+
*
|
|
72
|
+
* const result = await loadConfiguration('cloudcannon.config.yml');
|
|
73
|
+
* console.log(result.config);
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export async function loadConfiguration(configPath, options) {
|
|
77
|
+
const parseFile = options?.parseFile ?? (await createDefaultParser());
|
|
78
|
+
const absoluteConfigPath = resolve(configPath);
|
|
79
|
+
const configDir = dirname(absoluteConfigPath);
|
|
80
|
+
// Load and parse the base configuration file
|
|
81
|
+
const configContents = await readFile(absoluteConfigPath, 'utf8');
|
|
82
|
+
const config = (await parseFile(configContents, absoluteConfigPath));
|
|
83
|
+
// Create the merge options with default implementations
|
|
84
|
+
const mergeOptions = {
|
|
85
|
+
// Note: globSync is used here because the interface is synchronous by design.
|
|
86
|
+
// Glob operations are typically fast (just directory traversal), while file
|
|
87
|
+
// reading (handled by loadConfigFile) is the slower I/O operation.
|
|
88
|
+
findFilesMatchingGlobs: (globs) => {
|
|
89
|
+
const resolvedGlobs = globs.map((g) => {
|
|
90
|
+
if (g.startsWith('!')) {
|
|
91
|
+
return `!${resolve(configDir, g.slice(1))}`;
|
|
92
|
+
}
|
|
93
|
+
return resolve(configDir, g);
|
|
94
|
+
});
|
|
95
|
+
return globSync(resolvedGlobs);
|
|
96
|
+
},
|
|
97
|
+
loadConfigFile: async (filePath) => {
|
|
98
|
+
const contents = await readFile(filePath, 'utf8');
|
|
99
|
+
return await parseFile(contents, filePath);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
return mergeConfiguration(config, mergeOptions);
|
|
103
|
+
}
|
package/dist/loader.d.ts
CHANGED
package/dist/loader.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
function isObject(maybeObject) {
|
|
2
|
+
return maybeObject !== null && typeof maybeObject === 'object' && !Array.isArray(maybeObject);
|
|
3
|
+
}
|
|
4
|
+
class ConfigurationLoader {
|
|
5
|
+
warnings;
|
|
6
|
+
pathsToGlobKey = {};
|
|
7
|
+
globKeyToPaths = {
|
|
8
|
+
_structures_from_glob: new Set(),
|
|
9
|
+
values_from_glob: new Set(),
|
|
10
|
+
_snippets_from_glob: new Set(),
|
|
11
|
+
_snippets_imports_from_glob: new Set(),
|
|
12
|
+
_snippets_templates_from_glob: new Set(),
|
|
13
|
+
_snippets_definitions_from_glob: new Set(),
|
|
14
|
+
_inputs_from_glob: new Set(),
|
|
15
|
+
_editables_from_glob: new Set(),
|
|
16
|
+
collections_config_from_glob: new Set(),
|
|
17
|
+
schemas_from_glob: new Set(),
|
|
18
|
+
};
|
|
19
|
+
globPatterns;
|
|
20
|
+
findFilesMatchingGlobs;
|
|
21
|
+
loadConfigFile;
|
|
22
|
+
constructor(findFilesMatchingGlobs, loadConfigFile) {
|
|
23
|
+
this.warnings = {
|
|
24
|
+
cycles: [],
|
|
25
|
+
multipleGlobKeys: [],
|
|
26
|
+
arrayValues: [],
|
|
27
|
+
};
|
|
28
|
+
this.globPatterns = new Set();
|
|
29
|
+
this.findFilesMatchingGlobs = findFilesMatchingGlobs;
|
|
30
|
+
this.loadConfigFile = loadConfigFile;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Loads and merges configuration from files matching glob patterns.
|
|
34
|
+
* Processes all *_from_glob keys in the configuration.
|
|
35
|
+
* Recursively processes globs in loaded files as well.
|
|
36
|
+
* Also extracts glob patterns from loaded files for change detection.
|
|
37
|
+
*/
|
|
38
|
+
async mergeConfiguration(config) {
|
|
39
|
+
this.warnings = {
|
|
40
|
+
cycles: [],
|
|
41
|
+
multipleGlobKeys: [],
|
|
42
|
+
arrayValues: [],
|
|
43
|
+
};
|
|
44
|
+
this.globPatterns.clear();
|
|
45
|
+
// Process root-level glob keys in the correct order
|
|
46
|
+
await this.importFromGlobKey(config, '_snippets_from_glob', '_snippets', false);
|
|
47
|
+
await this.importFromGlobKey(config, '_snippets_imports_from_glob', '_snippets_imports', false);
|
|
48
|
+
await this.importFromGlobKey(config, '_snippets_templates_from_glob', '_snippets_templates', false);
|
|
49
|
+
await this.importFromGlobKey(config, '_snippets_definitions_from_glob', '_snippets_definitions', false);
|
|
50
|
+
await this.importFromGlobKey(config, 'collections_config_from_glob', 'collections_config', false);
|
|
51
|
+
await this.importFromGlobKey(config, '_inputs_from_glob', '_inputs', false);
|
|
52
|
+
await this.importFromGlobKey(config, '_structures_from_glob', '_structures', false);
|
|
53
|
+
await this.processInputsKey(config._inputs);
|
|
54
|
+
await this.processStructuresKey(config._structures);
|
|
55
|
+
// Process nested globs in collections_config
|
|
56
|
+
// Collections can have: schemas_from_glob, _inputs_from_glob, _structures_from_glob
|
|
57
|
+
if (config.collections_config && isObject(config.collections_config)) {
|
|
58
|
+
for (const [collectionKey, collectionConfig] of Object.entries(config.collections_config)) {
|
|
59
|
+
if (isObject(collectionConfig)) {
|
|
60
|
+
// Process schemas_from_glob (first in collections)
|
|
61
|
+
if ('schemas_from_glob' in collectionConfig) {
|
|
62
|
+
await this.importFromGlobKey(collectionConfig, 'schemas_from_glob', 'schemas', false);
|
|
63
|
+
}
|
|
64
|
+
const collection = config.collections_config[collectionKey];
|
|
65
|
+
await this.importFromGlobKey(collection, '_inputs_from_glob', '_inputs', false);
|
|
66
|
+
await this.importFromGlobKey(collection, '_structures_from_glob', '_structures', false);
|
|
67
|
+
await this.processInputsKey(collection._inputs);
|
|
68
|
+
await this.processStructuresKey(collection._structures);
|
|
69
|
+
// Process recursive globs in schemas
|
|
70
|
+
if (isObject(collection.schemas)) {
|
|
71
|
+
for (const schemaValue of Object.values(collection.schemas)) {
|
|
72
|
+
if (isObject(schemaValue)) {
|
|
73
|
+
await this.importFromGlobKey(schemaValue, '_inputs_from_glob', '_inputs', false);
|
|
74
|
+
await this.importFromGlobKey(schemaValue, '_structures_from_glob', '_structures', false);
|
|
75
|
+
await this.processInputsKey(schemaValue._inputs);
|
|
76
|
+
await this.processStructuresKey(schemaValue._structures);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Process recursive globs in _snippets (can have _inputs_from_glob)
|
|
84
|
+
if (config._snippets && isObject(config._snippets)) {
|
|
85
|
+
await this.importFromGlobKey(config._snippets, '_inputs_from_glob', '_inputs', false);
|
|
86
|
+
for (const snippetConfig of Object.values(config._snippets)) {
|
|
87
|
+
if (isObject(snippetConfig)) {
|
|
88
|
+
await this.importFromGlobKey(snippetConfig, '_inputs_from_glob', '_inputs', false);
|
|
89
|
+
await this.importFromGlobKey(snippetConfig, '_structures_from_glob', '_structures', false);
|
|
90
|
+
await this.processInputsKey(snippetConfig._inputs);
|
|
91
|
+
await this.processStructuresKey(snippetConfig._structures);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return config;
|
|
96
|
+
}
|
|
97
|
+
/*
|
|
98
|
+
* Process recursive globs in _inputs (can have _structures_from_glob)
|
|
99
|
+
* @param inputs - The _inputs key to process
|
|
100
|
+
* @returns A promise that resolves when the inputs key has been processed
|
|
101
|
+
*/
|
|
102
|
+
async processInputsKey(inputs, visited) {
|
|
103
|
+
if (inputs && isObject(inputs)) {
|
|
104
|
+
for (const inputValue of Object.values(inputs)) {
|
|
105
|
+
if (isObject(inputValue) && isObject(inputValue.options)) {
|
|
106
|
+
const structures = inputValue.options?.structures;
|
|
107
|
+
const visitedLocal = visited || new Set();
|
|
108
|
+
if (structures && isObject(structures)) {
|
|
109
|
+
if (structures.values_from_glob) {
|
|
110
|
+
await this.importFromGlobKey(structures, 'values_from_glob', 'values', true, visitedLocal);
|
|
111
|
+
}
|
|
112
|
+
if (Array.isArray(structures.values)) {
|
|
113
|
+
await Promise.all(structures.values.map(async (value) => this.processStructureValue(value, visitedLocal)));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async processStructuresKey(structures, visited) {
|
|
121
|
+
if (!structures || !isObject(structures)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
await Promise.all(Object.values(structures).map(async (structure) => {
|
|
125
|
+
if (!isObject(structure)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const visitedLocal = visited || new Set();
|
|
129
|
+
await this.importFromGlobKey(structure, 'values_from_glob', 'values', true, visitedLocal);
|
|
130
|
+
if (structure.values && Array.isArray(structure.values)) {
|
|
131
|
+
await Promise.all(structure.values.map(async (value) => this.processStructureValue(value, visitedLocal)));
|
|
132
|
+
}
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
async processStructureValue(structureValue, visited) {
|
|
136
|
+
if (!structureValue || !isObject(structureValue)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (structureValue._inputs_from_glob) {
|
|
140
|
+
await this.importFromGlobKey(structureValue, '_inputs_from_glob', '_inputs', false, visited);
|
|
141
|
+
}
|
|
142
|
+
if (structureValue._inputs) {
|
|
143
|
+
await this.processInputsKey(structureValue._inputs, visited);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Processes a glob key by loading files and merging them with the target key.
|
|
148
|
+
* Supports recursive processing with cycle detection.
|
|
149
|
+
*/
|
|
150
|
+
async importFromGlobKey(config, globKey, targetKey, isArray, visited) {
|
|
151
|
+
const globs = config[globKey];
|
|
152
|
+
if (!Array.isArray(globs)) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
globs.forEach((pattern) => {
|
|
156
|
+
if (typeof pattern === 'string' && !pattern.startsWith('!')) {
|
|
157
|
+
this.globPatterns.add(pattern);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
const loadedDataWithSources = await this.loadFilesFromGlobs(globs, globKey, visited);
|
|
161
|
+
if (isArray) {
|
|
162
|
+
config[targetKey] = this.mergeArrayValues((config[targetKey] || []), loadedDataWithSources);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
config[targetKey] = this.mergeObjectValues((config[targetKey] || {}), loadedDataWithSources);
|
|
166
|
+
}
|
|
167
|
+
delete config[globKey];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Loads configuration data from files matching the given glob patterns.
|
|
171
|
+
* Returns an array of parsed configuration objects with source tracking.
|
|
172
|
+
*/
|
|
173
|
+
async loadFilesFromGlobs(globs, globKey, visited) {
|
|
174
|
+
const matchingFiles = this.findFilesMatchingGlobs(globs);
|
|
175
|
+
// Sort by path to ensure consistent ordering
|
|
176
|
+
matchingFiles.sort((a, b) => a.localeCompare(b));
|
|
177
|
+
const loadedData = [];
|
|
178
|
+
for (const filePath of matchingFiles) {
|
|
179
|
+
if (visited?.has(filePath)) {
|
|
180
|
+
this.warnings.cycles.push({
|
|
181
|
+
path: filePath,
|
|
182
|
+
chain: Array.from(visited),
|
|
183
|
+
});
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const existingGlobKey = this.pathsToGlobKey[filePath];
|
|
187
|
+
if (existingGlobKey && existingGlobKey !== globKey) {
|
|
188
|
+
this.warnings.multipleGlobKeys.push({
|
|
189
|
+
path: filePath,
|
|
190
|
+
type1: existingGlobKey,
|
|
191
|
+
type2: globKey,
|
|
192
|
+
});
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
this.pathsToGlobKey[filePath] = globKey;
|
|
196
|
+
this.globKeyToPaths[globKey].add(filePath);
|
|
197
|
+
visited?.add(filePath);
|
|
198
|
+
try {
|
|
199
|
+
const data = await this.loadConfigFile(filePath);
|
|
200
|
+
if (data !== undefined && data !== null) {
|
|
201
|
+
const clonedData = JSON.parse(JSON.stringify(data));
|
|
202
|
+
// Handle both single objects and arrays
|
|
203
|
+
if (Array.isArray(clonedData)) {
|
|
204
|
+
this.warnings.arrayValues.push({
|
|
205
|
+
path: filePath,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
loadedData.push({ data: clonedData, source: filePath });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// Log error but continue processing other files
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return loadedData;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Merges object values from globbed files.
|
|
221
|
+
* Later files overwrite earlier ones for the same keys.
|
|
222
|
+
* Adds __source__ metadata to track where each key came from.
|
|
223
|
+
*/
|
|
224
|
+
mergeObjectValues(base, loaded) {
|
|
225
|
+
const result = { ...base };
|
|
226
|
+
for (const { data: item, source } of loaded) {
|
|
227
|
+
if (isObject(item)) {
|
|
228
|
+
// For objects, merge keys (later overwrites earlier)
|
|
229
|
+
// Add __source__ to each key that gets added/overwritten
|
|
230
|
+
for (const [key, value] of Object.entries(item)) {
|
|
231
|
+
if (isObject(value)) {
|
|
232
|
+
// Add __source__ to nested objects (e.g., input definitions, schema configs)
|
|
233
|
+
result[key] = {
|
|
234
|
+
...value,
|
|
235
|
+
__source__: source,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
// For primitive values, just assign the value
|
|
240
|
+
// The source is tracked at the key level via __sources__ if needed
|
|
241
|
+
result[key] = value;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Merges array values from globbed files.
|
|
250
|
+
* Arrays are appended (no overwriting since arrays aren't keyed).
|
|
251
|
+
* Adds __source__ metadata to track where each item came from.
|
|
252
|
+
*/
|
|
253
|
+
mergeArrayValues(base, loaded) {
|
|
254
|
+
const result = [...base];
|
|
255
|
+
for (const { data: item, source } of loaded) {
|
|
256
|
+
if (Array.isArray(item)) {
|
|
257
|
+
// If the loaded item is an array, add source to each element
|
|
258
|
+
for (const element of item) {
|
|
259
|
+
if (isObject(element)) {
|
|
260
|
+
result.push({
|
|
261
|
+
...element,
|
|
262
|
+
__source__: source,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
// Otherwise, add it as a single item
|
|
267
|
+
// This cannot be tracked with __source__ since it's not an object
|
|
268
|
+
result.push(element);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else if (isObject(item)) {
|
|
273
|
+
// Add __source__ to object items
|
|
274
|
+
result.push({
|
|
275
|
+
...item,
|
|
276
|
+
__source__: source,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
// Otherwise, add it as a single item
|
|
281
|
+
// This cannot be tracked with __source__ since it's not an object
|
|
282
|
+
result.push(item);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function removeDuplicateWarnings(warnings) {
|
|
289
|
+
const result = {
|
|
290
|
+
cycles: [],
|
|
291
|
+
multipleGlobKeys: [],
|
|
292
|
+
arrayValues: [],
|
|
293
|
+
};
|
|
294
|
+
for (const warning of warnings.cycles) {
|
|
295
|
+
if (!result.cycles.some((w) => w.path === warning.path && w.chain.join(' → ') === warning.chain.join(' → '))) {
|
|
296
|
+
result.cycles.push(warning);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
for (const warning of warnings.multipleGlobKeys) {
|
|
300
|
+
if (!result.multipleGlobKeys.some((w) => w.path === warning.path && w.type1 === warning.type1 && w.type2 === warning.type2)) {
|
|
301
|
+
result.multipleGlobKeys.push(warning);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
for (const warning of warnings.arrayValues) {
|
|
305
|
+
if (!result.arrayValues.some((w) => w.path === warning.path)) {
|
|
306
|
+
result.arrayValues.push(warning);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Merges split CloudCannon configuration files into a single configuration object.
|
|
313
|
+
*
|
|
314
|
+
* This function processes `*_from_glob` keys in the configuration, loading and merging
|
|
315
|
+
* configuration from files matching the specified glob patterns. This allows you to
|
|
316
|
+
* split your CloudCannon configuration across multiple files for better organization.
|
|
317
|
+
*
|
|
318
|
+
* @param config - The base CloudCannon configuration object
|
|
319
|
+
* @param options - Options for loading configuration files
|
|
320
|
+
* @returns A promise resolving to the merged configuration with metadata
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* ```typescript
|
|
324
|
+
* import { mergeConfiguration } from '@cloudcannon/configuration-loader';
|
|
325
|
+
* import { globSync } from 'glob';
|
|
326
|
+
* import { readFileSync } from 'fs';
|
|
327
|
+
* import YAML from 'yaml';
|
|
328
|
+
*
|
|
329
|
+
* const baseConfig = YAML.parse(readFileSync('cloudcannon.config.yml', 'utf8'));
|
|
330
|
+
*
|
|
331
|
+
* const result = await mergeConfiguration(baseConfig, {
|
|
332
|
+
* findFilesMatchingGlobs: (globs) => globSync(globs, { ignore: globs.filter(g => g.startsWith('!')).map(g => g.slice(1)) }),
|
|
333
|
+
* loadConfigFile: async (filePath) => YAML.parse(readFileSync(filePath, 'utf8'))
|
|
334
|
+
* });
|
|
335
|
+
*
|
|
336
|
+
* console.log(result.config); // Merged configuration
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
export async function mergeConfiguration(config, options) {
|
|
340
|
+
const loader = new ConfigurationLoader(options.findFilesMatchingGlobs, options.loadConfigFile);
|
|
341
|
+
const result = await loader.mergeConfiguration(JSON.parse(JSON.stringify(config)));
|
|
342
|
+
return {
|
|
343
|
+
config: result,
|
|
344
|
+
warnings: removeDuplicateWarnings(loader.warnings),
|
|
345
|
+
globPatterns: Array.from(loader.globPatterns),
|
|
346
|
+
pathsToGlobKey: loader.pathsToGlobKey,
|
|
347
|
+
globKeyToPaths: loader.globKeyToPaths,
|
|
348
|
+
};
|
|
349
|
+
}
|
package/dist/types.d.ts
CHANGED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcannon/configuration-loader",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Load and merge CloudCannon configuration files",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,33 +10,31 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"import": {
|
|
12
12
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"default": "./
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"./browser": {
|
|
17
17
|
"import": {
|
|
18
18
|
"types": "./dist/browser.d.ts",
|
|
19
|
-
"default": "./
|
|
19
|
+
"default": "./dist/browser.js"
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
+
"main": "dist/index.js",
|
|
23
24
|
"types": "./dist/index.d.ts",
|
|
24
25
|
"files": [
|
|
25
|
-
"src",
|
|
26
26
|
"dist"
|
|
27
27
|
],
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"
|
|
33
|
-
"build:types": "tsc --emitDeclarationOnly",
|
|
34
|
-
"build": "npm run build:types",
|
|
32
|
+
"build": "tsc",
|
|
35
33
|
"test": "node --experimental-strip-types --test src/**/*.test.ts",
|
|
36
34
|
"test:watch": "node --experimental-strip-types --watch --test",
|
|
37
35
|
"test:coverage": "node --experimental-strip-types --experimental-test-coverage --test src/**/*.test.ts",
|
|
38
|
-
"lint": "biome check src/",
|
|
39
|
-
"lint:fix": "biome check --
|
|
36
|
+
"lint": "biome check src/ && tsc --noEmit",
|
|
37
|
+
"lint:fix": "biome check --fix src/",
|
|
40
38
|
"format": "biome format --write src/",
|
|
41
39
|
"prepublishOnly": "npm run build"
|
|
42
40
|
},
|
package/dist/browser.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAGtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGjD,YAAY,EACX,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,yBAAyB,GACzB,MAAM,YAAY,CAAC"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAGtF,OAAO,EAAE,KAAK,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAG7E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGjD,YAAY,EACX,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,yBAAyB,GACzB,MAAM,YAAY,CAAC"}
|
package/dist/load.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAA6B,MAAM,YAAY,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACxC;;;;;OAKG;IACH,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC/E;AAmDD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,iBAAiB,CACtC,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,wBAAwB,GAChC,OAAO,CAAC,UAAU,CAAC,CA+BrB"}
|
package/dist/loader.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAkB,MAAM,kCAAkC,CAAC;AACtF,OAAO,KAAK,EACX,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,yBAAyB,EACzB,MAAM,YAAY,CAAC;AAEpB,YAAY,EAAE,kBAAkB,EAAE,UAAU,EAAE,WAAW,EAAE,yBAAyB,EAAE,CAAC;AAwdvF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,kBAAkB,CACvC,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,yBAAyB,GAChC,OAAO,CAAC,UAAU,CAAC,CAarB"}
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,WAAW,GACpB,uBAAuB,GACvB,kBAAkB,GAClB,qBAAqB,GACrB,6BAA6B,GAC7B,+BAA+B,GAC/B,iCAAiC,GACjC,mBAAmB,GACnB,sBAAsB,GACtB,8BAA8B,GAC9B,mBAAmB,CAAC;AAEvB;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IAClC;;;;;;;;;;;;OAYG;IACH,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;IAE5C;;;;;;;;;;;;;;OAcG;IACH,gBAAgB,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,WAAW,CAAA;KAAE,EAAE,CAAC;IAE7E;;;;;;;;;;;OAWG;IACH,WAAW,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,sCAAsC;IACtC,MAAM,EAAE,aAAa,CAAC;IACtB,iEAAiE;IACjE,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,yDAAyD;IACzD,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,oEAAoE;IACpE,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC5C,uEAAuE;IACvE,cAAc,EAAE,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;CACjD;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACzC;;;;OAIG;IACH,sBAAsB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,CAAC;IACtD;;;OAGG;IACH,cAAc,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CACvD"}
|