Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): migrate users/teams to Astro new content layer API #1169

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-eyes-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/core": patch
---

refactor(core): migrate `users`/`teams` to Astro new content layer API
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.

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 @@ -252,8 +254,16 @@ const ubiquitousLanguages = defineCollection({
}),
});

const projectDirBase = (() => {
if (process.platform === 'win32') {
const projectDirPath = process.env.PROJECT_DIR!.replace(/\\/g, '/');
return projectDirPath.startsWith('/') ? projectDirPath : `/${projectDirPath}`;
}
return process.env.PROJECT_DIR;
})();

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

const teams = defineCollection({
type: 'content',
loader: glob({ pattern: 'teams/*.md', base: projectDirBase, 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
10 changes: 1 addition & 9 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',
'changelogs',
Expand Down Expand Up @@ -81,18 +79,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 });
if (fs.existsSync(astroContentDir)) 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