Skip to content

Commit

Permalink
feat(OpenAPITools#644): add cli option for openapitools.json
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasphair committed Feb 16, 2024
1 parent 720b405 commit bb02422
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/generator-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {ConfigService, GeneratorService, PassThroughService, UIService, VersionM
VersionManagerService,
{
provide: COMMANDER_PROGRAM,
useValue: new Command('openapi-generator-cli').helpOption(false).usage('<command> [<args>]')
useValue: new Command('openapi-generator-cli').helpOption(false).usage('<command> [<args>]').option( '--openapitools <openapitools.json>', 'Use the specified openapi-generator-cli configuration file')
},
{provide: LOGGER, useValue: console}
]
Expand Down
101 changes: 99 additions & 2 deletions apps/generator-cli/src/app/services/config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { Test } from '@nestjs/testing';
import { Command, createCommand } from 'commander';
import { ConfigService } from './config.service';
import { LOGGER } from '../constants';
import { LOGGER, COMMANDER_PROGRAM } from '../constants';

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

describe('ConfigService', () => {
let fixture: ConfigService;
let program: Command;

const log = jest.fn();

beforeEach(async () => {
program = createCommand();
jest.spyOn(program, 'helpInformation');

const moduleRef = await Test.createTestingModule({
providers: [ConfigService, { provide: LOGGER, useValue: { log } }],
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();

fixture = moduleRef.get(ConfigService);
Expand Down Expand Up @@ -141,5 +150,93 @@ describe('ConfigService', () => {
);
});
});

describe('configFileOrDefault()', () => {
describe('--openapitools set', () => {
beforeEach(async () => {
program = createCommand();
program.opts().openapitools = '/tmp/myopenapitools.json';

const moduleRef = await Test.createTestingModule({
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();

fixture = moduleRef.get(ConfigService);
fs.writeJSONSync.mockReset();
fs.readJSONSync.mockReset();
fs.ensureFileSync.mockReset();
});
it('returns path set at cli, if openapitools argument provided', () => {
expect(fixture.configFile).toEqual('/tmp/myopenapitools.json');
});
});
describe('--openapitools not set', () => {
it('returns default path, if openapitools argument not provided', () => {
expect(
fixture.configFile.endsWith(
'openapi-generator-cli/openapitools.json'
)
).toBeTruthy();
});
});
});
});
});

//describe('ConfigService From CLI Config', () => {
// let fixture: ConfigService;
// let program: Command;
//
// const log = jest.fn();
//
// beforeEach(async () => {
// program = createCommand();
// program.opts().openapitools = '/tmp/openapitools.json';
//
// const moduleRef = await Test.createTestingModule({
// providers: [
// ConfigService,
// { provide: LOGGER, useValue: { log } },
// { provide: COMMANDER_PROGRAM, useValue: program },
// ],
// }).compile();
//
// fixture = moduleRef.get(ConfigService);
// fs.writeJSONSync.mockReset();
// fs.readJSONSync.mockReset();
// fs.ensureFileSync.mockReset();
// });
//
// describe('API', () => {
// describe('configFileOrDefault()', () => {
// describe('--openapitools set', () => {
// beforeEach(async () => {
// program.opts().openapitools = '/tmp/openapitools.json';
//
// const moduleRef = await Test.createTestingModule({
// providers: [
// ConfigService,
// { provide: LOGGER, useValue: { log } },
// { provide: COMMANDER_PROGRAM, useValue: program },
// ],
// }).compile();
//
// fixture = moduleRef.get(ConfigService);
// });
// it('returns path set at cli, if openapitools argument provided', () => {
// expect(fixture.configFile).toEqual('/tmp/openapitools.json');
// });
// });
// describe('--openapitools not set', () => {
// it('returns default path, if openapitools argument not provided', () => {
// expect(fixture.configFile).toEqual('/tmp/openapitools.json');
// });
// });
// });
// });
//});
//
17 changes: 15 additions & 2 deletions apps/generator-cli/src/app/services/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import {Inject, Injectable} from '@nestjs/common';
import * as path from 'path';
import {LOGGER} from '../constants';
import {COMMANDER_PROGRAM, LOGGER} from '../constants';
import {set, get, has, merge} from 'lodash';
import * as fs from 'fs-extra';
import { Command } from 'commander';

@Injectable()
export class ConfigService {

public readonly cwd = process.env.PWD || process.env.INIT_CWD || process.cwd()
public readonly configFile = path.resolve(this.cwd, 'openapitools.json')
public readonly configFile = this.configFileOrDefault();

private configFileOrDefault() {
this.program.parseOptions(process.argv);
const conf = this.program.opts().openapitools;

if(!conf) {
return path.resolve(this.cwd, 'openapitools.json');
}

return path.isAbsolute(conf) ? conf : path.resolve(this.cwd, conf);
}

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

constructor(
@Inject(LOGGER) private readonly logger: LOGGER,
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
) {
}

Expand Down

0 comments on commit bb02422

Please sign in to comment.