generateMarkdown: async function()

in packages/Ludown/lib/toLU.js [23:126]


    generateMarkdown: async function(program) {
        let outFolder = process.cwd();
        let LUISJSON = new helperClasses.readerObject();
        let QnAJSON = new helperClasses.readerObject();
        let QnAAltJSON = new helperClasses.readerObject();
        let outFileContent;
        let outFileName = '';
        if(program.out_folder) {
            if(path.isAbsolute(program.out_folder)) {
                outFolder = program.out_folder;
            } else {
                outFolder = path.resolve('', program.out_folder);
            }
            if(!fs.existsSync(outFolder)) {
                throw(new exception(retCode.errorCode.OUTPUT_FOLDER_INVALID, 'Output folder ' + outFolder + ' does not exist'));
            }
        }
        // Do we have a LUIS file? If so, get that and load into memory
        if(program.LUIS_File) {
            try {
                LUISJSON.model = await parseLUISFile(program.LUIS_File);
            } catch (err) {
                throw(err);
            }
            LUISJSON.sourceFile = program.LUIS_File;
        }

        //do we have a QnA JSON file? If so, get that and load into memory
        if(program.QNA_FILE) {
            try {
                QnAJSON.model = await parseQnAJSONFile(program.QNA_FILE);
            } catch (err) {
                throw (err);
            }
            QnAJSON.sourceFile = program.QNA_FILE;
        }

        //do we have a QnA alterations file? If so, get that and load into memory
        if(program.QNA_ALTERATION_FILE) {
            try {
                QnAAltJSON.model = await parseQnAJSONFile(program.QNA_ALTERATION_FILE);
            } catch (err) {
                throw (err);
            }
            QnAAltJSON.sourceFile = program.QNA_ALTERATION_FILE;
        }

        // do we have stdin specified? 
        if(program.stdin) {
            let parsedJsonFromStdin;
            try {
                parsedJsonFromStdin = JSON.parse(await stdin());
            } catch (err) {
                throw (new exception(retCode.errorCode.INVALID_INPUT, `Sorry, unable to parse stdin as JSON! \n\n ${JSON.stringify(err, null, 2)}\n\n`));
            }
            if (await validateLUISJSON(parsedJsonFromStdin)) {
                // if validation did not throw, then ground this as valid LUIS JSON
                LUISJSON.model = parsedJsonFromStdin;
                LUISJSON.sourceFile = 'stdin';
            } else if (parsedJsonFromStdin.qnaList || parsedJsonFromStdin.qnaDocuments) {
                QnAJSON.model = parsedJsonFromStdin;
                QnAJSON.sourceFile = 'stdin';
            } else {
                throw (new exception(retCode.errorCode.INVALID_INPUT, `Sorry, unable to parse stdin as LUIS or QnA Maker model!`));
            }
        }

        if(program.sort) {
            // sort LUIS, QnA and QnAAltJson
            await toLUHelpers.sortCollections(LUISJSON, QnAJSON, QnAAltJSON);
        }
        // construct the markdown file content
        outFileContent = await toLUHelpers.constructMdFileHelper(LUISJSON, QnAJSON, QnAAltJSON, program.LUIS_File, program.QNA_FILE, program.skip_header, program.model_info)
        if(!outFileContent) {
            throw(new exception(retCode.errorCode.UNKNOWN_ERROR,'Sorry, Unable to generate .lu file content!'));
        }
        if (!program.stdout) {
            // write out the file
            if(!program.lu_File) {
                if(LUISJSON.sourceFile) {
                    outFileName += path.basename(LUISJSON.sourceFile, path.extname(LUISJSON.sourceFile));
                }
                if(QnAJSON.sourceFile) {
                    outFileName += path.basename(QnAJSON.sourceFile, path.extname(QnAJSON.sourceFile));
                }   
                program.lu_File = outFileName + '.lu'; 
            } else {
                if(program.lu_File.lastIndexOf('.lu') === -1) {
                    program.lu_File += '.lu';
                }
            }
            outFileName = path.join(outFolder, program.lu_File);
            try {
                fs.writeFileSync(outFileName, outFileContent, 'utf-8');
            } catch (err) {
                throw(new exception(retCode.errorCode.UNABLE_TO_WRITE_FILE, 'Unable to write LU file - ' + outFileName));
            }
            if(program.verbose) process.stdout.write(outFileContent);
            if(program.verbose) process.stdout.write(chalk.default.italic('Successfully wrote to ' + path.join(outFolder, program.lu_File)));
        } else {
            process.stdout.write(outFileContent);
        }
        
    }