如何使用 setModuleDefaults 设置整个配置对象?

问题描述

我的代码

//config/dev.json
{
    …
    …
    “testAttribute”: {
        “testValue”: “dev value”
    }
    …
    …
}

//config/customer/default.json
{
    “testAttribute”: {
        “testValue”: “customer value”
    }
}

//ServiceConfig.ts
import * as config from 'config';
import * as path from 'path';

function init(): void {
    const configDir = path.join(process.cwd(),`config${path.sep}customer`);
    const customerConfig = config.util.loadFileConfigs(configDir);
    const extended = config.util.extendDeep(JSON.parse(JSON.stringify(config)),JSON.parse(JSON.stringify(customerConfig)));
}

到目前为止,init() 中的所有内容都按预期工作,因此extended.testAttribute.testValue 确实等于“客户价值”。 从这里,我有两个问题:

  1. 如果我像这样在 init() 中继续代码
    config.util.setModuleDefaults('testAttribute',extended. testAttribute);
    const finalValue = config.get('testAttribute');

finalValue 等于“开发值”。即 setModuleDefaults 不起作用。我在这里做错了什么?为什么“testAttribute”没有被扩展覆盖?

  1. 如果我想要 setModuleDefaults 替换整个配置而不是特定属性怎么办?即,用整个扩展对象覆盖配置。我该怎么做?我应该在 setModuleDefaults 的第一个参数中提供什么?

(当我不知道客户在 customer/default.json 中添加了哪些属性的情况下是必需的,而我只想让所有客户属性无缝地覆盖 dev.json 的属性。)

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)