From 6f6e52e536fb23fd6327f96305429862272225dd Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Mon, 6 Mar 2023 12:26:06 -0600 Subject: [PATCH 1/3] chore: merge changes from #152 --- source/cli.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/cli.ts b/source/cli.ts index 20142ac7..63c42e3d 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -54,8 +54,15 @@ const cli = meow(` throw new Error(formatter(diagnostics)); } } catch (error: unknown) { - if (error && typeof (error as Error).message === 'string') { - console.error((error as Error).message); + const potentialError = error as Error | undefined | null; + const errorMessage = + typeof potentialError?.stack === 'string' ? + potentialError.stack : + typeof potentialError?.message === 'string' ? + potentialError?.message : + undefined; + if (errorMessage) { + console.error(`Error running tsd: ${errorMessage}`); } process.exit(1); From af4708cd863f8def0df9c8146303877839170f31 Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Mon, 6 Mar 2023 12:27:42 -0600 Subject: [PATCH 2/3] fix: add changes mentioned in #152 --- source/cli.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/source/cli.ts b/source/cli.ts index 63c42e3d..ccf8ad0a 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -54,13 +54,9 @@ const cli = meow(` throw new Error(formatter(diagnostics)); } } catch (error: unknown) { - const potentialError = error as Error | undefined | null; - const errorMessage = - typeof potentialError?.stack === 'string' ? - potentialError.stack : - typeof potentialError?.message === 'string' ? - potentialError?.message : - undefined; + const potentialError = error as Error | undefined; + const errorMessage = potentialError?.stack ?? potentialError?.message; + if (errorMessage) { console.error(`Error running tsd: ${errorMessage}`); } From aa2d02d61a39d35e89025269a5017c1486014b08 Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Mon, 6 Mar 2023 14:42:42 -0600 Subject: [PATCH 3/3] feat: add test for logging error --- source/test/cli.ts | 10 ++++++++++ source/test/fixtures/empty-package-json/package.json | 0 2 files changed, 10 insertions(+) create mode 100644 source/test/fixtures/empty-package-json/package.json diff --git a/source/test/cli.ts b/source/test/cli.ts index 3806a5dd..32ef44f2 100644 --- a/source/test/cli.ts +++ b/source/test/cli.ts @@ -95,3 +95,13 @@ test('cli typings and files flags', async t => { t.is(exitCode, 1); t.true(stderr.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.')); }); + +test('tsd logs stacktrace on failure', async t => { + const {exitCode, stderr, stack} = await t.throwsAsync(execa('../../../cli.js', { + cwd: path.join(__dirname, 'fixtures/empty-package-json') + })); + + t.is(exitCode, 1); + t.true(stderr.includes('Error running tsd: JSONError: Unexpected end of JSON input while parsing empty string')); + t.truthy(stack); +}); diff --git a/source/test/fixtures/empty-package-json/package.json b/source/test/fixtures/empty-package-json/package.json new file mode 100644 index 00000000..e69de29b