Skip to content

Commit 57c6064

Browse files
authored
remove yeoman options/arguments api support (#31387)
1 parent fd04bed commit 57c6064

File tree

12 files changed

+132
-148
lines changed

12 files changed

+132
-148
lines changed

cli/cli.spec.ts

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { execaCommandSync } from 'execa';
99
import { coerce } from 'semver';
1010
import type FullEnvironment from 'yeoman-environment';
1111

12+
import type { JHipsterCommandDefinition } from '../generators/index.ts';
1213
import { createBlueprintFiles, defaultHelpers as helpers } from '../lib/testing/index.ts';
1314

1415
import type JHipsterCommand from './jhipster-command.ts';
@@ -37,15 +38,6 @@ const cliBlueprintFiles = {
3738
'generators/foo/index.js': `export const createGenerator = async env => {
3839
const BaseGenerator = await env.requireGenerator('jhipster:base');
3940
return class extends BaseGenerator {
40-
constructor(args, opts, features) {
41-
super(args, opts, features);
42-
43-
this.option('foo-bar', {
44-
description: 'Sample option',
45-
type: Boolean,
46-
});
47-
}
48-
4941
get [BaseGenerator.INITIALIZING]() {
5042
/* eslint-disable no-console */
5143
console.log('Running foo');
@@ -57,6 +49,18 @@ const cliBlueprintFiles = {
5749
}
5850
};
5951
};
52+
53+
export const command = {
54+
configs: {
55+
fooBar: {
56+
cli: {
57+
description: 'Sample option',
58+
type: Boolean,
59+
},
60+
scope: 'none',
61+
},
62+
},
63+
};
6064
`,
6165
};
6266

@@ -76,16 +80,21 @@ const cliSharedBlueprintFiles = {
7680
'generators/bar/index.js': `export const createGenerator = async env => {
7781
const BaseGenerator = await env.requireGenerator('jhipster:base');
7882
return class extends BaseGenerator {
79-
constructor(args, options) {
80-
super(args, options);
81-
this.option('foo', {
82-
description: 'foo description',
83-
type: Boolean,
84-
});
85-
}
8683
get [BaseGenerator.INITIALIZING]() {}
8784
};
8885
};
86+
87+
export const command = {
88+
configs: {
89+
foo: {
90+
cli: {
91+
description: 'foo description',
92+
type: Boolean,
93+
},
94+
scope: 'none',
95+
},
96+
},
97+
};
8998
`,
9099
};
91100

@@ -170,22 +179,34 @@ describe('cli', () => {
170179
let generator: Awaited<ReturnType<typeof helpers.instantiateDummyBaseCoreGenerator>>;
171180
let runArgs: any[];
172181
let env: FullEnvironment;
182+
let command: JHipsterCommandDefinition;
173183

174184
beforeEach(async () => {
185+
command = {
186+
arguments: {},
187+
configs: {
188+
foo: {
189+
cli: {
190+
description: 'Foo',
191+
type: Boolean,
192+
},
193+
scope: 'none',
194+
},
195+
fooBar: {
196+
cli: {
197+
description: 'Foo bar',
198+
type: Boolean,
199+
},
200+
scope: 'none',
201+
},
202+
},
203+
};
175204
getCommand.mockImplementation(actualGetCommand);
176205

177-
generator = await helpers.instantiateDummyBaseCoreGenerator();
178-
env = generator.env;
179-
Object.assign(generator._options, {
180-
foo: {
181-
description: 'Foo',
182-
},
183-
'foo-bar': {
184-
description: 'Foo bar',
185-
},
186-
});
187-
const runSpy = esmocha.spyOn(env, 'run');
188-
runSpy.mockImplementation((...args) => {
206+
const BaseGenerator = (await import('../generators/base/index.ts')).default;
207+
env = (await helpers.createTestEnv()) as FullEnvironment;
208+
generator = new (helpers.createDummyGenerator(BaseGenerator))([], { env });
209+
env.run = esmocha.fn<typeof env.run>((...args) => {
189210
runArgs = args;
190211
return Promise.resolve();
191212
});
@@ -197,7 +218,9 @@ describe('cli', () => {
197218
if (namespace === 'jhipster:mocked') {
198219
return {
199220
namespace,
200-
importModule: async () => ({}),
221+
importModule: async () => ({
222+
command,
223+
}),
201224
resolved: __filename,
202225
instantiateHelp: <G>() => Promise.resolve(generator as G),
203226
packageNamespace: undefined,
@@ -235,7 +258,7 @@ describe('cli', () => {
235258

236259
describe('with argument', () => {
237260
beforeEach(() => {
238-
generator._arguments.push({ name: 'name', type: String });
261+
command.arguments!.name = { type: String, scope: 'none' };
239262
argv = ['jhipster', 'jhipster', 'mocked', 'Foo', '--foo', '--foo-bar'];
240263
});
241264

@@ -252,7 +275,7 @@ describe('cli', () => {
252275

253276
describe('with variable arguments', () => {
254277
beforeEach(() => {
255-
generator._arguments.push({ name: 'name', type: Array });
278+
command.arguments!.name = { type: Array, scope: 'none' };
256279
argv = ['jhipster', 'jhipster', 'mocked', 'Foo', 'Bar', '--foo', '--foo-bar'];
257280
});
258281

@@ -549,20 +572,23 @@ describe('cli', () => {
549572
generatorContent: `export const createGenerator = async env => {
550573
const BaseGenerator = await env.requireGenerator('jhipster:base');
551574
return class extends BaseGenerator {
552-
constructor(args, opts, features) {
553-
super(args, opts, features);
554-
555-
this.option('foo-bar', {
556-
description: 'Sample option',
557-
type: Boolean,
558-
});
559-
}
560-
561575
get [BaseGenerator.INITIALIZING]() {
562576
return {};
563577
}
564578
};
565579
};
580+
581+
export const command = {
582+
configs: {
583+
fooBar: {
584+
cli: {
585+
description: 'Sample option',
586+
type: Boolean,
587+
},
588+
scope: 'none',
589+
},
590+
},
591+
};
566592
`,
567593
}),
568594
)

cli/environment-builder.spec.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ const cliBlueprintFiles = {
4949
'generators/foo/index.js': `export const createGenerator = async env => {
5050
const BaseGenerator = await env.requireGenerator('jhipster:base');
5151
return class extends BaseGenerator {
52-
constructor(args, opts, features) {
53-
super(args, opts, features);
54-
55-
this.option('foo-bar', {
56-
description: 'Sample option',
57-
type: Boolean,
58-
});
59-
}
60-
6152
get [BaseGenerator.INITIALIZING]() {
6253
/* eslint-disable no-console */
6354
console.log('Running foo');
@@ -69,6 +60,18 @@ const cliBlueprintFiles = {
6960
}
7061
};
7162
};
63+
64+
export const command = {
65+
configs: {
66+
fooBar: {
67+
cli: {
68+
description: 'Sample option',
69+
type: Boolean,
70+
},
71+
scope: 'none',
72+
},
73+
},
74+
};
7275
`,
7376
};
7477

@@ -88,16 +91,21 @@ const cliSharedBlueprintFiles = {
8891
'generators/bar/index.js': `export const createGenerator = async env => {
8992
const BaseGenerator = await env.requireGenerator('jhipster:base');
9093
return class extends BaseGenerator {
91-
constructor(args, options) {
92-
super(args, options);
93-
this.option('foo', {
94-
description: 'foo description',
95-
type: Boolean,
96-
});
97-
}
9894
get [BaseGenerator.INITIALIZING]() {}
9995
};
10096
};
97+
98+
export const command = {
99+
configs: {
100+
foo: {
101+
cli: {
102+
description: 'foo description',
103+
type: Boolean,
104+
},
105+
scope: 'none',
106+
},
107+
},
108+
};
101109
`,
102110
};
103111

cli/jhipster-command.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,6 @@ export default class JHipsterCommand extends Command {
138138
return this.addOption(new Option(opt.option, opt.desc + additionalDescription).default(opt.default));
139139
}
140140

141-
/**
142-
* Register arguments using generator._arguments structure.
143-
*/
144-
addGeneratorArguments(generatorArgs: Array<{ name: string; description?: string; required?: boolean; type?: unknown }> = []): this {
145-
if (!generatorArgs) return this;
146-
generatorArgs.forEach(argument => {
147-
let argName = argument.type === Array ? `${argument.name}...` : argument.name;
148-
argName = argument.required ? `<${argName}>` : `[${argName}]`;
149-
this.argument(argName, argument.description);
150-
});
151-
return this;
152-
}
153-
154-
/**
155-
* Register options using generator._options structure.
156-
*/
157-
addGeneratorOptions(options: Record<string, JHipsterCommandOptions>, blueprintOptionDescription?: string): this {
158-
Object.entries(options ?? {}).forEach(([key, value]) => {
159-
this._addGeneratorOption(key, value, blueprintOptionDescription);
160-
});
161-
return this;
162-
}
163-
164141
addJHipsterArguments(jhipsterArguments?: JHipsterArgumentsWithChoices): this {
165142
Object.entries(jhipsterArguments ?? {}).forEach(([key, value]) => {
166143
let argName = value.type === Array ? `${key}...` : key;

cli/program.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const buildAllDependencies = async (
140140
const addCommandGeneratorOptions = async (
141141
command: JHipsterCommand,
142142
generatorMeta: GeneratorMeta,
143-
{ root, blueprintOptionDescription, info }: { root?: boolean; blueprintOptionDescription?: string; info?: string } = {},
143+
{ blueprintOptionDescription }: { blueprintOptionDescription?: string } = {},
144144
) => {
145145
const generatorModule = (await generatorMeta.importModule!()) as JHipsterModule;
146146
if (generatorModule.command) {
@@ -149,30 +149,12 @@ const addCommandGeneratorOptions = async (
149149
command.addJHipsterConfigs(configs, blueprintOptionDescription);
150150
}
151151
}
152-
try {
153-
if (
154-
generatorModule.command?.loadGeneratorOptions !== false &&
155-
(root || !generatorModule.command || generatorModule.command.loadGeneratorOptions)
156-
) {
157-
const generator = await generatorMeta.instantiateHelp();
158-
// Add basic yeoman generator options
159-
command.addGeneratorOptions((generator as any)._options, blueprintOptionDescription);
160-
}
161-
} catch (error) {
162-
if (!info) {
163-
throw error;
164-
}
165-
logger.verboseInfo(`${info}, error: ${error}`);
166-
}
167152
};
168153

169154
const addCommandRootGeneratorOptions = async (command: JHipsterCommand, generatorMeta: GeneratorMeta, { usage = true } = {}) => {
170155
const generatorModule = (await generatorMeta.importModule!()) as JHipsterModule;
171156
if (generatorModule.command) {
172157
command.addJHipsterArguments(generatorModule.command.arguments ?? extractArgumentsFromConfigs(generatorModule.command.configs));
173-
} else {
174-
const generator = await generatorMeta.instantiateHelp();
175-
command.addGeneratorArguments((generator as any)._arguments);
176158
}
177159
if (usage) {
178160
const usagePath = path.resolve(path.dirname(generatorMeta.resolved!), 'USAGE');
@@ -207,6 +189,11 @@ export const createProgram = ({
207189
.option('--install-path', 'Show jhipster install path', false)
208190
.option('--skip-regenerate', "Don't regenerate identical files", false)
209191
.option('--skip-yo-resolve', 'Ignore .yo-resolve files', false)
192+
// Yeoman Generator options
193+
.option('--skip-cache', 'Do not remember prompt answers', false)
194+
.option('--skip-install', 'Do not automatically install dependencies', false)
195+
.option('--force-install', 'Fail on install dependencies error', false)
196+
.option('--ask-answered', 'Show prompts for already configured options', false)
210197
.addJHipsterConfigs(baseCommand.configs)
211198
);
212199
};
@@ -299,7 +286,7 @@ export const buildCommands = ({
299286
}
300287

301288
await addCommandRootGeneratorOptions(command, generatorMeta!, { usage: command.generatorNamespaces.length === 1 });
302-
await addCommandGeneratorOptions(command, generatorMeta!, { root: true });
289+
await addCommandGeneratorOptions(command, generatorMeta!);
303290
}),
304291
);
305292
return;
@@ -324,15 +311,14 @@ export const buildCommands = ({
324311
env,
325312
blueprintNamespaces: envBuilder?.getBlueprintsNamespaces(),
326313
});
327-
for (const [metaName, { meta: generatorMeta, blueprintNamespace }] of Object.entries(allDependencies)) {
314+
for (const [_metaName, { meta: generatorMeta, blueprintNamespace }] of Object.entries(allDependencies)) {
328315
if (blueprintNamespace) {
329316
const blueprintOptionDescription = chalk.yellow(` (blueprint option: ${blueprintNamespace.replace(/^jhipster-/, '')})`);
330317
await addCommandGeneratorOptions(command, generatorMeta, {
331318
blueprintOptionDescription,
332-
info: `Error parsing options for generator ${metaName}`,
333319
});
334320
} else {
335-
await addCommandGeneratorOptions(command, generatorMeta, { root: metaName === generator });
321+
await addCommandGeneratorOptions(command, generatorMeta);
336322
}
337323
}
338324
}
@@ -372,10 +358,15 @@ export const buildCommands = ({
372358
}
373359

374360
if (cmdName === 'run') {
375-
return Promise.all(command.generatorNamespaces.map(generator => env.run(generator, options))).then(
376-
() => silent || done(),
377-
errors => silent || done(errors.find((error: any) => error)),
378-
);
361+
try {
362+
args.shift(); // remove first argument which is handled in lazyBuildCommand
363+
await Promise.all(command.generatorNamespaces.map(generator => env.run(generator, options)));
364+
365+
silent || done();
366+
} catch (error) {
367+
silent || done(error as Error);
368+
}
369+
return;
379370
}
380371
if (cmdName === 'upgrade') {
381372
options.programName = program.name();

generators/app/__snapshots__/generator.spec.ts.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ Options:
1414
--dto-suffix <value> Add suffix after dtos name
1515
--pk-type <value> Default primary key type (beta)
1616
--test-frameworks <value...> Test frameworks to be generated
17-
--skip-cache Do not remember prompt answers (default: false)
18-
--skip-install Do not automatically install dependencies (default: false)
19-
--force-install Fail on install dependencies error (default: false)
20-
--ask-answered Show prompts for already configured options (default: false)
2117
--skip-checks Check the status of the required tools
2218
--experimental Enable experimental features. Please note that these features may be unstable and may undergo breaking changes at any time
2319
--blueprints <value> A comma separated list of one or more generator blueprints to use for the sub generators, e.g. --blueprints kotlin,vuejs

generators/base/__snapshots__/generator.spec.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Options:
1515
--install-path Show jhipster install path (default: false)
1616
--skip-regenerate Don't regenerate identical files (default: false)
1717
--skip-yo-resolve Ignore .yo-resolve files (default: false)
18+
--skip-cache Do not remember prompt answers (default: false)
19+
--skip-install Do not automatically install dependencies (default: false)
20+
--force-install Fail on install dependencies error (default: false)
21+
--ask-answered Show prompts for already configured options (default: false)
1822
--skip-checks Check the status of the required tools
1923
--experimental Enable experimental features. Please note that these features may be unstable and may undergo breaking changes at any time
2024
--disable-blueprints Disable blueprints support

0 commit comments

Comments
 (0)