From 44b816d5181611d1bc882bd87a20d98820a3f9a6 Mon Sep 17 00:00:00 2001 From: carlosallexandre Date: Tue, 18 Feb 2025 09:01:46 -0300 Subject: [PATCH 1/2] refactor(core): migrate `users`/`teams` to Astro new content layer API --- default-files-for-collections/teams.md | 11 ---------- default-files-for-collections/users.md | 11 ---------- .../src/components/DocsNavigation.astro | 8 ++++++-- .../{content/config.ts => content.config.ts} | 14 +++++++++++-- .../src/pages/docs/teams/[id]/index.astro | 19 ++++++------------ .../src/pages/docs/users/[id]/index.astro | 20 +++++++------------ eventcatalog/src/utils/users.ts | 4 ++-- ...catalog-to-astro-content-directory.spec.ts | 12 ----------- src/catalog-to-astro-content-directory.js | 10 +--------- src/map-catalog-to-astro.js | 2 -- 10 files changed, 34 insertions(+), 77 deletions(-) delete mode 100644 default-files-for-collections/teams.md delete mode 100644 default-files-for-collections/users.md rename eventcatalog/src/{content/config.ts => content.config.ts} (93%) diff --git a/default-files-for-collections/teams.md b/default-files-for-collections/teams.md deleted file mode 100644 index c502b0af..00000000 --- a/default-files-for-collections/teams.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -id: dboyne -name: David Boyne -avatarUrl: "https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png" -role: Lead developer -email: test@test.com -hidden: true -slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123 ---- - - \ No newline at end of file diff --git a/default-files-for-collections/users.md b/default-files-for-collections/users.md deleted file mode 100644 index c502b0af..00000000 --- a/default-files-for-collections/users.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -id: dboyne -name: David Boyne -avatarUrl: "https://pbs.twimg.com/profile_images/1262283153563140096/DYRDqKg6_400x400.png" -role: Lead developer -email: test@test.com -hidden: true -slackDirectMessageUrl: https://yourteam.slack.com/channels/boyney123 ---- - - \ No newline at end of file diff --git a/eventcatalog/src/components/DocsNavigation.astro b/eventcatalog/src/components/DocsNavigation.astro index b72062d3..b488b30d 100644 --- a/eventcatalog/src/components/DocsNavigation.astro +++ b/eventcatalog/src/components/DocsNavigation.astro @@ -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 }); @@ -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 || [], }; }); diff --git a/eventcatalog/src/content/config.ts b/eventcatalog/src/content.config.ts similarity index 93% rename from eventcatalog/src/content/config.ts rename to eventcatalog/src/content.config.ts index 1b60dfe5..c1b51ad9 100644 --- a/eventcatalog/src/content/config.ts +++ b/eventcatalog/src/content.config.ts @@ -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(), @@ -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(), @@ -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(), diff --git a/eventcatalog/src/pages/docs/teams/[id]/index.astro b/eventcatalog/src/pages/docs/teams/[id]/index.astro index fb6c7205..3f1cf268 100644 --- a/eventcatalog/src/pages/docs/teams/[id]/index.astro +++ b/eventcatalog/src/pages/docs/teams/[id]/index.astro @@ -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'; @@ -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'>[]; diff --git a/eventcatalog/src/pages/docs/users/[id]/index.astro b/eventcatalog/src/pages/docs/users/[id]/index.astro index a80516eb..5ab87368 100644 --- a/eventcatalog/src/pages/docs/users/[id]/index.astro +++ b/eventcatalog/src/pages/docs/users/[id]/index.astro @@ -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'; @@ -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'>[]; diff --git a/eventcatalog/src/utils/users.ts b/eventcatalog/src/utils/users.ts index 6cc60dc6..49eaa783 100644 --- a/eventcatalog/src/utils/users.ts +++ b/eventcatalog/src/utils/users.ts @@ -32,8 +32,8 @@ export const getUsers = async (): Promise => { }); const isOwnedByUserOrAssociatedTeam = (item: CollectionEntry) => { - 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); diff --git a/src/__tests__/catalog-to-astro-content-directory.spec.ts b/src/__tests__/catalog-to-astro-content-directory.spec.ts index 2538316d..bb922484 100644 --- a/src/__tests__/catalog-to-astro-content-directory.spec.ts +++ b/src/__tests__/catalog-to-astro-content-directory.spec.ts @@ -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'); diff --git a/src/catalog-to-astro-content-directory.js b/src/catalog-to-astro-content-directory.js index ca555966..349b0694 100644 --- a/src/catalog-to-astro-content-directory.js +++ b/src/catalog-to-astro-content-directory.js @@ -45,8 +45,6 @@ const ensureAstroCollectionNotEmpty = async (astroDir) => { 'events', 'commands', 'services', - 'users', - 'teams', 'domains', 'flows', 'pages', @@ -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 }); + 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); diff --git a/src/map-catalog-to-astro.js b/src/map-catalog-to-astro.js index 7860d37a..ed0133e4 100644 --- a/src/map-catalog-to-astro.js +++ b/src/map-catalog-to-astro.js @@ -4,8 +4,6 @@ const COLLECTION_KEYS = [ 'events', 'commands', 'services', - 'users', - 'teams', 'domains', 'flows', 'pages', From f0e2ef1804de29b02a1fc3233587166b277f4b33 Mon Sep 17 00:00:00 2001 From: David Boyne Date: Tue, 4 Mar 2025 09:39:54 +0000 Subject: [PATCH 2/2] Create empty-eyes-yell.md --- .changeset/empty-eyes-yell.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/empty-eyes-yell.md diff --git a/.changeset/empty-eyes-yell.md b/.changeset/empty-eyes-yell.md new file mode 100644 index 00000000..5a2407bc --- /dev/null +++ b/.changeset/empty-eyes-yell.md @@ -0,0 +1,5 @@ +--- +"@eventcatalog/core": patch +--- + +refactor(core): migrate `users`/`teams` to Astro new content layer API