feat: add replaceEnvVarsInArray function and integrate it into server transport configuration (#241)

Co-authored-by: samanhappy@qq.com <my6051199>
This commit is contained in:
samanhappy
2025-07-22 23:24:04 +08:00
committed by GitHub
parent 69afb865c0
commit bc3c8facfa
2 changed files with 35 additions and 10 deletions

View File

@@ -89,17 +89,42 @@ export const getSettingsCacheInfo = (): { hasCache: boolean } => {
};
};
export const replaceEnvVars = (env: Record<string, any>): Record<string, any> => {
const res: Record<string, string> = {};
for (const [key, value] of Object.entries(env)) {
if (typeof value === 'string') {
res[key] = expandEnvVars(value);
} else {
res[key] = String(value);
export function replaceEnvVars(input: Record<string, any>): Record<string, any>;
export function replaceEnvVars(input: string[] | undefined): string[];
export function replaceEnvVars(input: string): string;
export function replaceEnvVars(
input: Record<string, any> | string[] | string | undefined,
): Record<string, any> | string[] | string {
// Handle object input
if (input && typeof input === 'object' && !Array.isArray(input)) {
const res: Record<string, string> = {};
for (const [key, value] of Object.entries(input)) {
if (typeof value === 'string') {
res[key] = expandEnvVars(value);
} else {
res[key] = String(value);
}
}
return res;
}
return res;
};
// Handle array input
if (Array.isArray(input)) {
return input.map((item) => expandEnvVars(item));
}
// Handle string input
if (typeof input === 'string') {
return expandEnvVars(input);
}
// Handle undefined/null array input
if (input === undefined || input === null) {
return [];
}
return input;
}
export const expandEnvVars = (value: string): string => {
if (typeof value !== 'string') {

View File

@@ -183,7 +183,7 @@ const createTransportFromConfig = (name: string, conf: ServerConfig): any => {
transport = new StdioClientTransport({
command: conf.command,
args: conf.args,
args: replaceEnvVars(conf.args) as string[],
env: env,
stderr: 'pipe',
});