Skip to content

Commit ce2ac4c

Browse files
committed
fix: add quotes to accept spaces in the JAVA_HOME path
1 parent f96430c commit ce2ac4c

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* If JAVA_HOME is set, it returns `$JAVA_HOME/bin/java`
3+
* otherwise it returns `java` and it has to be in the `PATH`
4+
*/
5+
export const javaCmd: string = process.env['JAVA_HOME']
6+
? `"${process.env['JAVA_HOME']}/bin/java"`
7+
: 'java';

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Test } from '@nestjs/testing';
22
import { GeneratorService } from './generator.service';
33
import { LOGGER } from '../constants';
4+
import { javaCmd } from '../helpers';
45
import { VersionManagerService } from './version-manager.service';
56
import { ConfigService } from './config.service';
67

@@ -145,9 +146,6 @@ describe('GeneratorService', () => {
145146
)}`,
146147
});
147148

148-
const javaHome = process.env['JAVA_HOME'];
149-
const javaCmd = javaHome ? `${javaHome}/bin/java` : 'java';
150-
151149
describe.each([
152150
[
153151
'foo.json',

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as os from 'os';
1010
import { VersionManagerService } from './version-manager.service';
1111
import { ConfigService } from './config.service';
1212
import { LOGGER } from '../constants';
13+
import { javaCmd } from '../helpers';
1314

1415
interface GeneratorConfig {
1516
glob: string;
@@ -242,17 +243,10 @@ export class GeneratorService {
242243
)}" org.openapitools.codegen.OpenAPIGenerator`
243244
: `-jar "${cliPath}"`;
244245

245-
const javaHome = process.env['JAVA_HOME'];
246-
const javaCmd = javaHome ? `${javaHome}/bin/java` : 'java';
247-
248-
return [
249-
javaCmd,
250-
process.env['JAVA_OPTS'],
251-
subCmd,
252-
'generate',
253-
appendix,
254-
].filter(isString).join(' ');
255-
}
246+
return [javaCmd, process.env['JAVA_OPTS'], subCmd, 'generate', appendix]
247+
.filter(isString)
248+
.join(' ');
249+
};
256250

257251
private isWin = () => process.platform === 'win32';
258252
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Test } from '@nestjs/testing';
22
import chalk from 'chalk';
33
import { Command, createCommand } from 'commander';
44
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
5+
import { javaCmd } from '../helpers';
56
import { GeneratorService } from './generator.service';
67
import { PassThroughService } from './pass-through.service';
78
import { VersionManagerService } from './version-manager.service';
@@ -192,7 +193,7 @@ describe('PassThroughService', () => {
192193
await program.parseAsync([name, ...argv], { from: 'user' });
193194
expect(childProcess.spawn).toHaveBeenNthCalledWith(
194195
1,
195-
'java -jar "/some/path/to/4.2.1.jar"',
196+
`${javaCmd} -jar "/some/path/to/4.2.1.jar"`,
196197
[name, ...argv],
197198
{
198199
stdio: 'inherit',
@@ -206,7 +207,7 @@ describe('PassThroughService', () => {
206207
await program.parseAsync([name, ...argv], { from: 'user' });
207208
expect(childProcess.spawn).toHaveBeenNthCalledWith(
208209
1,
209-
'java java-opt-1=1 -jar "/some/path/to/4.2.1.jar"',
210+
`${javaCmd} java-opt-1=1 -jar "/some/path/to/4.2.1.jar"`,
210211
[name, ...argv],
211212
{
212213
stdio: 'inherit',
@@ -224,7 +225,7 @@ describe('PassThroughService', () => {
224225

225226
expect(childProcess.spawn).toHaveBeenNthCalledWith(
226227
1,
227-
`java -cp "${[
228+
`${javaCmd} -cp "${[
228229
'/some/path/to/4.2.1.jar',
229230
'../some/custom.jar',
230231
].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`,
@@ -303,7 +304,7 @@ describe('PassThroughService', () => {
303304
it('spawns the correct process', () => {
304305
expect(childProcess.spawn).toHaveBeenNthCalledWith(
305306
1,
306-
'java -jar "/some/path/to/4.2.1.jar"',
307+
`${javaCmd} -jar "/some/path/to/4.2.1.jar"`,
307308
cmd.split(' '),
308309
{ stdio: 'inherit', shell: true }
309310
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { exec, spawn } from 'child_process';
44
import { Command } from 'commander';
55
import { isString, startsWith, trim } from 'lodash';
66
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
7+
import { javaCmd } from '../helpers';
78
import { GeneratorService } from './generator.service';
89
import { VersionManagerService } from './version-manager.service';
910
import { ConfigService } from './config.service';
@@ -142,7 +143,7 @@ export class PassThroughService {
142143
)}" org.openapitools.codegen.OpenAPIGenerator`
143144
: `-jar "${cliPath}"`;
144145

145-
return ['java', process.env['JAVA_OPTS'], subCmd]
146+
return [javaCmd, process.env['JAVA_OPTS'], subCmd]
146147
.filter(isString)
147148
.join(' ');
148149
}

0 commit comments

Comments
 (0)