@@ -20,21 +20,22 @@ import DefaultServient, { ScriptOptions } from "./cli-default-servient";
2020import * as path from "path" ;
2121import { Command , Argument , Option } from "commander" ;
2222import Ajv , { ValidateFunction } from "ajv" ;
23- import ConfigSchema from "./wot-servient-schema.conf.json " ;
24- import { version } from "@node-wot/core/package.json " ;
23+ import ConfigSchema from "./generated/ wot-servient-schema.conf" ;
24+ import version from "./generated/version " ;
2525import { createLoggers } from "@node-wot/core" ;
26- import { buildConfig } from "./config-builder" ;
2726import { loadCompiler , loadEnvVariables } from "./utils" ;
2827import { runScripts } from "./script-runner" ;
2928import { readdir } from "fs/promises" ;
3029import { parseConfigFile , parseConfigParams , parseIp } from "./parsers" ;
3130import { setLogLevel } from "./utils/set-log-level" ;
31+ import { buildConfig , buildConfigFromFile , Configuration , defaultConfiguration } from "./configuration" ;
32+ import { cloneDeep } from "lodash" ;
3233
3334const { error, info, warn, debug } = createLoggers ( "cli" , "cli" ) ;
3435
3536const program = new Command ( ) ;
36- const ajv = new Ajv ( { strict : true } ) ;
37- const schemaValidator = ajv . compile ( ConfigSchema ) as ValidateFunction ;
37+ const ajv = new Ajv ( { strict : true , allErrors : true } ) ;
38+ const schemaValidator = ajv . compile ( ConfigSchema ) as ValidateFunction < Configuration > ;
3839const defaultFile = "wot-servient.conf.json" ;
3940const baseDir = "." ;
4041
@@ -127,7 +128,7 @@ program
127128 ) . choices ( [ "debug" , "info" , "warn" , "error" ] )
128129 )
129130 . option ( "-f, --config-file <file>" , "load configuration from specified file" , ( value , previous ) =>
130- parseConfigFile ( value , previous , schemaValidator )
131+ parseConfigFile ( value , previous )
131132 )
132133 . option (
133134 "-p, --config-params <param...>" ,
@@ -148,6 +149,7 @@ program.action(async function (_, options, cmd) {
148149 if ( process . env . DEBUG == null ) {
149150 // by default enable error logs and warnings
150151 // user can override using command line option
152+ // or later by config file.
151153 setLogLevel ( options . logLevel ?? "warn" ) ;
152154 }
153155
@@ -161,18 +163,21 @@ program.action(async function (_, options, cmd) {
161163 debug ( "command line environment variables" , args ) ;
162164
163165 try {
164- const config = await buildConfig ( options , defaultFilePath , env ) ;
165- // eslint-disable-next-line @typescript-eslint/no-explicit-any
166- setLogLevel ( ( config . log as any ) . level ?? options . logLevel ?? "warn" ) ;
167- servient = new DefaultServient ( options . clientOnly , config ) ;
166+ const config = await buildConfigFromFile ( options , defaultFilePath , env , schemaValidator ) ;
167+ setLogLevel ( options . logLevel ?? config . logLevel ) ;
168+ config . servient . clientOnly = options . clientOnly ?? config . servient . clientOnly ;
169+ servient = new DefaultServient ( config ) ;
168170 } catch ( err ) {
169171 if ( ( err as NodeJS . ErrnoException ) ?. code !== "ENOENT" || options . configFile != null ) {
170- error ( "WoT-Servient config file error. %O " , err ) ;
172+ error ( "WoT-Servient configuration file error:\n%O\nClose. " , err ) ;
171173 process . exit ( ( err as NodeJS . ErrnoException ) . errno ?? 1 ) ;
172174 }
173175
174176 warn ( `WoT-Servient using defaults as %s does not exist` , defaultFile ) ;
175- servient = new DefaultServient ( options . clientOnly ) ;
177+
178+ const config = await buildConfig ( options , cloneDeep ( defaultConfiguration ) , env , schemaValidator ) ;
179+ config . servient . clientOnly = options . clientOnly ?? config . servient . clientOnly ;
180+ servient = new DefaultServient ( config ) ;
176181 }
177182
178183 await servient . start ( ) ;
0 commit comments