1
+ import * as fs from "fs-extra" ;
1
2
import * as jsonConfig from "./annotationConfig" ;
3
+ import { ConfigParseError } from "./configParseError" ;
2
4
import * as model from "./model" ;
5
+ import { lowerInitialChar } from "./utils" ;
3
6
4
7
interface BeanWithName {
5
8
qualifiedClassName : string ;
@@ -8,7 +11,31 @@ interface BeanWithName {
8
11
9
12
const beansById = new Map < string , BeanWithName > ( ) ;
10
13
11
- export async function collectBeansFromConfigFile ( _filePath : string ) : Promise < string [ ] > {
14
+ export async function collectBeansFromConfigFile ( filePath : string ) : Promise < string [ ] > {
15
+ let json : string ;
16
+ try {
17
+ json = await fs . readFile ( filePath , "utf8" ) ;
18
+ } catch ( err ) {
19
+ throw new ConfigParseError ( `Can't open input file '${ filePath } ': ${ err . message } ` ) ;
20
+ }
21
+ const annotationConfig : jsonConfig . AnnotationConfig = JSON . parse ( json ) ;
22
+ for ( const qualifiedClassName in annotationConfig . beans ) {
23
+ if ( ! annotationConfig . beans . hasOwnProperty ( qualifiedClassName ) )
24
+ continue ;
25
+ // Create bean ID
26
+ // TODO: Handle name parameter to @Component constructor
27
+ const id = lowerInitialChar ( < string > qualifiedClassName . split ( "." ) . pop ( ) ) ;
28
+ // Check for duplicate ids from other beans
29
+ if ( beansById . has ( id ) )
30
+ throw new ConfigParseError ( `Found multiple beans with id '${ id } '` ) ;
31
+ const aliasedBeanId = model . Bean . getAlias ( id ) ;
32
+ if ( aliasedBeanId !== undefined )
33
+ throw new ConfigParseError ( `Found a bean with id same as an alias for '${ aliasedBeanId } '` ) ;
34
+ // Create a bean model for the bean
35
+ const bean = { qualifiedClassName : qualifiedClassName , bean : annotationConfig . beans [ qualifiedClassName ] } ;
36
+ beansById . set ( id , bean ) ;
37
+ }
38
+ // TODO: Return paths to XML config files specified in code
12
39
return [ ] ;
13
40
}
14
41
0 commit comments