-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #857 from MoralisWeb3/readme-generator
docs: readme generator
- Loading branch information
Showing
34 changed files
with
666 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NodePlopAPI } from 'plop'; | ||
import SdkReadmeGenerator from './readme'; | ||
|
||
export default function setNextGenerators(plop: NodePlopAPI) { | ||
SdkReadmeGenerator(plop); | ||
} |
101 changes: 101 additions & 0 deletions
101
packages/codegen/src/sdk/generators/readme/ReadmeGenerator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* eslint-disable no-console */ | ||
import { ActionType } from 'node-plop'; | ||
import { paths } from './constants'; | ||
import path from 'node:path'; | ||
import { PackageData, PackageInfo } from 'packages/codegen/src/utils/PackageInfo'; | ||
import { markdownTable } from 'markdown-table'; | ||
import { packageTypeTitles } from './config'; | ||
|
||
const FALLBACK_TYPE = 'other'; | ||
|
||
const isSupportedPackageType = (pkg: PackageData) => { | ||
const isValid = pkg.type && Object.keys(packageTypeTitles).includes(pkg.type); | ||
if (!isValid) { | ||
console.warn(`Package ${pkg.name} omitted as the type "${pkg.type}" is not implemented`); | ||
} | ||
return isValid; | ||
}; | ||
const isActivePackage = (pkg: PackageData) => pkg.isPublishable && !pkg.isDeprecated; | ||
const hasMoralisMetadata = (pkg: PackageData) => pkg.moralis !== undefined; | ||
const sortByPackageType = (typeA: string, typeB: string) => | ||
Object.keys(packageTypeTitles).indexOf(typeA) - Object.keys(packageTypeTitles).indexOf(typeB); | ||
|
||
const createMarkdownLink = (title: string, url: string) => `[${title}](${url})`; | ||
|
||
export class ReadmeGenerator { | ||
private packageInfo: PackageInfo; | ||
|
||
constructor() { | ||
this.packageInfo = PackageInfo.create(); | ||
} | ||
|
||
get packagesByType() { | ||
return this.packageInfo.packages | ||
.filter(isActivePackage) | ||
.filter(hasMoralisMetadata) | ||
.filter(isSupportedPackageType) | ||
.reduce<Record<string, PackageData[]>>((acc, pkg) => { | ||
const type = pkg.type ?? FALLBACK_TYPE; | ||
|
||
return { | ||
...acc, | ||
[type]: [...(acc[type] ?? []), pkg], | ||
}; | ||
}, {}); | ||
} | ||
|
||
generatePackageTable = (packageType: string, rootPath: string) => { | ||
const packages = this.packagesByType[packageType]; | ||
|
||
return `\ | ||
## ${packageTypeTitles[packageType].title} | ||
${packageTypeTitles[packageType].description} | ||
${markdownTable([ | ||
['package', 'changelog', 'description'], | ||
...packages.map((pck) => [ | ||
createMarkdownLink(pck.name, path.relative(rootPath, path.join(pck.fullPath, 'README.md'))), | ||
pck.changelog | ||
? createMarkdownLink(pck.version, path.relative(rootPath, path.join(pck.fullPath, 'CHANGELOG.md'))) | ||
: pck.version, | ||
pck.description, | ||
]), | ||
])} | ||
`; | ||
}; | ||
|
||
getAllPackagesTables({ rootPath }: { rootPath: string }) { | ||
const packageTypes = Object.keys(this.packagesByType); | ||
|
||
return `\ | ||
${packageTypes | ||
.sort(sortByPackageType) | ||
.map((pkg) => this.generatePackageTable(pkg, rootPath)) | ||
.join('\n')} | ||
`; | ||
} | ||
|
||
public get actions(): ActionType[] { | ||
return [ | ||
{ | ||
type: 'add', | ||
templateFile: path.join(paths.templates, 'README.md.hbs'), | ||
path: path.join(paths.root, 'README.md'), | ||
force: true, | ||
data: { | ||
packagesTables: this.getAllPackagesTables({ rootPath: paths.root }), | ||
}, | ||
}, | ||
{ | ||
type: 'add', | ||
templateFile: path.join(paths.templates, 'README.md.hbs'), | ||
path: path.join(paths.umbrellaPackage, 'README.md'), | ||
force: true, | ||
data: { | ||
packagesTables: this.getAllPackagesTables({ rootPath: paths.umbrellaPackage }), | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// We only show package types that are defined in this list | ||
export const packageTypeTitles: Record<string, { title: string; description: string }> = { | ||
main: { title: 'Main modules', description: 'The main modules of the SDK' }, | ||
integration: { title: 'Integrations', description: 'Integrations with frameworks and services' }, | ||
feature: { | ||
title: 'Features', | ||
description: | ||
'Feature modules. Only use these directly for advanced use-cases, the prefered way is to use these features via the umbrella package "moralis"', | ||
}, | ||
core: { title: 'Core modules', description: 'Core modules are the building blocks of Moralis.' }, | ||
utils: { | ||
title: 'Utilities', | ||
description: 'Utilities, types, operations and datatypes related used by other modules.', | ||
}, | ||
tools: { | ||
title: 'Tools', | ||
description: 'Stand-alone tools and utilities', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { fileURLToPath } from 'node:url'; | ||
import path from 'node:path'; | ||
|
||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const root = path.join(dirname, '../../../../../..'); | ||
|
||
export const paths = { | ||
root, | ||
packages: path.join(root, 'packages'), | ||
templates: path.join(dirname, './templates'), | ||
umbrellaPackage: path.join(root, 'packages/moralis'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodePlopAPI } from 'plop'; | ||
import { ReadmeGenerator } from './ReadmeGenerator'; | ||
|
||
export default function NextReadmeGenerator(plop: NodePlopAPI) { | ||
plop.setGenerator('sdk-readme', { | ||
description: 'readme the whole SDK', | ||
prompts: [], | ||
actions: () => new ReadmeGenerator().actions, | ||
}); | ||
} |
Oops, something went wrong.
1310a8b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coverage