-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import path from 'path'; | ||
import ts from 'typescript'; | ||
import { errorMessage, ErrorCode } from './error'; | ||
import { formatDiagnostics } from './diagnostic'; | ||
import { getCurrentDirectory, fileExists, readFile } from './util'; | ||
|
||
export interface Options { | ||
configName?: string; | ||
searchPath?: string; | ||
compilerOptions?: ts.CompilerOptions; | ||
} | ||
|
||
export function findConfigFile(options: Options = {}) { | ||
const configName = options.configName ?? 'tsconfig.json'; | ||
const searchPath = options.searchPath ?? getCurrentDirectory(); | ||
const filePath = ts.findConfigFile(searchPath, fileExists, configName); | ||
|
||
// TODO: allow fallback to default config with a warning? | ||
if (!filePath) { | ||
return { | ||
error: errorMessage(ErrorCode.TSCONFIG_FILE_NOT_FOUND, { searchPath, configName }), | ||
}; | ||
} | ||
|
||
return { filePath }; | ||
} | ||
|
||
export function readConfigFile(filePath: string) { | ||
try { | ||
const jsonText = readFile(filePath); | ||
|
||
if (!jsonText) { | ||
return { | ||
error: errorMessage(ErrorCode.TSCONFIG_FILE_NOT_READABLE, { filePath }), | ||
}; | ||
} | ||
|
||
return { filePath, jsonText }; | ||
} catch (_err) { | ||
return { | ||
error: errorMessage(ErrorCode.TSCONFIG_FILE_NOT_READABLE, { filePath }), | ||
}; | ||
} | ||
} | ||
|
||
export function parseConfigFile(fileName: string, jsonText: string, compilerOptions?: ts.CompilerOptions) { | ||
const configObject = ts.parseConfigFileTextToJson(fileName, jsonText); | ||
|
||
if (configObject.error) { | ||
return { error: new Error(formatDiagnostics([configObject.error])) }; | ||
} | ||
|
||
const config = ts.parseJsonConfigFileContent(configObject.config, ts.sys, path.dirname(fileName), compilerOptions); | ||
|
||
if (config.errors.length > 0) { | ||
return { error: new Error(formatDiagnostics(config.errors)) }; | ||
} | ||
|
||
return { fileName, config }; | ||
} | ||
export function loadConfig(options?: Options) { | ||
try { | ||
const configFile = findConfigFile(options); | ||
|
||
if (configFile.error) { | ||
throw configFile.error; | ||
} | ||
|
||
const configFileContent = readConfigFile(configFile.filePath); | ||
|
||
if (configFileContent.error) { | ||
throw configFileContent.error; | ||
} | ||
|
||
const parsedConfig = parseConfigFile( | ||
configFileContent.filePath, | ||
configFileContent.jsonText, | ||
options?.compilerOptions, | ||
); | ||
|
||
if (parsedConfig.error) { | ||
throw parsedConfig.error; | ||
} | ||
|
||
return { config: parsedConfig }; | ||
} catch (error) { | ||
return { error: error as Error }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import ts from 'typescript'; | ||
import { getCurrentDirectory, newLine } from './util'; | ||
|
||
export const diagnosticsHost: ts.FormatDiagnosticsHost = { | ||
getNewLine: () => newLine, | ||
getCurrentDirectory: () => getCurrentDirectory(), | ||
getCanonicalFileName: (fileName: string) => fileName, | ||
}; | ||
|
||
export function formatDiagnostics(diagnostics: ts.Diagnostic[]) { | ||
return ts.formatDiagnostics(diagnostics, diagnosticsHost); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export enum ErrorCode { | ||
TSCONFIG_FILE_NOT_FOUND, | ||
TSCONFIG_FILE_NOT_READABLE, | ||
} | ||
|
||
export const errorMessages: Record<ErrorCode, string> = { | ||
[ErrorCode.TSCONFIG_FILE_NOT_FOUND]: 'TS config file not found. File name: {configName} - Search path: {searchPath}', | ||
[ErrorCode.TSCONFIG_FILE_NOT_READABLE]: 'TS config file not readable or empty. File path: {filePath}', | ||
}; | ||
|
||
export function errorMessage(code: ErrorCode, data?: Record<string, unknown>) { | ||
let message = errorMessages[code]; | ||
|
||
Object.entries(data ?? {}).forEach(([index, value]) => { | ||
message = message.replace(new RegExp(`{${index}}`, 'g'), String(value)); | ||
}); | ||
|
||
return new Error(message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { loadConfig } from './config'; | ||
|
||
const config = loadConfig({ | ||
configName: 'tsconfig.check.json', | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import ts from 'typescript'; | ||
|
||
export const newLine = '\n'; | ||
|
||
export function getCurrentDirectory() { | ||
return ts.sys.getCurrentDirectory(); | ||
} | ||
|
||
export function fileExists(fileName: string) { | ||
return ts.sys.fileExists(fileName); | ||
} | ||
|
||
export function readFile(fileName: string) { | ||
return ts.sys.readFile(fileName); | ||
} |