Skip to content

Commit

Permalink
fix: deduplicate path
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Sep 26, 2023
1 parent 0ddff3e commit 1d4e690
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { BaseProvider, ProviderFile } from './provider/base.js';
import { FakeProvider } from './provider/fake.js';
import { FSProvider, FSProviderOptions } from './provider/fs.js';
import { register, registeredRules, registeredTech } from './register.js';
import { Analyser, AnalyserJson } from './types/index.js';
import {
Analyser,
AnalyserJson,
Dependency,
GraphEdge,
} from './types/index.js';
import {
SupportedDeps,
RuleDependency,
Expand All @@ -36,9 +41,11 @@ export {
AnalyserJson,
BaseProvider,
ComponentMatcher,
Dependency,
FakeProvider,
FSProvider,
FSProviderOptions,
GraphEdge,
Payload,
ProviderFile,
Rule,
Expand Down
16 changes: 8 additions & 8 deletions src/payload/__snapshots__/helpers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Payload {
"languages": {},
"name": "2",
"parent": undefined,
"path": [
"path": Set {
"/",
],
},
"reason": [],
"tech": null,
"techs": Set {},
Expand All @@ -40,9 +40,9 @@ Payload {
"languages": {},
"name": "2",
"parent": undefined,
"path": [
"path": Set {
"/",
],
},
"reason": [],
"tech": null,
"techs": Set {},
Expand All @@ -55,9 +55,9 @@ Payload {
"languages": {},
"name": "3",
"parent": undefined,
"path": [
"path": Set {
"/",
],
},
"reason": [],
"tech": null,
"techs": Set {},
Expand All @@ -70,9 +70,9 @@ Payload {
"languages": {},
"name": "main",
"parent": undefined,
"path": [
"path": Set {
"",
],
},
"reason": [],
"tech": null,
"techs": Set {},
Expand Down
2 changes: 1 addition & 1 deletion src/payload/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function findImplicitComponent(
const comp = new Payload({
name: ref.name,
tech: tech,
folderPath: pl.path[0],
folderPath: pl.path,
parent: pl,
reason,
});
Expand Down
17 changes: 17 additions & 0 deletions src/payload/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ describe('Payload', () => {
expect(root.childs[1].id).toBe('5');
expect(root.childs[1].inComponent!.id).toBe('4');
});

it('should dedup child and path', () => {
const root = new Payload({ name: 'root', folderPath: '/' });
root.addChild(
new Payload({ name: 'GCP1', folderPath: 'foo', tech: 'gcp' })
);
root.addChild(
new Payload({ name: 'GCP2', folderPath: 'foo', tech: 'gcp' })
);
root.addChild(
new Payload({ name: 'GCP3', folderPath: 'bar.xml', tech: 'gcp' })
);

expect(root.childs).toHaveLength(1);
expect(root.childs[0].path).toStrictEqual(new Set(['foo', 'bar.xml']));
expect(root.toJson().childs[0].path).toStrictEqual(['foo', 'bar.xml']);
});
});
18 changes: 11 additions & 7 deletions src/payload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ export class Payload implements Analyser {
}: {
id?: Analyser['id'];
name: Analyser['name'];
folderPath: string;
folderPath: Set<string> | string;
parent?: Payload | null;
tech?: Analyser['tech'];
dependencies?: Analyser['dependencies'];
reason?: string[] | string;
}) {
this.id = id || nid();
this.name = name;
this.path = [folderPath];
this.path = new Set(
typeof folderPath === 'string' ? [folderPath] : folderPath
);
this.tech = tech || null;
this.inComponent = null;
this.childs = [];
Expand Down Expand Up @@ -133,7 +135,7 @@ export class Payload implements Analyser {
if (tech.type === 'hosting') {
const pl = new Payload({
name: tech.name,
folderPath: service.path[0],
folderPath: service.path,
tech: tech.tech,
reason: `implicit: ${service.tech}`,
});
Expand All @@ -144,7 +146,9 @@ export class Payload implements Analyser {

if (exist) {
// Log all paths were it was found
exist.path.push(...service.path);
for (const p of service.path) {
exist.path.add(p);
}

// Update edges to point to the initial component
if (service.parent) {
Expand Down Expand Up @@ -263,7 +267,7 @@ export class Payload implements Analyser {
*/
combine(pl: Payload): void {
// Log all paths were it was found
this.path = [...new Set([...this.path, ...pl.path])];
this.path = new Set([...this.path, ...pl.path]);

// Merge dependencies
this.combineDependencies(pl);
Expand All @@ -286,7 +290,7 @@ export class Payload implements Analyser {
const cp = new Payload({
id: this.id,
name: this.name,
folderPath: this.path[0],
folderPath: this.path,
parent: this.parent,
tech: this.tech,
dependencies: this.dependencies,
Expand All @@ -313,7 +317,7 @@ export class Payload implements Analyser {
return {
id: this.id,
name: this.name,
path: cleanPath(this.path, root),
path: cleanPath([...this.path], root),
tech: this.tech,
edges: this.edges.map((edge) => {
return { ...edge, target: edge.target.id };
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Analyser {
* Where this payload was found.
* When flatten() it will contain all path that were deduplicated
*/
path: string[];
path: Set<string>;

/**
* If this payload is a specific Technology.
Expand Down Expand Up @@ -76,6 +76,7 @@ export type AnalyserJson = Modify<
Analyser,
{
childs: AnalyserJson[];
path: string[];
techs: AllowedKeys[];
inComponent: string | null;
edges: Array<Modify<GraphEdge, { target: string }>>;
Expand Down

0 comments on commit 1d4e690

Please sign in to comment.