Skip to content

Commit

Permalink
refactor(core): migrate users/teams to Astro new content layer API
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosallexandre committed Feb 18, 2025
1 parent 831081f commit bdaa435
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 76 deletions.
11 changes: 0 additions & 11 deletions default-files-for-collections/teams.md

This file was deleted.

11 changes: 0 additions & 11 deletions default-files-for-collections/users.md

This file was deleted.

8 changes: 6 additions & 2 deletions eventcatalog/src/components/DocsNavigation.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getUsers } from '@utils/users';
import config, { type CatalogConfig } from '@utils/eventcatalog-config/catalog';
import { buildUrl } from '@utils/url-builder';
import { getQueries } from '@utils/queries';
import { render } from 'astro:content';
const events = await getEvents({ getAllVersions: false });
const commands = await getCommands({ getAllVersions: false });
Expand Down Expand Up @@ -51,10 +52,13 @@ const visibleCollections: { [key: string]: boolean } = {
const fetchHeadings = allData.map(async (item) => {
const renderHeadings = showPageHeadings;
const headings = renderHeadings ? await item.render() : { headings: [] };
let headings = null;
if (renderHeadings) {
({ headings } = 'render' in item ? await item.render() : await render(item));
}
return {
...item,
headings: headings.headings,
headings: headings || [],
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { z, defineCollection, reference } from 'astro:content';
import { glob } from 'astro/loaders';
import { join } from 'node:path';

const badge = z.object({
content: z.string(),
Expand Down Expand Up @@ -253,7 +255,7 @@ const ubiquitousLanguages = defineCollection({
});

const users = defineCollection({
type: 'content',
loader: glob({ pattern: 'users/*.md', base: process.env.PROJECT_DIR, generateId: ({ data }) => data.id as string }),
schema: z.object({
id: z.string(),
name: z.string(),
Expand All @@ -274,7 +276,7 @@ const users = defineCollection({
});

const teams = defineCollection({
type: 'content',
loader: glob({ pattern: 'teams/*.md', base: process.env.PROJECT_DIR, generateId: ({ data }) => data.id as string }),
schema: z.object({
id: z.string(),
name: z.string(),
Expand Down
19 changes: 6 additions & 13 deletions eventcatalog/src/pages/docs/teams/[id]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import components from '@components/MDX/components';
// SideBars
import { getTeams } from '@utils/teams';
import { getEntry } from 'astro:content';
import { getEntry, render } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import OwnersList from '@components/Lists/OwnersList';
import PillListFlat from '@components/Lists/PillListFlat';
Expand All @@ -15,23 +15,16 @@ export async function getStaticPaths() {
const teams = await getTeams();
return teams.map((team) => ({
params: {
type: 'teams',
id: team.data.id,
},
props: {
type: 'team',
...team,
},
params: { id: team.data.id },
props: team,
}));
}
const { render, ...props } = Astro.props;
const { Content } = await render();
const props = Astro.props;
const { Content } = await render(props);
const membersRaw = props.data.members || [];
const members = await Promise.all(membersRaw.map((m) => getEntry(m)));
const members = (await Promise.all(membersRaw.map((m) => getEntry(m)))).filter(Boolean);
const domains = props.data.ownedDomains as CollectionEntry<'domains'>[];
const services = props.data.ownedServices as CollectionEntry<'services'>[];
Expand Down
20 changes: 7 additions & 13 deletions eventcatalog/src/pages/docs/users/[id]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import components from '@components/MDX/components';
// SideBars
import { render } from 'astro:content';
import type { CollectionEntry } from 'astro:content';
import OwnersList from '@components/Lists/OwnersList';
import PillListFlat from '@components/Lists/PillListFlat';
Expand All @@ -11,23 +12,16 @@ import { buildUrl } from '@utils/url-builder';
import VerticalSideBarLayout from '@layouts/VerticalSideBarLayout.astro';
export async function getStaticPaths() {
const teams = await getUsers();
const users = await getUsers();
return teams.map((team) => ({
params: {
type: team.collection,
id: team.data.id,
},
props: {
type: 'team',
...team,
},
return users.map((user) => ({
params: { id: user.data.id },
props: user,
}));
}
const { render, ...props } = Astro.props;
const { Content } = await render();
const props = Astro.props;
const { Content } = await render(props);
const domains = props.data.ownedDomains as CollectionEntry<'domains'>[];
const services = props.data.ownedServices as CollectionEntry<'services'>[];
Expand Down
4 changes: 2 additions & 2 deletions eventcatalog/src/utils/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const getUsers = async (): Promise<User[]> => {
});

const isOwnedByUserOrAssociatedTeam = (item: CollectionEntry<CollectionTypes>) => {
const associatedTeamsSlug: string[] = associatedTeams.map((team) => team.slug);
return item.data.owners?.some((owner) => owner.id === user.data.id || associatedTeamsSlug.includes(owner.id));
const associatedTeamsId: string[] = associatedTeams.map((team) => team.data.id);
return item.data.owners?.some((owner) => owner.id === user.data.id || associatedTeamsId.includes(owner.id));
};

const ownedServices = services.filter(isOwnedByUserOrAssociatedTeam);
Expand Down
12 changes: 0 additions & 12 deletions src/__tests__/catalog-to-astro-content-directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,6 @@ describe('catalog-to-astro-content-directory', () => {
});
});

describe('users', () => {
it('takes users from the catalog and puts it into the expected directory structure', async () => {
expect(existsSync(path.join(ASTRO_CONTENT_DIRECTORY, 'users', 'dboyne.md'))).toBe(true);
});
});

describe('teams', () => {
it('takes teams from the catalog and puts it into the expected directory structure', async () => {
expect(existsSync(path.join(ASTRO_CONTENT_DIRECTORY, 'teams', 'full-stack.md'))).toBe(true);
});
});

// describe('eventcatalog.config.js file', () => {
// it('adds cId missing property on the eventcatalog.config.js file', async () => {
// const file = await fs.readFile(path.join(CATALOG_DIR, 'eventcatalog.config.js'), 'utf8');
Expand Down
8 changes: 0 additions & 8 deletions src/catalog-to-astro-content-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ const ensureAstroCollectionNotEmpty = async (astroDir) => {
'events',
'commands',
'services',
'users',
'teams',
'domains',
'flows',
'pages',
Expand Down Expand Up @@ -82,18 +80,12 @@ const ensureAstroCollectionNotEmpty = async (astroDir) => {
export const catalogToAstro = async (source, astroDir) => {
const astroContentDir = path.join(astroDir, 'src/content/');

// Config file
const astroConfigFile = fs.readFileSync(path.join(astroContentDir, 'config.ts'));

// Clear the astro directory before we copy files over
fs.rmSync(astroContentDir, { recursive: true });

// Create the folder again
fs.mkdirSync(astroContentDir);

// Write config file back
fs.writeFileSync(path.join(astroContentDir, 'config.ts'), astroConfigFile);

// Verify required fields are in the catalog config file
await verifyRequiredFieldsAreInCatalogConfigFile(source);

Expand Down
2 changes: 0 additions & 2 deletions src/map-catalog-to-astro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const COLLECTION_KEYS = [
'events',
'commands',
'services',
'users',
'teams',
'domains',
'flows',
'pages',
Expand Down

0 comments on commit bdaa435

Please sign in to comment.