From b81ce0ba588e799e6b9ce7a1ac914b5ee4957bf0 Mon Sep 17 00:00:00 2001 From: Nathan Smyth <3888305+jarjee@users.noreply.github.com> Date: Mon, 2 May 2022 01:29:31 +1000 Subject: [PATCH] fix: Stop fresh builds from failing (#81) * chore: Reenable package-lock.json We really need some kind of lock for this (looks like we're aligning on npm anyway), since master is currently completely unable to be compiled due to lots of minor patches that have bitrotted the codebase. I'm now going to try and fix all the issues so this is a standard, compiling state to actually work from. * fix: Correct the unknown error issues Using instanceof since we know what the thrown error should be. * fix: Tests now pass as expected * fix: Lowest version we support is v14.0 While this is possibly controversial, v12 goes EOL in about a month, so might as well set v14 as the baseline, which makes our lives a little bit easier. * feat: Use npm ci instead of npm i Since we have a package-lock.json, we can download the dependencies much faster with ci. * feat: Hopefully improve cache performance Done based on https://www.voorhoede.nl/en/blog/super-fast-npm-install-on-github-actions/#fn1 * chore: update package-lock.json * chore: remove type cast * chore: rename variable to be more descriptive * ci: ensure postinstall is run for patch-package * ci: revert to state from main Co-authored-by: Nathan Smyth Co-authored-by: Stephan Meijer --- package.json | 3 +++ src/cache.ts | 4 ++++ src/index.ts | 21 ++++++++++----------- src/traverse.ts | 11 ++++++++--- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 3dc4dfd..abd306c 100644 --- a/package.json +++ b/package.json @@ -87,5 +87,8 @@ "hooks": { "pre-commit": "lint-staged" } + }, + "engines": { + "node": ">=14.0.0" } } diff --git a/src/cache.ts b/src/cache.ts index 7b14e3b..42012b4 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -57,6 +57,10 @@ export class InvalidCacheError extends Error { this.name = 'InvalidCacheError'; this.path = path; } + + static wrap(e: Error, path: string): InvalidCacheError { + return new InvalidCacheError(e.message, path); + } } export async function resolveEntry( diff --git a/src/index.ts b/src/index.ts index 761cec4..f524bf7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -274,21 +274,20 @@ export async function main(args: CliArguments): Promise { } catch (error) { spinner.stop(); - if (!(error instanceof Error)) { - console.error(`something weird happened: ${error}`); - process.exit(1); - } - // console.log is intercepted for output comparison, this helps debugging - if (process.env.NODE_ENV === 'test') { + if (process.env.NODE_ENV === 'test' && error instanceof Error) { console.log(error.message); } - console.error( - chalk.redBright( - error['path'] ? `\nFailed parsing ${error['path']}` : error.message, - ), - ); + if (error instanceof InvalidCacheError) { + console.error(chalk.redBright(`\nFailed parsing ${error['path']}`)); + } else if (error instanceof Error) { + console.error(chalk.redBright(error.message)); + } else { + // Who knows what this is, hopefully the .toString() is meaningful + console.error(`Unexpected value thrown: ${error}`); + } + process.exit(1); } } diff --git a/src/traverse.ts b/src/traverse.ts index a4e4cf6..3e42da7 100644 --- a/src/traverse.ts +++ b/src/traverse.ts @@ -13,7 +13,12 @@ import type { } from '@typescript-eslint/types/dist/ast-spec'; import resolve from 'resolve'; import removeFlowTypes from 'flow-remove-types'; -import { invalidateEntries, invalidateEntry, resolveEntry } from './cache'; +import { + invalidateEntries, + invalidateEntry, + InvalidCacheError, + resolveEntry, +} from './cache'; import { log } from './log'; import { MapLike } from 'typescript'; @@ -357,8 +362,8 @@ export async function traverse( }); } - if (error instanceof Error && !error['path']) { - error['path'] = path; + if (error instanceof Error && !(error instanceof InvalidCacheError)) { + throw InvalidCacheError.wrap(error, path); } throw error;