/*
function dumpingDatabase that create a json dump file of all the content of the database (name in config.json)
Using the curl command for this operation. If the "-p" flag is specified in command line, we construct the curl commande
without the proxy server.
@testDump : Only used by the test/tests-warehouse.js file. It allows the creation of a different file name for the tests.
#dumpEmitter : Event Emitter that emit two event "dumpDone" and "dumpFailed".
#wstream : Writable stream object.
#chunkRes : Result chunk from the stream (standard output).
#chunkError : Error chunk from the stream (standard error).
function dumpingDatabase(testDump = false) {
    let dumpEmitter = new EventEmitter();
    let wstream;
    if (testDump) {
        wstream = fs.createWriteStream(`${nameDB}-dump-test.json`);
    }
    else {
        wstream = fs.createWriteStream(`${nameDB}-dump.json`);
    }
    let chunkRes = '';
    let chunkError = '';
    let curl;
    if (proxyBool) {
        curl = child_process_1.spawn('curl', ['--noproxy', `${addressDB}`, `-X`, `GET`, `http://${accountDB}:${passwordDB}@${addressDB}:${portDB}/${nameDB}/_all_docs?include_docs=true`]);
    }
    else {
        curl = child_process_1.spawn('curl', [`-X`, `GET`, `http://${accountDB}:${passwordDB}@${addressDB}:${portDB}/${nameDB}/_all_docs?include_docs=true`]);
    }
    curl.stdout.on('data', (data) => {
        chunkRes += data.toString('utf8');
    });
    curl.stderr.on('data', (data) => {
        chunkError += data.toString('utf8');
    });
    curl.on('close', (code) => {
        let split = chunkError.replace(/(\r\n\t|\n|\r\t)/gm, " ").split(" ");
        if (chunkError.length > 0 && chunkRes.length === 0) {
            logger_1.logger.log('error', `Dumping of ${nameDB} database failed \n ${chunkError}`);
        }
        else {
            try {
                wstream.write(chunkRes);
                if (testDump)
                    logger_1.logger.log('success', `Dumping of ${nameDB} database succeed, ${nameDB}-dump-test.json file created`);
                else
                    logger_1.logger.log('success', `Dumping of ${nameDB} database succeed, ${nameDB}-dump.json file created`);
                dumpEmitter.emit('dumpDone');
            }
            catch (err) {
                logger_1.logger.log('error', `Dumping of ${nameDB} database failed \n ${err}`);
                dumpEmitter.emit('dumpFailed', err);
            }
        }
    });
    return dumpEmitter;
}