Skip to content

Commit a34e06c

Browse files
committed
Close #1
1 parent d6c022c commit a34e06c

10 files changed

+154
-30
lines changed

Diff for: index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export declare function validate(configuration: any): void;
66
export declare function applyEnvVariables(configuration: any, envVariables: NodeJS.ProcessEnv, envPrefix?: string): void;
77
export declare function applyConfigFile(configuration: any, configFile: string): void;
88
export declare function applyCommandArgs(configuration: any, argv: string[]): void;
9-
export declare function setDeepProperty(obj: any, propertyPath: string, value: any): void;
9+
export declare function setDeepProperty(obj: {
10+
[key: string]: any;
11+
}, propertyPath: string, value: any): void;
1012
export declare function getDeepProperty(obj: any, propertyPath: string): any;
1113
export declare function objectsAreEqual(obj1: any, obj2: any, leftOnly?: boolean): boolean;

Diff for: index.js

+38-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: index.ts

+44-12
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ export function applyCommandArgs(configuration: any, argv: string[]) {
7575

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

78-
if (parsedArgv.config) {
79-
const configFile = path.resolve(process.cwd(), parsedArgv.config);
78+
const CONFIG_PROP = 'config';
79+
80+
if (parsedArgv[CONFIG_PROP]) {
81+
const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
8082
applyConfigFile(configuration, configFile);
8183
}
8284

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

90+
if (key.startsWith('_')) {
91+
continue;
92+
}
93+
if (key.endsWith('_')) {
94+
continue;
95+
}
96+
if (key === CONFIG_PROP) {
97+
continue;
98+
}
99+
88100
const configKey = key
89101
.replace(/_/g, ".");
90102

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

97109

98-
export function setDeepProperty(obj: any, propertyPath: string, value: any): void {
99-
const a = splitPath(propertyPath);
100-
const n = a.length;
110+
export function setDeepProperty(obj: {[key: string]: any}, propertyPath: string, value: any): void {
111+
if (!obj) {
112+
throw new Error("Invalid object");
113+
}
114+
if (!propertyPath) {
115+
throw new Error("Invalid property path");
116+
}
101117

102-
for (let i = 0; i < n - 1; i++) {
103-
const k = a[i];
118+
const pathParts = splitPath(propertyPath);
119+
const pathPartsLen = pathParts.length;
120+
121+
for (let i = 0; i < pathPartsLen - 1; i++) {
122+
const pathPart = pathParts[i];
104123

105-
if (!(k in obj)) {
106-
obj[k] = {};
124+
if (!(pathPart in obj)) {
125+
setProp(obj, pathPart, {});
107126
}
108-
obj = obj[k];
127+
obj = getProp(obj, pathPart);
109128
}
110129

111-
112-
obj[a[n - 1]] = value;
130+
setProp(obj, pathParts[pathPartsLen - 1], value);
113131
return;
114132
}
115133

134+
function setProp(obj: {[key: string]: any}, property: string, value: any): void {
135+
if (!obj.hasOwnProperty(property)) {
136+
throw new Error(`Property '${property}' is not valid`);
137+
}
138+
obj[property] = value;
139+
}
140+
141+
function getProp(obj: {[key: string]: any}, property: string): any {
142+
if (!obj.hasOwnProperty(property)) {
143+
throw new Error(`Property '${property}' is not valid`);
144+
}
145+
return obj[property];
146+
}
147+
116148
export function getDeepProperty(obj: any, propertyPath: string): any {
117149
let ret: any = obj;
118150

Diff for: sample/index.js

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: sample/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: sample/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// See README.md for details
2+
13
import * as confinit from "../index";
24
import * as path from "path";
35

@@ -37,11 +39,15 @@ export class Configuration {
3739
if (!env) {
3840
env = process.env;
3941
}
42+
43+
// Enable config file
4044
if (env.config) {
4145
const configFile = path.resolve(process.cwd(), env.config);
4246
confinit.applyConfigFile(this, configFile);
4347
}
48+
// Enable environment variables
4449
confinit.applyEnvVariables(this, process.env, "cfg_");
50+
// Enable command arguments
4551
confinit.applyCommandArgs(this, process.argv);
4652

4753
confinit.validate(this);

0 commit comments

Comments
 (0)