Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/corporate maven repository #836

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions apps/generator-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,39 @@ import { VersionManagerController } from './controllers/version-manager.controll
import {
ConfigService,
GeneratorService,
NpmrcService,
PassThroughService,
UIService,
VersionManagerService,
} from './services';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { Agent } from 'https';

const proxyUrl = process.env.HTTP_PROXY || process.env.HTTPS_PROXY;
const httpModuleConfig: HttpModuleOptions = {};

if (proxyUrl) {
httpModuleConfig.proxy = false;
httpModuleConfig.httpsAgent = new HttpsProxyAgent(proxyUrl);
}
export const httpModuleConfigFactory = async (configService: ConfigService, npmrcService: NpmrcService): Promise<HttpModuleOptions> => {
const strictSsl = configService.useNpmrc() ? npmrcService.getStrictSsl() : true;
const rejectUnauthorized = configService.get('generator-cli.http.rejectUnauthorized', true);
const httpsAgent = !strictSsl || !rejectUnauthorized ? new Agent({
rejectUnauthorized: false,
}) : undefined;
return {
httpsAgent: httpsAgent,
};
};

@Module({
imports: [
HttpModule.register({
...httpModuleConfig,
HttpModule.registerAsync({
imports: [AppModule],
useFactory: httpModuleConfigFactory,
inject: [ConfigService, NpmrcService],
}),
],
controllers: [VersionManagerController],
exports: [ConfigService, NpmrcService],
providers: [
UIService,
ConfigService,
GeneratorService,
NpmrcService,
PassThroughService,
VersionManagerService,
{
Expand Down
40 changes: 38 additions & 2 deletions apps/generator-cli/src/app/services/config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test } from '@nestjs/testing';
import { Command, createCommand } from 'commander';
import { ConfigService } from './config.service';
import { LOGGER, COMMANDER_PROGRAM } from '../constants';
import * as path from 'path';

jest.mock('fs-extra');
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -56,7 +57,7 @@ describe('ConfigService', () => {
expect(fixture.get('unknown', 'foo')).toEqual('foo');
});
});

describe('the config has values', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
Expand Down Expand Up @@ -90,6 +91,41 @@ describe('ConfigService', () => {
);
});
});

describe('the config has values having placeholders', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
$schema: 'foo.json',
spaces: 4,
'generator-cli': {
version: '1.2.3',
repository: {
queryUrl: 'https://${__unit_test_username}:${__unit_test_password}@server/api',
downloadUrl: 'https://${__unit_test_non_matching}@server/api'
}
},
});
process.env['__unit_test_username'] = 'myusername';
process.env['__unit_test_password'] = 'mypassword';
});

afterEach(() => {
delete process.env['__unit_test_username'];
delete process.env['__unit_test_password'];
})

it('verify placeholder replaced with env vars', () => {
const value = fixture.get('generator-cli.repository.queryUrl');

expect(value).toEqual('https://myusername:mypassword@server/api');
});

it('verify placeholders not matching env vars are not replaced', () => {
const value = fixture.get('generator-cli.repository.downloadUrl');

expect(value).toEqual('https://${__unit_test_non_matching}@server/api');
});
});
});

describe('has()', () => {
Expand Down Expand Up @@ -178,7 +214,7 @@ describe('ConfigService', () => {
it('returns default path, if openapitools argument not provided', () => {
expect(
fixture.configFile.endsWith(
'openapi-generator-cli/openapitools.json'
path.join('openapi-generator-cli', 'openapitools.json')
)
).toBeTruthy();
});
Expand Down
34 changes: 31 additions & 3 deletions apps/generator-cli/src/app/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class ConfigService {
return this.get('generator-cli.dockerImageName', 'openapitools/openapi-generator-cli');
}

public useNpmrc() {
return this.get('generator-cli.useNpmrc', false);
}

private readonly defaultConfig = {
$schema: './node_modules/@openapitools/openapi-generator-cli/config.schema.json',
spaces: 2,
Expand All @@ -53,21 +57,45 @@ export class ConfigService {
}

set(path: string, value: unknown) {
this.write(set(this.read(), path, value))
this.write(set(this.read(false), path, value))
return this
}

private read() {
private read(replaceEnvVars: boolean = true) {
fs.ensureFileSync(this.configFile)

return merge(
const config = merge(
this.defaultConfig,
fs.readJSONSync(this.configFile, {throws: false, encoding: 'utf8'}),
)

return replaceEnvVars ? replaceEnvPlaceholders(config) : config
}

private write(config) {
fs.writeJSONSync(this.configFile, config, {encoding: 'utf8', spaces: config.spaces || 2})
}

}

function replaceEnvPlaceholders(config: any): any {
const envPlaceholderPattern = /\${(\w+)}/g;

const replacePlaceholders = (value: any): any => {
if (typeof value === 'string') {
return value.replace(envPlaceholderPattern, (_, varName) => {
return process.env[varName] || `\${${varName}}`;
});
} else if (Array.isArray(value)) {
return value.map(replacePlaceholders);
} else if (typeof value === 'object' && value !== null) {
return Object.keys(value).reduce((acc, key) => {
acc[key] = replacePlaceholders(value[key]);
return acc;
}, {});
}
return value;
};

return replacePlaceholders(config);
}
27 changes: 14 additions & 13 deletions apps/generator-cli/src/app/services/generator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GeneratorService } from './generator.service';
import { LOGGER } from '../constants';
import { VersionManagerService } from './version-manager.service';
import { ConfigService } from './config.service';
import * as path from 'path';

jest.mock('fs-extra');
jest.mock('glob');
Expand Down Expand Up @@ -139,7 +140,7 @@ describe('GeneratorService', () => {
appendix: string[]
) => ({
name,
command: `java -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(
command: `java -cp "/path/to/4.2.1.jar${path.delimiter}${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(
' '
)}`,
});
Expand All @@ -149,39 +150,39 @@ describe('GeneratorService', () => {
'foo.json',
[
cmd('[angular] abc/app/pet.yaml', [
`--input-spec="${cwd}/abc/app/pet.yaml"`,
`--input-spec="${path.resolve(cwd, 'abc/app/pet.yaml')}"`,
`--output="${cwd}/generated-sources/openapi/typescript-angular/pet"`,
`--generator-name="typescript-angular"`,
`--additional-properties="fileNaming=kebab-case,apiModulePrefix=Pet,npmName=petRestClient,supportsES6=true,withInterfaces=true"`,
]),
cmd('[angular] abc/app/car.yaml', [
`--input-spec="${cwd}/abc/app/car.yaml"`,
`--input-spec="${path.resolve(cwd, 'abc/app/car.yaml')}"`,
`--output="${cwd}/generated-sources/openapi/typescript-angular/car"`,
`--generator-name="typescript-angular"`,
`--additional-properties="fileNaming=kebab-case,apiModulePrefix=Car,npmName=carRestClient,supportsES6=true,withInterfaces=true"`,
]),
cmd('[baz] def/app/pet.yaml', [
`--input-spec="${cwd}/def/app/pet.yaml"`,
`--input-spec="${path.resolve(cwd, 'def/app/pet.yaml')}"`,
`--name="pet"`,
`--name-uc-first="Pet"`,
`--cwd="${cwd}"`,
`--base="pet.yaml"`,
`--dir="${cwd}/def/app"`,
`--path="${cwd}/def/app/pet.yaml"`,
`--dir="${path.resolve(cwd, 'def/app')}"`,
`--path="${path.resolve(cwd, 'def/app/pet.yaml')}"`,
`--rel-dir="def/app"`,
`--rel-path="def/app/pet.yaml"`,
`--ext="yaml"`,
'--some-bool',
'--some-int=1',
]),
cmd('[baz] def/app/car.json', [
`--input-spec="${cwd}/def/app/car.json"`,
`--input-spec="${path.resolve(cwd, 'def/app/car.json')}"`,
`--name="car"`,
`--name-uc-first="Car"`,
`--cwd="${cwd}"`,
`--base="car.json"`,
`--dir="${cwd}/def/app"`,
`--path="${cwd}/def/app/car.json"`,
`--dir="${path.resolve(cwd, 'def/app')}"`,
`--path="${path.resolve(cwd, 'def/app/car.json')}"`,
`--rel-dir="def/app"`,
`--rel-path="def/app/car.json"`,
`--ext="json"`,
Expand All @@ -194,12 +195,12 @@ describe('GeneratorService', () => {
'bar.json',
[
cmd('[bar] api/cat.yaml', [
`--input-spec="${cwd}/api/cat.yaml"`,
`--input-spec="${path.resolve(cwd, 'api/cat.yaml')}"`,
`--output="bar/cat"`,
'--some-bool',
]),
cmd('[bar] api/bird.json', [
`--input-spec="${cwd}/api/bird.json"`,
`--input-spec="${path.resolve(cwd, 'api/bird.json')}"`,
`--output="bar/bird"`,
'--some-bool',
]),
Expand All @@ -209,12 +210,12 @@ describe('GeneratorService', () => {
'bar.json',
[
cmdWithCustomJar('[bar] api/cat.yaml', '../some/custom.jar', [
`--input-spec="${cwd}/api/cat.yaml"`,
`--input-spec="${path.resolve(cwd, 'api/cat.yaml')}"`,
`--output="bar/cat"`,
'--some-bool',
]),
cmdWithCustomJar('[bar] api/bird.json', '../some/custom.jar', [
`--input-spec="${cwd}/api/bird.json"`,
`--input-spec="${path.resolve(cwd, 'api/bird.json')}"`,
`--output="bar/bird"`,
'--some-bool',
]),
Expand Down
1 change: 1 addition & 0 deletions apps/generator-cli/src/app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './config.service'
export * from './generator.service'
export * from './pass-through.service'
export * from './version-manager.service'
export * from './npmrc.service'
Loading
Loading