Extract ConfigReader to @rondo.dev/config pkg

This commit is contained in:
Jerko Steiner 2019-08-25 20:43:09 +07:00
parent ba45deda4f
commit 1bbc3ed97d
21 changed files with 82 additions and 16 deletions

3
package-lock.json generated
View File

@ -2130,6 +2130,9 @@
"@rondo.dev/common": {
"version": "file:packages/common"
},
"@rondo.dev/config": {
"version": "file:packages/config"
},
"@rondo.dev/image-upload": {
"version": "file:packages/image-upload"
},

View File

@ -11,7 +11,8 @@
"@rondo.dev/jsonrpc": "file:packages/jsonrpc",
"@rondo.dev/scripts": "file:packages/scripts",
"@rondo.dev/argparse": "file:packages/argparse",
"@rondo.dev/logger": "file:packages/logger"
"@rondo.dev/logger": "file:packages/logger",
"@rondo.dev/config": "file:packages/config"
},
"devDependencies": {
"@types/bcrypt": "^3.0.0",

View File

@ -0,0 +1,2 @@
app:
name: 'config-test'

View File

@ -0,0 +1,16 @@
module.exports = {
roots: [
'<rootDir>/src'
],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testRegex: '(/__tests__/.*|\\.(test|spec))\\.tsx?$',
moduleFileExtensions: [
'ts',
'tsx',
'js',
'jsx'
],
setupFiles: ['<rootDir>/jest.setup.js']
}

View File

@ -0,0 +1,4 @@
if (!process.env.LOG) {
process.env.LOG = 'sql:warn'
}
process.chdir(__dirname)

4
packages/config/package-lock.json generated Normal file
View File

@ -0,0 +1,4 @@
{
"name": "@rondo.dev/config",
"lockfileVersion": 1
}

View File

@ -0,0 +1,13 @@
{
"name": "@rondo.dev/config",
"private": true,
"scripts": {
"test": "jest",
"lint": "tslint --project .",
"compile": "tsc",
"clean": "rm -rf lib/"
},
"dependencies": {},
"main": "lib/index.js",
"types": "lib/index.d.ts"
}

View File

@ -1,9 +1,9 @@
import { readFileSync } from 'fs'
import YAML from 'js-yaml'
import {Config} from './Config'
import {findPackageRoot} from '../files/Find'
import {join} from 'path'
import {readFileSync} from 'fs'
import {apiLogger} from '../logger'
import { join } from 'path'
import { Config } from './Config'
import { findPackageRoot } from './findPackageRoot'
import loggerFactory, {ILogger} from '@rondo.dev/logger'
const isObject = (value: any) => value !== null && typeof value === 'object'
@ -14,8 +14,9 @@ export class ConfigReader {
constructor(
readonly path: string,
readonly cwd: string|undefined = process.cwd(),
readonly cwd: string | undefined = process.cwd(),
readonly environment = 'CONFIG',
readonly logger: ILogger = loggerFactory.getLogger('config'),
) {
const packageRoot = path && findPackageRoot(path)
this.locations = packageRoot ? [packageRoot] : []
@ -41,7 +42,7 @@ export class ConfigReader {
}
continue
}
apiLogger.info('config: Found config file: %s', configFilename)
this.logger.info('config: Found config file: %s', configFilename)
success += 1
}
}
@ -52,12 +53,12 @@ export class ConfigReader {
}
if (filename) {
apiLogger.info('config: Reading extra config file: %s', filename)
this.logger.info('config: Reading extra config file: %s', filename)
this.readFile(filename)
}
if (env) {
apiLogger.info('config: Parsing env variable: %s', this.environment)
this.logger.info('config: Parsing env variable: %s', this.environment)
this.parse(env)
}

View File

@ -1,7 +1,7 @@
import {resolve, join} from 'path'
import {statSync, Stats} from 'fs'
export function findNearestDirectory(
function findNearestDirectory(
dir: string, filename: string,
) {
let currentDir = dir

View File

@ -0,0 +1,4 @@
export * from './Config'
import {ConfigReader} from './ConfigReader'
export default ConfigReader

View File

@ -0,0 +1,10 @@
{
"extends": "../tsconfig.common.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"references": [
{"path": "../logger"}
]
}

View File

@ -0,0 +1,10 @@
{
"extends": [
"../tslint.json"
],
"linterOptions": {
"exclude": [
"src/migrations/*.ts"
]
}
}

View File

@ -1,4 +1,4 @@
import {ConfigReader} from './config/index'
import ConfigReader from '@rondo.dev/config'
import {IConfig} from './application'
import URL from 'url'

View File

@ -1,2 +0,0 @@
export * from './Config'
export * from './ConfigReader'

View File

@ -1,5 +1,4 @@
export * from './application'
export * from './config/index'
export * from './database'
export * from './entities'
export * from './error'

View File

@ -7,6 +7,7 @@
"references": [
{"path": "../common"},
{"path": "../jsonrpc"},
{"path": "../logger"}
{"path": "../logger"},
{"path": "../config"}
]
}