Socket
Socket
Sign inDemoInstall

@geek/config

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@geek/config - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

1

bin/cli.js

@@ -14,2 +14,3 @@ #!/usr/bin/env node

const config = new Config({ cwd: args[0], name: args[1], profiles: args[2], overrides: args[3], defaults: args[4] });
// const config = new Config({ cwd: args[0], name: args[1], profiles: args[2], overrides: args[3], defaults: args[4], files: [ '~/.titanium/config.json', '~/.config/@geek/mobile/config.json', '~/.titanium/config.json', '~/.tn.config' ] });
console.log(config.store);

@@ -16,0 +17,0 @@

138

index.js

@@ -6,4 +6,8 @@ 'use strict';

const _ = require('lodash');
const os = require('os');
const cacheSubDirName = '.';
// TODO: Implement json5, yaml, and yml
// const supportedFileExtensions = [ '.json5', '.yaml', '.yml', '.json' ];
const supportedFileExtensions = [ '.json' ];

@@ -13,5 +17,9 @@ class Config {

options = {
fileExtension: 'json',
deepMerge: false,
profiles: [],
deepMerge: false,
profiles: [],
files: [],
userFilenameSuffix: '.user',
projectFilenameSuffix: '.project',
globalFilenameSuffix: '.global',
systemFilenameSuffix: '.system',
...options,

@@ -31,5 +39,16 @@ };

this.name = options.name;
this.fileExtensions = options.fileExtensions || options.fileExtension || supportedFileExtensions;
if (_.isString(this.fileExtensions)) {
this.fileExtensions = this.fileExtensions.split(',').map(item => item.trim());
}
if (_.isString(options.files)) {
options.files = this.files.split(',').map(item => item.trim());
}
const globalConfigDirectory = getConfigDirectory();
const configFiles = [];
if (_.isString(options.profile)) {

@@ -65,5 +84,16 @@ options.profiles = options.profile.split(',').map(item => item.trim());

// specified file configs
_.forEach(options.files, file => {
const filePath = path.parse(file);
filePath.dir = filePath.dir.replace('~', os.homedir);
// console.error(`filePath: ${JSON.stringify(filePath, null, 2)}`);
addConfigFile(this.getConfigFile({ cwd: filePath.dir, filename: filePath.name, ext: filePath.exports }));
});
// console.error(`configFiles: ${JSON.stringify(configFiles, null, 2)}`);
// project user configs
_.forEach(options.profiles, profile => {
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}.user`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}${options.userFilenameSuffix}` }));
});

@@ -73,3 +103,3 @@

_.forEach(options.profiles, profile => {
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}.project`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}${options.projectFilenameSuffix}` }));
});

@@ -79,7 +109,7 @@

// user config
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.user`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}${options.userFilenameSuffix}` }));
// project config
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.project`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}${options.projectFilenameSuffix}` }));

@@ -89,9 +119,13 @@

_.forEach(options.profiles, profile => {
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.${profile}.global`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.${profile}${options.globalFilenameSuffix}` }));
});
// global config
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.global`, ext: options.fileExtension }));
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}${options.globalFilenameSuffix}` }));
// system config
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}${options.systemFilenameSuffix}` }));
if (options.defaults) {

@@ -128,3 +162,2 @@ if (_.isString(options.defaults)) {

const os = require('os');
const homedir = os.homedir();

@@ -140,3 +173,2 @@ const cacheDirName = path.join(homedir, cacheSubDirName, `.${options.name}`);

getConfigFile(options = {}) {
options = { ...options };

@@ -149,31 +181,67 @@ if (!options.cwd) {

}
if (!options.ext) {
throw new Error('options.ext is required.');
}
const filepath = path.join(options.cwd, `${options.filename}.${options.ext}`);
// console.error(`filepath: ${JSON.stringify(filepath, null, 2)}`);
if (! fs.pathExistsSync(filepath)) {
// console.error('you are here β†’ file not found');
return;
}
try {
let configFile;
// TODO: Add support for JSON5 and yaml
switch (options.ext) {
case 'json':
return fs.readJsonSync(filepath, 'utf8');
_.forEach(this.fileExtensions, ext => {
default:
throw Error(`extension not supported by config: ${options.ext}`);
const filepath = path.join(options.cwd, `${options.filename}${ext}`);
// console.error(`filepath: ${JSON.stringify(filepath, null, 2)}`);
if (! fs.pathExistsSync(filepath)) {
// console.error('you are here β†’ file not found');
return true;
}
} catch (error) {
if (error.code === 'ENOENT') {
return;
try {
// TODO: Add support for JSON5 and yaml
switch (ext) {
case '.json':
configFile = fs.readJsonSync(filepath, 'utf8');
break;
default:
throw Error(`file extension not supported by @geek/config: ${ext}`);
}
} catch (error) {
if (error.code === 'ENOENT') {
return true;
}
console.error(error);
throw error;
}
console.error(error);
throw error;
}
return false;
});
return configFile;
// _.forEach(supportedFileExtensions, ext => {
// const configFile = this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}.user`, ext: ext });
// if (configFile) {
// addConfigFile(configFile);
// return false;
// }
// });
// try {
// // TODO: Add support for JSON5 and yaml
// switch (options.ext) {
// case 'json':
// return fs.readJsonSync(filepath, 'utf8');
// default:
// throw Error(`extension not supported by config: ${options.ext}`);
// }
// } catch (error) {
// if (error.code === 'ENOENT') {
// return;
// }
// console.error(error);
// throw error;
// }
}

