@@ -84,48 +84,57 @@ export const log = (conversion: Conversion, log: string | NodeJS.ErrnoException,
8484} ;
8585
8686/**
87- * Reads the configuration file.
87+ * Reads and parses JOSN file under given path .
8888 */
89- export const readConfig = ( confPath : string , logsPath : string , configFileName : string = 'config.json' ) : Promise < any > => {
89+ const readAndParseJsonFile = ( pathToFile : string ) : Promise < any > => {
9090 return new Promise < any > ( resolve => {
91- const pathToConfig = path . join ( confPath , configFileName ) ;
92-
93- fs . readFile ( pathToConfig , ( error : ErrnoException | null , data : Buffer ) => {
91+ fs . readFile ( pathToFile , ( error : ErrnoException | null , data : Buffer ) => {
9492 if ( error ) {
95- console . log ( `\n\t--Cannot run migration\nCannot read configuration info from ${ pathToConfig } ` ) ;
93+ console . log ( `\n\t--Cannot run migration\nCannot read configuration info from ${ pathToFile } ` ) ;
9694 process . exit ( 1 ) ;
9795 }
9896
9997 const config : any = JSON . parse ( data . toString ( ) ) ;
100- config . logsDirPath = path . join ( logsPath , 'logs_directory' ) ;
101- config . dataTypesMapAddr = path . join ( confPath , 'data_types_map.json' ) ;
10298 resolve ( config ) ;
10399 } ) ;
104100 } ) ;
105101} ;
106102
107103/**
108- * Reads the extra configuration file, if necessary .
104+ * Reads the configuration file.
109105 */
110- export const readExtraConfig = ( config : any , confPath : string , extraConfigFileName : string = 'extra_config .json' ) : Promise < any > => {
111- return new Promise < any > ( resolve => {
112- if ( config . enable_extra_config !== true ) {
113- config . extraConfig = null ;
114- return resolve ( config ) ;
115- }
116-
117- const pathToExtraConfig = path . join ( confPath , extraConfigFileName ) ;
106+ export const readConfig = async ( confPath : string , logsPath : string , configFileName : string = 'config .json' ) : Promise < any > => {
107+ const pathToConfig = path . join ( confPath , configFileName ) ;
108+ const config : any = await readAndParseJsonFile ( pathToConfig ) ;
109+ config . logsDirPath = path . join ( logsPath , 'logs_directory' ) ;
110+ config . dataTypesMapAddr = path . join ( confPath , 'data_types_map.json' ) ;
111+ config . indexTypesMapAddr = path . join ( confPath , 'index_types_map.json' ) ;
112+ return config ;
113+ } ;
118114
119- fs . readFile ( pathToExtraConfig , ( error : ErrnoException | null , data : Buffer ) => {
120- if ( error ) {
121- console . log ( `\n\t--Cannot run migration\nCannot read configuration info from ${ pathToExtraConfig } ` ) ;
122- process . exit ( 1 ) ;
123- }
115+ /**
116+ * Reads the extra configuration file, if necessary.
117+ */
118+ export const readExtraConfig = async ( config : any , confPath : string , extraConfigFileName : string = 'extra_config.json' ) : Promise < any > => {
119+ if ( config . enable_extra_config !== true ) {
120+ config . extraConfig = null ;
121+ return config ;
122+ }
123+
124+ const pathToExtraConfig = path . join ( confPath , extraConfigFileName ) ;
125+ config . extraConfig = await readAndParseJsonFile ( pathToExtraConfig ) ;
126+ return config ;
127+ } ;
124128
125- config . extraConfig = JSON . parse ( data . toString ( ) ) ;
126- resolve ( config ) ;
127- } ) ;
128- } ) ;
129+ /**
130+ * Reads both "./config/data_types_map.json" and "./config/index_types_map.json" and converts its json content to js object.
131+ */
132+ export const readDataAndIndexTypesMap = async ( conversion : Conversion ) : Promise < Conversion > => {
133+ const logTitle : string = 'FsOps::readDataAndIndexTypesMap' ;
134+ conversion . _dataTypesMap = await readAndParseJsonFile ( conversion . _dataTypesMapAddr ) ;
135+ conversion . _indexTypesMap = await readAndParseJsonFile ( conversion . _indexTypesMapAddr ) ;
136+ log ( conversion , `\t--[${ logTitle } ] Data and Index Types Maps are loaded...` ) ;
137+ return conversion ;
129138} ;
130139
131140/**
@@ -166,23 +175,3 @@ const createDirectory = (conversion: Conversion, directoryPath: string, logTitle
166175 } ) ;
167176 } ) ;
168177} ;
169-
170- /**
171- * Reads "./config/data_types_map.json" and converts its json content to js object.
172- */
173- export const readDataTypesMap = ( conversion : Conversion ) : Promise < Conversion > => {
174- return new Promise < Conversion > ( resolve => {
175- fs . readFile ( conversion . _dataTypesMapAddr , ( error : ErrnoException | null , data : Buffer ) => {
176- const logTitle : string = 'FsOps::readDataTypesMap' ;
177-
178- if ( error ) {
179- console . log ( `\t--[${ logTitle } ] Cannot read "DataTypesMap" from ${ conversion . _dataTypesMapAddr } ` ) ;
180- process . exit ( 1 ) ;
181- }
182-
183- conversion . _dataTypesMap = JSON . parse ( data . toString ( ) ) ;
184- console . log ( `\t--[${ logTitle } ] Data Types Map is loaded...` ) ;
185- resolve ( conversion ) ;
186- } ) ;
187- } ) ;
188- } ;
0 commit comments