Skip to content

Commit

Permalink
Merge pull request #11 from boyney123/feat-description-and-maintainers
Browse files Browse the repository at this point in the history
  • Loading branch information
boyney123 authored Aug 25, 2021
2 parents 3810932 + 33e2ce4 commit 5485d12
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 56 deletions.
12 changes: 11 additions & 1 deletion src/models/Registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Schema from './Schema'
import fs from 'fs-extra'
import path from 'path'

export default class Registry {
constructor(awsSchemas, allRulesForEvents, eventBusName) {
Expand All @@ -24,7 +26,15 @@ export default class Registry {
)
}

getEvents(){
getSourceMetadata(source) {
try {
return fs.readJsonSync(path.join(__dirname, `../../data/${source}/source-metadata.json`))
} catch (error) {
return {}
}
}

getEvents() {
return this.events
}

Expand Down
10 changes: 10 additions & 0 deletions src/models/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class Schema {
const source = Content['x-amazon-events-source']
const detailType = Content['x-amazon-events-detail-type']
let eventMetaData = {}
let sourceMetaData = {}

try {
eventMetaData = fs.readJsonSync(
Expand All @@ -16,7 +17,16 @@ export default class Schema {
eventMetaData = {}
}

try {
sourceMetaData = fs.readJsonSync(
path.join(__dirname, `../../data/${source}/source-metadata.json`)
)
} catch (error) {
sourceMetaData = {}
}

this.eventMetaData = eventMetaData
this.sourceMetaData = sourceMetaData

this.source = source
this.detailType = detailType
Expand Down
3 changes: 2 additions & 1 deletion src/parsers/docuowl/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export default async ({ buildDir, registry }) => {
const makeDocs = registry.getEvents().map(async (schema) => {
const source = schema.source
const detailType = schema.detailType
const { description: sourceDescription, maintainers = [] } = registry.getSourceMetadata(source)

await fs.ensureDirSync(path.join(buildDir, source))

await fs.writeFileSync(
path.join(buildDir, `${source}/meta.md`),
generateSourceMetadata({ source, sourceDescription: schema.sourceDescription })
generateSourceMetadata({ source, sourceDescription: sourceDescription, maintainers })
)

// Generate the file for each detail type
Expand Down
75 changes: 54 additions & 21 deletions src/parsers/docuowl/utils/generate-docuowl-markdown.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,90 @@
export const generateContentForDetailType = ({ detailType, description = '', rulesForDetailType, schemaName, schemaVersion, descriptionsForProperties}) => {

return `---
export const generateContentForDetailType = ({
detailType,
description = '',
rulesForDetailType,
schemaName,
schemaVersion,
descriptionsForProperties,
}) => {
return `---
Title: ${detailType}
---
${description}
${descriptionsForProperties ? `
${
descriptionsForProperties
? `
## Detail Definitions
${Object.keys(descriptionsForProperties).map(rootKey => `
${Object.keys(descriptionsForProperties).map(
(rootKey) => `
#### ${rootKey}
${Object.keys(descriptionsForProperties[rootKey]).map(property => `
- \`${property}\` - ${descriptionsForProperties[rootKey][property]?.type} - ${descriptionsForProperties[rootKey][property].description}`)}
`)}` : ''}
${Object.keys(descriptionsForProperties[rootKey]).map(
(property) => `
- \`${property}\` - ${descriptionsForProperties[rootKey][property]?.type} - ${descriptionsForProperties[rootKey][property].description}`
)}
`
)}`
: ''
}
<br />
${rulesForDetailType.length > 0 ? `
${
rulesForDetailType.length > 0
? `
## Event Rules
${rulesForDetailType.map(rule => `- \`${rule}\``)}
` : ''}
${rulesForDetailType.map((rule) => `- \`${rule}\``)}
`
: ''
}
<br />
# **Schema details**
#### Schema name: \`${schemaName}\`
#### Version: \`${schemaVersion}\`
`;
`
}


export const generateSideNotesForDetailType = ({ schema, example }) => {
return `
return `
${example ? `
${
example
? `
#! Event Example
\`\`\`json
${JSON.stringify(example, null, 4)}
\`\`\`
` : ''}
`
: ''
}
#! Event Schema
\`\`\`json
${JSON.stringify(schema, null, 4)}
\`\`\``;
\`\`\``
}

export const generateSourceMetadata = ({ source, sourceDescription }) => {
return `---
export const generateSourceMetadata = ({ source, sourceDescription, maintainers }) => {
return `---
Title: ${source}
---
${sourceDescription}`;
}
${sourceDescription}
${
maintainers.length > 0
? `
**Maintainers**:
${maintainers.join(', ')}
`
: ''
}
`
}
4 changes: 2 additions & 2 deletions src/parsers/flow/src/init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import fs from 'fs-extra'
import path from 'path'

export default async ({ registry }) => {
await fs.writeFile(path.join(__dirname, './lib/data/registry.json'), registry.toString())
Expand Down
16 changes: 14 additions & 2 deletions src/parsers/flow/src/lib/components/Diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,30 @@ export default memo(({ registry = {} }) => {
const eventNodeWidth = 450
const paddingBetweenSourceAndEvents = 50
const ruleHeight = 120
let sourceMetadata = {}

const eventSourceYPosition = eventSourceIndex * (10 + sourceNodeHeight) + currentYPosition

const eventsForSource = eventsGroupedByEventSource[eventSource]

const sourceYPosition = currentYPosition + (eventsForSource.length * eventNodeHeight) / 2
const { description, maintainers = [] } =
eventsForSource.find((event) => event?.sourceMetaData?.description !== undefined)
?.sourceMetaData || {}

const sourceYPosition =
currentYPosition +
(eventsForSource.length * eventNodeHeight) / 2 -
(description ? eventNodeHeight / 2 : 0)

// The soruce node
const sourceNode = {
id: eventSource,
type: 'sourceNode', // input node
data: { source: eventSource, description },
data: {
source: eventSource,
description,
maintainers,
},
style: { width: 'auto' },
position: {
x: 0,
Expand Down
12 changes: 10 additions & 2 deletions src/parsers/flow/src/lib/components/SourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import React, { memo } from 'react'

import { Handle } from 'react-flow-renderer'

export default memo(({ data: { source } = {} }) => {
export default memo(({ data: { source, description, maintainers = [] } = {}, ...rest }) => {
return (
<div className="border-4 border-red-500 bg-gray-200 rounded-xl">
<div className="mx-auto text-center">
<div className="w-full bg-red-500 py-2 text-white">
<div className="mb-1">Event Source</div>
</div>
<div className="p-4">
<div className="text-xs inline-block pl-2 pr-6">{source}</div>
<div className="text-xs inline-block pl-2 pr-6 font-bold">{source}</div>
{description && (
<div className="mt-2 text-xs italic w-64 block pl-2 pr-6">{description}</div>
)}
{maintainers && maintainers.length > 0 && (
<div className="mt-2 text-xs italic w-64 block pl-2 pr-6">
<span className="font-bold">Maintainers</span>: {maintainers.join(', ')}
</div>
)}
<Handle type="source" position="right" id="a" style={{ background: '#e91e63' }} />
</div>
</div>
Expand Down
14 changes: 10 additions & 4 deletions src/parsers/slate/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ export default async ({ eventBus, buildDir, registry }) => {
const markdown = [{ h1: `AWS EventBridge: Event bus ${eventBus}` }, { p: registry.description }]

Object.keys(eventsBySource).map((eventSource) => {
const { description: sourceDescription, maintainers = [] } =
registry.getSourceMetadata(eventSource)

markdown.push({ h1: eventSource })
eventsBySource[eventSource].map((event) => {
const hasDescriptionsOnEventProperties = Object.keys(event.properties).length > 0

if (sourceDescription)
markdown.push({ p: `<strong>Description:</strong> ${sourceDescription}` })
if (maintainers.length > 0) {
markdown.push({ p: `<strong>Maintained by:</strong> ${maintainers.join(', ')}` })
}

eventsBySource[eventSource].map((event) => {
markdown.push({ h2: event.detailType })
markdown.push({ p: event.description })

Expand Down Expand Up @@ -87,8 +95,6 @@ export default async ({ eventBus, buildDir, registry }) => {
})
})
}


})
})

Expand Down
54 changes: 31 additions & 23 deletions src/utils/generate-metadata-templates.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require('dotenv').config({ path: path.resolve(process.cwd(), '.env') })
import path from 'path'
import fs from 'fs'
import { v4 as uuid } from 'uuid';
import jsf from 'json-schema-faker';
import { v4 as uuid } from 'uuid'
import jsf from 'json-schema-faker'

import { getAllSchemas, getAllSchemasAsJSONSchema } from './aws'

Expand All @@ -14,29 +14,29 @@ const parseSchemaIntoTemplate = (test) => {
return Object.keys(definitions).reduce((templates, definition) => {
const { properties = {} } = definitions[definition]

const definitionLowerCase = definition.toLocaleLowerCase();
const definitionLowerCase = definition.toLocaleLowerCase()

templates[definitionLowerCase] = templates[definitionLowerCase] || {}

templates[definitionLowerCase] = Object.keys(properties).reduce((newProperties, property) => {
const { type, $ref } = properties[property]

if($ref){
if ($ref) {
return newProperties
}

newProperties[property] = {
type,
description: '',
example: ''
example: '',
}

return newProperties
}, {})

// remove any data we are not interested in
if(Object.keys(templates[definitionLowerCase]).length === 0){
delete templates[definitionLowerCase];
if (Object.keys(templates[definitionLowerCase]).length === 0) {
delete templates[definitionLowerCase]
}

return templates
Expand Down Expand Up @@ -73,36 +73,44 @@ const generateMetadataFromSchemas = async () => {

const events = schemasGroupedBySource[source]

Object.keys(events).forEach((event) => {
fs.writeFileSync(
`${folderDir}/source-metadata.json`,
JSON.stringify(
{
description: '',
maintainers: [],
},
null,
4
)
)

const { detail } = jsf.generate(events[event].JSONSchema);
Object.keys(events).forEach((event) => {
const { detail } = jsf.generate(events[event].JSONSchema)

const extendedSchema = parseSchemaIntoTemplate(events[event])

const file = {
description: `${event} event raised on the ${source} event source`,
detail: {
...extendedSchema
...extendedSchema,
},
example: {
"detail-type": event,
"resources": [],
"detail": detail,
"id": uuid(),
"source": source,
"time": new Date(),
"region": process.env.REGION,
"version": events[event].SchemaVersion,
"account": "YOUR_ACCOUNT"
}
'detail-type': event,
resources: [],
detail: detail,
id: uuid(),
source: source,
time: new Date(),
region: process.env.REGION,
version: events[event].SchemaVersion,
account: 'YOUR_ACCOUNT',
},
}

fs.writeFileSync(`${folderDir}/${event}.json`, JSON.stringify(file, null, 4))
})
})

}

generateMetadataFromSchemas()


0 comments on commit 5485d12

Please sign in to comment.