use the default error handling for the CLI

Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
aramissennyeydd
2024-11-10 10:21:21 -05:00
parent afe647a81b
commit 104a1499fe
2 changed files with 34 additions and 36 deletions
+15 -33
View File
@@ -25,7 +25,6 @@ export default createCliPlugin({
path: ['config:docs'],
description: 'Browse the configuration reference documentation',
execute: async ({ args }) => {
console.log(args);
const command = new Command();
const defaultCommand = command
.option(
@@ -42,28 +41,21 @@ export default createCliPlugin({
path: ['config', 'docs'],
description: 'Browse the configuration reference documentation',
execute: async ({ args }) => {
await yargs
const argv = await yargs
.options({
package: { type: 'string' },
})
.command(
'$0',
'',
async () => {},
async argv => {
const m = await import('./commands/docs');
await m.default(argv);
},
)
.help()
.parse(args);
const m = await import('./commands/docs');
await m.default(argv);
},
});
reg.addCommand({
path: ['config:print'],
description: 'Print the app configuration for the current package',
execute: async ({ args }) => {
await yargs
const argv = await yargs
.options({
package: { type: 'string' },
lax: { type: 'boolean' },
@@ -72,17 +64,10 @@ export default createCliPlugin({
format: { type: 'string' },
config: { type: 'string', array: true },
})
.command(
'$0',
'',
async () => {},
async argv => {
const m = await import('./commands/print');
await m.default(argv);
},
)
.help()
.parse(args);
const m = await import('./commands/print');
await m.default(argv);
},
});
reg.addCommand({
@@ -90,26 +75,23 @@ export default createCliPlugin({
description:
'Validate that the given configuration loads and matches schema',
execute: async ({ args }) => {
await yargs
const argv = await yargs
.options({
package: { type: 'string' },
lax: { type: 'boolean' },
frontend: { type: 'boolean' },
deprecated: { type: 'boolean' },
strict: { type: 'boolean' },
config: { type: 'string', array: true },
})
.command(
'$0',
'',
async () => {},
async argv => {
const m = await import('./commands/validate');
await m.default(argv);
strict: { type: 'boolean', required: true },
config: {
type: 'string',
array: true,
default: [],
},
)
})
.help()
.parse(args);
const m = await import('./commands/validate');
await m.default(argv);
},
});
},
+19 -3
View File
@@ -20,6 +20,8 @@ import { CommandRegistry } from './CommandRegistry';
import { program } from 'commander';
import { version } from '../lib/version';
import chalk from 'chalk';
import { exitWithError } from '../lib/errors';
import { assertError } from '@backstage/errors';
type UninitializedFeature = CliFeature | Promise<CliFeature>;
@@ -83,9 +85,15 @@ export class CliInitializer {
.allowUnknownOption(true)
.allowExcessArguments(true)
.action(async () => {
await node.command.execute({
args: program.parseOptions(process.argv).unknown,
});
try {
await node.command.execute({
args: program.parseOptions(process.argv).unknown,
});
process.exit(0);
} catch (error) {
assertError(error);
exitWithError(error);
}
});
}
}
@@ -97,6 +105,14 @@ export class CliInitializer {
process.exit(1);
});
process.on('unhandledRejection', rejection => {
if (rejection instanceof Error) {
exitWithError(rejection);
} else {
exitWithError(new Error(`Unknown rejection: '${rejection}'`));
}
});
program.parse(process.argv);
}
}