File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { describe , it , expect } from 'vitest' ;
2+ import { Project } from './project.js' ;
3+ import { Parser } from '../parser/index.js' ;
4+ import { File } from '../parser/reader/entities/file.js'
5+ import { ResourceConfig } from './resource-config.js' ;
6+
7+ describe ( 'Project Unit Tests' , ( ) => {
8+ it ( 'Can add unique names for duplicate resources' , async ( ) => {
9+ const parser = Parser . supportedParsers [ 'json' ]
10+
11+ const resourceConfigs = await parser . parse ( new File ( {
12+ fileName : 'test' ,
13+ fileType : 'json' ,
14+ contents : JSON . stringify ( [
15+ { type : 'git-clone' , remote : 'git@git1' } ,
16+ { type : 'git-clone' , remote : 'git@git1' } ,
17+ { type : 'git-clone' , remote : 'git@git2' } ,
18+ { type : 'other' }
19+ ] )
20+ } ) )
21+
22+ const project = new Project ( null , resourceConfigs as ResourceConfig [ ] )
23+ project . addUniqueNamesForDuplicateResources ( )
24+
25+ expect ( project . resourceConfigs [ 0 ] . id ) . to . eq ( 'git-clone.0' )
26+ expect ( project . resourceConfigs [ 1 ] . id ) . to . eq ( 'git-clone.1' )
27+ expect ( project . resourceConfigs [ 2 ] . id ) . to . eq ( 'git-clone.2' )
28+ expect ( project . resourceConfigs [ 3 ] . id ) . to . eq ( 'other' )
29+ } )
30+
31+ } )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { ValidateResponseData } from 'codify-schemas';
33import { ctx } from '../events/context.js' ;
44import { DependencyMap } from '../plugins/plugin-manager.js' ;
55import { DependencyGraphResolver } from '../utils/dependency-graph-resolver.js' ;
6+ import { groupBy } from '../utils/index.js' ;
67import { ProjectConfig } from './project-config.js' ;
78import { ResourceConfig } from './resource-config.js' ;
89
@@ -20,6 +21,21 @@ export class Project {
2021 return this . resourceConfigs . length === 0 ;
2122 }
2223
24+ addUniqueNamesForDuplicateResources ( ) {
25+ const groups = groupBy ( this . resourceConfigs , ( i ) => i . id )
26+ const duplicates = Object . entries ( groups ) . filter ( ( [ , arr ] ) => arr . length > 1 ) ;
27+
28+ for ( const [ id , resourceConfigs ] of duplicates ) {
29+ if ( resourceConfigs . some ( ( r ) => r . name ) ) {
30+ throw new Error ( `Duplicate name found for resource: ${ id } ` ) ;
31+ }
32+
33+ for ( const [ idx , r ] of resourceConfigs . entries ( ) ) {
34+ r . name = String ( idx )
35+ }
36+ }
37+ }
38+
2339 addXCodeToolsConfig ( ) {
2440 this . resourceConfigs . unshift ( new ResourceConfig ( {
2541 type : 'xcode-tools'
Original file line number Diff line number Diff line change 1+ import { InternalError } from '../common/errors.js' ;
12import { ConfigBlock } from '../entities/config.js' ;
23import { Project } from '../entities/project.js' ;
34import { ProjectConfig } from '../entities/project-config.js' ;
45import { ResourceConfig } from '../entities/resource-config.js' ;
5- import { InternalError } from '../common/errors.js' ;
66import { ConfigClass } from './language-definition.js' ;
77import { FileParser } from './parser/index.js' ;
88import { JsonFileParser } from './parser/json/file-parser.js' ;
@@ -26,10 +26,13 @@ export class Parser {
2626 const configBlocks = await parser . parse ( configFile ) ;
2727 const projectConfig = Parser . findProjectConfig ( configBlocks ) ;
2828
29- return new Project (
29+ const project = new Project (
3030 projectConfig ,
3131 configBlocks . filter ( ( u ) => u . configClass !== ConfigClass . PROJECT ) as ResourceConfig [ ] ,
3232 )
33+ project . addUniqueNamesForDuplicateResources ( )
34+
35+ return project ;
3336 }
3437
3538 private static findProjectConfig ( configBlocks : ConfigBlock [ ] ) : ProjectConfig | null {
You can’t perform that action at this time.
0 commit comments