Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: makeomatic/ms-conf
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.0.0
Choose a base ref
...
head repository: makeomatic/ms-conf
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.0.1
Choose a head ref
  • 2 commits
  • 8 files changed
  • 1 contributor

Commits on Sep 14, 2020

  1. fix: update deps

    AVVS committed Sep 14, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    AVVS Vitaly Aminev
    Copy the full SHA
    d7e0106 View commit details
  2. fix: update deps

    AVVS committed Sep 14, 2020

    Verified

    This commit was signed with the committer’s verified signature.
    AVVS Vitaly Aminev
    Copy the full SHA
    4c673db View commit details
Showing with 1,220 additions and 714 deletions.
  1. +1 −0 .gitignore
  2. +0 −33 .npmignore
  3. +19 −17 package.json
  4. +9 −8 src/index.ts
  5. +26 −16 src/load-config.ts
  6. +2 −1 test/dir/nice.js
  7. +9 −8 test/index.spec.ts
  8. +1,154 −631 yarn.lock
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -32,3 +32,4 @@ node_modules
# compiled source code
lib
*.tsbuildinfo
.vscode/
33 changes: 0 additions & 33 deletions .npmignore

This file was deleted.

36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -32,35 +32,37 @@
},
"homepage": "https://github.com/makeomatic/ms-conf#readme",
"devDependencies": {
"@makeomatic/deploy": "^10.1.4",
"@types/bluebird": "^3.5.30",
"@makeomatic/deploy": "^10.3.0",
"@types/bluebird": "^3.5.32",
"@types/debug": "^4.1.5",
"@types/glob": "^7.1.1",
"@types/glob": "^7.1.3",
"@types/lodash.mergewith": "^4.6.6",
"@types/lodash.reduce": "^4.6.6",
"@types/lodash.uniq": "^4.5.6",
"@types/mocha": "^7.0.2",
"@types/mocha": "^8.0.3",
"@types/nconf": "^0.10.0",
"@types/sinon": "^9.0.0",
"@types/node": "^14.10.1",
"@types/sinon": "^9.0.5",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"bluebird": "^3.7.2",
"codecov": "^3.6.5",
"codecov": "^3.7.2",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
"eslint-config-makeomatic": "^5.0.0",
"eslint-plugin-mocha": "^6.3.0",
"mocha": "^7.1.1",
"nyc": "^15.0.1",
"sinon": "^9.0.2",
"source-map-support": "^0.5.18",
"ts-node": "^8.9.0",
"typescript": "^3.8.3"
"eslint": "^7.9.0",
"eslint-config-makeomatic": "^5.0.2",
"eslint-plugin-mocha": "^8.0.0",
"mocha": "^8.1.3",
"nyc": "^15.1.0",
"sinon": "^9.0.3",
"source-map-support": "^0.5.19",
"ts-node": "^9.0.0",
"typescript": "^4.0.2"
},
"dependencies": {
"@makeomatic/confidence": "6.0.1",
"@makeomatic/confidence": "6.0.2",
"camelcase": "^6.0.0",
"debug": "^4.1.0",
"dotenv": "^8.2.0",
"eventemitter3": "^4.0.0",
"eventemitter3": "^4.0.7",
"glob": "^7.1.6",
"lodash.mergewith": "^4.6.2",
"lodash.reduce": "^4.6.0",
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import EventEmitter = require('eventemitter3');
import _debug = require('debug');
import { Store, Criteria } from '@makeomatic/confidence'
import type { Criteria } from '@makeomatic/confidence' // eslint-disable-line @typescript-eslint/no-unused-vars
import { Store } from '@makeomatic/confidence'
import { strict as assert } from 'assert'
import { loadConfiguration, append, prependDefaultConfiguration } from './load-config'

@@ -15,21 +16,21 @@ const EE = new EventEmitter()
export { append, prependDefaultConfiguration, EE }

// use this on sighup
export function reload() {
export function reload(): void {
debug('reloading configuration')
store = new Store(loadConfiguration(crashOnError))
EE.emit('reload', store)
}

// hot-reload enabler
export function enableReload() {
export function enableReload(): void {
debug('enabling sigusr')
process.on('SIGUSR1', reload)
process.on('SIGUSR2', reload)
}

// hot-reload disabler
export function disableReload() {
export function disableReload(): void {
debug('disabling sigusr')
process.removeListener('SIGUSR1', reload)
process.removeListener('SIGUSR2', reload)
@@ -45,21 +46,21 @@ export function meta<Response>(key: string, opts: Criteria = defaultOpts): Respo
return store.meta(key, opts)
}

export function setDefaultOpts(opts: Criteria) {
export function setDefaultOpts(opts: Criteria): void {
assert.ok(opts, 'must be an object')
assert.ok(typeof opts === 'object', 'must be an object')
defaultOpts = opts
}

export function onReload(fn: EventEmitter.ListenerFn) {
export function onReload(fn: EventEmitter.ListenerFn): void {
EE.on('reload', fn)
}

export function offReload(fn: EventEmitter.ListenerFn) {
export function offReload(fn: EventEmitter.ListenerFn): void {
EE.off('reload', fn)
}

export function setCrashOnError(val: boolean) {
export function setCrashOnError(val: boolean): void {
crashOnError = val
}

42 changes: 26 additions & 16 deletions src/load-config.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ const cwd = process.cwd()
let appendConfiguration: any

// safe json parse
function parseJSONSafe(possibleJSON: string) {
function parseJSONSafe(possibleJSON: string): unknown {
try {
return JSON.parse(possibleJSON)
} catch (e) {
@@ -29,13 +29,23 @@ function parseJSONSafe(possibleJSON: string) {
}

// make camelCase keys
const camelCaseKeys = (camelize: boolean) => function processKeys(obj: any, value: any, key: string) {
const camelCaseKeys = (camelize: boolean) => function processKeys(obj: Record<string, unknown>, value: unknown, key: string) {
const camelized = camelize ? key : camelCase(key)

if (value && typeof value === 'object') {
reduce(value, processKeys, (obj[camelized] = {}))
} else {
obj[camelized] = parseJSONSafe(value)
if (value == null) {
obj[camelized] = value
return obj
}

switch (typeof value) {
case 'object':
reduce(value, processKeys, (obj[camelized] = Object.create(null)))
break
case 'string':
obj[camelized] = parseJSONSafe(value)
break
default:
obj[camelized] = value
}

return obj
@@ -45,7 +55,7 @@ const camelCaseKeys = (camelize: boolean) => function processKeys(obj: any, valu
* @param _ overwrite value, not used
* @param srcValue
*/
const customizer = (_: any, srcValue: any | any[]) => {
const customizer = (_: any, srcValue: unknown | unknown[]): unknown | unknown[] | undefined => {
if (Array.isArray(srcValue)) {
return srcValue
}
@@ -71,7 +81,7 @@ const readFile = (configuration: any, crashOnError: boolean) => (absPath: string
}
}

export function possibleJSONStringToArray(filePaths: string) {
export function possibleJSONStringToArray(filePaths: string): string[] {
let files
try {
files = JSON.parse(filePaths)
@@ -86,11 +96,11 @@ export function possibleJSONStringToArray(filePaths: string) {
return files
}

function resolve(filePath: string) {
function resolve(filePath: string): string {
return require.resolve(filePath)
}

function resolveAbsPaths(paths: string[]) {
function resolveAbsPaths(paths: string[]): string[] {
const absolutePaths = paths.reduce((resolvedPaths: string[], filePath) => {
const stats = fs.statSync(filePath)
if (stats.isFile()) {
@@ -112,7 +122,7 @@ function resolveAbsPaths(paths: string[]) {
return uniq(absolutePaths)
}

export function globFiles(filePaths: string | string[], configuration: any = {}, crashOnError: boolean) {
export function globFiles(filePaths: string | string[], configuration: any = {}, crashOnError: boolean): void {
// if we get parsed JSON array - use it right away
const files = isArray(filePaths)
? filePaths
@@ -127,7 +137,7 @@ export function globFiles(filePaths: string | string[], configuration: any = {},
return configuration
}

export function loadConfiguration(crashOnError: boolean) {
export function loadConfiguration(crashOnError: boolean): void {
// load dotenv
const dotenvConfig = {
verbose,
@@ -186,9 +196,9 @@ export function loadConfiguration(crashOnError: boolean) {
/**
* Add base configuration
*/
export function prependDefaultConfiguration(baseConfig: unknown) {
assert.ok(baseConfig, 'must be a path to specific location')
assert.ok(typeof baseConfig === 'string')
export function prependDefaultConfiguration(baseConfig: unknown): void {
assert(baseConfig, 'must be a path to specific location')
assert(typeof baseConfig === 'string')

let files = null
if (env.NCONF_FILE_PATH) {
@@ -204,6 +214,6 @@ export function prependDefaultConfiguration(baseConfig: unknown) {
/**
* Appends passed configuration to resolved config
*/
export function append(configuration: any) {
export function append(configuration: unknown): void {
appendConfiguration = configuration
}
3 changes: 2 additions & 1 deletion test/dir/nice.js
Original file line number Diff line number Diff line change
@@ -6,4 +6,5 @@ module.exports = {
1,
3,
],
};
ok: null
}
17 changes: 9 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ describe('Configuration loader', () => {
})

it('should correctly use match env option', () => {
assert.equal(Object.keys(mod).length, 9)
assert.strictEqual(Object.keys(mod).length, 10)
assert.ok(mod.amqp)
assert.ok(mod.value)
assert.ok(mod.expanded)
@@ -35,18 +35,18 @@ describe('Configuration loader', () => {
})

it('does not expand values', () => {
assert.equal(mod.expanded, '$MS_CONF___VALUE')
assert.equal(mod.value, 'darn')
assert.strictEqual(mod.expanded, '$MS_CONF___VALUE')
assert.strictEqual(mod.value, 'darn')
})

it('parses correct json and returns original text if it is not', () => {
assert.deepEqual(mod.amqp.hosts, ['127.0.0.1'])
assert.equal(mod.amqp.invalidJson, '{"test":bad}')
assert.strictEqual(mod.amqp.invalidJson, '{"test":bad}')
})

it('file was loaded and configuration was merged', () => {
assert.ok(mod.my)
assert.equal(mod.amqp.ssl, false)
assert.strictEqual(mod.amqp.ssl, false)
})

it('produces correct configuration', () => {
@@ -70,6 +70,7 @@ describe('Configuration loader', () => {
},
pot: 'is-json',
array: [1, 3],
ok: null,
overwritten: { by: { env: true } },
})
})
@@ -98,7 +99,7 @@ describe('Configuration loader', () => {
boose: 'works',
})

assert.equal(store.get('/boose'), undefined)
assert.strictEqual(store.get('/boose'), undefined)
process.kill(process.pid, 'SIGUSR2')

// SIGHUP comes in as async action
@@ -125,8 +126,8 @@ describe('Configuration loader', () => {
// SIGHUP comes in as async action
return delay(10)
.then(() => {
assert.equal(store.get('/reloaded'), true)
assert.equal(spy.calledOnce, true)
assert.strictEqual(store.get('/reloaded'), true)
assert.strictEqual(spy.calledOnce, true)
return null
})
.finally(() => {
1,785 changes: 1,154 additions & 631 deletions yarn.lock

Large diffs are not rendered by default.