Skip to content

Commit

Permalink
feat(common): add OpenAPI spec exporter utility function
Browse files Browse the repository at this point in the history
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
petermetz committed Dec 1, 2020
1 parent 281bf95 commit 6d2e93c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/cactus-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/cactus-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
6 changes: 6 additions & 0 deletions packages/cactus-common/src/main/typescript/index.ts
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";
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));
}

0 comments on commit 6d2e93c

Please sign in to comment.