Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

lxghtless/occams-conf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

occams-conf

A simple config module

npm version CircleCI codecov XO code style

Why Occam's Config?

  • Encourages Build Once Run Anywhere Node.js App Containers
  • No opinion of config file name or location
  • Load additional config files to merge properties
  • Load configs from https endpoints

previous versions

first things's first...

$ npm i occams-conf

Usage

load main config via occams settings

const config = require('occams-conf');

console.log(config.yourProp);

load another config merged with main

const config = require('occams-conf');
config.loadConfig({
    name: 'other-config.js',
    path: 'src'
});

console.log(config.yourProp);
console.log(config.otherProp);

load config from https url with loaderConfig

// example loaderConfig:
// {
//     "path": "https://raw.githubusercontent.com/lxghtless/occams-conf/v2.0.1/config.js"
// }
const config = require('occams-conf');

(async () => {
    await config.init();
    console.log(config.resourceUrl);
});

load config from https url with loadConfig method

const config = require('occams-conf');

(async () => {
    await config.loadConfig({
        path: 'https://raw.githubusercontent.com/lxghtless/occams-conf/v2.0.1/config.js'
    });
    console.log(config.resourceUrl);
});

occams-conf settings file (aka locatorConfig)

  • name (config file name) [String]
  • path (config path) [String]

Any of these will work and will resolve the first one found in this order.

occams.conf.json

{
    "name": "config.js",
    "path": "src"
}

occams.conf.js

const conf = {
    name: 'config.js',
    path: 'src'
};

module.exports = conf;

package.json

{
    "occams-conf": {
        "name": "config.js",
        "path": "src"
    }
}

default

  • name: config.js
  • path: $HOME directory if globally installed || cwd

Example config.js

module.exports = {
    port: process.env.TEST_PORT || 8011,
    name: process.env.TEST_NAME || 'starlord',
    resourceUrl: process.env.TEST_URL || 'https://reqres.in/api/mixtapes'
};

Windows Users

If an error resembling the following occurs, please check out the link below it.

PS C:\src\{projectfolder}> npm start

> file-sage@1.0.0 start C:\src\{projectfolder}
> node src/index.js

Error: ENOENT: no such file or directory, lstat 'C:\Users\{username}\AppData\Roaming\npm\node_modules'
    at Object.realpathSync (fs.js:1561:7)
    at Object.<anonymous> (C:\src\{projectfolder}\node_modules\is-installed-globally\index.js:8:29)
    at Module._compile (internal/modules/cjs/loader.js:1139:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
    at Module.load (internal/modules/cjs/loader.js:988:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1028:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at isGloballyInstalled (C:\src\{projectfolder}\node_modules\occams-conf\src\util.js:17:10)
    at getLocatorConfigPath (C:\src\{projectfolder}\node_modules\occams-conf\src\util.js:26:6) {
  errno: -4058,
  syscall: 'lstat',
  code: 'ENOENT',
  path: 'C:\\Users\\{username}\\AppData\\Roaming\\npm\\node_modules'
}

https://stackoverflow.com/questions/19874582/change-default-global-installation-directory-for-node-js-modules-in-windows