const writeOutFiles = function()

in packages/Ludown/lib/parser.js [75:194]


const writeOutFiles = function(program,finalLUISJSON,finalQnAJSON, finalQnAAlterations, rootFile, cmd) {
    let outFolder;
    try {
        outFolder = getOutputFolder(program)
    } catch (err) {
        throw (err);
    }

    if(finalLUISJSON) {
        finalLUISJSON.luis_schema_version = program.luis_schema_version || finalLUISJSON.luis_schema_version || "3.2.0";
        finalLUISJSON.versionId = program.luis_versionId || finalLUISJSON.versionId || "0.1";
        finalLUISJSON.name = program.luis_name || finalLUISJSON.name || path.basename(rootFile, path.extname(rootFile)),
        finalLUISJSON.desc = program.luis_desc || finalLUISJSON.desc || "";
        finalLUISJSON.culture = program.luis_culture || finalLUISJSON.culture || "en-us";
        finalLUISJSON.culture = finalLUISJSON.culture.toLowerCase();
    }

    if (!program.luis_name && finalLUISJSON && finalLUISJSON.name) program.luis_name = finalLUISJSON.name;    

    if (finalQnAJSON) finalQnAJSON.name = program.qna_name || finalQnAJSON.name || path.basename(rootFile, path.extname(rootFile));

    if (!program.qna_name && finalQnAJSON && finalQnAJSON.name) program.qna_name = finalQnAJSON.name || '';

    var writeQnAFile = (finalQnAJSON.qnaList.length > 0) || 
                        (finalQnAJSON.urls.length > 0) || 
                        (finalQnAJSON.files.length > 0);

    var  writeLUISFile = finalLUISJSON?true:false;

    if(!writeLUISFile && program.verbose) {
        process.stdout.write(chalk.default.yellowBright('No LUIS content found in .lu file(s)! \n'));
    }

    if(!writeQnAFile && program.verbose) {
        process.stdout.write(chalk.default.yellowBright('No QnA Maker content found in .lu file(s)! \n'));
    }

    if(program.verbose) {
        if((cmd == cmdEnum.luis) && writeLUISFile) {
            process.stdout.write(JSON.stringify(finalLUISJSON, null, 2) + '\n');
        }
        if((cmd == cmdEnum.qna) && writeQnAFile) {
            process.stdout.write(JSON.stringify(finalQnAJSON, null, 2) + '\n');
        }
    }
    
    if (!program.lOutFile) {
        if (program.out) {
            program.lOutFile = program.out.includes('.') ? program.out : program.out + ".json"
        } else {
            if (!program.luis_name) {
                program.lOutFile = path.basename(rootFile, path.extname(rootFile)) + "_LUISApp.json";  
            } else {
                program.lOutFile = program.luis_name.includes('.') ? program.luis_name : program.luis_name + ".json";
            }
        }
    }
    if (!program.qOutFile) {
        if (program.out) {
            program.qOutFile = program.out.includes('.') ? program.out : program.out + ".json"
        } else {
            if (!program.qna_name) {
                program.qOutFile = path.basename(rootFile, path.extname(rootFile)) + "_qnaKB.json";
            } else {
                program.qOutFile = program.qna_name.includes('.') ? program.qna_name : program.qna_name + ".json";
            }
        }
    }
    if((cmd == cmdEnum.luis) && writeLUISFile) {
        var fLuisJson = JSON.stringify(finalLUISJSON, null, 2);
        var luisFilePath = path.join(outFolder, program.lOutFile);
        // write out the final LUIS Json
        try {
            fs.writeFileSync(luisFilePath, fLuisJson, 'utf-8');
        } catch (err) {
            throw(new exception(retCode.errorCode.UNABLE_TO_WRITE_FILE,'Unable to write LUIS JSON file - ' + path.join(outFolder, program.lOutFile)));
        }
        if(program.verbose) process.stdout.write(chalk.default.italic('Successfully wrote LUIS model to ' + path.join(outFolder, program.lOutFile) + '\n'));
    }
    if((cmd == cmdEnum.qna) && writeQnAFile) {
        let qnaJson = JSON.stringify(finalQnAJSON, null, 2);
        let qnaFilePath = path.join(outFolder, program.qOutFile);
        // write out the final QnA Json
        try {
            fs.writeFileSync(qnaFilePath, qnaJson, 'utf-8');
        } catch (err) {
            throw(new exception(retCode.errorCode.UNABLE_TO_WRITE_FILE,'Unable to write QnA JSON file - ' + path.join(outFolder, program.qOutFile)));
        }
        if(program.verbose) process.stdout.write(chalk.default.italic('Successfully wrote QnA KB to ' + path.join(outFolder, program.qOutFile) + '\n'));
    }
    // write luis batch test file if requested
    if((cmd == cmdEnum.luis) && program.write_luis_batch_tests) {
        // catch and label list entities in utterances
        let batchTestJson = tagListEntities(finalLUISJSON);
        let lBatchFile = JSON.stringify(batchTestJson.utterances, null, 2);
        let LUISBatchFileName = program.lOutFile.endsWith('.json')?program.lOutFile.replace('.json','_LUISBatchTest.json'):program.lOutFile + '_LUISBatchTest.json';
        let lBFileName = path.join(outFolder, LUISBatchFileName);
        // write out the final LUIS Json
        try {
            fs.writeFileSync(lBFileName, lBatchFile, 'utf-8');
        } catch (err) {
            throw(new exception(retCode.errorCode.UNABLE_TO_WRITE_FILE, 'Unable to write LUIS batch test JSON file - ' + path.join(outFolder, LUISBatchFileName)));
        }
        if(program.verbose) console.log(chalk.default.italic('Successfully wrote LUIS batch test JSON file to ' + path.join(outFolder, LUISBatchFileName) + '\n'));
    }

    // write out QnA Alterations if requested
    if((cmd == cmdEnum.qna) && program.write_qna_alterations) {
        let qAlterationsFileContent = JSON.stringify(finalQnAAlterations, null, 2);
        let qAlterationsFileName = program.qOutFile.endsWith('.json')?program.qOutFile.replace('.json','_Alterations.json'):program.qOutFile + '_Alterations.json';
        let qAFileName = path.join(outFolder, qAlterationsFileName);
        // write out the final QnA alterations file
        try {
            fs.writeFileSync(qAFileName, qAlterationsFileContent, 'utf-8');
        } catch (err) {
            throw(new exception(retCode.errorCode.UNABLE_TO_WRITE_FILE, 'Unable to write QnA Alterations JSON file - ' + qAFileName));
        }
        if(program.verbose) console.log(chalk.default.italic('Successfully wrote QnA Alterations JSON file to ' + qAFileName + '\n'));
    }
}