Skip to content

Commit 4c0788d

Browse files
authored
Support optional prefix for endpoints markdown files (#10)
* 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.
1 parent 63f2aa8 commit 4c0788d

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-vuepress-markdown",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "This package generates vuepress compatible markdown from an openapi schema",
55
"main": "./dist/openapi-vuepress-markdown.js",
66
"repository": "https://github.com/lune-climate/openapi-vuepress-markdown.git",

src/markdownAdapters.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ function generateResource(
394394
export function generateMarkdownFiles(
395395
api: OpenAPIV3.Document,
396396
refs: IRefs,
397-
endpointsDirectory?: string,
398-
resourcesDirectory?: string,
397+
outputDirectory?: string,
398+
endpointsPrefix?: string,
399399
) {
400400
// resources
401401
const schemas = api.components?.schemas as Record<string, OpenAPIV3.SchemaObject> | undefined
@@ -405,19 +405,21 @@ export function generateMarkdownFiles(
405405
return generateResource(name, schemaObject, refs)
406406
},
407407
)
408-
resources.map((resource: Resource) =>
409-
generateResourceMarkdownFile(resource, resourcesDirectory),
410-
)
408+
resources.map((resource: Resource) => generateResourceMarkdownFile(resource, outputDirectory))
411409

412410
// endpoints
413411
const endpoints = generateEndpoints(api, refs)
414412
const markdownTemplatesData = groupEndpointsByTag(api, endpoints)
415413
markdownTemplatesData.map((markdownTemplateData) =>
416-
generateEndpointsMarkdownFile(markdownTemplateData, endpointsDirectory),
414+
generateEndpointsMarkdownFile(markdownTemplateData, outputDirectory, endpointsPrefix),
417415
)
418416
}
419417

420-
function generateEndpointsMarkdownFile(data: MarkdownTemplateData, outputDirectory?: string) {
418+
function generateEndpointsMarkdownFile(
419+
data: MarkdownTemplateData,
420+
outputDirectory?: string,
421+
endpointsPrefix?: string,
422+
) {
421423
const templateContent = readFileSync(path.join(__dirname, '/../endpoints.md'))
422424
const template = Handlebars.compile(templateContent.toString())
423425

@@ -430,7 +432,7 @@ function generateEndpointsMarkdownFile(data: MarkdownTemplateData, outputDirecto
430432
.replace(/\s\s+/g, ' ')
431433
.replace(/\s/g, '-')
432434
.toLowerCase() + '.md'
433-
const file = path.join(outputDirectory!, filename)
435+
const file = path.join(outputDirectory!, `${endpointsPrefix ?? ''}${filename}`)
434436
writeFileSync(file, result)
435437
console.log(`Endpoint saved: ${file}`)
436438
return

src/openapi-vuepress-markdown.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ async function main(): Promise<void> {
1010
program
1111
.requiredOption('-s, --schema <schema>', 'OpenAPI spec in json or yml format')
1212
.option(
13-
'-e, --endpoints-directory <endpoints-directory>',
14-
'Endpoints destination directory. If not specified, then the output is redirected to standard output',
13+
'-o, --output-directory <output-directory>',
14+
'Output destination directory. If not specified, then the output is redirected to standard output',
1515
)
1616
.option(
17-
'-r, --resources-directory <resources-directory>',
18-
'Resources destination directory. If not specified, then the output is redirected to standard output',
17+
'-e, --endpoints-prefix <endpoints-prefix>',
18+
'Endpoint file prefix, to avoid filename conflicts with resources. Defaults to blank (no prefix)',
1919
)
2020
.parse()
2121
.parse()
2222

2323
const options = program.opts()
24-
const { schema, endpointsDirectory, resourcesDirectory } = options
24+
const { schema, outputDirectory, endpointsPrefix } = options
2525

2626
const parser = new SwaggerParser()
2727
// let it throw
2828
const api = await parser.bundle(schema)
2929
const refs = parser.$refs
3030

31-
generateMarkdownFiles(api, refs, endpointsDirectory, resourcesDirectory)
31+
generateMarkdownFiles(api, refs, outputDirectory, endpointsPrefix)
3232
}
3333

3434
// eslint-disable-next-line no-extra-semi

0 commit comments

Comments
 (0)