1
1
import { SansDependencies } from "../binding" ;
2
2
import { ResultStatus , TSLintToESLintSettings , ResultWithDataStatus } from "../types" ;
3
+ import { isDefined } from "../utils" ;
3
4
import { findESLintConfiguration , ESLintConfiguration } from "./findESLintConfiguration" ;
4
5
import { PackagesConfiguration , findPackagesConfiguration } from "./findPackagesConfiguration" ;
5
6
import {
@@ -54,20 +55,42 @@ export const findOriginalConfigurations = async (
54
55
// Out of those configurations, only TSLint's is always required to run
55
56
if ( tslint instanceof Error ) {
56
57
return {
57
- complaints : [ tslint . message ] ,
58
+ complaints : [ getMissingPackageMessage ( tslint ) ?? tslint . message ] ,
58
59
status : ResultStatus . ConfigurationError ,
59
60
} ;
60
61
}
61
62
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 ) {
69
92
return {
70
- complaints : configurationErrors . map ( ( [ configuration ] ) => configuration . message ) ,
93
+ complaints : errorMessages ,
71
94
status : ResultStatus . ConfigurationError ,
72
95
} ;
73
96
}
@@ -83,6 +106,13 @@ export const findOriginalConfigurations = async (
83
106
} ;
84
107
} ;
85
108
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?` ;
88
118
} ;
0 commit comments