Skip to content

Commit

Permalink
fix(core): handleErrors returns an exit code (#26343)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Jun 4, 2024
1 parent cda799b commit d06992e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
export function addHandler(options: AddOptions): Promise<number> {
if (options.verbose) {
process.env.NX_VERBOSE_LOGGING = 'true';
}
Expand Down
7 changes: 5 additions & 2 deletions packages/nx/src/command-line/add/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const yargsAddCommand: CommandModule<
'$0 add @nx/[email protected]',
'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)))
);
},
};
15 changes: 10 additions & 5 deletions packages/nx/src/command-line/affected/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -46,6 +46,7 @@ export const yargsAffectedCommand: CommandModule = {
);
}
);
process.exit(exitCode);
},
};

Expand All @@ -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', {
Expand All @@ -69,6 +70,7 @@ export const yargsAffectedTestCommand: CommandModule = {
});
}
);
process.exit(exitCode);
},
};

Expand All @@ -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', {
Expand All @@ -92,6 +94,7 @@ export const yargsAffectedBuildCommand: CommandModule = {
});
}
);
process.exit(exitCode);
},
};

Expand All @@ -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', {
Expand All @@ -115,6 +118,7 @@ export const yargsAffectedLintCommand: CommandModule = {
});
}
);
process.exit(exitCode);
},
};

Expand All @@ -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', {
Expand All @@ -138,5 +142,6 @@ export const yargsAffectedE2ECommand: CommandModule = {
});
}
);
process.exit(exitCode);
},
};
16 changes: 10 additions & 6 deletions packages/nx/src/command-line/deprecated/command-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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,
};
20 changes: 4 additions & 16 deletions packages/nx/src/command-line/release/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ const releaseCommand: CommandModule<NxReleaseArgs, ReleaseOptions> = {
logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`);
}

if (typeof result === 'number') {
process.exit(result);
}
process.exit(0);
process.exit(result);
},
};

Expand Down Expand Up @@ -223,10 +220,7 @@ const versionCommand: CommandModule<NxReleaseArgs, VersionOptions> = {
logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`);
}

if (typeof result === 'number') {
process.exit(result);
}
process.exit(0);
process.exit(result);
},
};

Expand Down Expand Up @@ -286,10 +280,7 @@ const changelogCommand: CommandModule<NxReleaseArgs, ChangelogOptions> = {
logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`);
}

if (typeof result === 'number') {
process.exit(result);
}
process.exit(0);
process.exit(result);
},
};

Expand Down Expand Up @@ -359,10 +350,7 @@ const planCommand: CommandModule<NxReleaseArgs, PlanOptions> = {
logger.warn(`\nNOTE: The "dryRun" flag means no changes were made.`);
}

if (typeof result === 'number') {
process.exit(result);
}
process.exit(0);
process.exit(result);
},
};

Expand Down
10 changes: 6 additions & 4 deletions packages/nx/src/command-line/run-many/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};
12 changes: 8 additions & 4 deletions packages/nx/src/command-line/run/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/nx/src/command-line/show/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
'$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);
},
};

Expand Down Expand Up @@ -177,14 +177,14 @@ const showProjectCommand: CommandModule<NxShowArgs, ShowProjectOptions> = {
'$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);
},
};
11 changes: 9 additions & 2 deletions packages/nx/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
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') {
Expand Down

0 comments on commit d06992e

Please sign in to comment.