/*
Indexation goal is to retrieve the array of caches directory. Find all jobID.json file with their IDs,
and add them into couchDB database.
@cacheArray : represent the previousCacheDir key content from the config file, which is paths of all cache directory.
#dataToCouch : array that will contain all jobID.json file that attempt to be add inside the couchDB database.
#pathResult : list of all jobID.json path found in all caches directory.
function indexation(cacheArray) {
    let emitterIndex = new EventEmitter();
    let pathResult = globCaches(cacheArray);
    // Adding "_id" key to all jobID.json content from pathResult.
    // directorySearch function return the name of the directory (uuid) that contain the jobID.json file.
    let dataToCouch = [];
    for (let path of pathResult) {
        let result = extractDoc(path, directorySearch(path));
        result && dataToCouch.push(result); // this method remove "0" but we don't have "0" so it's OK
    }
    if (program.test)
        logger_1.logger.log('success', '----> OK');
    logger_1.logger.log('info', `Number of jobID.json files found in directories: ${dataToCouch.length}`);
    logger_1.logger.log('debug', `${JSON.stringify(dataToCouch)}`);
    dbMod.addToDB(dataToCouch, nameDB, accountDB, passwordDB, addressDB, portDB, proxyBool)
        .then(() => {
        logger_1.logger.log('success', `Insertion of ${dataToCouch.length} jobID.json file(s) in ${nameDB}`);
        emitterIndex.emit('indexDone');
    })
        .catch((err) => {
        emitterIndex.emit('indexError', err);
    });
    return emitterIndex;
}