UNPKG

config-sets

Version:
193 lines (182 loc) 8.26 kB
<!DOCTYPE html> <html> <head> <title>Config Sets</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" /> <script crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism.min.css" /> <script crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js"></script> <style> #list-headers ul { list-style: none; padding-left: .5em; } #list-headers > ul { padding: 0; } #list-headers h1, h2, h3, h4, h5 { white-space: nowrap; } .markdown-body { padding-left: 2em; } @media (min-width: 992px) { .vh-lg-100{ height: 100vh !important; } } </style> </head> <body> <div class="row w-100"> <div class="col-12 text-center"> <h1 id="config-sets">Config Sets</h1> <p>Easy app configure in real time.</p> <p>This manual is also available in <a href="https://manuel-lohmus.github.io/config-sets/README.html">HTML5</a>.</p> </div> </div> <div class="row w-100"> <div class="col-lg-3 d-lg-inline"> <div class="sticky-top overflow-auto vh-lg-100"> <div id="list-headers" class="list-group mt-2 ms-lg-2 ms-4"> <h4 id="table-of-contents">Table of contents</h4> <ul> <li><a href="#introduction"><strong>Introduction</strong></a></li> <li><a href="#installation"><strong>Installation</strong></a></li> <li><a href="#usage"><strong>Usage</strong></a> <ul> <li><a href="#basic-usage">Basic Usage</a></li> <li><a href="#command-line-arguments">Command-Line Arguments</a></li> <li><a href="#watching-for-changes">Watching for Changes</a></li> </ul> </li> <li><a href="#testing"><strong>Testing</strong></a></li> <li><a href="#api"><strong>API</strong></a> <ul> <li><a href="#main-function">Main Function</a></li> <li><a href="#static-properties">Static Properties</a></li> <li><a href="#helper-functions">Helper Functions</a></li> </ul> </li> <li><a href="#license"><strong>License</strong></a></li> </ul> </div> </div> </div> <div class="col-lg-9 mt-2"> <div class="ps-4 markdown-body" data-bs-spy="scroll" data-bs-target="#list-headers" data-bs-offset="0" tabindex="0"> <h2 id="introduction">Introduction</h2> <p>This Node.js module manages configuration settings by reading from and writing to a <code>config-sets.json</code> file. It handles command-line arguments and watches for changes to the configuration file. It allows you to create applications that can be configured in real time. This module is part of the <a href="https://www.npmjs.com/package/conextra">'conextra'</a> framework, which is a simple and easy-to-use single-page application (SPA) framework. You have to try it! A different solution than MVC (model–view–controller).</p> <blockquote> <p>Please note, this version is not backward compatible with version 2.x<br> Please note that JSON string is not 100% compatible.<br> It has been extended to allow for incremental updates of JSON files.<br> Added the ability to include metadata and comments.<br> Parsing of JSON files is enabled.</p> </blockquote> <h2 id="installation">Installation</h2> <p>To install the module, use npm:</p> <p><code>npm install config-sets</code></p> <h2 id="usage">Usage</h2> <h3 id="basic-usage">Basic Usage</h3> <p>To use the module, require it in your script and call the <code>configSets</code> function:</p> <pre><code class="language-javascript"> const configSets = require('config-sets'); const config = configSets({ '-metadata-key1': ' key1 comment ', key1: 'value1', '-metadata-key2': ' key2 comment ', key2: 'value2' }); const moduleConfig = configSets('moduleName', { key1: 'value1', key2: 'value2' }); console.log(config); config.on('key1', function (ev) { console.log(ev); return true; }); </code></pre> <p>file: <code>config-sets.json</code></p> <pre><code class="language-javascript">{ &quot;isProduction&quot;: true, &quot;production&quot;: { /* key1 comment */ &quot;key1&quot;: &quot;value1&quot;, /* key2 comment */ &quot;key2&quot;: &quot;value2&quot;, &quot;moduleName&quot;: { &quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot; } }, &quot;development&quot;: {} } </code></pre> <h3 id="command-line-arguments">Command-Line Arguments</h3> <p>You can pass configuration settings via command-line arguments:</p> <p><code>node index.js --key1=value1 --key2=value2</code></p> <p><code>node index.js --help</code></p> <h3 id="watching-for-changes">Watching for Changes</h3> <p>The module watches the <code>config-sets.json</code> file for changes and updates the configuration settings accordingly.<br> This uses the <a href="https://www.npmjs.com/package/data-context">'data-context'</a> module. Read more about how to use it.</p> <pre><code class="language-javascript">config.on('key1', function (ev) { console.log(ev); return true; }); </code></pre> <h2 id="testing">Testing</h2> <p>You can test <code>config-sets</code> on your system using this command:</p> <p><code>node ./node_modules/config-sets/index.test</code></p> <p>or in the <code>config-sets</code> project directory:</p> <p><code>npm test</code></p> <h2 id="api">API</h2> <h3 id="main-function">Main Function</h3> <h4 id="configsetsconfigmodulename-defaultconfigsettings"><code>configSets(configModuleName, defaultConfigSettings)</code></h4> <ul> <li><code>configModuleName</code> (optional): The name of the module to set or update configuration settings for.</li> <li><code>defaultConfigSettings</code>: The default configuration settings.</li> </ul> <p>Returns the current configuration settings.</p> <h3 id="static-properties">Static Properties</h3> <ul> <li><strong><code>configSets.isProduction</code></strong>: Indicates if the environment is production.</li> <li><strong><code>configSets.production</code></strong>: Contains production-specific configuration settings.</li> <li><strong><code>configSets.development</code></strong>: Contains development-specific configuration settings.</li> <li><strong><code>configSets.enableFileReadWrite</code></strong> (default: true): Determines if changes to the configuration should be saved automatically.</li> </ul> <h3 id="helper-functions">Helper Functions</h3> <h4 id="assigntarget-source-overwritechanges"><code>assign(target, source, overwriteChanges)</code></h4> <ul> <li><code>target</code>: The target object to merge properties into.</li> <li><code>source</code>: The source object containing properties to merge.</li> <li><code>overwriteChanges</code> (optional): A boolean indicating whether to overwrite existing properties in the target object.</li> </ul> <p>Merges source objects into a target object.</p> <h4 id="arg_options"><code>arg_options()</code></h4> <p>Parses command-line arguments into an object.</p> <p>Returns an object containing the parsed command-line arguments.</p> <h4 id="print_help"><code>print_help()</code></h4> <p>Prints the help message.</p> <h2 id="license">License</h2> <p>This project is licensed under the MIT License.</p> <p>Copyright © Manuel Lõhmus</p> <p><a href="https://www.paypal.com/donate?hosted_button_id=ESBQQHBB9LVWC"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Donate" /></a></p> <p>Donations are welcome and will go towards further development of this project.</p> <br> <br> <br> </div> </div> </div> <script> (function () { 'use strict'; var isIE = !!document.documentMode; // Detect IE if (!isIE) { // list-group style for headers document.querySelectorAll('#list-headers a') .forEach(function (a) { a.classList.add('list-group-item', 'list-group-item-action') }); } })(); </script> </body> </html>