diff --git a/src/api.mjs b/src/api.mjs index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_c3JjL2FwaS5tanM=..d96f365173d5830c9984042e6b221b88ed8d0c89_c3JjL2FwaS5tanM= 100644 --- a/src/api.mjs +++ b/src/api.mjs @@ -52,7 +52,16 @@ if (globalOptions.simpleMode) { return getNamespace(undefined).exposeProxy[key] } - return getNamespace(key)?.exposeProxy + const ns = getNamespace(key) + if (!ns) { + return undefined + } + // if the namespace is a scalar, return it directly + if (typeof ns.value?.valueOf() !== 'object') { + return ns.value?.valueOf() + } + // otherwise, return a proxy on the value + return ns.exposeProxy }, getOwnPropertyDescriptor (_, key) { const { configurable, enumerable, writable } = Object.getOwnPropertyDescriptor(_, key) ?? { diff --git a/src/configuration.mjs b/src/configuration.mjs index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_c3JjL2NvbmZpZ3VyYXRpb24ubWpz..d96f365173d5830c9984042e6b221b88ed8d0c89_c3JjL2NvbmZpZ3VyYXRpb24ubWpz 100644 --- a/src/configuration.mjs +++ b/src/configuration.mjs @@ -123,7 +123,7 @@ addTask (task) { if (!this.options.allowAsync) { - throw new Error(`The configuration for ns ${this.ns || '#'} is not allowing async tasks!`) + throw new Error(`The configuration for ns ${this.ns || '@'} is not allowing async tasks!`) } const promise = task().then(() => this.tasks.delete(promise)) this.tasks.add(promise) @@ -147,10 +147,10 @@ getLocalEnv () { const lastEnv = this.environments[this.environments.length - 1] if (lastEnv.name !== 'local') { - throw new Error(`local env seems to be missing in namespace ${this.ns || '#'}!`) + throw new Error(`local env seems to be missing in namespace ${this.ns || '@'}!`) } return lastEnv } setProperty (key, value) { if (this.options.readOnly) { @@ -151,10 +151,10 @@ } return lastEnv } setProperty (key, value) { if (this.options.readOnly) { - throw new Error(`Impossible to write a value in config ${this.ns || '#'}: it is in read-only mode!`) + throw new Error(`Impossible to write a value in config ${this.ns || '@'}: it is in read-only mode!`) } this.getLocalEnv().setProperty(key, value) if (this.options.autoSave) { @@ -211,7 +211,7 @@ get exposeProxy () { if (!this.isReady && this.options.throwIfNotReady) { - throw new Error(`You should not access the ${this.ns || '#'} configuration: it is not ready yet!`) + throw new Error(`You should not access the ${this.ns || '@'} configuration: it is not ready yet!`) } return exposeProxy(this) } diff --git a/src/load.mjs b/src/load.mjs index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_c3JjL2xvYWQubWpz..d96f365173d5830c9984042e6b221b88ed8d0c89_c3JjL2xvYWQubWpz 100644 --- a/src/load.mjs +++ b/src/load.mjs @@ -16,7 +16,5 @@ ns = undefined } - const camelNs = _.camelCase(ns) || undefined - if (!ns && !options.simpleMode) { options.autoload.forEach(ns => { @@ -21,7 +19,7 @@ if (!ns && !options.simpleMode) { options.autoload.forEach(ns => { - load(ns, genericToConfigurationOpts(camelNs, options)) + load(ns, genericToConfigurationOpts(_.camelCase(ns), options)) }) return } @@ -24,7 +22,9 @@ }) return } + const camelNs = ns && _.camelCase(ns) + if (options === globalOptions) { options = genericToConfigurationOpts(camelNs, options) } diff --git a/src/schemas.mjs b/src/schemas.mjs index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_c3JjL3NjaGVtYXMubWpz..d96f365173d5830c9984042e6b221b88ed8d0c89_c3JjL3NjaGVtYXMubWpz 100644 --- a/src/schemas.mjs +++ b/src/schemas.mjs @@ -5,7 +5,7 @@ import { configTags } from './tags/all.mjs' const NAME_RE = /^[a-zA-Z_][\w-]*$/ -const NS_RE = /^#|(?:[a-zA-Z_][\w-]*)$/ +const NS_RE = /^@|(?:[a-zA-Z_][\w-]*)$/ const simpleMode = Joi.boolean() .default(false) @@ -123,6 +123,6 @@ export const genericToConfigurationOpts = (ns, options) => { const { simpleMode, autoload, ...configOpts } = options - configOpts.resolve = configOpts.resolve?.[ns || '#'] || {} + configOpts.resolve = configOpts.resolve?.[ns || '@'] || {} return configOpts } diff --git a/test/index.spec.js b/test/index.spec.js index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9pbmRleC5zcGVjLmpz..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9pbmRleC5zcGVjLmpz 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -27,5 +27,5 @@ const createSubGroups = (testFiles, pos = 0) => _(testFiles) - .groupBy(file => dirname(dirname(file)).split(sep)[pos] || '#' + basename(dirname(file))) + .groupBy(file => dirname(dirname(file)).split(sep)[pos] || '@' + basename(dirname(file))) .mapValues( @@ -31,5 +31,5 @@ .mapValues( - (group, key) => key[0] === '#' + (group, key) => key[0] === '@' ? group.map(test => loadTest(test)) : createSubGroups(group, pos + 1), ) @@ -108,7 +108,7 @@ const goOverTests = tests => _.forEach( tests, (group, key) => - key[0] === '#' + key[0] === '@' ? addTests(type, describeName(key.substr(1)), group) : describe(describeName(key), () => goOverTests(group)), ) diff --git a/test/load/options/11-resolve/1-change-filename.spec.yaml b/test/load/options/11-resolve/1-change-filename.spec.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS8xLWNoYW5nZS1maWxlbmFtZS5zcGVjLnlhbWw= --- /dev/null +++ b/test/load/options/11-resolve/1-change-filename.spec.yaml @@ -0,0 +1,12 @@ +it: should allow to change the loaded filename for one environment only +start: + autoload: + - first + - second + resolve: + second: + default: modified.yaml +res: + first: first + second: + modified: yes diff --git a/test/load/options/11-resolve/2-change-default-ns-filename.spec.yaml b/test/load/options/11-resolve/2-change-default-ns-filename.spec.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS8yLWNoYW5nZS1kZWZhdWx0LW5zLWZpbGVuYW1lLnNwZWMueWFtbA== --- /dev/null +++ b/test/load/options/11-resolve/2-change-default-ns-filename.spec.yaml @@ -0,0 +1,8 @@ +it: 'should allow to change the loaded filename for one environment only in simpleMode using @' +start: + simpleMode: true + resolve: + @: + default: modified.yaml +res: + modified: yes diff --git a/test/load/options/11-resolve/3-change-folder.spec.yaml b/test/load/options/11-resolve/3-change-folder.spec.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS8zLWNoYW5nZS1mb2xkZXIuc3BlYy55YW1s --- /dev/null +++ b/test/load/options/11-resolve/3-change-folder.spec.yaml @@ -0,0 +1,11 @@ +it: should allow to change the folder for loading one environment only +start: + autoload: + - first + - second + resolve: + second: + default: ./sub +res: + first: first + second: in sub diff --git a/test/load/options/11-resolve/4-change-folder-and-filename.spec.yaml b/test/load/options/11-resolve/4-change-folder-and-filename.spec.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS80LWNoYW5nZS1mb2xkZXItYW5kLWZpbGVuYW1lLnNwZWMueWFtbA== --- /dev/null +++ b/test/load/options/11-resolve/4-change-folder-and-filename.spec.yaml @@ -0,0 +1,11 @@ +it: should allow to change both the folder and filename for loading one environment only +start: + autoload: + - first + - second + resolve: + second: + default: ./sub/modified.yaml +res: + first: first + second: modified in sub diff --git a/test/load/options/11-resolve/first.yaml b/test/load/options/11-resolve/first.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS9maXJzdC55YW1s --- /dev/null +++ b/test/load/options/11-resolve/first.yaml @@ -0,0 +1,1 @@ +first diff --git a/test/load/options/11-resolve/modified.yaml b/test/load/options/11-resolve/modified.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS9tb2RpZmllZC55YW1s --- /dev/null +++ b/test/load/options/11-resolve/modified.yaml @@ -0,0 +1,1 @@ +modified: yes diff --git a/test/load/options/11-resolve/sub/modified.yaml b/test/load/options/11-resolve/sub/modified.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS9zdWIvbW9kaWZpZWQueWFtbA== --- /dev/null +++ b/test/load/options/11-resolve/sub/modified.yaml @@ -0,0 +1,1 @@ +modified in sub diff --git a/test/load/options/11-resolve/sub/second.yaml b/test/load/options/11-resolve/sub/second.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMTEtcmVzb2x2ZS9zdWIvc2Vjb25kLnlhbWw= --- /dev/null +++ b/test/load/options/11-resolve/sub/second.yaml @@ -0,0 +1,1 @@ +in sub diff --git a/test/load/options/3-read-only/1-default-value.spec.yaml b/test/load/options/3-read-only/1-default-value.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvMy1yZWFkLW9ubHkvMS1kZWZhdWx0LXZhbHVlLnNwZWMueWFtbA==..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMy1yZWFkLW9ubHkvMS1kZWZhdWx0LXZhbHVlLnNwZWMueWFtbA== 100644 --- a/test/load/options/3-read-only/1-default-value.spec.yaml +++ b/test/load/options/3-read-only/1-default-value.spec.yaml @@ -2,4 +2,4 @@ start: simpleMode: true run: ../write-value.js -err: 'Impossible to write a value in config #: it is in read-only mode!' +err: 'Impossible to write a value in config @: it is in read-only mode!' diff --git a/test/load/options/3-read-only/2-true.spec.yaml b/test/load/options/3-read-only/2-true.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvMy1yZWFkLW9ubHkvMi10cnVlLnNwZWMueWFtbA==..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvMy1yZWFkLW9ubHkvMi10cnVlLnNwZWMueWFtbA== 100644 --- a/test/load/options/3-read-only/2-true.spec.yaml +++ b/test/load/options/3-read-only/2-true.spec.yaml @@ -3,4 +3,4 @@ simpleMode: true readOnly: true run: ../write-value.js -err: 'Impossible to write a value in config #: it is in read-only mode!' +err: 'Impossible to write a value in config @: it is in read-only mode!' diff --git a/test/load/options/6-allow-async/1-default-value.spec.yaml b/test/load/options/6-allow-async/1-default-value.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvNi1hbGxvdy1hc3luYy8xLWRlZmF1bHQtdmFsdWUuc3BlYy55YW1s..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvNi1hbGxvdy1hc3luYy8xLWRlZmF1bHQtdmFsdWUuc3BlYy55YW1s 100644 --- a/test/load/options/6-allow-async/1-default-value.spec.yaml +++ b/test/load/options/6-allow-async/1-default-value.spec.yaml @@ -3,4 +3,4 @@ start: simpleMode: true property: async -err: 'The configuration for ns # is not allowing async tasks!' +err: 'The configuration for ns @ is not allowing async tasks!' diff --git a/test/load/options/6-allow-async/2-false.spec.yaml b/test/load/options/6-allow-async/2-false.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvNi1hbGxvdy1hc3luYy8yLWZhbHNlLnNwZWMueWFtbA==..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvNi1hbGxvdy1hc3luYy8yLWZhbHNlLnNwZWMueWFtbA== 100644 --- a/test/load/options/6-allow-async/2-false.spec.yaml +++ b/test/load/options/6-allow-async/2-false.spec.yaml @@ -4,4 +4,4 @@ simpleMode: true allowAsync: false property: async -err: 'The configuration for ns # is not allowing async tasks!' +err: 'The configuration for ns @ is not allowing async tasks!' diff --git a/test/load/options/7-throw-if-not-ready/1-default-value.spec.yaml b/test/load/options/7-throw-if-not-ready/1-default-value.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvNy10aHJvdy1pZi1ub3QtcmVhZHkvMS1kZWZhdWx0LXZhbHVlLnNwZWMueWFtbA==..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvNy10aHJvdy1pZi1ub3QtcmVhZHkvMS1kZWZhdWx0LXZhbHVlLnNwZWMueWFtbA== 100644 --- a/test/load/options/7-throw-if-not-ready/1-default-value.spec.yaml +++ b/test/load/options/7-throw-if-not-ready/1-default-value.spec.yaml @@ -4,4 +4,4 @@ simpleMode: true allowAsync: true property: async -err: 'You should not access the # configuration: it is not ready yet!' +err: 'You should not access the @ configuration: it is not ready yet!' diff --git a/test/load/options/7-throw-if-not-ready/2-true.spec.yaml b/test/load/options/7-throw-if-not-ready/2-true.spec.yaml index 77273ba2ecda6a09c326e53b71ca0b51eee7ac1e_dGVzdC9sb2FkL29wdGlvbnMvNy10aHJvdy1pZi1ub3QtcmVhZHkvMi10cnVlLnNwZWMueWFtbA==..d96f365173d5830c9984042e6b221b88ed8d0c89_dGVzdC9sb2FkL29wdGlvbnMvNy10aHJvdy1pZi1ub3QtcmVhZHkvMi10cnVlLnNwZWMueWFtbA== 100644 --- a/test/load/options/7-throw-if-not-ready/2-true.spec.yaml +++ b/test/load/options/7-throw-if-not-ready/2-true.spec.yaml @@ -5,4 +5,4 @@ allowAsync: true throwIfNotReady: true property: async -err: 'You should not access the # configuration: it is not ready yet!' +err: 'You should not access the @ configuration: it is not ready yet!'