@geek/config
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -13,3 +13,3 @@ #!/usr/bin/env node | ||
const Config = require('..'); | ||
const config = new Config({ cwd: args[0], name: args[1], env: args[2], overrides: args[3] }); | ||
const config = new Config({ cwd: args[0], name: args[1], profiles: args[2], overrides: args[3], defaults: args[4] }); | ||
console.log(config.store); | ||
@@ -16,0 +16,0 @@ |
101
index.js
@@ -6,3 +6,2 @@ 'use strict'; | ||
const _ = require('lodash'); | ||
const plainObject = () => Object.create(null); | ||
@@ -16,2 +15,3 @@ const cacheSubDirName = '.'; | ||
deepMerge: false, | ||
profiles: [], | ||
...options, | ||
@@ -31,3 +31,12 @@ }; | ||
this.name = options.name; | ||
const globalConfigDirectory = getConfigDirectory(); | ||
const configFiles = []; | ||
if (_.isString(options.profile)) { | ||
options.profiles = options.profile.split(',').map(item => item.trim()); | ||
} else if (_.isString(options.profiles)) { | ||
options.profiles = options.profiles.split(',').map(item => item.trim()); | ||
} | ||
if (options.overrides) { | ||
@@ -41,33 +50,71 @@ if (_.isString(options.overrides)) { | ||
} | ||
} else if (typeof options.overrides !== 'object') { | ||
} else if (!_.isObject(options.overrides)) { | ||
throw new Error(`options.overrides is not an object: ${typeof options.overrides}`); | ||
} | ||
configFiles.push(options.overrides); | ||
} | ||
const projectConfig = this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.project`, ext: options.fileExtension }); | ||
const userConfig = this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.user`, ext: options.fileExtension }); | ||
const userEnvConfig = options.env ? this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${options.env}.user`, ext: options.fileExtension, env: options.env }) : plainObject; | ||
const projectEnvConfig = options.env ? this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${options.env}.project`, ext: options.fileExtension, env: options.env }) : plainObject; | ||
const addConfigFile = configFile => { | ||
if (_.isObject(configFile)) { | ||
configFiles.push(configFile); | ||
} | ||
}; | ||
const globalConfigDirectory = getConfigDirectory(); | ||
console.error(`options: ${JSON.stringify(options, null, 2)}`); | ||
const globalConfig = this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.global`, ext: options.fileExtension }); | ||
const globalEnvConfig = options.env ? this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.${options.env}.global`, ext: options.fileExtension }) : plainObject; | ||
// project user configs | ||
_.forEach(options.profiles, profile => { | ||
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}.user`, ext: options.fileExtension })); | ||
}); | ||
// project profile configs | ||
_.forEach(options.profiles, profile => { | ||
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.${profile}.project`, ext: options.fileExtension })); | ||
}); | ||
// console.error(`projectConfig: ${JSON.stringify(projectConfig, null, 2)}`); | ||
// console.error(`userConfig: ${JSON.stringify(userConfig, null, 2)}`); | ||
// console.error(`userEnvConfig: ${JSON.stringify(userEnvConfig, null, 2)}`); | ||
// console.error(`projectEnvConfig: ${JSON.stringify(projectEnvConfig, null, 2)}`); | ||
// console.error(`globalConfig: ${JSON.stringify(globalConfig, null, 2)}`); | ||
// console.error(`globalEnvConfig: ${JSON.stringify(globalEnvConfig, null, 2)}`); | ||
// user config | ||
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.user`, ext: options.fileExtension })); | ||
// project config | ||
addConfigFile(this.getConfigFile({ cwd: options.cwd, filename: `${options.name}.project`, ext: options.fileExtension })); | ||
// global profile configs | ||
_.forEach(options.profiles, profile => { | ||
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.${profile}.global`, ext: options.fileExtension })); | ||
}); | ||
// global config | ||
addConfigFile(this.getConfigFile({ cwd: globalConfigDirectory, filename: `${options.name}.global`, ext: options.fileExtension })); | ||
if (options.defaults) { | ||
if (_.isString(options.defaults)) { | ||
try { | ||
//TODO: Add support for yaml and JSON5 | ||
options.defaults = JSON.parse(options.defaults); | ||
} catch (err) { | ||
throw new Error(`Cannot parse options.defaults: ${err.message}`); | ||
} | ||
} else if (!_.isObject(options.defaults)) { | ||
throw new Error(`options.defaults is not an object: ${typeof options.defaults}`); | ||
} | ||
configFiles.push(options.defaults); | ||
} | ||
console.error(`configFiles: ${JSON.stringify(configFiles, null, 2)}`); | ||
const finalConfig = {}; | ||
if (options.deepMerge) { | ||
_.merge(finalConfig, globalConfig, globalEnvConfig, projectConfig, projectEnvConfig, userConfig, userEnvConfig, options.overrides); | ||
_.merge(finalConfig, ...configFiles.reverse()); | ||
} else { | ||
_.defaults(finalConfig, options.overrides, userEnvConfig, userConfig, projectEnvConfig, projectConfig, globalEnvConfig, globalConfig); | ||
_.defaults(finalConfig, ...configFiles); | ||
} | ||
// console.error(`finalConfig: ${JSON.stringify(finalConfig, null, 2)}`); | ||
console.error(`finalConfig: ${JSON.stringify(finalConfig, null, 2)}`); | ||
@@ -89,6 +136,3 @@ this.store = finalConfig; | ||
getConfigFile(options = {}) { | ||
options = { | ||
env: '', | ||
...options, | ||
}; | ||
options = { ...options }; | ||
@@ -104,12 +148,7 @@ if (!options.cwd) { | ||
} | ||
// let filename = `${options.name}.${options.type}`; | ||
// options.env && (filename += `.${options.env}`); | ||
// filename += `.${options.ext}`; | ||
const filepath = path.join(options.cwd, `${options.filename}.${options.ext}`); | ||
// console.error(`filepath: ${JSON.stringify(filepath, null, 2)}`); | ||
console.error(`filepath: ${JSON.stringify(filepath, null, 2)}`); | ||
if (! fs.pathExistsSync(filepath)) { | ||
// console.error('you are here → file not found'); | ||
return plainObject; | ||
console.error('you are here → file not found'); | ||
return; | ||
} | ||
@@ -129,3 +168,3 @@ | ||
if (error.code === 'ENOENT') { | ||
return plainObject; | ||
return; | ||
} | ||
@@ -132,0 +171,0 @@ console.error(error); |
{ | ||
"name": "@geek/config", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Geek Configuration Manager for Node.js - The complete solution for managing config settings for your Node.js application", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node ./bin/cli.js ./tests/test1-json test1 dev '{ \"property1\": \"overrides\", \"property2\": \"overrides\"}'", | ||
"test": "node ./bin/cli.js ./tests/test1-json test1 'ios, dev' '{ \"property1\": \"overrides\", \"property2\": \"overrides\"}' '{ \"property8\": \"defaults\", \"property9\": \"defaults\"}'", | ||
"list-files": "npm pack && tar -xvzf *.tgz && rm -rf package *.tgz" | ||
@@ -9,0 +9,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9390
151
0