(opts: Options = {})
| 125 | * Register TypeScript compiler. |
| 126 | */ |
| 127 | export function register(opts: Options = {}): Register { |
| 128 | const options = Object.assign({}, DEFAULTS, opts); |
| 129 | |
| 130 | const ignoreDiagnostics = [ |
| 131 | 6059, // "'rootDir' is expected to contain all source files." |
| 132 | 18002, // "The 'files' list in config file is empty." |
| 133 | 18003, // "No inputs were found in config file." |
| 134 | ...(options.ignoreDiagnostics || []), |
| 135 | ].map(Number); |
| 136 | |
| 137 | // Require the TypeScript compiler and configuration. |
| 138 | const cwd = options.basePath || process.cwd(); |
| 139 | let compiler: string; |
| 140 | try { |
| 141 | compiler = require_.resolve(options.compiler || 'typescript', { |
| 142 | paths: [options.project || cwd], |
| 143 | }); |
| 144 | } catch (_e) { |
| 145 | compiler = 'typescript'; |
| 146 | } |
| 147 | const ts: typeof _ts = require_(compiler); |
| 148 | if (compiler === 'typescript') { |
| 149 | console.log( |
| 150 | `Using built-in TypeScript ${ts.version} since "typescript" is missing from "devDependencies"` |
| 151 | ); |
| 152 | } else { |
| 153 | console.log(`Using TypeScript ${ts.version} (local user-provided)`); |
| 154 | } |
| 155 | const transformers = options.transformers || undefined; |
| 156 | const readFile = options.readFile || ts.sys.readFile; |
| 157 | const fileExists = options.fileExists || ts.sys.fileExists; |
| 158 | |
| 159 | const formatDiagnostics = |
| 160 | process.stdout.isTTY || options.pretty |
| 161 | ? ts.formatDiagnosticsWithColorAndContext |
| 162 | : ts.formatDiagnostics; |
| 163 | |
| 164 | const diagnosticHost: _ts.FormatDiagnosticsHost = { |
| 165 | getNewLine: () => ts.sys.newLine, |
| 166 | getCurrentDirectory: () => cwd, |
| 167 | getCanonicalFileName: path => path, |
| 168 | }; |
| 169 | |
| 170 | function reportTSError( |
| 171 | diagnostics: _ts.Diagnostic[], |
| 172 | shouldExit: boolean | undefined |
| 173 | ) { |
| 174 | if (!diagnostics || diagnostics.length === 0) { |
| 175 | return; |
| 176 | } |
| 177 | const message = formatDiagnostics(diagnostics, diagnosticHost); |
| 178 | |
| 179 | if (shouldExit) { |
| 180 | throw new NowBuildError({ code: 'NODE_TYPESCRIPT_ERROR', message }); |
| 181 | } else { |
| 182 | // Print error in red color and continue execution. |
| 183 | console.error(message); |
| 184 | } |
no test coverage detected