2

I checked here first before I come here. My question is a little different than that. I have following folder structure.

  • app.js
  • global.config.js
  • node-modules
    • my module
      • module.config.js
      • index.js

global.config.js:

var config = {};
config.loggingLevel: "error"; // global setting for all custom modules

module.config.js:

var config = {};
config.loggingLevel: "info"; // ideally should override global setting

index.js

var globalDir = path.dirname(require.main.filename) + "\\global.config.js";
var globalConfig = require(globalDir);
var moduleConfig = require("./module.config.js");
// merge configs here and use only one config object

As seen here, I have a global and module specific config files. I would like to merge them into one config file and use that way.

Is there any way to achieve this easily? (Like a pre-written module) Or should I iterate through each property and override if same key exists?

Community
  • 1
  • 1
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • 1
    please check this for merge objects http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – Krzysztof Sztompka Jan 14 '16 at 19:22
  • 1
    @KrzysztofSztompka this fixed my issue for now. If not exists, I will be thinking writing a module for this or add functionality to existing one. Thanks. – Teoman shipahi Jan 14 '16 at 19:33

1 Answers1

0

I wrote a module located under https://github.com/quimsy/espresso which contains a Config Tree Builder. Its (partly) a port of Symfony's Config Component.

You can define a configuration tree:

var TreeBuilder = require("Espresso/Config/Definition/Builder/TreeBuilder");
var builder = new TreeBuilder();
var root = builder.root("connection");

root
    .children()
        .scalarNode("host").defaultValue("localhost").end()
    .end();

And with a Processor You can merge multiple configurations respecting these rules:

var Processor = require("Espresso/Config/Definition/Processor");
var processor = new Processor();
var mergedConfiguration = processor.process( root.getNode(), [ config1, config2 ] );

Regarding the example above, You would do something like:

var builder = new (require("Espresso/Config/Definition/Builder/TreeBuilder")();
var root = builder.root("myConfigRoot");
/* some definitions */

var processor = new (require("Espresso/Config/Definition/Processor"))();
var config = processor.process( root.getNode(), [ globalConfig, moduleConfig ] );

Look at Defining and Processing Configuration Values, since this module is 90% compatible - some different naming conventions and taking respect to Array/Objects (in PHP there is only the Array type).

  • Please be patient. I just started using git. Let me know, if there are some issues. Im so excited :D – Potsdamer Schnitzel May 10 '16 at 14:47
  • is this supporting json configuration? Looks like it is made for working with YAML and XML. – Teoman shipahi May 10 '16 at 15:29
  • Config/Definition only deals with defining/processing configuration objects. You can just require a .json file and process it as described above. Loading is handled by Config/Loader Subclasses. I havent implement them, because I'm also satisfied with `require("some.json")` – Potsdamer Schnitzel May 10 '16 at 15:34
  • It would be great have this as separate module. Anyway I will pull out dependencies and give it a try. Since I am not using espresso framework, I do not want to pull out extra dependencies which I will not need. – Teoman shipahi May 10 '16 at 15:37