All files / core/use-cases handle-config.ts

78.12% Statements 25/32
40% Branches 4/10
100% Functions 6/6
80.76% Lines 21/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 591x       1x         1x     1x       1x     1x     1x 1x 1x   1x     1x 1x   1x 1x 1x 1x 1x       1x                 1x           1x   1x  
import * as path from 'path';
 
import { CLIProgram } from '../entities/cli-program';
 
import { cosmiconfig } from 'cosmiconfig';
 
/**
 * Detect config file with cosmiconfig
 */
export class HandleConfigFile {
    private static instance: HandleConfigFile;
 
    private cosmiconfigModuleName = 'compodoc';
    private configExplorer;
    private configExplorerResult;
 
    public configFilePath: string = '';
 
    constructor() {
        this.configExplorer = cosmiconfig(this.cosmiconfigModuleName);
    }
 
    public static getInstance() {
        Eif (!HandleConfigFile.instance) {
            HandleConfigFile.instance = new HandleConfigFile();
        }
        return HandleConfigFile.instance;
    }
 
    public async handle(currentProgram: CLIProgram) {
        return new Promise(async (resolve, reject) => {
            try {
                if (IcurrentProgram.config) {
                    let configFilePath = currentProgram.config;
                    this.configFilePath = configFilePath;
                    let testConfigFilePath = configFilePath.match(process.cwd());
                    Iif (testConfigFilePath && testConfigFilePath.length > 0) {
                        configFilePath = configFilePath.replace(process.cwd() + path.sep, '');
                    }
 
                    this.configExplorerResult = await this.configExplorer.load(
                        path.resolve(configFilePath)
                    );
                } else {
                    this.configExplorerResult = await this.configExplorer.search();
                    if (this.configExplorerResult) {
                        this.configFilePath = path.basename(this.configExplorerResult.filepath);
                    }
                }
                resolve(this.configExplorerResult);
            } catch (error) {
                reject(error);
            }
        });
    }
}
 
export default HandleConfigFile.getInstance();