Skip to content

Commit

Permalink
Close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi committed Mar 1, 2020
1 parent d6c022c commit a34e06c
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 30 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export declare function validate(configuration: any): void;
export declare function applyEnvVariables(configuration: any, envVariables: NodeJS.ProcessEnv, envPrefix?: string): void;
export declare function applyConfigFile(configuration: any, configFile: string): void;
export declare function applyCommandArgs(configuration: any, argv: string[]): void;
export declare function setDeepProperty(obj: any, propertyPath: string, value: any): void;
export declare function setDeepProperty(obj: {
[key: string]: any;
}, propertyPath: string, value: any): void;
export declare function getDeepProperty(obj: any, propertyPath: string): any;
export declare function objectsAreEqual(obj1: any, obj2: any, leftOnly?: boolean): boolean;
48 changes: 38 additions & 10 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 44 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ export function applyCommandArgs(configuration: any, argv: string[]) {

debug("Appling command arguments:", parsedArgv);

if (parsedArgv.config) {
const configFile = path.resolve(process.cwd(), parsedArgv.config);
const CONFIG_PROP = 'config';

if (parsedArgv[CONFIG_PROP]) {
const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
applyConfigFile(configuration, configFile);
}

Expand All @@ -85,6 +87,16 @@ export function applyCommandArgs(configuration: any, argv: string[]) {
continue;
}

if (key.startsWith('_')) {
continue;
}
if (key.endsWith('_')) {
continue;
}
if (key === CONFIG_PROP) {
continue;
}

const configKey = key
.replace(/_/g, ".");

Expand All @@ -95,24 +107,44 @@ export function applyCommandArgs(configuration: any, argv: string[]) {
}


export function setDeepProperty(obj: any, propertyPath: string, value: any): void {
const a = splitPath(propertyPath);
const n = a.length;
export function setDeepProperty(obj: {[key: string]: any}, propertyPath: string, value: any): void {
if (!obj) {
throw new Error("Invalid object");
}
if (!propertyPath) {
throw new Error("Invalid property path");
}

for (let i = 0; i < n - 1; i++) {
const k = a[i];
const pathParts = splitPath(propertyPath);
const pathPartsLen = pathParts.length;

for (let i = 0; i < pathPartsLen - 1; i++) {
const pathPart = pathParts[i];

if (!(k in obj)) {
obj[k] = {};
if (!(pathPart in obj)) {
setProp(obj, pathPart, {});
}
obj = obj[k];
obj = getProp(obj, pathPart);
}


obj[a[n - 1]] = value;
setProp(obj, pathParts[pathPartsLen - 1], value);
return;
}

function setProp(obj: {[key: string]: any}, property: string, value: any): void {
if (!obj.hasOwnProperty(property)) {
throw new Error(`Property '${property}' is not valid`);
}
obj[property] = value;
}

function getProp(obj: {[key: string]: any}, property: string): any {
if (!obj.hasOwnProperty(property)) {
throw new Error(`Property '${property}' is not valid`);
}
return obj[property];
}

export function getDeepProperty(obj: any, propertyPath: string): any {
let ret: any = obj;

Expand Down
4 changes: 4 additions & 0 deletions sample/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sample/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// See README.md for details

import * as confinit from "../index";
import * as path from "path";

Expand Down Expand Up @@ -37,11 +39,15 @@ export class Configuration {
if (!env) {
env = process.env;
}

// Enable config file
if (env.config) {
const configFile = path.resolve(process.cwd(), env.config);
confinit.applyConfigFile(this, configFile);
}
// Enable environment variables
confinit.applyEnvVariables(this, process.env, "cfg_");
// Enable command arguments
confinit.applyCommandArgs(this, process.argv);

confinit.validate(this);
Expand Down
Loading

0 comments on commit a34e06c

Please sign in to comment.