@jsnw/srv-utils 1.2.0 → 1.3.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.
@@ -37,10 +37,10 @@ class ConfigLoader {
37
37
  console.error(loadError.message);
38
38
  process.exit(1);
39
39
  }
40
- const [processedYaml, includeErrors] = loader.processIncludes((0, node_path_1.dirname)(loader.resolveFilePath(path)), yaml);
41
- if (includeErrors && includeErrors.length > 0) {
42
- for (const err of includeErrors)
43
- console.error(`$include error: ${err.message}`);
40
+ const [processedYaml, directiveErrors] = loader.processDirectives((0, node_path_1.dirname)(loader.resolveFilePath(path)), yaml);
41
+ if (directiveErrors && directiveErrors.length > 0) {
42
+ for (const err of directiveErrors)
43
+ console.error(`directive error: ${err.message}`);
44
44
  process.exit(1);
45
45
  }
46
46
  const [validatedYaml, validateError] = loader.validateYaml(processedYaml, schema);
@@ -105,7 +105,7 @@ class ConfigLoader {
105
105
  * @returns {ErrorResult<any, Error[]>}
106
106
  * @protected
107
107
  */
108
- processIncludes(mainYamlDirname, yaml) {
108
+ processDirectives(mainYamlDirname, yaml) {
109
109
  if (typeof yaml !== 'object')
110
110
  return yaml;
111
111
  const nodesToVisit = [yaml], errors = [];
@@ -113,32 +113,35 @@ class ConfigLoader {
113
113
  const node = nodesToVisit[i];
114
114
  if (typeof node !== 'object' || Array.isArray(node))
115
115
  continue;
116
- if (node['$include']) {
117
- const $includePaths = [];
118
- if (typeof node['$include'] === 'string') {
119
- $includePaths.push(node['$include']);
120
- }
121
- else if (Array.isArray(node['$include'])) {
122
- $includePaths.push(...(node['$include']
123
- .filter(v => v && typeof v === 'string' && v.trim() !== '')));
124
- }
125
- delete node['$include'];
126
- for (const path of $includePaths) {
127
- const [loadedYaml, loadError] = this.loadYamlFile(this.resolveIncludePath(mainYamlDirname, path));
128
- if (loadError) {
129
- errors.push(loadError);
116
+ const nodeFields = Object.keys(node);
117
+ for (const field of nodeFields) {
118
+ if (field === '$include') {
119
+ const $includePaths = [];
120
+ if (typeof node['$include'] === 'string') {
121
+ $includePaths.push(node['$include']);
122
+ }
123
+ else if (Array.isArray(node['$include'])) {
124
+ $includePaths.push(...(node['$include']
125
+ .filter(v => v && typeof v === 'string' && v.trim() !== '')));
130
126
  }
131
- else {
132
- for (const [k, v] of Object.entries(loadedYaml))
133
- node[k] = v;
127
+ delete node['$include'];
128
+ for (const path of $includePaths) {
129
+ const [loadedYaml, loadError] = this.loadYamlFile(this.resolveIncludePath(mainYamlDirname, path));
130
+ if (loadError) {
131
+ errors.push(loadError);
132
+ }
133
+ else {
134
+ for (const [k, v] of Object.entries(loadedYaml))
135
+ node[k] = v;
136
+ }
134
137
  }
135
138
  }
136
- }
137
- for (const k of Object.keys(node)) {
138
- if (Object.hasOwn(node, k)
139
- && typeof node[k] === 'object'
140
- && !Array.isArray(node[k]))
141
- nodesToVisit.push(node[k]);
139
+ else {
140
+ if (typeof node[field] === 'string' && /^\s*\$[A-Z0-9_]+$/.test(node[field]))
141
+ node[field] = process.env?.[node[field].trim().slice(1)] ?? '';
142
+ if (typeof node[field] === 'object' && !Array.isArray(node[field]))
143
+ nodesToVisit.push(node[field]);
144
+ }
142
145
  }
143
146
  }
144
147
  return [yaml, errors];
@@ -40,7 +40,7 @@ export declare class ConfigLoader {
40
40
  * @returns {ErrorResult<any, Error[]>}
41
41
  * @protected
42
42
  */
43
- protected processIncludes(mainYamlDirname: string, yaml: any): ErrorResult<any, Error[]>;
43
+ protected processDirectives(mainYamlDirname: string, yaml: any): ErrorResult<any, Error[]>;
44
44
  private resolveFilePath;
45
45
  /**
46
46
  * @param {string} fromDirname
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnw/srv-utils",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Server-side utilities for Node.js/TypeScript: tsconfig paths, Nest helpers, and config loading.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/types/index.d.ts",