From 51116debf7b3e1b6852cf35778f63e6faa55e15c Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Thu, 3 Nov 2022 00:05:56 +0100 Subject: [PATCH] fix(core): workspace-generator errors should be propagated to nx --- packages/nx/src/command-line/nx-commands.ts | 49 ++++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/nx/src/command-line/nx-commands.ts b/packages/nx/src/command-line/nx-commands.ts index 04c88d457fb6c..8c09ca70abf5b 100644 --- a/packages/nx/src/command-line/nx-commands.ts +++ b/packages/nx/src/command-line/nx-commands.ts @@ -771,6 +771,14 @@ function withRunOneOptions(yargs: yargs.Argv) { } } +type OptionArgumentDefinition = { + type: yargs.Options['type']; + describe?: string; + default?: any; + choices?: yargs.Options['type'][]; + demandOption?: boolean; +}; + type WorkspaceGeneratorProperties = { [name: string]: | { @@ -778,6 +786,7 @@ type WorkspaceGeneratorProperties = { description?: string; default?: any; enum?: yargs.Options['type'][]; + demandOption?: boolean; } | { type: yargs.PositionalOptionsType; @@ -839,7 +848,7 @@ async function withCustomGeneratorOptions( Object.entries(schema.properties as WorkspaceGeneratorProperties).forEach( ([name, prop]) => { - options.push({ + const option: { name: string; definition: OptionArgumentDefinition } = { name, definition: { describe: prop.description, @@ -847,7 +856,11 @@ async function withCustomGeneratorOptions( default: prop.default, choices: prop.enum, }, - }); + }; + if (schema.required && schema.required.includes(name)) { + option.definition.demandOption = true; + } + options.push(option); if (isPositionalProperty(prop)) { positionals.push({ name, @@ -869,21 +882,23 @@ async function withCustomGeneratorOptions( command += ' (options)'; } - yargs.command({ - // this is the default and only command - command, - describe: schema.description || '', - builder: (y) => { - options.forEach(({ name, definition }) => { - y.option(name, definition); - }); - positionals.forEach(({ name, definition }) => { - y.positional(name, definition); - }); - return linkToNxDevAndExamples(y, 'workspace-generator'); - }, - handler: workspaceGeneratorHandler, - }); + yargs + .command({ + // this is the default and only command + command, + describe: schema.description || '', + builder: (y) => { + options.forEach(({ name, definition }) => { + y.option(name, definition); + }); + positionals.forEach(({ name, definition }) => { + y.positional(name, definition); + }); + return linkToNxDevAndExamples(y, 'workspace-generator'); + }, + handler: workspaceGeneratorHandler, + }) + .fail(() => void 0); // no action is needed on failure as Nx will handle it based on schema validation return yargs; }