使用默认文件中的策略覆盖YAML中的设置?

时间:2018-01-11 19:13:53

标签: node.js yaml node-config

experienceList:
  - dbAnalyst: &dbAnalyst
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex: &webDevXetex
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'
# ...
experienceStrategies:
  - defaultExperience: &defaultExperience
    - <<: *dbAnalyst
    - <<: *webDevXetex
  - webDevExperience: &webDevExperience
    - <<: *webDevXetex
#...
experience: *defaultExperience

配置/ default.yml

这导致experience在YAML中输出为:

experience: 
  - dbAnalyst:
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

现在我要做的是,保留config/default.yml中的默认值并覆盖它们在覆盖文件中选择不同的策略(上面的experienceStrategies:之一),类似于以下内容:

experience: *webDevExperience

配置/ override.yml

希望产生以下结果:

experience: 
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

...

但是,使用node-config库我最终会出错;有没有办法在yaml和node.js中使用其他一些库或坚持使用node-config?我已经看到另一个名为node-config-yaml的库允许您包含其他yaml文件,但不确定是否允许您以node-config的方式覆盖设置。

/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:933
    throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3);
    ^

Error: Cannot parse config file: '/home/leeand00/Documents/lifehacker organized/docs/projdir/config/company_job.yml'
: YAMLException: unidentified alias "webDevExperience" at line 14, column 30:
    experience: *webDevExperience
                                 ^
    at util.parseFile (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:
933:11)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:702:28
    at Array.forEach (native)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:698:14
    at Array.forEach (native)
    at util.loadFileConfigs (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/conf
ig.js:697:13)
    at new Config (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:122:
27)
    at Object.<anonymous> (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config
.js:1767:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

1 个答案:

答案 0 :(得分:0)

使用node-config-yml库实际上做了这个伎俩,尽管它在这个问题上的文档很少,但它完美无缺。

它似乎将配置分别放在内存中的一个文件中。您可以使用别名和node-config中的合并功能,并根据需要进行覆盖。