@@ -185,5 +253,5 @@

const env_var_name = `${this.name.toUpperCase()}_${key.toUpperCase().replace('.', '_')}`;
console.error(`env_var_name: ${JSON.stringify(env_var_name, null, 2)}`);
// console.error(`env_var_name: ${JSON.stringify(env_var_name, null, 2)}`);
const env_var = process.env[env_var_name];
console.error(`env_var: ${JSON.stringify(env_var, null, 2)}`);
// console.error(`env_var: ${JSON.stringify(env_var, null, 2)}`);
if (! _.isNil(env_var)) {

@@ -190,0 +258,0 @@ return env_var;

{
"name": "@geek/config",
"version": "0.1.1",
"version": "0.2.0",
"description": "Geek Configuration Manager for Node.js - The complete solution for managing config settings for your Node.js application",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -51,15 +51,114 @@ # @geek/config

| 2. | **overrides** | `{ "property1": "override-value" }` | parameter: `overrides` |
| 3. | **user profile config file** | `myapp.dev.user.json` | *project directory* |
| 4. | **user config file** | `myapp.user.json` | *project directory* |
| 5. | **project profile config file** | `myapp.dev.project.json` | *project directory* |
| 6. | **project config file** | `myapp.project.json` | *project directory* |
| 7. | **global profile config fil**e | `myapp.dev.global.json` | *home config directory* |
| 8. | **global config file** | `myapp.global.json` | *home config directory* |
| 9. | **default values** | `{ "property1": "default-value" }` | parameter: `defaults` |
| 4. | **files** | `['~/.titanium/config.json', '~/.tn.json']` | parameter: `files` |
| 5. | **user profile config file** | `myapp.dev.user.json` | *project directory* |
| 6. | **user config file** | `myapp.user.json` | *project directory* |
| 7. | **project profile config file** | `myapp.dev.project.json` | *project directory* |
| 8. | **project config file** | `myapp.project.json` | *project directory* |
| 9. | **global profile config fil**e | `myapp.dev.global.json` | *home config directory* |
| 10. | **global config file** | `myapp.global.json` | *home config directory* |
| 11. | **system config file** | `myapp.system.json` | *home config directory* |
| 12. | **default values** | `{ "property1": "default-value" }` | parameter: `defaults` |
## Examples
> **NOTE: Currently only the file extension `.json` is supported. Support for more file extensions will be added very soon.**
> if no parameters are passed in
### Parameters: name
```JavaScript
cost Config = require('@geek/config');
const config = new Config({ name: 'myapp' });
```
* `process.env variables` *(if exists)*
> Loaded objects fromm all the manually entered filenames:
* `files` *(from parameter )*
> One of the following:
* `myapp.user.json5` *(if exists)*
* `myapp.user.yaml` *(if exists)*
* `myapp.user.yml` *(if exists)*
* `myapp.user.json` *(if exists)*
> One of the following:
* `myapp.project.json5` *(if exists)*
* `myapp.project.yaml` *(if exists)*
* `myapp.project.yml` *(if exists)*
* `myapp.project.json` *(if exists)*
> One of the following:
* `~/.myapp/global.json5` *(if exists)*
* `~/.myapp/global.yaml` *(if exists)*
* `~/.myapp/global.yml` *(if exists)*
* `~/.myapp/global.json` *(if exists)*
> One of the following:
* `~/.myapp/system.json5` *(if exists)*
* `~/.myapp/system.yaml` *(if exists)*
* `~/.myapp/system.yml` *(if exists)*
* `~/.myapp/system.json` *(if exists)*
### Parameters: `name, overrides, defaults`
```JavaScript
cost Config = require('@geek/config');
const config = new Config({ name: 'myapp', overrides: { variable2: 'overrides' }, defaults: variable9: 'defaults' });
```
* `process.env variables` *(if exists)*
* `overrides` (from parameter)
> Loaded objects fromm all the manually entered filenames:
* `files` *(from parameter )*
> One of the following:
* `myapp.user.json5` *(if exists)*
* `myapp.user.yaml` *(if exists)*
* `myapp.user.yml` *(if exists)*
* `myapp.user.json` *(if exists)*
> One of the following:
* `myapp.project.json5` *(if exists)*
* `myapp.project.yaml` *(if exists)*
* `myapp.project.yml` *(if exists)*
* `myapp.project.json` *(if exists)*
> One of the following:
* `~/.myapp/global.json5` *(if exists)*
* `~/.myapp/global.yaml` *(if exists)*
* `~/.myapp/global.yml` *(if exists)*
* `~/.myapp/global.json` *(if exists)*
> One of the following:
* `~/.myapp/system.json5` *(if exists)*
* `~/.myapp/system.yaml` *(if exists)*
* `~/.myapp/system.yml` *(if exists)*
* `~/.myapp/system.json` *(if exists)*
> default values
* `defaults` (from parameter)
### No parameters are passed in
- process.env

@@ -66,0 +165,0 @@ - manual overrides

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc