Skip to content

Commit 9374ae6

Browse files
committed
fix: allow custom generator to pass through generate command
1 parent 0f86410 commit 9374ae6

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class GeneratorService {
2929
) {
3030
}
3131

32-
public async generate(...keys: string[]) {
32+
public async generate(customGenerator?: string, ...keys: string[]) {
3333

3434
const cwd = this.configService.cwd
3535
const generators = Object.entries(this.configService.get<{ [name: string]: GeneratorConfig }>(this.configPath, {}))
@@ -54,7 +54,7 @@ export class GeneratorService {
5454
if (!globPattern) {
5555
return [{
5656
name: `[${name}] ${params.inputSpec}`,
57-
command: this.buildCommand(cwd, params)
57+
command: this.buildCommand(cwd, params, customGenerator)
5858
}]
5959
}
6060

@@ -66,7 +66,7 @@ export class GeneratorService {
6666

6767
return glob.sync(globPattern, {cwd}).map(spec => ({
6868
name: `[${name}] ${spec}`,
69-
command: this.buildCommand(cwd, params, spec)
69+
command: this.buildCommand(cwd, params, customGenerator, spec)
7070
}))
7171
}))
7272

@@ -95,7 +95,7 @@ export class GeneratorService {
9595
}).join('\n'))
9696
}
9797

98-
private buildCommand(cwd: string, params: Record<string, unknown>, specFile?: string) {
98+
private buildCommand(cwd: string, params: Record<string, unknown>, customGenerator?: string, specFile?: string) {
9999
const absoluteSpecPath = specFile ? path.resolve(cwd, specFile) : String(params.inputSpec)
100100

101101
const command = Object.entries({
@@ -139,19 +139,26 @@ export class GeneratorService {
139139
ext: ext.split('.').slice(-1).pop()
140140
}
141141

142-
return this.cmd(Object.entries(placeholders)
142+
return this.cmd(customGenerator, Object.entries(placeholders)
143143
.filter(([, replacement]) => !!replacement)
144144
.reduce((cmd, [search, replacement]) => {
145145
return cmd.split(`#{${search}}`).join(replacement)
146146
}, command))
147147
}
148148

149-
private cmd = (appendix: string) => [
150-
'java',
151-
process.env['JAVA_OPTS'],
152-
`-jar "${this.versionManager.filePath()}"`,
153-
'generate',
154-
appendix,
155-
].filter(isString).join(' ');
149+
private cmd = (customGenerator: string | undefined, appendix: string) => {
150+
const cliPath = this.versionManager.filePath();
151+
const subCmd = customGenerator
152+
? `-cp "${[cliPath, customGenerator].join(this.isWin() ? ';' : ':')}" org.openapitools.codegen.OpenAPIGenerator`
153+
: `-jar "${cliPath}"`;
154+
return [
155+
'java',
156+
process.env['JAVA_OPTS'],
157+
subCmd,
158+
'generate',
159+
appendix,
160+
].filter(isString).join(' ');
161+
}
156162

163+
private isWin = () => process.platform === "win32"
157164
}

apps/generator-cli/src/app/services/pass-through.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ export class PassThroughService {
5656
.option("--generator-key <generator...>", "Run generator by key. Separate by comma to run many generators")
5757
.action(async (_, cmd) => {
5858
if (cmd.args.length === 0 || cmd.opts().generatorKey) {
59+
const customGenerator = this.program.opts()?.customGenerator;
5960
const generatorKeys = cmd.opts().generatorKey || [];
6061

6162
if (this.generatorService.enabled) {
6263
// @todo cover by unit test
63-
if (!await this.generatorService.generate(...generatorKeys)) {
64+
if (!await this.generatorService.generate(customGenerator, ...generatorKeys)) {
6465
this.logger.log(chalk.red('Code generation failed'));
6566
process.exit(1);
6667
}

0 commit comments

Comments
 (0)