import * as fs from 'fs';
import { convertCompilerOptionsFromJson, findConfigFile, parseConfigFileTextToJson, } from 'typescript';
import { diagnosticToString } from './diagnosticToString';
export function findTsConfig(cwd = process.cwd()) {
    const configPath = findConfigFile(cwd, (fileName) => fs.existsSync(fileName));
    if (!configPath) {
        throw new Error('Unable to find TypeScript configuration');
    }
    const configContents = fs.readFileSync(configPath).toString();
    const { config } = parseConfigFileTextToJson(configPath, configContents);
    const { compilerOptions: unparsedCompilerOptions } = config;
    const { options, errors } = convertCompilerOptionsFromJson(unparsedCompilerOptions, cwd, 'tsconfig.json');
    if (errors && errors.length > 0) {
        throw new Error(errors.map(x => diagnosticToString(x, cwd)).join('\n'));
    }
    return Object.assign({}, config, { compilerOptions: options, configPath });
}
//# sourceMappingURL=findTsConfig.js.map