11import { SansDependencies } from "../binding" ;
22import { ResultStatus , TSLintToESLintSettings , ResultWithDataStatus } from "../types" ;
3+ import { isDefined } from "../utils" ;
34import { findESLintConfiguration , ESLintConfiguration } from "./findESLintConfiguration" ;
45import { PackagesConfiguration , findPackagesConfiguration } from "./findPackagesConfiguration" ;
56import {
@@ -54,20 +55,42 @@ export const findOriginalConfigurations = async (
5455 // Out of those configurations, only TSLint's is always required to run
5556 if ( tslint instanceof Error ) {
5657 return {
57- complaints : [ tslint . message ] ,
58+ complaints : [ getMissingPackageMessage ( tslint ) ?? tslint . message ] ,
5859 status : ResultStatus . ConfigurationError ,
5960 } ;
6061 }
6162
62- // Other configuration errors only halt the program if the user asked for them
63- const configurationErrors = [
64- [ eslint , rawSettings . eslint ] ,
65- [ packages , rawSettings . package ] ,
66- [ typescript , rawSettings . typescript ] ,
67- ] . filter ( configurationResultIsError ) ;
68- if ( configurationErrors . length !== 0 ) {
63+ const configurationResults = [
64+ [ eslint , "eslint" ] ,
65+ [ packages , "package" ] ,
66+ [ typescript , "typescript" ] ,
67+ ] as const ;
68+
69+ // Other configuration errors only halt the program if...
70+ const errorMessages = configurationResults
71+ . map ( ( [ error , key ] ) => {
72+ if ( ! ( error instanceof Error ) ) {
73+ return undefined ;
74+ }
75+
76+ // * Their failure was caused by a missing package that needs to be installed
77+ const missingPackageMessage = getMissingPackageMessage ( error ) ;
78+ if ( missingPackageMessage !== undefined ) {
79+ return missingPackageMessage ;
80+ }
81+
82+ // * The user explicitly asked for them
83+ if ( typeof rawSettings [ key ] === "string" ) {
84+ return error . message ;
85+ }
86+
87+ return undefined ;
88+ } )
89+ . filter ( isDefined ) ;
90+
91+ if ( errorMessages . length !== 0 ) {
6992 return {
70- complaints : configurationErrors . map ( ( [ configuration ] ) => configuration . message ) ,
93+ complaints : errorMessages ,
7194 status : ResultStatus . ConfigurationError ,
7295 } ;
7396 }
@@ -83,6 +106,13 @@ export const findOriginalConfigurations = async (
83106 } ;
84107} ;
85108
86- const configurationResultIsError = ( result : unknown [ ] ) : result is [ Error , string ] => {
87- return result [ 0 ] instanceof Error && typeof result [ 1 ] === "string" ;
109+ const getMissingPackageMessage = ( error : Error ) => {
110+ const match = / ( C a n n o t f i n d m o d u l e | c o u l d n o t r e q u i r e | c o u l d n ' t f i n d t h e p l u g i n ) ( [ a - z A - Z 0 - 9 - _ " ' @ / ] + ) / . exec (
111+ error . message ,
112+ ) ;
113+ if ( match === null ) {
114+ return undefined ;
115+ }
116+
117+ return `Could not import the ${ match [ 2 ] } module. Do you need to install packages?` ;
88118} ;
0 commit comments