Any suggestions on how to create a cli to read and write a json configuration file object style....

75 views
Skip to first unread message

Dino Lopez

unread,
May 6, 2014, 3:14:54 PM5/6/14
to nod...@googlegroups.com
I like to create a nodejs cli program that allow me to create a simple json configuration file.
 
This configuration file will have the definition of the Application.
 
{
  "SiteName": "My NodeJS Site",
  "Look and Feel": {
    "Theme": "Business 1",
    "Skin": "Blue",
    "Layout": "1H-3C-1F"
  },
  "Pages": {
    "Home": {
      "Region-1": {
        "Acitve": "Yes"
      },
      "Region-2": {
        "Active": "Yes"
      },
      "Region-3": {
        "Active": "No"
      }
    },
    "Products": {
      "Region-1": {
        "Acitve": "Yes"
      },
      "Region-2": {
        "Active": "No"
      },
      "Region-3": {
        "Active": "No"
      }
    },
    "Services": {
      "Region-1": {
        "Acitve": "Yes"
      },
      "Region-2": {
        "Active": "Yes"
      }
    },
    "About Us": {
      "Region-1": {
        "Acitve": "Yes"
      },
      "Region-2": {
        "Active": "No"
      }
    }
  }
}
 
 
NOTE This JSON was created with: [ http://www.jsoneditoronline.org/ ]
 
So basically, Like to have a cli program able to read the file and let me setup the configuration of the site, add ages, configure regions just like if I'm setting up objects and at the end save everything in a json format with later will be used to generate all the site.
 
The regions will be feed data from another file where the text file can be stored or a reference to a dynamic List from a datase is called, perhaps some XML object /map or third party plugin.
 
What would be the best recommendation to achieve this ?
 

Dino Lopez

unread,
May 6, 2014, 5:54:58 PM5/6/14
to nod...@googlegroups.com
I guess what I'm looking is somehting like https://www.npmjs.org/package/prompt is providing, to allow me interactively setup the values of the variables for my web application and save all this in a json file to be read later by a builder which will create the web application.
 
The main goal is to have a very simple text file to propagate and re-use building webapps. simple to edit with a compehensive and consistant API. and once ready run it to the builder which will be some other project similar to grunt.
 
Any ideas on how to achieve will be appreciate it, trying to have something similar to the Cisco CLI for configuring Routers, but this will configure web apps and build the file. 

Branden Visser

unread,
May 6, 2014, 6:53:35 PM5/6/14
to nod...@googlegroups.com
It sounds really adhoc, you might just need to write most of it.

IIUC Cisco CLI is an interactive stateful prompt, so these tools might
help you out:

node-readcommand [1] will give you a stateful prompt loop where users
can input commands. It will parse that user input into an args array
like argv (sorry this is a shameless plug, but I don't know of any
other modules that will achieve this)

minimist [2] can be used to parse those argv arrays into options. Or
yargs [3] if you want fancy help outputs and validation and if you
think you have a chance at understand the README :)

As for loading, modifying and saving the JSON file, I can't think of
anything else to help you there aside from the internal Node.js APIs.

Hope that helps,
Branden

[1] https://www.npmjs.org/package/readcommand
[2] https://www.npmjs.org/package/minimist
[3] https://www.npmjs.org/package/yargs
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> To post to this group, send email to nod...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/375314f4-aa0f-452a-850a-77eb20739715%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Dino Lopez

unread,
May 6, 2014, 10:47:59 PM5/6/14
to nod...@googlegroups.com
It does perfect sense Branden, thank you so much for your feedback will look into this.

Best Regards Dino.

Alex Kocharin

unread,
May 7, 2014, 2:53:20 AM5/7/14
to nod...@googlegroups.com
 
Suggestion 1:
 
Don't write generators. Generic text editors are by far more powerful then request-response kind of programs.
 
Will you allow you users to return to previous item when they submit it? Will you allow to skip a couple of them? Will you allow users to correct their mistakes using search/replace? If you do that in generator, it will be way too complex. But text editors already can do any of this out of the box.
 
So just write a generic commented config file which people can just edit in their favorite text editor.
 
 
Suggestion 2:
 
Screw JSON.
 
This is an equivalent document written in YAML. By "equivalent" I mean it have exactly the same meaning (which you can check in https://nodeca.github.io/js-yaml/ ), but much shorter and it can be written by hand like I just did.
 
---
SiteName: My NodeJS Site

Look and Feel:
  Theme: Business 1
  Skin: Blue
  Layout: 1H-3C-1F
Pages:
  Home:
    Region-1: &ACTIVE {Active: 'Yes'}
    Region-2: *ACTIVE
    Region-3: &INACTIVE {Active: 'No'}
  Products:
    Region-1: *ACTIVE
    Region-2: *INACTIVE
    Region-3: *INACTIVE
  Services:
    Region-1: *ACTIVE
    Region-2: *ACTIVE
  About Us:
    Region-1: *ACTIVE
    Region-2: *INACTIVE
...
 
 
06.05.2014, 23:35, "Dino Lopez" <bernardi...@gmail.com>:
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/515766be-8e15-4678-b869-fd18cefb94d4%40googlegroups.com.

Dino Lopez

unread,
May 9, 2014, 4:08:15 PM5/9/14
to nod...@googlegroups.com, al...@kocharin.ru
I like suggestion 2... simple is good. and will focus on read the content of the YAML file [ https://www.npmjs.org/package/node-yaml-config ] then parse and run the generator using those parameters.
 
Perhaps some checks to make sure there is not something previously build and need to be left alone, delete or created depending on the changes of the main configuration File.
 
Thank you Alex.
 
Brains R Like books only work when they are open.
Reply all
Reply to author
Forward
0 new messages