@jayfong/x-server 2.97.0 → 2.98.1

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.
@@ -120,33 +120,54 @@ class EnvUtil {
120
120
  }
121
121
  return file;
122
122
  }
123
- static parseContent(src) {
123
+ static parseContent(src, _envItemMap, _envValueMap) {
124
124
  const envs = [];
125
125
  const envObj = (0, _yaml.parse)(src);
126
+ const inlineVars = value => {
127
+ if (Array.isArray(value)) {
128
+ return value.map(inlineVars);
129
+ }
130
+ if ((0, _vtils.isPlainObject)(value)) {
131
+ return Object.keys(value).reduce((acc, key) => {
132
+ acc[key] = inlineVars(value[key]);
133
+ return acc;
134
+ }, {});
135
+ }
136
+ if (typeof value === 'string') {
137
+ return value.replace(/\{\{\s*(.+?)\s*\}\}/g, (_, key) => (0, _vtils.get)(_envValueMap || envObj, key) || process.env[key] || '');
138
+ }
139
+ return value;
140
+ };
126
141
  Object.keys(envObj).forEach(key => {
127
- envs.push({
142
+ const value = inlineVars(envObj[key]);
143
+ const envItem = {
128
144
  key: key,
129
- value: envObj[key],
145
+ value: value,
130
146
  comment: src.match(new RegExp(`((#\\s*(.+?)\\s*[\\r\\n]+)+)\\s*${(0, _vtils.escapeRegExp)(key)}:`))?.[1].split(/(\r\n){2,}|\r{2,}|\n{2,}/g).pop()?.replace(/^#/gm, '').trim() || ''
131
- });
147
+ };
148
+ if (_envItemMap) {
149
+ _envItemMap[key] = envItem;
150
+ }
151
+ if (_envValueMap) {
152
+ _envValueMap[key] = value;
153
+ }
154
+ envs.push(envItem);
132
155
  });
133
156
  return envs;
134
157
  }
135
158
  static async parseFile(options) {
136
- const envMap = {};
159
+ const envItemMap = {};
160
+ const envValueMap = {};
137
161
  for (const file of options.file) {
138
162
  const envYmlFile = _nodePath.default.join(options.cwd, `${file}.yml`);
139
163
  const envYamlFile = _nodePath.default.join(options.cwd, `${file}.yaml`);
140
164
  const envFile = (await _fsExtra.default.pathExists(envYmlFile)) ? envYmlFile : (await _fsExtra.default.pathExists(envYamlFile)) ? envYamlFile : '';
141
165
  if (envFile) {
142
166
  const envContent = await _fsExtra.default.readFile(envFile, 'utf-8');
143
- const envs = EnvUtil.parseContent(envContent);
144
- envs.forEach(env => {
145
- envMap[env.key] = env;
146
- });
167
+ EnvUtil.parseContent(envContent, envItemMap, envValueMap);
147
168
  }
148
169
  }
149
- return Object.values(envMap);
170
+ return Object.values(envItemMap);
150
171
  }
151
172
  static async parseFileAsMap(options) {
152
173
  const envsObj = {
@@ -18,7 +18,7 @@ export declare class EnvUtil {
18
18
  cb: (channel: string | undefined) => any;
19
19
  }): Promise<void>;
20
20
  static getFile(env?: string, channel?: string, noBase?: boolean): string[];
21
- static parseContent(src: string): ParsedEnv[];
21
+ static parseContent(src: string, _envItemMap?: Record<string, ParsedEnv>, _envValueMap?: Record<string, any>): ParsedEnv[];
22
22
  static parseFile(options: {
23
23
  cwd: string;
24
24
  file: string[];
@@ -2,7 +2,7 @@ import path from 'node:path';
2
2
  import fs from 'fs-extra';
3
3
  import globby from 'globby';
4
4
  import { pinyin } from 'pinyin-pro';
5
- import { asyncLimit, dedent, difference, escapeRegExp, isPlainObject, uniq } from 'vtils';
5
+ import { asyncLimit, dedent, difference, escapeRegExp, get, isPlainObject, uniq } from 'vtils';
6
6
  import { parse as yamlParse } from 'yaml';
7
7
  export class EnvUtil {
8
8
  static async getAllChannel(cwd, excludeChannels) {
@@ -115,33 +115,54 @@ export class EnvUtil {
115
115
  }
116
116
  return file;
117
117
  }
118
- static parseContent(src) {
118
+ static parseContent(src, _envItemMap, _envValueMap) {
119
119
  const envs = [];
120
120
  const envObj = yamlParse(src);
121
+ const inlineVars = value => {
122
+ if (Array.isArray(value)) {
123
+ return value.map(inlineVars);
124
+ }
125
+ if (isPlainObject(value)) {
126
+ return Object.keys(value).reduce((acc, key) => {
127
+ acc[key] = inlineVars(value[key]);
128
+ return acc;
129
+ }, {});
130
+ }
131
+ if (typeof value === 'string') {
132
+ return value.replace(/\{\{\s*(.+?)\s*\}\}/g, (_, key) => get(_envValueMap || envObj, key) || process.env[key] || '');
133
+ }
134
+ return value;
135
+ };
121
136
  Object.keys(envObj).forEach(key => {
122
- envs.push({
137
+ const value = inlineVars(envObj[key]);
138
+ const envItem = {
123
139
  key: key,
124
- value: envObj[key],
140
+ value: value,
125
141
  comment: src.match(new RegExp(`((#\\s*(.+?)\\s*[\\r\\n]+)+)\\s*${escapeRegExp(key)}:`))?.[1].split(/(\r\n){2,}|\r{2,}|\n{2,}/g).pop()?.replace(/^#/gm, '').trim() || ''
126
- });
142
+ };
143
+ if (_envItemMap) {
144
+ _envItemMap[key] = envItem;
145
+ }
146
+ if (_envValueMap) {
147
+ _envValueMap[key] = value;
148
+ }
149
+ envs.push(envItem);
127
150
  });
128
151
  return envs;
129
152
  }
130
153
  static async parseFile(options) {
131
- const envMap = {};
154
+ const envItemMap = {};
155
+ const envValueMap = {};
132
156
  for (const file of options.file) {
133
157
  const envYmlFile = path.join(options.cwd, `${file}.yml`);
134
158
  const envYamlFile = path.join(options.cwd, `${file}.yaml`);
135
159
  const envFile = (await fs.pathExists(envYmlFile)) ? envYmlFile : (await fs.pathExists(envYamlFile)) ? envYamlFile : '';
136
160
  if (envFile) {
137
161
  const envContent = await fs.readFile(envFile, 'utf-8');
138
- const envs = EnvUtil.parseContent(envContent);
139
- envs.forEach(env => {
140
- envMap[env.key] = env;
141
- });
162
+ EnvUtil.parseContent(envContent, envItemMap, envValueMap);
142
163
  }
143
164
  }
144
- return Object.values(envMap);
165
+ return Object.values(envItemMap);
145
166
  }
146
167
  static async parseFileAsMap(options) {
147
168
  const envsObj = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.97.0",
3
+ "version": "2.98.1",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",