Skip to content

Commit 46ad3d9

Browse files
committed
feat(cli): add environment variable support for CLI commands
1 parent 47b6748 commit 46ad3d9

File tree

2 files changed

+88
-71
lines changed

2 files changed

+88
-71
lines changed

.changeset/clean-grapes-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/cli": minor
3+
---
4+
5+
Added environment variable support for CLI arguments using Yargs `.env()` method to parse environment variables with matching prefixes.

packages/cli/src/index.ts

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@ yargs(hideBin(process.argv))
4343
'stats [api]',
4444
'Show statistics for an API description.',
4545
(yargs) =>
46-
yargs.positional('api', { type: 'string' }).option({
47-
config: { description: 'Path to the config file.', type: 'string' },
48-
'lint-config': {
49-
description: 'Severity level for config file linting.',
50-
choices: ['warn', 'error', 'off'] as ReadonlyArray<RuleSeverity>,
51-
default: 'warn' as RuleSeverity,
52-
},
53-
format: {
54-
description: 'Use a specific output format.',
55-
choices: ['stylish', 'json', 'markdown'] as ReadonlyArray<OutputFormat>,
56-
default: 'stylish' as OutputFormat,
57-
},
58-
}),
46+
yargs
47+
.env('REDOCLY_CLI_STATS')
48+
.positional('api', { type: 'string' })
49+
.option({
50+
config: { description: 'Path to the config file.', type: 'string' },
51+
'lint-config': {
52+
description: 'Severity level for config file linting.',
53+
choices: ['warn', 'error', 'off'] as ReadonlyArray<RuleSeverity>,
54+
default: 'warn' as RuleSeverity,
55+
},
56+
format: {
57+
description: 'Use a specific output format.',
58+
choices: ['stylish', 'json', 'markdown'] as ReadonlyArray<OutputFormat>,
59+
default: 'stylish' as OutputFormat,
60+
},
61+
}),
5962
(argv) => {
6063
process.env.REDOCLY_CLI_COMMAND = 'stats';
6164
commandWrapper(handleStats)(argv);
@@ -66,6 +69,7 @@ yargs(hideBin(process.argv))
6669
'Split an API description into a multi-file structure.',
6770
(yargs) =>
6871
yargs
72+
.env('REDOCLY_CLI_SPLIT')
6973
.positional('api', {
7074
description: 'API description file that you want to split',
7175
type: 'string',
@@ -104,6 +108,7 @@ yargs(hideBin(process.argv))
104108
'Join multiple API descriptions into one [experimental].',
105109
(yargs) =>
106110
yargs
111+
.env('REDOCLY_CLI_JOIN')
107112
.positional('apis', {
108113
array: true,
109114
type: 'string',
@@ -154,6 +159,7 @@ yargs(hideBin(process.argv))
154159
false,
155160
(yargs) =>
156161
yargs
162+
.env('REDOCLY_CLI_PUSH_STATUS')
157163
.positional('pushId', {
158164
description: 'Push id.',
159165
type: 'string',
@@ -205,6 +211,7 @@ yargs(hideBin(process.argv))
205211
'Push documents to Reunite.',
206212
(yargs) =>
207213
yargs
214+
.env('REDOCLY_CLI_PUSH')
208215
.positional('files', {
209216
type: 'string',
210217
array: true,
@@ -313,59 +320,61 @@ yargs(hideBin(process.argv))
313320
'lint [apis...]',
314321
'Lint an API or Arazzo description.',
315322
(yargs) =>
316-
yargs.positional('apis', { array: true, type: 'string', demandOption: true }).option({
317-
format: {
318-
description: 'Use a specific output format.',
319-
choices: [
320-
'stylish',
321-
'codeframe',
322-
'json',
323-
'checkstyle',
324-
'codeclimate',
325-
'summary',
326-
'markdown',
327-
'github-actions',
328-
] as ReadonlyArray<OutputFormat>,
329-
default: 'codeframe' as OutputFormat,
330-
},
331-
'max-problems': {
332-
requiresArg: true,
333-
description: 'Reduce output to a maximum of N problems.',
334-
type: 'number',
335-
default: 100,
336-
coerce: validatePositiveNumber('max-problems', true),
337-
},
338-
'generate-ignore-file': {
339-
description: 'Generate an ignore file.',
340-
type: 'boolean',
341-
},
342-
'skip-rule': {
343-
description: 'Ignore certain rules.',
344-
array: true,
345-
type: 'string',
346-
},
347-
'skip-preprocessor': {
348-
description: 'Ignore certain preprocessors.',
349-
array: true,
350-
type: 'string',
351-
},
352-
'lint-config': {
353-
description: 'Severity level for config file linting.',
354-
choices: ['warn', 'error', 'off'] as ReadonlyArray<RuleSeverity>,
355-
default: 'warn' as RuleSeverity,
356-
},
357-
config: {
358-
description: 'Path to the config file.',
359-
requiresArg: true,
360-
type: 'string',
361-
},
362-
extends: {
363-
description: 'Override extends configurations (defaults or config file settings).',
364-
requiresArg: true,
365-
array: true,
366-
type: 'string',
367-
},
368-
}),
323+
yargs
324+
.env('REDOCLY_CLI_LINT')
325+
.positional('apis', { array: true, type: 'string', demandOption: true })
326+
.option({
327+
format: {
328+
description: 'Use a specific output format.',
329+
choices: [
330+
'stylish',
331+
'codeframe',
332+
'json',
333+
'checkstyle',
334+
'codeclimate',
335+
'summary',
336+
'markdown',
337+
'github-actions',
338+
] as ReadonlyArray<OutputFormat>,
339+
default: 'codeframe' as OutputFormat,
340+
},
341+
'max-problems': {
342+
requiresArg: true,
343+
description: 'Reduce output to a maximum of N problems.',
344+
type: 'number',
345+
default: 100,
346+
},
347+
'generate-ignore-file': {
348+
description: 'Generate an ignore file.',
349+
type: 'boolean',
350+
},
351+
'skip-rule': {
352+
description: 'Ignore certain rules.',
353+
array: true,
354+
type: 'string',
355+
},
356+
'skip-preprocessor': {
357+
description: 'Ignore certain preprocessors.',
358+
array: true,
359+
type: 'string',
360+
},
361+
'lint-config': {
362+
description: 'Severity level for config file linting.',
363+
choices: ['warn', 'error', 'off'] as ReadonlyArray<RuleSeverity>,
364+
default: 'warn' as RuleSeverity,
365+
},
366+
config: {
367+
description: 'Path to the config file.',
368+
requiresArg: true,
369+
type: 'string',
370+
},
371+
extends: {
372+
description: 'Override extends configurations (defaults or config file settings).',
373+
requiresArg: true,
374+
array: true,
375+
type: 'string',
376+
},
377+
}),
369378
(argv) => {
370379
process.env.REDOCLY_CLI_COMMAND = 'lint';
371380
commandWrapper(handleLint)(argv);
@@ -376,6 +385,7 @@ yargs(hideBin(process.argv))
376385
'Bundle a multi-file API description to a single file.',
377386
(yargs) =>
378387
yargs
388+
.env('REDOCLY_CLI_BUNDLE')
379389
.positional('apis', { array: true, type: 'string', demandOption: true })
380390
.options({
381391
output: {
@@ -453,7 +463,7 @@ yargs(hideBin(process.argv))
453463
'check-config',
454464
'Lint the Redocly configuration file.',
455465
async (yargs) =>
456-
yargs.option({
466+
yargs.env('REDOCLY_CLI_CHECK_CONFIG').option({
457467
config: {
458468
description: 'Path to the config file.',
459469
type: 'string',
@@ -473,7 +483,7 @@ yargs(hideBin(process.argv))
473483
'login',
474484
'Log in to Redocly.',
475485
async (yargs) =>
476-
yargs.options({
486+
yargs.env('REDOCLY_CLI_LOGIN').options({
477487
residency: {
478488
description: 'Residency of the application. Defaults to `us`.',
479489
alias: ['r'],
@@ -503,7 +513,7 @@ yargs(hideBin(process.argv))
503513
'preview',
504514
'Preview Redocly project using one of the product NPM packages.',
505515
(yargs) =>
506-
yargs.options({
516+
yargs.env('REDOCLY_CLI_PREVIEW').options({
507517
product: {
508518
type: 'string',
509519
choices: ['redoc', 'revel', 'reef', 'realm', 'redoc-revel', 'redoc-reef', 'revel-reef'],
@@ -605,6 +615,7 @@ yargs(hideBin(process.argv))
605615
'Creates or updates translations.yaml files and fills them with missing built-in translations and translations from the redocly.yaml and sidebars.yaml files.',
606616
(yargs) =>
607617
yargs
618+
.env('REDOCLY_CLI_TRANSLATE')
608619
.positional('locale', {
609620
description:
610621
'Locale code to generate translations for, or `all` for all current project locales.',
@@ -635,6 +646,7 @@ yargs(hideBin(process.argv))
635646
'Helper function to eject project elements for customization.',
636647
(yargs) =>
637648
yargs
649+
.env('REDOCLY_CLI_EJECT')
638650
.positional('type', {
639651
description:
640652
'Specifies what type of project element to eject. Currently this value must be `component`.',
@@ -675,13 +687,13 @@ yargs(hideBin(process.argv))
675687
'Run Arazzo tests.',
676688
(yargs) => {
677689
return yargs
690+
.env('REDOCLY_CLI_RESPECT')
678691
.positional('files', {
679692
describe: 'Test files or glob pattern.',
680693
type: 'string',
681694
array: true,
682695
default: [],
683696
})
684-
.env('REDOCLY_CLI_RESPECT')
685697
.options({
686698
input: {
687699
alias: 'i',
@@ -769,11 +781,11 @@ yargs(hideBin(process.argv))
769781
'Auto-generate arazzo description file from an API description.',
770782
(yargs) => {
771783
return yargs
784+
.env('REDOCLY_CLI_GENERATE_ARAZZO')
772785
.positional('descriptionPath', {
773786
describe: 'Description file path.',
774787
type: 'string',
775788
})
776-
.env('REDOCLY_CLI_RESPECT')
777789
.options({
778790
'output-file': {
779791
alias: 'o',

0 commit comments

Comments
 (0)