@cloudcannon/configuration-loader 0.0.2
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/LICENSE +5 -0
- package/README.md +215 -0
- package/dist/browser.d.ts +20 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/load.d.ts +36 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/loader.d.ts +33 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
- package/src/browser.ts +30 -0
- package/src/index.ts +36 -0
- package/src/load.ts +125 -0
- package/src/loader.test.ts +528 -0
- package/src/loader.ts +525 -0
- package/src/types.ts +101 -0
package/src/load.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { globSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, resolve } from 'node:path';
|
|
4
|
+
import type { Configuration } from '@cloudcannon/configuration-types';
|
|
5
|
+
import { mergeConfiguration } from './loader.ts';
|
|
6
|
+
import type { GlobResult, MergeConfigurationOptions } from './types.ts';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Options for loadConfiguration.
|
|
10
|
+
*/
|
|
11
|
+
export interface LoadConfigurationOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Custom function to parse file contents. Can be sync or async.
|
|
14
|
+
* Defaults to JSON.parse for .json files, and js-yaml for .yml/.yaml files.
|
|
15
|
+
* If not provided and a YAML file is encountered, an error will be thrown
|
|
16
|
+
* suggesting to install the 'js-yaml' package.
|
|
17
|
+
*/
|
|
18
|
+
parseFile?: (contents: string, filePath: string) => unknown | Promise<unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Attempts to load the js-yaml package dynamically.
|
|
23
|
+
* Returns the load function if available, otherwise returns undefined.
|
|
24
|
+
*/
|
|
25
|
+
async function tryLoadYamlParser(): Promise<((content: string) => unknown) | undefined> {
|
|
26
|
+
try {
|
|
27
|
+
// Use a variable to prevent TypeScript from trying to resolve the module statically
|
|
28
|
+
const jsYamlModuleName = 'js-yaml';
|
|
29
|
+
const jsYaml = (await import(jsYamlModuleName)) as { load: (s: string) => unknown };
|
|
30
|
+
return jsYaml.load;
|
|
31
|
+
} catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a file parser that handles JSON and YAML files.
|
|
38
|
+
*/
|
|
39
|
+
async function createDefaultParser(): Promise<(contents: string, filePath: string) => unknown> {
|
|
40
|
+
const yamlParse = await tryLoadYamlParser();
|
|
41
|
+
|
|
42
|
+
return (contents: string, filePath: string): unknown => {
|
|
43
|
+
if (filePath.endsWith('.json')) {
|
|
44
|
+
return JSON.parse(contents);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) {
|
|
48
|
+
if (!yamlParse) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Cannot parse YAML file "${filePath}". Install js-yaml: npm install js-yaml`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return yamlParse(contents);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Try JSON first, then YAML if available
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(contents);
|
|
59
|
+
} catch {
|
|
60
|
+
if (yamlParse) {
|
|
61
|
+
return yamlParse(contents);
|
|
62
|
+
}
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Cannot parse file "${filePath}". Install js-yaml for YAML support: npm install js-yaml`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Loads and merges a CloudCannon configuration file with all its split configuration files.
|
|
72
|
+
*
|
|
73
|
+
* This is the simplest way to load CloudCannon configuration. It handles:
|
|
74
|
+
* - Reading the configuration file from disk
|
|
75
|
+
* - Parsing YAML and JSON files
|
|
76
|
+
* - Finding files matching glob patterns
|
|
77
|
+
* - Merging all configuration into a single object
|
|
78
|
+
*
|
|
79
|
+
* @param configPath - Path to the CloudCannon configuration file (e.g., 'cloudcannon.config.yml')
|
|
80
|
+
* @param options - Optional configuration options
|
|
81
|
+
* @returns A promise resolving to the merged configuration with metadata
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import { loadConfiguration } from '@cloudcannon/configuration-loader';
|
|
86
|
+
*
|
|
87
|
+
* const result = await loadConfiguration('cloudcannon.config.yml');
|
|
88
|
+
* console.log(result.config);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export async function loadConfiguration(
|
|
92
|
+
configPath: string,
|
|
93
|
+
options?: LoadConfigurationOptions
|
|
94
|
+
): Promise<GlobResult> {
|
|
95
|
+
const parseFile = options?.parseFile ?? (await createDefaultParser());
|
|
96
|
+
const absoluteConfigPath = resolve(configPath);
|
|
97
|
+
const configDir = dirname(absoluteConfigPath);
|
|
98
|
+
|
|
99
|
+
// Load and parse the base configuration file
|
|
100
|
+
const configContents = await readFile(absoluteConfigPath, 'utf8');
|
|
101
|
+
const config = (await parseFile(configContents, absoluteConfigPath)) as Configuration;
|
|
102
|
+
|
|
103
|
+
// Create the merge options with default implementations
|
|
104
|
+
const mergeOptions: MergeConfigurationOptions = {
|
|
105
|
+
// Note: globSync is used here because the interface is synchronous by design.
|
|
106
|
+
// Glob operations are typically fast (just directory traversal), while file
|
|
107
|
+
// reading (handled by loadConfigFile) is the slower I/O operation.
|
|
108
|
+
findFilesMatchingGlobs: (globs: string[]) => {
|
|
109
|
+
const resolvedGlobs = globs.map((g) => {
|
|
110
|
+
if (g.startsWith('!')) {
|
|
111
|
+
return `!${resolve(configDir, g.slice(1))}`;
|
|
112
|
+
}
|
|
113
|
+
return resolve(configDir, g);
|
|
114
|
+
});
|
|
115
|
+
return globSync(resolvedGlobs) as string[];
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
loadConfigFile: async (filePath: string) => {
|
|
119
|
+
const contents = await readFile(filePath, 'utf8');
|
|
120
|
+
return await parseFile(contents, filePath);
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return mergeConfiguration(config, mergeOptions);
|
|
125
|
+
}
|
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import type { Configuration } from '@cloudcannon/configuration-types';
|
|
4
|
+
import { mergeConfiguration } from './loader.ts';
|
|
5
|
+
|
|
6
|
+
describe('mergeConfiguration', () => {
|
|
7
|
+
it('returns config unchanged when no glob keys present', async () => {
|
|
8
|
+
const config: Configuration = {
|
|
9
|
+
collections_config: {
|
|
10
|
+
posts: { path: 'content/posts' },
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const result = await mergeConfiguration(config, {
|
|
15
|
+
findFilesMatchingGlobs: () => [],
|
|
16
|
+
loadConfigFile: async () => ({}),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
assert.deepEqual(result.config.collections_config, {
|
|
20
|
+
posts: { path: 'content/posts' },
|
|
21
|
+
});
|
|
22
|
+
assert.deepEqual(result.warnings.cycles, []);
|
|
23
|
+
assert.deepEqual(result.warnings.multipleGlobKeys, []);
|
|
24
|
+
assert.deepEqual(result.warnings.arrayValues, []);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('merges collections from glob patterns', async () => {
|
|
28
|
+
const config: Configuration = {
|
|
29
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const mockFiles: Record<string, unknown> = {
|
|
33
|
+
'/.cloudcannon/collections/posts.yml': {
|
|
34
|
+
posts: { path: 'content/posts', icon: 'event' },
|
|
35
|
+
},
|
|
36
|
+
'/.cloudcannon/collections/pages.yml': {
|
|
37
|
+
pages: { path: 'content/pages', icon: 'wysiwyg' },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const result = await mergeConfiguration(config, {
|
|
42
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
43
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
assert.ok(result.config.collections_config);
|
|
47
|
+
assert.equal(Object.keys(result.config.collections_config).length, 2);
|
|
48
|
+
assert.equal(
|
|
49
|
+
(result.config.collections_config as Record<string, { path?: string }>).posts?.path,
|
|
50
|
+
'content/posts'
|
|
51
|
+
);
|
|
52
|
+
assert.equal(
|
|
53
|
+
(result.config.collections_config as Record<string, { path?: string }>).pages?.path,
|
|
54
|
+
'content/pages'
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('merges inputs from glob patterns', async () => {
|
|
59
|
+
const config: Configuration = {
|
|
60
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml'],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const mockFiles: Record<string, unknown> = {
|
|
64
|
+
'/.cloudcannon/inputs/seo.yml': {
|
|
65
|
+
title: { type: 'text', label: 'Page Title' },
|
|
66
|
+
description: { type: 'textarea', label: 'Meta Description' },
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const result = await mergeConfiguration(config, {
|
|
71
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
72
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
assert.ok(result.config._inputs);
|
|
76
|
+
assert.ok('title' in result.config._inputs);
|
|
77
|
+
assert.ok('description' in result.config._inputs);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('tracks glob patterns for change detection', async () => {
|
|
81
|
+
const config: Configuration = {
|
|
82
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
83
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml'],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const result = await mergeConfiguration(config, {
|
|
87
|
+
findFilesMatchingGlobs: () => [],
|
|
88
|
+
loadConfigFile: async () => ({}),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
assert.deepEqual(
|
|
92
|
+
result.globPatterns.sort(),
|
|
93
|
+
['/.cloudcannon/collections/*.yml', '/.cloudcannon/inputs/*.yml'].sort()
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('excludes negation patterns from tracked glob patterns', async () => {
|
|
98
|
+
const config: Configuration = {
|
|
99
|
+
collections_config_from_glob: [
|
|
100
|
+
'/.cloudcannon/collections/*.yml',
|
|
101
|
+
'!/.cloudcannon/collections/draft.yml',
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const result = await mergeConfiguration(config, {
|
|
106
|
+
findFilesMatchingGlobs: () => [],
|
|
107
|
+
loadConfigFile: async () => ({}),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
assert.deepEqual(result.globPatterns, ['/.cloudcannon/collections/*.yml']);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('warns about array values in config files', async () => {
|
|
114
|
+
const config: Configuration = {
|
|
115
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const mockFiles: Record<string, unknown> = {
|
|
119
|
+
'/.cloudcannon/collections/invalid.yml': [{ posts: { path: 'content/posts' } }],
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const result = await mergeConfiguration(config, {
|
|
123
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
124
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
assert.equal(result.warnings.arrayValues.length, 1);
|
|
128
|
+
assert.equal(result.warnings.arrayValues[0]?.path, '/.cloudcannon/collections/invalid.yml');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('warns when same file matches multiple glob types', async () => {
|
|
132
|
+
const config: Configuration = {
|
|
133
|
+
collections_config_from_glob: ['/.cloudcannon/*.yml'],
|
|
134
|
+
_inputs_from_glob: ['/.cloudcannon/*.yml'],
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const mockFiles = {
|
|
138
|
+
'/.cloudcannon/config.yml': { test: { type: 'text' } },
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const result = await mergeConfiguration(config, {
|
|
142
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
143
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
assert.equal(result.warnings.multipleGlobKeys.length, 1);
|
|
147
|
+
assert.equal(result.warnings.multipleGlobKeys[0]?.path, '/.cloudcannon/config.yml');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('adds __source__ metadata to merged objects', async () => {
|
|
151
|
+
const config: Configuration = {
|
|
152
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml'],
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const mockFiles: Record<string, unknown> = {
|
|
156
|
+
'/.cloudcannon/inputs/seo.yml': {
|
|
157
|
+
title: { type: 'text' },
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const result = await mergeConfiguration(config, {
|
|
162
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
163
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
assert.ok(result.config._inputs);
|
|
167
|
+
const titleInput = result.config._inputs.title as Record<string, unknown>;
|
|
168
|
+
assert.equal(titleInput.__source__, '/.cloudcannon/inputs/seo.yml');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('tracks paths to glob keys for reverse lookup', async () => {
|
|
172
|
+
const config: Configuration = {
|
|
173
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const mockFiles: Record<string, unknown> = {
|
|
177
|
+
'/.cloudcannon/collections/posts.yml': {
|
|
178
|
+
posts: { path: 'content/posts' },
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const result = await mergeConfiguration(config, {
|
|
183
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
184
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
assert.equal(
|
|
188
|
+
result.pathsToGlobKey['/.cloudcannon/collections/posts.yml'],
|
|
189
|
+
'collections_config_from_glob'
|
|
190
|
+
);
|
|
191
|
+
assert.ok(
|
|
192
|
+
result.globKeyToPaths.collections_config_from_glob.has('/.cloudcannon/collections/posts.yml')
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('does not mutate the original config', async () => {
|
|
197
|
+
const config: Configuration = {
|
|
198
|
+
collections_config: {
|
|
199
|
+
existing: { path: 'content/existing' },
|
|
200
|
+
},
|
|
201
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const originalConfigString = JSON.stringify(config);
|
|
205
|
+
|
|
206
|
+
await mergeConfiguration(config, {
|
|
207
|
+
findFilesMatchingGlobs: () => ['/.cloudcannon/collections/posts.yml'],
|
|
208
|
+
loadConfigFile: async () => ({ posts: { path: 'content/posts' } }),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
assert.equal(JSON.stringify(config), originalConfigString);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('processes nested schemas_from_glob in collections', async () => {
|
|
215
|
+
const config: Configuration = {
|
|
216
|
+
collections_config: {
|
|
217
|
+
posts: {
|
|
218
|
+
path: 'content/posts',
|
|
219
|
+
schemas_from_glob: ['/.cloudcannon/schemas/posts/*.yml'],
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const mockFiles: Record<string, unknown> = {
|
|
225
|
+
'/.cloudcannon/schemas/posts/default.yml': {
|
|
226
|
+
default: { path: 'schemas/posts/default.md' },
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const result = await mergeConfiguration(config, {
|
|
231
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
232
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const postsConfig = result.config.collections_config?.posts as Record<string, unknown>;
|
|
236
|
+
assert.ok(postsConfig?.schemas);
|
|
237
|
+
assert.ok((postsConfig.schemas as Record<string, unknown>).default);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('handles file load errors gracefully', async () => {
|
|
241
|
+
const config: Configuration = {
|
|
242
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml'],
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const result = await mergeConfiguration(config, {
|
|
246
|
+
findFilesMatchingGlobs: () => ['/.cloudcannon/inputs/broken.yml'],
|
|
247
|
+
loadConfigFile: async () => {
|
|
248
|
+
throw new Error('File not found');
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// Should not throw and should return empty inputs
|
|
253
|
+
assert.ok(result.config);
|
|
254
|
+
assert.deepEqual(result.warnings.cycles, []);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('GlobResult types', () => {
|
|
259
|
+
it('has correct structure', async () => {
|
|
260
|
+
const result = await mergeConfiguration(
|
|
261
|
+
{},
|
|
262
|
+
{
|
|
263
|
+
findFilesMatchingGlobs: () => [],
|
|
264
|
+
loadConfigFile: async () => ({}),
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
// Type checking - these assertions verify the shape
|
|
269
|
+
assert.ok('config' in result);
|
|
270
|
+
assert.ok('warnings' in result);
|
|
271
|
+
assert.ok('globPatterns' in result);
|
|
272
|
+
assert.ok('pathsToGlobKey' in result);
|
|
273
|
+
assert.ok('globKeyToPaths' in result);
|
|
274
|
+
|
|
275
|
+
// Verify warnings structure
|
|
276
|
+
assert.ok(Array.isArray(result.warnings.cycles));
|
|
277
|
+
assert.ok(Array.isArray(result.warnings.multipleGlobKeys));
|
|
278
|
+
assert.ok(Array.isArray(result.warnings.arrayValues));
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('warnings', () => {
|
|
283
|
+
describe('cycles', () => {
|
|
284
|
+
it('warns when a structure value references a file already in the chain', async () => {
|
|
285
|
+
// Structure values loaded via values_from_glob can have _inputs
|
|
286
|
+
// If those _inputs have nested structures with values_from_glob
|
|
287
|
+
// that reference the same file, a cycle is detected
|
|
288
|
+
const config: Configuration = {
|
|
289
|
+
_structures: {
|
|
290
|
+
blocks: {
|
|
291
|
+
values_from_glob: ['/.cloudcannon/structures/*.yml'],
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// The structure value file contains an object that will be added to values[]
|
|
297
|
+
// It has _inputs with a nested structure that references the same file
|
|
298
|
+
const mockFiles: Record<string, unknown> = {
|
|
299
|
+
'/.cloudcannon/structures/hero.yml': {
|
|
300
|
+
label: 'Hero',
|
|
301
|
+
_inputs: {
|
|
302
|
+
nested_blocks: {
|
|
303
|
+
type: 'array',
|
|
304
|
+
options: {
|
|
305
|
+
structures: {
|
|
306
|
+
// This creates a cycle - the same file is already being processed
|
|
307
|
+
values_from_glob: ['/.cloudcannon/structures/*.yml'],
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const result = await mergeConfiguration(config, {
|
|
316
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
317
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
assert.equal(result.warnings.cycles.length, 1);
|
|
321
|
+
assert.equal(result.warnings.cycles[0]?.path, '/.cloudcannon/structures/hero.yml');
|
|
322
|
+
assert.ok(Array.isArray(result.warnings.cycles[0]?.chain));
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it('includes the reference chain in cycle warnings', async () => {
|
|
326
|
+
const config: Configuration = {
|
|
327
|
+
_structures: {
|
|
328
|
+
blocks: {
|
|
329
|
+
values_from_glob: ['/.cloudcannon/structures/a.yml'],
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const mockFiles: Record<string, unknown> = {
|
|
335
|
+
'/.cloudcannon/structures/a.yml': {
|
|
336
|
+
label: 'Block A',
|
|
337
|
+
_inputs: {
|
|
338
|
+
nested: {
|
|
339
|
+
type: 'array',
|
|
340
|
+
options: {
|
|
341
|
+
structures: {
|
|
342
|
+
values_from_glob: ['/.cloudcannon/structures/a.yml'],
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const result = await mergeConfiguration(config, {
|
|
351
|
+
findFilesMatchingGlobs: () => ['/.cloudcannon/structures/a.yml'],
|
|
352
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
assert.equal(result.warnings.cycles.length, 1);
|
|
356
|
+
assert.ok(result.warnings.cycles[0]?.chain.includes('/.cloudcannon/structures/a.yml'));
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('does not warn when different files reference each other without cycles', async () => {
|
|
360
|
+
const config: Configuration = {
|
|
361
|
+
_structures: {
|
|
362
|
+
blocks: {
|
|
363
|
+
values_from_glob: ['/.cloudcannon/structures/a.yml'],
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const mockFiles: Record<string, unknown> = {
|
|
369
|
+
'/.cloudcannon/structures/a.yml': {
|
|
370
|
+
label: 'Block A',
|
|
371
|
+
_inputs: {
|
|
372
|
+
nested: {
|
|
373
|
+
type: 'array',
|
|
374
|
+
options: {
|
|
375
|
+
structures: {
|
|
376
|
+
// References a different file - no cycle
|
|
377
|
+
values_from_glob: ['/.cloudcannon/structures/b.yml'],
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
'/.cloudcannon/structures/b.yml': {
|
|
384
|
+
label: 'Block B',
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const result = await mergeConfiguration(config, {
|
|
389
|
+
findFilesMatchingGlobs: (globs: string[]) => {
|
|
390
|
+
if (globs.includes('/.cloudcannon/structures/a.yml')) {
|
|
391
|
+
return ['/.cloudcannon/structures/a.yml'];
|
|
392
|
+
}
|
|
393
|
+
if (globs.includes('/.cloudcannon/structures/b.yml')) {
|
|
394
|
+
return ['/.cloudcannon/structures/b.yml'];
|
|
395
|
+
}
|
|
396
|
+
return [];
|
|
397
|
+
},
|
|
398
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
assert.equal(result.warnings.cycles.length, 0);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
describe('multipleGlobKeys', () => {
|
|
406
|
+
it('warns when a file is matched by different glob key types', async () => {
|
|
407
|
+
const config: Configuration = {
|
|
408
|
+
collections_config_from_glob: ['/.cloudcannon/shared/*.yml'],
|
|
409
|
+
_inputs_from_glob: ['/.cloudcannon/shared/*.yml'],
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const mockFiles = {
|
|
413
|
+
'/.cloudcannon/shared/config.yml': { test: { type: 'text' } },
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const result = await mergeConfiguration(config, {
|
|
417
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
418
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
assert.equal(result.warnings.multipleGlobKeys.length, 1);
|
|
422
|
+
assert.equal(result.warnings.multipleGlobKeys[0]?.path, '/.cloudcannon/shared/config.yml');
|
|
423
|
+
assert.equal(result.warnings.multipleGlobKeys[0]?.type1, 'collections_config_from_glob');
|
|
424
|
+
assert.equal(result.warnings.multipleGlobKeys[0]?.type2, '_inputs_from_glob');
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('does not warn when same file is matched by same glob key type multiple times', async () => {
|
|
428
|
+
const config: Configuration = {
|
|
429
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml', '/.cloudcannon/inputs/seo.yml'],
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const mockFiles = {
|
|
433
|
+
'/.cloudcannon/inputs/seo.yml': { title: { type: 'text' } },
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
const result = await mergeConfiguration(config, {
|
|
437
|
+
findFilesMatchingGlobs: () => ['/.cloudcannon/inputs/seo.yml'],
|
|
438
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
assert.equal(result.warnings.multipleGlobKeys.length, 0);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it('skips file processing after multipleGlobKeys warning', async () => {
|
|
445
|
+
const config: Configuration = {
|
|
446
|
+
collections_config_from_glob: ['/.cloudcannon/shared/*.yml'],
|
|
447
|
+
_inputs_from_glob: ['/.cloudcannon/shared/*.yml'],
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const mockFiles = {
|
|
451
|
+
'/.cloudcannon/shared/config.yml': { posts: { path: 'content/posts' } },
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
const result = await mergeConfiguration(config, {
|
|
455
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
456
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// File should only be processed for the first glob key type
|
|
460
|
+
assert.ok(result.config.collections_config);
|
|
461
|
+
assert.ok((result.config.collections_config as Record<string, unknown>).posts);
|
|
462
|
+
// Should not be added to _inputs since it was already matched
|
|
463
|
+
assert.ok(!result.config._inputs?.posts);
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
describe('arrayValues', () => {
|
|
468
|
+
it('warns when a config file contains an array instead of an object', async () => {
|
|
469
|
+
const config: Configuration = {
|
|
470
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const mockFiles: Record<string, unknown> = {
|
|
474
|
+
'/.cloudcannon/collections/invalid.yml': [
|
|
475
|
+
{ posts: { path: 'content/posts' } },
|
|
476
|
+
{ pages: { path: 'content/pages' } },
|
|
477
|
+
],
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
const result = await mergeConfiguration(config, {
|
|
481
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
482
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
assert.equal(result.warnings.arrayValues.length, 1);
|
|
486
|
+
assert.equal(result.warnings.arrayValues[0]?.path, '/.cloudcannon/collections/invalid.yml');
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('does not merge array config files into the result', async () => {
|
|
490
|
+
const config: Configuration = {
|
|
491
|
+
_inputs_from_glob: ['/.cloudcannon/inputs/*.yml'],
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const mockFiles: Record<string, unknown> = {
|
|
495
|
+
'/.cloudcannon/inputs/invalid.yml': [{ title: { type: 'text' } }],
|
|
496
|
+
'/.cloudcannon/inputs/valid.yml': { description: { type: 'textarea' } },
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
const result = await mergeConfiguration(config, {
|
|
500
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
501
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
// Valid file should be merged
|
|
505
|
+
assert.ok(result.config._inputs?.description);
|
|
506
|
+
// Invalid array file should be skipped (title would be in array[0].title)
|
|
507
|
+
assert.ok(!result.config._inputs?.title);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('warns for multiple array config files', async () => {
|
|
511
|
+
const config: Configuration = {
|
|
512
|
+
collections_config_from_glob: ['/.cloudcannon/collections/*.yml'],
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const mockFiles: Record<string, unknown> = {
|
|
516
|
+
'/.cloudcannon/collections/invalid1.yml': [{ a: 1 }],
|
|
517
|
+
'/.cloudcannon/collections/invalid2.yml': [{ b: 2 }],
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
const result = await mergeConfiguration(config, {
|
|
521
|
+
findFilesMatchingGlobs: () => Object.keys(mockFiles),
|
|
522
|
+
loadConfigFile: async (path: string) => mockFiles[path],
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
assert.equal(result.warnings.arrayValues.length, 2);
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
});
|