@@ -2,6 +2,14 @@ import fs from "fs-extra";
2
2
import path from "path" ;
3
3
import ts from "typescript" ;
4
4
5
+ const enum ExitCodes {
6
+ OK ,
7
+ EmitErrors ,
8
+ NoDataOrOptions ,
9
+ NoOptions ,
10
+ ParseError ,
11
+ }
12
+
5
13
/**
6
14
* @param basePath -
7
15
* @param file -
@@ -104,21 +112,21 @@ async function compile(basePath: string, type: "browser" | "cjs" | "esm" | "type
104
112
}
105
113
106
114
if ( ! data && ! options ) {
107
- return 2 ;
115
+ return ExitCodes . NoDataOrOptions ;
108
116
}
109
117
110
118
if ( ! options && data ) {
111
119
options = JSON . parse ( data ) ;
112
120
}
113
121
114
122
if ( ! options ) {
115
- return 3 ;
123
+ return ExitCodes . NoOptions ;
116
124
}
117
125
118
126
const parsed = ts . parseJsonConfigFileContent ( options , ts . sys , basePath ) ;
119
127
120
128
if ( ! parsed ) {
121
- return 4 ;
129
+ return ExitCodes . ParseError ;
122
130
}
123
131
124
132
const program = ts . createProgram ( parsed . fileNames , parsed . options ) ,
@@ -131,16 +139,21 @@ async function compile(basePath: string, type: "browser" | "cjs" | "esm" | "type
131
139
failed = failed || diagnostic . category === ts . DiagnosticCategory . Error ;
132
140
133
141
if ( diagnostic . file ) {
134
- const { line, character } = ts . getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ?? 0 ) ,
135
- message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ;
136
-
137
- console . log ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
142
+ const startingPos = 0 ,
143
+ { line, character } = ts . getLineAndCharacterOfPosition (
144
+ diagnostic . file ,
145
+ diagnostic . start ?? startingPos ,
146
+ ) ,
147
+ message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ,
148
+ increment = 1 ;
149
+
150
+ console . log ( `${ diagnostic . file . fileName } (${ line + increment } ,${ character + increment } ): ${ message } ` ) ;
138
151
} else {
139
152
console . log ( ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ) ;
140
153
}
141
154
} ) ;
142
155
143
- const exitCode = emitResult . emitSkipped || failed ? 1 : 0 ;
156
+ const exitCode = emitResult . emitSkipped || failed ? ExitCodes . EmitErrors : ExitCodes . OK ;
144
157
145
158
console . log ( `TSC for ${ type } done with exit code: '${ exitCode } '.` ) ;
146
159
0 commit comments