@@ -3,38 +3,45 @@ import { ProjectConfig } from '../entities/project-config.js';
33import { ResourceConfig } from '../entities/resource-config.js' ;
44import { InternalError } from '../utils/errors.js' ;
55import { ConfigClass } from './language-definition.js' ;
6- import { ConfigLoader } from './loader/index.js' ;
76import { FileParser } from './parser/index.js' ;
87import { JsonFileParser } from './parser/json/file-parser.js' ;
8+ import { ProjectReader } from './reader/index.js' ;
9+ import { ConfigBlock } from '../entities/index.js' ;
910
10- export class ConfigReader {
11+ export class ConfigParser {
1112
1213 static readonly supportedParsers : Record < string , FileParser > = {
1314 'json' : new JsonFileParser ( ) ,
1415 }
1516
1617 static async parseProject ( directory : string ) : Promise < Project > {
17- const loadedProject = await ( new ConfigLoader ( ) . loadProject ( directory ) ) ;
18+ const configReader = new ProjectReader ( ) ;
19+ const loadedProject = await configReader . readProject ( directory ) ;
1820
19- const configBlocksResult = await Promise . all ( loadedProject . coreModule . files . map ( ( file ) => {
20- const parser = ConfigReader . supportedParsers [ file . fileType ] ;
21+ const configBlocksResult = await Promise . all ( loadedProject . files . map ( ( file ) => {
22+ const parser = ConfigParser . supportedParsers [ file . fileType ] ;
2123 if ( ! parser ) {
2224 throw new InternalError ( `Unsupported file format loaded into parser: ${ file . fileName } ` ) ;
2325 }
2426
2527 return parser . parse ( file ) ;
2628 } ) ) ;
29+
2730 const configBlocks = configBlocksResult . flat ( 1 ) ;
31+ const projectConfig = ConfigParser . findProjectConfig ( configBlocks ) ;
2832
33+ return new Project (
34+ projectConfig ,
35+ configBlocks . filter ( ( u ) => u . configClass !== ConfigClass . PROJECT ) as ResourceConfig [ ] ,
36+ )
37+ }
38+
39+ private static findProjectConfig ( configBlocks : ConfigBlock [ ] ) : ProjectConfig | null {
2940 const parsedProjectConfigs = configBlocks . filter ( ( u ) => u . configClass === ConfigClass . PROJECT ) ;
3041 if ( parsedProjectConfigs . length > 1 ) {
3142 throw new Error ( 'One or zero project config can be specified' ) ;
3243 }
3344
34- const projectConfig = parsedProjectConfigs [ 0 ] as unknown as ProjectConfig | undefined ;
35- return new Project (
36- projectConfig ?? null ,
37- configBlocks . filter ( ( u ) => u . configClass !== ConfigClass . PROJECT ) as ResourceConfig [ ] ,
38- )
45+ return ( parsedProjectConfigs [ 0 ] ?? null ) as unknown as ProjectConfig | null ;
3946 }
4047}
0 commit comments