diff --git a/packages/openapi-merge-cli/README.md b/packages/openapi-merge-cli/README.md index 49a1e71..a4552b8 100644 --- a/packages/openapi-merge-cli/README.md +++ b/packages/openapi-merge-cli/README.md @@ -73,6 +73,28 @@ If you wish, you may write your configuration file in YAML format and then run: npx openapi-merge-cli --config path/to/openapi-merge.yaml ``` +You can pass the configuration as a JSON string: + +```shell +npx openapi-merge-cli --json-config '{ + "inputs": [ + ... + ], + "output": ... +} +' +``` + +You can pass the configuration as a YAML string: + +```shell +npx openapi-merge-cli --yaml-config '--- +inputs: + ... +output: ... +' +``` + And the merge should be run and complete! Congratulations and enjoy! If you experience any issues then please [raise them in the bug tracker][1]. diff --git a/packages/openapi-merge-cli/src/index.ts b/packages/openapi-merge-cli/src/index.ts index 828c77d..f0457bb 100644 --- a/packages/openapi-merge-cli/src/index.ts +++ b/packages/openapi-merge-cli/src/index.ts @@ -21,7 +21,9 @@ const program = new Command(); program.version(pjson.version); program - .option('-c, --config ', 'The path to the configuration file for the merge tool.'); + .option('-c, --config ', 'The path to the configuration file for the merge tool.') + .option('-j, --json-config ', 'The configuration as a JSON string to be parsed by the merge tool.') + .option('-y, --yaml-config ', 'The configuration as a YAML string to be parsed by the merge tool.'); class LogWithMillisDiff { @@ -129,7 +131,40 @@ export async function main(): Promise { program.parse(process.argv); logger.log(`## ${process.argv[0]}: Running v${pjson.version}`); - const config = await loadConfiguration(program.config); + var config + if (program.jsonConfig) { + try { + config = JSON.parse(program.jsonConfig); + } catch (e) { + // We need to make sure the code does propagate the error to the console! + if (e instanceof Error) { + config = e.message; + } else if (typeof e === 'string' || e instanceof String) { + // Make sure it's a string not a String + // Not sure i really need this + config = e.toString() + } else { + config = 'There was an error parsing the config' + } + } + } else if (program.yamlConfig) { + try { + config = yaml.load(program.yamlConfig); + } catch (e) { + // We need to make sure the code does propagate the error to the console! + if (e instanceof Error) { + config = e.message; + } else if (typeof e === 'string' || e instanceof String) { + // Make sure it's a string not a String + // Not sure i really need this + config = e.toString() + } else { + config = 'There was an error parsing the config' + } + } + } else { + config = await loadConfiguration(program.config); + } if (typeof config === 'string') { console.error(config);