diff --git a/packages/cactus-common/package-lock.json b/packages/cactus-common/package-lock.json index ced0620072..e273ddf71e 100644 --- a/packages/cactus-common/package-lock.json +++ b/packages/cactus-common/package-lock.json @@ -201,6 +201,12 @@ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.2.3.tgz", "integrity": "sha512-MN6ZpzmfNCRM+3t57PTJHgHyw/h4OWnZ6mR8P5j/uZtqQr46RRuDE/P+g3n0YR/AiYXeWixZZzaip77gdICfRg==" }, + "openapi-types": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-7.0.1.tgz", + "integrity": "sha512-6pi4/Fw+JIW1HHda2Ij7LRJ5QJ8f6YzaXnsRA6m44BJz8nLq/j5gVFzPBKJo+uOFhAeHqZC/3uzhTpYPga3Q/A==", + "dev": true + }, "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", diff --git a/packages/cactus-common/package.json b/packages/cactus-common/package.json index 848d9dda32..b1fc543dfb 100644 --- a/packages/cactus-common/package.json +++ b/packages/cactus-common/package.json @@ -74,6 +74,7 @@ "devDependencies": { "@types/joi": "14.3.4", "@types/json-stable-stringify": "1.0.32", - "@types/secp256k1": "4.0.1" + "@types/secp256k1": "4.0.1", + "openapi-types": "7.0.1" } -} \ No newline at end of file +} diff --git a/packages/cactus-common/src/main/typescript/index.ts b/packages/cactus-common/src/main/typescript/index.ts index 8f6c6cd3ca..20be4a6266 100755 --- a/packages/cactus-common/src/main/typescript/index.ts +++ b/packages/cactus-common/src/main/typescript/index.ts @@ -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"; diff --git a/packages/cactus-common/src/main/typescript/nodejs/openapi-spec-json-to-fs.ts b/packages/cactus-common/src/main/typescript/nodejs/openapi-spec-json-to-fs.ts new file mode 100644 index 0000000000..13a346d439 --- /dev/null +++ b/packages/cactus-common/src/main/typescript/nodejs/openapi-spec-json-to-fs.ts @@ -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 { + 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)); +}