Skip to content

Commit

Permalink
Support optional prefix for endpoints markdown files (#10)
Browse files Browse the repository at this point in the history
* Revert "Allow endpoints and resources to have different output directories (#9)"

This reverts commit 63f2aa8.

Vuepress does not allow sub-directories to be shown in the same sidebar, therefore reverting.

* Support optional prefix for endpoints markdown files

This is useful in order to avoid naming conflicts between resource and endpoint files.

This is the second attempt to achieve the objective.
Previous attempt: see commit 63f2aa8.
  • Loading branch information
rbruggem authored Dec 8, 2021
1 parent 63f2aa8 commit 4c0788d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-vuepress-markdown",
"version": "0.0.9",
"version": "0.0.10",
"description": "This package generates vuepress compatible markdown from an openapi schema",
"main": "./dist/openapi-vuepress-markdown.js",
"repository": "https://github.com/lune-climate/openapi-vuepress-markdown.git",
Expand Down
18 changes: 10 additions & 8 deletions src/markdownAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ function generateResource(
export function generateMarkdownFiles(
api: OpenAPIV3.Document,
refs: IRefs,
endpointsDirectory?: string,
resourcesDirectory?: string,
outputDirectory?: string,
endpointsPrefix?: string,
) {
// resources
const schemas = api.components?.schemas as Record<string, OpenAPIV3.SchemaObject> | undefined
Expand All @@ -405,19 +405,21 @@ export function generateMarkdownFiles(
return generateResource(name, schemaObject, refs)
},
)
resources.map((resource: Resource) =>
generateResourceMarkdownFile(resource, resourcesDirectory),
)
resources.map((resource: Resource) => generateResourceMarkdownFile(resource, outputDirectory))

// endpoints
const endpoints = generateEndpoints(api, refs)
const markdownTemplatesData = groupEndpointsByTag(api, endpoints)
markdownTemplatesData.map((markdownTemplateData) =>
generateEndpointsMarkdownFile(markdownTemplateData, endpointsDirectory),
generateEndpointsMarkdownFile(markdownTemplateData, outputDirectory, endpointsPrefix),
)
}

function generateEndpointsMarkdownFile(data: MarkdownTemplateData, outputDirectory?: string) {
function generateEndpointsMarkdownFile(
data: MarkdownTemplateData,
outputDirectory?: string,
endpointsPrefix?: string,
) {
const templateContent = readFileSync(path.join(__dirname, '/../endpoints.md'))
const template = Handlebars.compile(templateContent.toString())

Expand All @@ -430,7 +432,7 @@ function generateEndpointsMarkdownFile(data: MarkdownTemplateData, outputDirecto
.replace(/\s\s+/g, ' ')
.replace(/\s/g, '-')
.toLowerCase() + '.md'
const file = path.join(outputDirectory!, filename)
const file = path.join(outputDirectory!, `${endpointsPrefix ?? ''}${filename}`)
writeFileSync(file, result)
console.log(`Endpoint saved: ${file}`)
return
Expand Down
12 changes: 6 additions & 6 deletions src/openapi-vuepress-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ async function main(): Promise<void> {
program
.requiredOption('-s, --schema <schema>', 'OpenAPI spec in json or yml format')
.option(
'-e, --endpoints-directory <endpoints-directory>',
'Endpoints destination directory. If not specified, then the output is redirected to standard output',
'-o, --output-directory <output-directory>',
'Output destination directory. If not specified, then the output is redirected to standard output',
)
.option(
'-r, --resources-directory <resources-directory>',
'Resources destination directory. If not specified, then the output is redirected to standard output',
'-e, --endpoints-prefix <endpoints-prefix>',
'Endpoint file prefix, to avoid filename conflicts with resources. Defaults to blank (no prefix)',
)
.parse()
.parse()

const options = program.opts()
const { schema, endpointsDirectory, resourcesDirectory } = options
const { schema, outputDirectory, endpointsPrefix } = options

const parser = new SwaggerParser()
// let it throw
const api = await parser.bundle(schema)
const refs = parser.$refs

generateMarkdownFiles(api, refs, endpointsDirectory, resourcesDirectory)
generateMarkdownFiles(api, refs, outputDirectory, endpointsPrefix)
}

// eslint-disable-next-line no-extra-semi
Expand Down

0 comments on commit 4c0788d

Please sign in to comment.