feat(config): add global -c/--config option and OPENAPI_GENERATOR_CLI_CONFIG env var for the openapitools.json location

IN
13h ago
+502-26

Fixes #782

What

Adds a way to point openapi-generator-cli at an openapitools.json outside the current working directory (monorepo use-case):

  • new global option -c, --config <path> on the root command — the existing --openapitools <openapitools.json> option is kept and works as an alias
  • new OPENAPI_GENERATOR_CLI_CONFIG environment variable as a fallback, so the path can be supplied without changing the command line (e.g. in CI)

Resolution order: explicit flag > OPENAPI_GENERATOR_CLI_CONFIG > ./openapitools.json (default).

Behavior guarantees:

  • when neither the flag nor the env var is set, nothing changes — ./openapitools.json is used and created on first run, exactly as before
  • relative paths are resolved against the current working directory
  • when an explicit path (flag or env var) is given but the file does not exist, the CLI fails fast with a clear error message and exit code 1 instead of silently creating/using a default config (the silent-failure mode also reported in cli#386)

Design note: design comment on #782.

How

  • ConfigService.configFileOrDefault() resolves the location (flag > env var > default). It remains the single source of truth for every read/write of openapitools.json, so all commands (version-manager list/set/resolve, use, generate, pass-through) pick it up without further changes.
  • AppModule registers the new option on the root program, and isVersionManagerResolveCommand() now also skips the new option forms (-c <path>, --config <path>, --config=<path>, -c<path>) when scanning argv.

Tests

Unit tests (config.service.spec.ts, app.module.spec.ts):

  • explicit path used via -c/--config and via --openapitools (existing behavior kept)
  • explicit path used via OPENAPI_GENERATOR_CLI_CONFIG
  • relative explicit paths (flag and env var) resolved against the cwd
  • flag takes precedence over the env var
  • default ./openapitools.json unchanged when neither is set
  • missing explicit file (flag and env var) → error message contains the resolved path and process.exit(1)
  • missing default file → no error, no exit (backwards compatible)
  • version-manager resolve detection skips the new option forms

Full suite: 263 tests / 7 suites passing, plus lint and build, run in a node:20 Docker container. Manually smoke-tested the built CLI: valid flag, valid env var, precedence (flag wins), missing-path error paths, and the untouched default flow.


Summary by cubic

Lets the CLI point at an openapitools.json outside the current working directory, for monorepos and CI.

  • Adds a global -c, --config <path> option before the command name, with --openapitools kept as a working alias; it is not registered with commander to avoid colliding with the Java generator's own generate -c/--config option, but is listed in help and stripped from the argv handed to commander.
  • Adds the OPENAPI_GENERATOR_CLI_CONFIG env var as a fallback; resolution order is flag, env var, then the default ./openapitools.json (unchanged when neither is set).
  • Relative paths resolve against the working directory, and a missing explicitly-set config now fails fast with a clear error and exit code 1 instead of creating a default config.
  • Only occurrences placed before the command name count; -c/--config after it belong to the invoked command and pass through untouched.