Skip to content

Commit

Permalink
Merge pull request #46 from event-catalog/federation-generator-config…
Browse files Browse the repository at this point in the history
…-simplfied

feat(plugin): federation generator config now simplified for direct m…
  • Loading branch information
boyney123 authored Feb 6, 2025
2 parents 29de598 + c0303e6 commit f94285b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-guests-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/generator-federation": patch
---

feat(plugin): federation generator config now simplified for direct m…
24 changes: 20 additions & 4 deletions packages/generator-federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ type GeneratorProps = {
licenseKey?: string;
source: string;
branch?: string;
copy: CopyProps[];
copy?: CopyProps[];
debug?: boolean;
override?: boolean;
destination?: string;
enforceUniqueResources?: boolean;
};

const log = console.log;

const resources = ['services', 'events', 'domains', 'commands', 'queries', 'flows'];
const resources = ['services', 'events', 'domains', 'commands', 'queries', 'flows', 'teams', 'users'];
const tmpDir = path.join(os.tmpdir(), 'eventcatalog');

const getFrontmatter = async (filename: string) => {
Expand Down Expand Up @@ -127,15 +128,30 @@ export default async (_: EventCatalogConfig, options: GeneratorProps) => {
// Sparse checkout the content
await execSync(`git sparse-checkout init`, { cwd: tmpDir });

let contentsToCopy = options.copy || resources.map((resource) => ({ content: resource, destination: resource }));
const isRootCopyConfiguration = !options.copy;

// Collect all content paths for sparse checkout
const allContentPaths = options.copy.flatMap(({ content }) => (Array.isArray(content) ? content : [content]));
const allContentPaths = contentsToCopy.flatMap(({ content }) => (Array.isArray(content) ? content : [content]));
await execSync(`git sparse-checkout set ${allContentPaths.join(' ')} --no-cone`, { cwd: tmpDir });

// Checkout the branch
await execSync(`git checkout ${options.branch || 'main'}`, { cwd: tmpDir });

// No copy values have been provides, lets try and copy all EventCatalog Resources, first we have to check if they exist in the project
if (isRootCopyConfiguration) {
const existingPaths = await Promise.all(
allContentPaths.map(async (content) => {
const exists = await fsExtra.pathExists(join(tmpDir, content));
return exists ? content : null;
})
);
const validPaths = existingPaths.filter((path): path is string => path !== null);
contentsToCopy = validPaths.map((path) => ({ content: path, destination: join(options.destination || process.cwd(), path) }));
}

// Check for existing paths first and copy for each configuration
for (const copyConfig of options.copy) {
for (const copyConfig of contentsToCopy) {
if (!options.override) {
try {
await checkForExistingPaths(copyConfig.content, copyConfig.destination, options.enforceUniqueResources || false);
Expand Down
24 changes: 17 additions & 7 deletions packages/generator-federation/src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ describe('generator-federation', () => {
}
});

// afterEach(async () => {
// // const exists = await fs.access(join(catalogDir)).then(() => true).catch(() => false);
// // if(exists) {
// // await fs.rm(join(catalogDir), { recursive: true });
// // }
// });

it('clones the source directory and copies the files specified in the content to the destination directory', async () => {
await plugin(eventCatalogConfig, {
source: 'https://github.com/event-catalog/eventcatalog.git',
Expand Down Expand Up @@ -62,6 +55,23 @@ describe('generator-federation', () => {
expect(services).toHaveLength(4);
});

it('if no copy configuration is provided then it clones target directory and copies all resources (e.g events, services, domains, teams, users) into the catalog', async () => {
await plugin(eventCatalogConfig, {
source: 'https://github.com/event-catalog/eventcatalog-ai-demo',
override: true,
destination: path.join(catalogDir),
});

const domains = await fs.readdir(path.join(catalogDir, 'domains'));
expect(domains).toHaveLength(3);

const teams = await fs.readdir(path.join(catalogDir, 'teams'));
expect(teams).toHaveLength(2);

const users = await fs.readdir(path.join(catalogDir, 'users'));
expect(users).toHaveLength(3);
});

describe('branch', () => {
it(
'clones the source directory (with the given branch) and copies the files specified in the content array to the destination directory',
Expand Down

0 comments on commit f94285b

Please sign in to comment.