Skip to content

Commit 56ec1e7

Browse files
committed
[CODE-100] Add unique names for duplicate resources
1 parent a27e854 commit 56ec1e7

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/entities/project.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
})

src/entities/project.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ValidateResponseData } from 'codify-schemas';
33
import { ctx } from '../events/context.js';
44
import { DependencyMap } from '../plugins/plugin-manager.js';
55
import { DependencyGraphResolver } from '../utils/dependency-graph-resolver.js';
6+
import { groupBy } from '../utils/index.js';
67
import { ProjectConfig } from './project-config.js';
78
import { 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'

src/parser/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { InternalError } from '../common/errors.js';
12
import { ConfigBlock } from '../entities/config.js';
23
import { Project } from '../entities/project.js';
34
import { ProjectConfig } from '../entities/project-config.js';
45
import { ResourceConfig } from '../entities/resource-config.js';
5-
import { InternalError } from '../common/errors.js';
66
import { ConfigClass } from './language-definition.js';
77
import { FileParser } from './parser/index.js';
88
import { 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 {

0 commit comments

Comments
 (0)