From 94fb214980f27f867659c37d1137f8864158ca9b Mon Sep 17 00:00:00 2001 From: simonhaenisch Date: Mon, 27 Jan 2020 00:09:40 +0800 Subject: [PATCH] feat: get rid of the --debug flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: The `--debug` flag has been removed. All errors are printed instead, in the hopes that giving the user every hint possible will help them get down to the root of their problem. Hopefully the error stacks don't confuse anyone. 🤓 Closes #54. --- readme.md | 1 - src/cli.ts | 40 ++++++++++++++++++++++------------------ src/lib/help.ts | 1 - 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/readme.md b/readme.md index d1d8fc9..ca19144 100644 --- a/readme.md +++ b/readme.md @@ -71,7 +71,6 @@ Options: --as-html ................ Output as HTML instead --config-file ............ Path to a JSON or JS configuration file --devtools ............... Open the browser with devtools instead of creating PDF - --debug .................. Show more output on errors ``` The pdf is generated into the same directory as the source file and uses the same filename (with `.pdf` extension) by default. Multiple files can be specified by using shell globbing, e. g.: diff --git a/src/cli.ts b/src/cli.ts index e8501b0..958a046 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -39,7 +39,6 @@ const cliFlags = arg({ '--as-html': Boolean, '--config-file': String, '--devtools': Boolean, - '--debug': Boolean, // aliases '-h': '--help', @@ -96,12 +95,7 @@ async function main(args: typeof cliFlags, config: Config) { }; } catch (error) { console.warn(chalk.red(`Warning: couldn't read config file: ${path.resolve(args['--config-file'])}`)); - - if (args['--debug']) { - console.error(error); - } else if (error instanceof SyntaxError) { - console.error(error.message); - } + console.warn(error instanceof SyntaxError ? error.message : error); } } @@ -122,14 +116,13 @@ async function main(args: typeof cliFlags, config: Config) { */ if (stdin) { - await convertMdToPdf({ content: stdin }, config, args).catch(async (error: Error) => { - await closeServer(server); - - console.error(error); - process.exit(1); - }); + await convertMdToPdf({ content: stdin }, config, args) + .then(async () => closeServer(server)) + .catch(async (error: Error) => { + await closeServer(server); - await closeServer(server); + throw error; + }); return; } @@ -145,12 +138,23 @@ async function main(args: typeof cliFlags, config: Config) { if (args['--watch']) { console.log(chalk.bgBlue('\n watching for changes \n')); - watch(files).on('change', async file => { - await new Listr([getListrTask(file)]).run().catch((error: Error) => args['--debug'] && console.error(error)); - }); + watch(files).on('change', async file => + new Listr([getListrTask(file)], { exitOnError: false }).run().catch(console.error), + ); } else { server.close(); } }) - .catch((error: Error) => (args['--debug'] && console.error(error)) || process.exit(1)); + .catch((error: Error) => { + /** + * In watch mode the error needs to be shown immediately because the `main` function's catch handler will never execute. + * + * @todo is this correct or does `main` actually finish and the process is just kept alive because of the file server? + */ + if (args['--watch']) { + return console.error(error); + } + + throw error; + }); } diff --git a/src/lib/help.ts b/src/lib/help.ts index 24e77ee..c8df37f 100644 --- a/src/lib/help.ts +++ b/src/lib/help.ts @@ -22,7 +22,6 @@ const helpText = ` --as-html ${chalk.dim('................')} Output as HTML instead --config-file ${chalk.dim('............')} Path to a JSON or JS configuration file --devtools ${chalk.dim('...............')} Open the browser with devtools instead of creating PDF - --debug ${chalk.dim('..................')} Show more output on errors ${chalk.dim.underline.bold('Examples:')}