Skip to content

Commit

Permalink
refactor(v2): add exception handling if external command is fails (#4870
Browse files Browse the repository at this point in the history
)

* refactor(v2): add handle exception if external command is fails

* Use `unhandledRejection` event
  • Loading branch information
lex111 authored Jun 3, 2021
1 parent f0d4d8d commit 85e87b5
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ if (!semver.satisfies(process.version, requiredVersion)) {
process.exit(1);
}

function wrapCommand(fn) {
return (...args) =>
fn(...args).catch((err) => {
console.error(chalk.red(err.stack));
process.exitCode = 1;
});
}

cli.version(require('../package.json').version).usage('<command> [options]');

cli
Expand All @@ -123,7 +115,7 @@ cli
'Build website without minimizing JS bundles (default: false)',
)
.action((siteDir = '.', {bundleAnalyzer, config, outDir, locale, minify}) => {
wrapCommand(build)(path.resolve(siteDir), {
build(path.resolve(siteDir), {
bundleAnalyzer,
outDir,
config,
Expand All @@ -141,7 +133,7 @@ cli
)
.option('--danger', 'Enable swizzle for internal component of themes')
.action((themeName, componentName, siteDir = '.', {typescript, danger}) => {
wrapCommand(swizzle)(
swizzle(
path.resolve(siteDir),
themeName,
componentName,
Expand Down Expand Up @@ -170,7 +162,7 @@ cli
'Skip building website before deploy it (default: false)',
)
.action((siteDir = '.', {outDir, skipBuild, config}) => {
wrapCommand(deploy)(path.resolve(siteDir), {
deploy(path.resolve(siteDir), {
outDir,
config,
skipBuild,
Expand Down Expand Up @@ -198,7 +190,7 @@ cli
)
.action(
(siteDir = '.', {port, host, locale, config, hotOnly, open, poll}) => {
wrapCommand(start)(path.resolve(siteDir), {
start(path.resolve(siteDir), {
port,
host,
locale,
Expand Down Expand Up @@ -235,7 +227,7 @@ cli
config,
},
) => {
wrapCommand(serve)(path.resolve(siteDir), {
serve(path.resolve(siteDir), {
dir,
port,
build: buildSite,
Expand All @@ -249,7 +241,7 @@ cli
.command('clear [siteDir]')
.description('Remove build artifacts')
.action((siteDir = '.') => {
wrapCommand(clear)(path.resolve(siteDir));
clear(path.resolve(siteDir));
});

cli
Expand All @@ -276,7 +268,7 @@ cli
siteDir = '.',
{locale = undefined, override = false, messagePrefix = '', config},
) => {
wrapCommand(writeTranslations)(path.resolve(siteDir), {
writeTranslations(path.resolve(siteDir), {
locale,
override,
config,
Expand All @@ -289,7 +281,7 @@ cli
.command('write-heading-ids [contentDir]')
.description('Generate heading ids in Markdown content')
.action((siteDir = '.') => {
wrapCommand(writeHeadingIds)(siteDir);
writeHeadingIds(siteDir);
});

cli.arguments('<command>').action((cmd) => {
Expand Down Expand Up @@ -324,3 +316,8 @@ async function run() {
}

run();

process.on('unhandledRejection', (err) => {
console.error(chalk.red(err.stack));
process.exit(1);
});

0 comments on commit 85e87b5

Please sign in to comment.