forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add OpenAPI spec exporter utility function
This will be useful to reuse among the individual packages own openapi-spec.ts files which at the moment all repeat the same logic to export themselves to JSON... Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export * from "./public-api"; | ||
export { IListenOptions, Servers } from "./servers"; | ||
export { | ||
DEFAULT_FILENAME, | ||
DEFAULT_RELATIVE_PATH, | ||
IOpenApiSpecJsonToFsOptions, | ||
openApiSpecJsonToFs, | ||
} from "./nodejs/openapi-spec-json-to-fs"; |
51 changes: 51 additions & 0 deletions
51
packages/cactus-common/src/main/typescript/nodejs/openapi-spec-json-to-fs.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,51 @@ | ||
import { OpenAPIV3 } from "openapi-types"; | ||
|
||
/** | ||
* Options to customize how the `openApiSpecJsonToFs()` function exports | ||
* an OpenAPI spec to JSON format to the local file-system. | ||
*/ | ||
export interface IOpenApiSpecJsonToFsOptions { | ||
/** | ||
* The actual specification document (object) that holds the information we | ||
* will export to JSON onto the local filesystem. | ||
*/ | ||
openApiSpec: OpenAPIV3.Document; | ||
/** | ||
* Absolute path on the file-system for the calling script file | ||
* (e.g. `__dirname` in a NodeJS environment). | ||
*/ | ||
callerScriptPath: string; | ||
/** | ||
* Defaults to "../json/generated/" | ||
*/ | ||
relativePath?: string; | ||
/** | ||
* The target filename to export the JSON created from the OpenAPI spec. | ||
* Defaults to "openapi-spec.json" | ||
*/ | ||
filename?: string; | ||
} | ||
|
||
export const DEFAULT_RELATIVE_PATH: string = "../json/generated/"; | ||
|
||
export const DEFAULT_FILENAME: string = "openapi-spec.json"; | ||
|
||
export async function openApiSpecJsonToFs( | ||
opts: IOpenApiSpecJsonToFsOptions | ||
): Promise<void> { | ||
const fnTag = "#openApiSpecJsonToFs()"; | ||
|
||
const fs = await import("fs"); | ||
const path = await import("path"); | ||
|
||
const filename = opts.filename || DEFAULT_FILENAME; | ||
const relativePath = opts.relativePath || DEFAULT_RELATIVE_PATH; | ||
const callerScriptPath = opts.callerScriptPath; | ||
const defaultDest = path.join(callerScriptPath, relativePath, filename); | ||
const destination = process.argv[2] || defaultDest; | ||
|
||
// tslint:disable-next-line: no-console | ||
console.log(`${fnTag} destination=${destination}`); | ||
|
||
fs.writeFileSync(destination, JSON.stringify(opts.openApiSpec, null, 4)); | ||
} |