@cloudcannon/configuration-loader 0.0.12 → 0.0.14
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/README.md +3 -3
- package/dist/load.d.ts +2 -2
- package/dist/load.js +7 -7
- package/dist/loader.js +57 -15
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -37,10 +37,10 @@ That's it! The `loadConfiguration` function handles:
|
|
|
37
37
|
|
|
38
38
|
### YAML Support
|
|
39
39
|
|
|
40
|
-
For YAML files, install `
|
|
40
|
+
For YAML files, install `yaml`:
|
|
41
41
|
|
|
42
42
|
```bash
|
|
43
|
-
npm install
|
|
43
|
+
npm install yaml
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
The loader will automatically detect and use it.
|
|
@@ -49,7 +49,7 @@ The loader will automatically detect and use it.
|
|
|
49
49
|
|
|
50
50
|
We don't bundle a YAML parser because:
|
|
51
51
|
|
|
52
|
-
1. **You likely already have one** — most projects using CloudCannon configuration already have `
|
|
52
|
+
1. **You likely already have one** — most projects using CloudCannon configuration already have `yaml` or similar installed for their build tooling
|
|
53
53
|
2. **Keeps the package lightweight** — no unnecessary dependencies if you only use JSON configs
|
|
54
54
|
3. **Flexibility** — you can use any YAML parser via the `parseFile` option if you prefer a different one
|
|
55
55
|
|
package/dist/load.d.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type { GlobResult } from './types.ts';
|
|
|
5
5
|
export interface LoadConfigurationOptions {
|
|
6
6
|
/**
|
|
7
7
|
* Custom function to parse file contents. Can be sync or async.
|
|
8
|
-
* Defaults to JSON.parse for .json files, and
|
|
8
|
+
* Defaults to JSON.parse for .json files, and the 'yaml' package for .yml/.yaml files.
|
|
9
9
|
* If not provided and a YAML file is encountered, an error will be thrown
|
|
10
|
-
* suggesting to install the '
|
|
10
|
+
* suggesting to install the 'yaml' package.
|
|
11
11
|
*/
|
|
12
12
|
parseFile?: (contents: string, filePath: string) => unknown | Promise<unknown>;
|
|
13
13
|
}
|
package/dist/load.js
CHANGED
|
@@ -11,15 +11,15 @@ import { readFile } from 'node:fs/promises';
|
|
|
11
11
|
import { dirname, relative, resolve } from 'node:path';
|
|
12
12
|
import { mergeConfiguration } from './loader.js';
|
|
13
13
|
/**
|
|
14
|
-
* Attempts to load the
|
|
15
|
-
* Returns the
|
|
14
|
+
* Attempts to load the yaml package dynamically.
|
|
15
|
+
* Returns the parse function if available, otherwise returns undefined.
|
|
16
16
|
*/
|
|
17
17
|
async function tryLoadYamlParser() {
|
|
18
18
|
try {
|
|
19
19
|
// Use a variable to prevent TypeScript from trying to resolve the module statically
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
return
|
|
20
|
+
const yamlModuleName = 'yaml';
|
|
21
|
+
const yaml = (await import(__rewriteRelativeImportExtension(yamlModuleName)));
|
|
22
|
+
return yaml.parse;
|
|
23
23
|
}
|
|
24
24
|
catch {
|
|
25
25
|
return undefined;
|
|
@@ -36,7 +36,7 @@ async function createDefaultParser() {
|
|
|
36
36
|
}
|
|
37
37
|
if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) {
|
|
38
38
|
if (!yamlParse) {
|
|
39
|
-
throw new Error(`Cannot parse YAML file "${filePath}". Install
|
|
39
|
+
throw new Error(`Cannot parse YAML file "${filePath}". Install yaml: npm install yaml`);
|
|
40
40
|
}
|
|
41
41
|
return yamlParse(contents);
|
|
42
42
|
}
|
|
@@ -48,7 +48,7 @@ async function createDefaultParser() {
|
|
|
48
48
|
if (yamlParse) {
|
|
49
49
|
return yamlParse(contents);
|
|
50
50
|
}
|
|
51
|
-
throw new Error(`Cannot parse file "${filePath}". Install
|
|
51
|
+
throw new Error(`Cannot parse file "${filePath}". Install yaml for YAML support: npm install yaml`);
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
}
|
package/dist/loader.js
CHANGED
|
@@ -98,17 +98,41 @@ class ConfigurationLoader {
|
|
|
98
98
|
// Process recursive globs in _snippets (can have _inputs_from_glob)
|
|
99
99
|
if (config._snippets && isObject(config._snippets)) {
|
|
100
100
|
await this.importFromGlobKey(config._snippets, '_inputs_from_glob', '_inputs', false);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
await this.processSnippetsKey(config._snippets);
|
|
102
|
+
}
|
|
103
|
+
// Snippet templates are spread into snippets at runtime, so they share the snippet shape
|
|
104
|
+
if (config._snippets_templates && isObject(config._snippets_templates)) {
|
|
105
|
+
await this.processSnippetsKey(config._snippets_templates);
|
|
106
|
+
}
|
|
107
|
+
// Process nested globs in file_config entries
|
|
108
|
+
// Entries can have: _inputs_from_glob, _editables_from_glob, _structures_from_glob
|
|
109
|
+
if (Array.isArray(config.file_config)) {
|
|
110
|
+
for (const entry of config.file_config) {
|
|
111
|
+
if (isObject(entry)) {
|
|
112
|
+
await this.importFromGlobKey(entry, '_inputs_from_glob', '_inputs', false);
|
|
113
|
+
await this.importFromGlobKey(entry, '_editables_from_glob', '_editables', false);
|
|
114
|
+
await this.importFromGlobKey(entry, '_structures_from_glob', '_structures', false);
|
|
115
|
+
await this.processInputsKey(entry._inputs);
|
|
116
|
+
await this.processStructuresKey(entry._structures);
|
|
107
117
|
}
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
120
|
return config;
|
|
111
121
|
}
|
|
122
|
+
/*
|
|
123
|
+
* Process recursive globs in each snippet (or snippet template) entry.
|
|
124
|
+
* Snippets can have: _inputs_from_glob, _structures_from_glob
|
|
125
|
+
*/
|
|
126
|
+
async processSnippetsKey(snippets) {
|
|
127
|
+
for (const snippetConfig of Object.values(snippets)) {
|
|
128
|
+
if (isObject(snippetConfig)) {
|
|
129
|
+
await this.importFromGlobKey(snippetConfig, '_inputs_from_glob', '_inputs', false);
|
|
130
|
+
await this.importFromGlobKey(snippetConfig, '_structures_from_glob', '_structures', false);
|
|
131
|
+
await this.processInputsKey(snippetConfig._inputs);
|
|
132
|
+
await this.processStructuresKey(snippetConfig._structures);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
112
136
|
/*
|
|
113
137
|
* Process recursive globs in _inputs (can have _structures_from_glob)
|
|
114
138
|
* @param inputs - The _inputs key to process
|
|
@@ -118,20 +142,31 @@ class ConfigurationLoader {
|
|
|
118
142
|
if (inputs && isObject(inputs)) {
|
|
119
143
|
for (const inputValue of Object.values(inputs)) {
|
|
120
144
|
if (isObject(inputValue) && isObject(inputValue.options)) {
|
|
121
|
-
const
|
|
145
|
+
const options = inputValue.options;
|
|
122
146
|
const visitedLocal = visited || new Set();
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (Array.isArray(structures.values)) {
|
|
128
|
-
await Promise.all(structures.values.map(async (value) => this.processStructureValue(value, visitedLocal)));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
147
|
+
// Array and object inputs: options.structures
|
|
148
|
+
await this.processInputStructures(options.structures, visitedLocal);
|
|
149
|
+
// Mutable object inputs: options.entries.structures
|
|
150
|
+
await this.processInputStructures(options.entries?.structures, visitedLocal);
|
|
131
151
|
}
|
|
132
152
|
}
|
|
133
153
|
}
|
|
134
154
|
}
|
|
155
|
+
/*
|
|
156
|
+
* Process an inline structure on an input (options.structures or options.entries.structures).
|
|
157
|
+
* Structure references (strings) are skipped as they point at _structures, which is processed separately.
|
|
158
|
+
*/
|
|
159
|
+
async processInputStructures(structures, visited) {
|
|
160
|
+
if (!structures || !isObject(structures)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (structures.values_from_glob) {
|
|
164
|
+
await this.importFromGlobKey(structures, 'values_from_glob', 'values', true, visited);
|
|
165
|
+
}
|
|
166
|
+
if (Array.isArray(structures.values)) {
|
|
167
|
+
await Promise.all(structures.values.map(async (value) => this.processStructureValue(value, visited)));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
135
170
|
async processStructuresKey(structures, visited) {
|
|
136
171
|
if (!structures || !isObject(structures)) {
|
|
137
172
|
return;
|
|
@@ -157,6 +192,13 @@ class ConfigurationLoader {
|
|
|
157
192
|
if (structureValue._inputs) {
|
|
158
193
|
await this.processInputsKey(structureValue._inputs, visited);
|
|
159
194
|
}
|
|
195
|
+
// Structure values can scope their own _structures for the inputs they contain
|
|
196
|
+
if (structureValue._structures_from_glob) {
|
|
197
|
+
await this.importFromGlobKey(structureValue, '_structures_from_glob', '_structures', false, visited);
|
|
198
|
+
}
|
|
199
|
+
if (structureValue._structures) {
|
|
200
|
+
await this.processStructuresKey(structureValue._structures, visited);
|
|
201
|
+
}
|
|
160
202
|
}
|
|
161
203
|
/**
|
|
162
204
|
* Processes a glob key by loading files and merging them with the target key.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcannon/configuration-loader",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "Load and merge CloudCannon configuration files",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -52,19 +52,19 @@
|
|
|
52
52
|
},
|
|
53
53
|
"homepage": "https://cloudcannon.com/documentation/articles/why-split-your-configuration-file/",
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@cloudcannon/configuration-types": "0.0.
|
|
55
|
+
"@cloudcannon/configuration-types": "0.0.64"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"
|
|
58
|
+
"yaml": "^2.0.0"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
61
|
-
"
|
|
61
|
+
"yaml": {
|
|
62
62
|
"optional": true
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@biomejs/biome": "2.5.
|
|
67
|
-
"@types/node": "26.
|
|
66
|
+
"@biomejs/biome": "2.5.14",
|
|
67
|
+
"@types/node": "26.6.3",
|
|
68
68
|
"typescript": "7.0.2"
|
|
69
69
|
}
|
|
70
70
|
}
|