diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index b227b11d3b602..20c771b43d17e 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -16,7 +16,7 @@ import { workspaceRoot } from '../../utils/workspace-root'; import type { AddOptions } from './command-object'; import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts'; -export function addHandler(options: AddOptions): Promise { +export function addHandler(options: AddOptions): Promise { if (options.verbose) { process.env.NX_VERBOSE_LOGGING = 'true'; } diff --git a/packages/nx/src/command-line/add/command-object.ts b/packages/nx/src/command-line/add/command-object.ts index 21bbbae903e07..a06d51dd2ae2f 100644 --- a/packages/nx/src/command-line/add/command-object.ts +++ b/packages/nx/src/command-line/add/command-object.ts @@ -47,6 +47,9 @@ export const yargsAddCommand: CommandModule< '$0 add @nx/react@17.0.0', 'Install version `17.0.0` of the `@nx/react` package and run its `@nx/react:init` generator' ) as any, - handler: (args) => - import('./add').then((m) => m.addHandler(withOverrides(args))), + handler: async (args) => { + process.exit( + await import('./add').then((m) => m.addHandler(withOverrides(args))) + ); + }, }; diff --git a/packages/nx/src/command-line/affected/command-object.ts b/packages/nx/src/command-line/affected/command-object.ts index 268a040126f20..167017e30a0dd 100644 --- a/packages/nx/src/command-line/affected/command-object.ts +++ b/packages/nx/src/command-line/affected/command-object.ts @@ -37,7 +37,7 @@ export const yargsAffectedCommand: CommandModule = { 'affected' ), handler: async (args) => { - return handleErrors( + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { return (await import('./affected')).affected( @@ -46,6 +46,7 @@ export const yargsAffectedCommand: CommandModule = { ); } ); + process.exit(exitCode); }, }; @@ -60,7 +61,7 @@ export const yargsAffectedTestCommand: CommandModule = { 'affected' ), handler: async (args) => { - return handleErrors( + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { return (await import('./affected')).affected('affected', { @@ -69,6 +70,7 @@ export const yargsAffectedTestCommand: CommandModule = { }); } ); + process.exit(exitCode); }, }; @@ -83,7 +85,7 @@ export const yargsAffectedBuildCommand: CommandModule = { 'affected' ), handler: async (args) => { - return handleErrors( + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { return (await import('./affected')).affected('affected', { @@ -92,6 +94,7 @@ export const yargsAffectedBuildCommand: CommandModule = { }); } ); + process.exit(exitCode); }, }; @@ -106,7 +109,7 @@ export const yargsAffectedLintCommand: CommandModule = { 'affected' ), handler: async (args) => { - return handleErrors( + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { return (await import('./affected')).affected('affected', { @@ -115,6 +118,7 @@ export const yargsAffectedLintCommand: CommandModule = { }); } ); + process.exit(exitCode); }, }; @@ -129,7 +133,7 @@ export const yargsAffectedE2ECommand: CommandModule = { 'affected' ), handler: async (args) => { - return handleErrors( + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { return (await import('./affected')).affected('affected', { @@ -138,5 +142,6 @@ export const yargsAffectedE2ECommand: CommandModule = { }); } ); + process.exit(exitCode); }, }; diff --git a/packages/nx/src/command-line/deprecated/command-objects.ts b/packages/nx/src/command-line/deprecated/command-objects.ts index ec19de708ac42..78db5b0c51b64 100644 --- a/packages/nx/src/command-line/deprecated/command-objects.ts +++ b/packages/nx/src/command-line/deprecated/command-objects.ts @@ -19,10 +19,12 @@ export const yargsAffectedGraphCommand: CommandModule = { describe: false, aliases: ['affected:dep-graph'], builder: (yargs) => withAffectedOptions(withGraphOptions(yargs)), - handler: (args) => - handleErrors(false, () => { + handler: async (args) => { + const exitCode = await handleErrors(false, () => { throw new Error(affectedGraphDeprecationMessage); - }), + }); + process.exit(exitCode); + }, deprecated: affectedGraphDeprecationMessage, }; @@ -45,9 +47,11 @@ export const yargsPrintAffectedCommand: CommandModule = { describe: 'Select the type of projects to be returned (e.g., --type=app)', }), - handler: (args) => - handleErrors(false, () => { + handler: async (args) => { + const exitCode = await handleErrors(false, () => { throw new Error(printAffectedDeprecationMessage); - }), + }); + process.exit(exitCode); + }, deprecated: printAffectedDeprecationMessage, }; diff --git a/packages/nx/src/command-line/release/command-object.ts b/packages/nx/src/command-line/release/command-object.ts index 5704dd36e1531..dd70f7ef4025e 100644 --- a/packages/nx/src/command-line/release/command-object.ts +++ b/packages/nx/src/command-line/release/command-object.ts @@ -182,10 +182,7 @@ const releaseCommand: CommandModule = { logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`); } - if (typeof result === 'number') { - process.exit(result); - } - process.exit(0); + process.exit(result); }, }; @@ -223,10 +220,7 @@ const versionCommand: CommandModule = { logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`); } - if (typeof result === 'number') { - process.exit(result); - } - process.exit(0); + process.exit(result); }, }; @@ -286,10 +280,7 @@ const changelogCommand: CommandModule = { logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`); } - if (typeof result === 'number') { - process.exit(result); - } - process.exit(0); + process.exit(result); }, }; @@ -359,10 +350,7 @@ const planCommand: CommandModule = { logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`); } - if (typeof result === 'number') { - process.exit(result); - } - process.exit(0); + process.exit(result); }, }; diff --git a/packages/nx/src/command-line/run-many/command-object.ts b/packages/nx/src/command-line/run-many/command-object.ts index 4cc6ff2f89043..a14a718b49e22 100644 --- a/packages/nx/src/command-line/run-many/command-object.ts +++ b/packages/nx/src/command-line/run-many/command-object.ts @@ -21,11 +21,13 @@ export const yargsRunManyCommand: CommandModule = { ), 'run-many' ), - handler: async (args) => - await handleErrors( + handler: async (args) => { + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { - (await import('./run-many')).runMany(withOverrides(args)); + await import('./run-many').then((m) => m.runMany(withOverrides(args))); } - ), + ); + process.exit(exitCode); + }, }; diff --git a/packages/nx/src/command-line/run/command-object.ts b/packages/nx/src/command-line/run/command-object.ts index 571dfd33499b5..6b27f0cd3b3ee 100644 --- a/packages/nx/src/command-line/run/command-object.ts +++ b/packages/nx/src/command-line/run/command-object.ts @@ -16,13 +16,17 @@ export const yargsRunCommand: CommandModule = { You can skip the use of Nx cache by using the --skip-nx-cache option.`, builder: (yargs) => withRunOneOptions(withBatch(yargs)), - handler: async (args) => - await handleErrors( + handler: async (args) => { + const exitCode = await handleErrors( (args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { - (await import('./run-one')).runOne(process.cwd(), withOverrides(args)); + await import('./run-one').then((m) => + m.runOne(process.cwd(), withOverrides(args)) + ); } - ), + ); + process.exit(exitCode); + }, }; /** diff --git a/packages/nx/src/command-line/show/command-object.ts b/packages/nx/src/command-line/show/command-object.ts index 890275b5d05c9..aacc0e9f62ba5 100644 --- a/packages/nx/src/command-line/show/command-object.ts +++ b/packages/nx/src/command-line/show/command-object.ts @@ -123,15 +123,15 @@ const showProjectsCommand: CommandModule = { '$0 show projects --affected --exclude=*-e2e', 'Show affected projects in the workspace, excluding end-to-end projects' ) as any, - handler: (args) => { - return handleErrors( + handler: async (args) => { + const exitCode = await handleErrors( args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { const { showProjectsHandler } = await import('./projects'); await showProjectsHandler(args); - process.exit(0); } ); + process.exit(exitCode); }, }; @@ -177,14 +177,14 @@ const showProjectCommand: CommandModule = { '$0 show project my-app --web', 'View project information for my-app in the browser' ), - handler: (args) => { - return handleErrors( + handler: async (args) => { + const exitCode = await handleErrors( args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true', async () => { const { showProjectHandler } = await import('./project'); await showProjectHandler(args); - process.exit(0); } ); + process.exit(exitCode); }, }; diff --git a/packages/nx/src/utils/params.ts b/packages/nx/src/utils/params.ts index daa0237c9b0ac..c7a80fba5093a 100644 --- a/packages/nx/src/utils/params.ts +++ b/packages/nx/src/utils/params.ts @@ -89,9 +89,16 @@ export type Options = { [k: string]: string | number | boolean | string[] | Unmatched[] | undefined; }; -export async function handleErrors(isVerbose: boolean, fn: Function) { +export async function handleErrors( + isVerbose: boolean, + fn: Function +): Promise { try { - return await fn(); + const result = await fn(); + if (typeof result === 'number') { + return result; + } + return 0; } catch (err) { err ||= new Error('Unknown error caught'); if (err.constructor.name === 'UnsuccessfulWorkflowExecution') {