<commit_before>import * as typescript from 'typescript';
import * as path from 'path';

import * as logger from './logger';
import { formatErrors } from './utils';
import { 
    LoaderOptions,
    TSCompatibleCompiler,
    Webpack,
    WebpackError
} from './interfaces';
import { green } from 'chalk';

interface ConfigFile {
    config?: any;
    error?: typescript.Diagnostic;
}

export function getConfigFile(
    compiler: typeof typescript,
    loader: Webpack,
    loaderOptions: LoaderOptions,
    compilerCompatible: boolean,
    log: logger.Logger,
    compilerDetailsLogMessage: string
) {
    const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFileName);
    let configFileError: WebpackError | undefined;
    let configFile: ConfigFile;

    if (configFilePath !== undefined) {
        if (compilerCompatible) {
            log.logInfo(green(`${compilerDetailsLogMessage} and ${configFilePath}`));
        } else {
            log.logInfo(green(`ts-loader: Using config file at ${configFilePath}`));
        }

        // HACK: relies on the fact that passing an extra argument won't break
        // the old API that has a single parameter
        configFile = (<TSCompatibleCompiler> <any> compiler).readConfigFile(
            configFilePath,
            compiler.sys.readFile
        );

        if (configFile.error !== undefined) {
            configFileError = formatErrors([configFile.error], loaderOptions, compiler, { file: configFilePath })[0];
        }
    } else {
        if (compilerCompatible) { log.logInfo(green(compilerDetailsLogMessage)); }

        configFile = {
            config: {
                compilerOptions: {},
                files: [],
            },
        };
    }

    if (configFileError === undefined) {
        configFile.config.compilerOptions = Object.assign({},
            configFile.config.compilerOptions,
            loaderOptions.compilerOptions);
    }

    return {
        configFilePath,
        configFile,
        configFileError
    };
}

/**
 * The tsconfig.json is found using the same method as `tsc`, starting in the current directory
 * and continuing up the parent directory chain.
 */
function findConfigFile(compiler: typeof typescript, searchPath: string, configFileName: string): string | undefined {
    while (true) {
        const fileName = path.join(searchPath, configFileName);
        if (compiler.sys.fileExists(fileName)) {<commit_msg>Fix findConfigFile in Windows (#605)<commit_after>import * as typescript from 'typescript';
import * as path from 'path';

import * as logger from './logger';
import { formatErrors } from './utils';
import { 
    LoaderOptions,
    TSCompatibleCompiler,
    Webpack,
    WebpackError
} from './interfaces';
import { green } from 'chalk';

interface ConfigFile {
    config?: any;
    error?: typescript.Diagnostic;
}

export function getConfigFile(
    compiler: typeof typescript,
    loader: Webpack,
    loaderOptions: LoaderOptions,
    compilerCompatible: boolean,
    log: logger.Logger,
    compilerDetailsLogMessage: string
) {
    const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFileName);
    let configFileError: WebpackError | undefined;
    let configFile: ConfigFile;

    if (configFilePath !== undefined) {
        if (compilerCompatible) {
            log.logInfo(green(`${compilerDetailsLogMessage} and ${configFilePath}`));
        } else {
            log.logInfo(green(`ts-loader: Using config file at ${configFilePath}`));
        }

        // HACK: relies on the fact that passing an extra argument won't break
        // the old API that has a single parameter
        configFile = (<TSCompatibleCompiler> <any> compiler).readConfigFile(
            configFilePath,
            compiler.sys.readFile
        );

        if (configFile.error !== undefined) {
            configFileError = formatErrors([configFile.error], loaderOptions, compiler, { file: configFilePath })[0];
        }
    } else {
        if (compilerCompatible) { log.logInfo(green(compilerDetailsLogMessage)); }

        configFile = {
            config: {
                compilerOptions: {},
                files: [],
            },
        };
    }

    if (configFileError === undefined) {
        configFile.config.compilerOptions = Object.assign({},
            configFile.config.compilerOptions,
            loaderOptions.compilerOptions);
    }

    return {
        configFilePath,
        configFile,
        configFileError
    };
}

/**
 * The tsconfig.json is found using the same method as `tsc`, starting in the current directory
 * and continuing up the parent directory chain.
 */
function findConfigFile(compiler: typeof typescript, searchPath: string, configFileName: string): string | undefined {
    // Remove path.root from configFileName to fix windows specific issue. #604
    configFileName = configFileName.substr(path.parse(configFileName).root.length)

    while (true) {
        const fileName = path.join(searchPath, configFileName);
        if (compiler.sys.fileExists(fileName)) {
{'fraction_non_alphanumeric': 0.1025092394475783, 'fraction_numerical': 0.0015561174868702586, 'mean_word_length': 2.8032544378698225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '174573', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1310, 'n_words': 364}