Skip to content

Commit 1d2a1a2

Browse files
feat(#644): add cli option for openapitools.json (#754)
1 parent c042887 commit 1d2a1a2

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

apps/generator-cli/src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {ConfigService, GeneratorService, PassThroughService, UIService, VersionM
1919
VersionManagerService,
2020
{
2121
provide: COMMANDER_PROGRAM,
22-
useValue: new Command('openapi-generator-cli').helpOption(false).usage('<command> [<args>]')
22+
useValue: new Command('openapi-generator-cli').helpOption(false).usage('<command> [<args>]').option( '--openapitools <openapitools.json>', 'Use the specified openapi-generator-cli configuration file')
2323
},
2424
{provide: LOGGER, useValue: console}
2525
]

apps/generator-cli/src/app/services/config.service.spec.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import { Test } from '@nestjs/testing';
2+
import { Command, createCommand } from 'commander';
23
import { ConfigService } from './config.service';
3-
import { LOGGER } from '../constants';
4+
import { LOGGER, COMMANDER_PROGRAM } from '../constants';
45

56
jest.mock('fs-extra');
67
// eslint-disable-next-line @typescript-eslint/no-var-requires
78
const fs = jest.mocked(require('fs-extra'));
89

910
describe('ConfigService', () => {
1011
let fixture: ConfigService;
12+
let program: Command;
1113

1214
const log = jest.fn();
1315

1416
beforeEach(async () => {
17+
program = createCommand();
18+
jest.spyOn(program, 'helpInformation');
19+
1520
const moduleRef = await Test.createTestingModule({
16-
providers: [ConfigService, { provide: LOGGER, useValue: { log } }],
21+
providers: [
22+
ConfigService,
23+
{ provide: LOGGER, useValue: { log } },
24+
{ provide: COMMANDER_PROGRAM, useValue: program },
25+
],
1726
}).compile();
1827

1928
fixture = moduleRef.get(ConfigService);
@@ -141,5 +150,39 @@ describe('ConfigService', () => {
141150
);
142151
});
143152
});
153+
154+
describe('configFileOrDefault()', () => {
155+
describe('--openapitools set', () => {
156+
beforeEach(async () => {
157+
program = createCommand();
158+
program.opts().openapitools = '/tmp/myopenapitools.json';
159+
160+
const moduleRef = await Test.createTestingModule({
161+
providers: [
162+
ConfigService,
163+
{ provide: LOGGER, useValue: { log } },
164+
{ provide: COMMANDER_PROGRAM, useValue: program },
165+
],
166+
}).compile();
167+
168+
fixture = moduleRef.get(ConfigService);
169+
fs.writeJSONSync.mockReset();
170+
fs.readJSONSync.mockReset();
171+
fs.ensureFileSync.mockReset();
172+
});
173+
it('returns path set at cli, if openapitools argument provided', () => {
174+
expect(fixture.configFile).toEqual('/tmp/myopenapitools.json');
175+
});
176+
});
177+
describe('--openapitools not set', () => {
178+
it('returns default path, if openapitools argument not provided', () => {
179+
expect(
180+
fixture.configFile.endsWith(
181+
'openapi-generator-cli/openapitools.json'
182+
)
183+
).toBeTruthy();
184+
});
185+
});
186+
});
144187
});
145188
});

apps/generator-cli/src/app/services/config.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import {Inject, Injectable} from '@nestjs/common';
22
import * as path from 'path';
3-
import {LOGGER} from '../constants';
3+
import {COMMANDER_PROGRAM, LOGGER} from '../constants';
44
import {set, get, has, merge} from 'lodash';
55
import * as fs from 'fs-extra';
6+
import { Command } from 'commander';
67

78
@Injectable()
89
export class ConfigService {
910

1011
public readonly cwd = process.env.PWD || process.env.INIT_CWD || process.cwd()
11-
public readonly configFile = path.resolve(this.cwd, 'openapitools.json')
12+
public readonly configFile = this.configFileOrDefault();
13+
14+
private configFileOrDefault() {
15+
this.program.parseOptions(process.argv);
16+
const conf = this.program.opts().openapitools;
17+
18+
if(!conf) {
19+
return path.resolve(this.cwd, 'openapitools.json');
20+
}
21+
22+
return path.isAbsolute(conf) ? conf : path.resolve(this.cwd, conf);
23+
}
1224

1325
public get useDocker() {
1426
return this.get('generator-cli.useDocker', false);
@@ -28,6 +40,7 @@ export class ConfigService {
2840

2941
constructor(
3042
@Inject(LOGGER) private readonly logger: LOGGER,
43+
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
3144
) {
3245
}
3346

0 commit comments

Comments
 (0)