UNPKG

bot-script

Version:

Scripting tool to Write Bot's Workflow

81 lines (77 loc) 3.58 kB
/** * Created by agnibha on 12/1/17. */ var fs = require('fs'); var newLineChar = require('os').EOL; function ScriptCreator(script, filePath) { var file_meta = {}; for (var section in script) { if (script.hasOwnProperty(section)) { if (section !== 'common') { var file_name = section.split(".")[0]; var section_name = section.split(".")[1]; if (!file_meta.hasOwnProperty(file_name)) { file_meta[file_name] = {}; } if (!file_meta[file_name].hasOwnProperty(section_name)) { file_meta[file_name][section_name] = {}; } file_meta[file_name][section_name] = script[section]; } } } for (var file in file_meta) { if (file_meta.hasOwnProperty(file)) { if(fs.existsSync(filePath)) { fs.unlinkSync(filePath); } for (var script_section in file_meta[file]) { if (file_meta[file].hasOwnProperty(script_section)) { var section_str = '['.concat(script_section, ']\n'); // if(fs.existsSync(write_file_name)) // { // fs.unlinkSync(write_file_name); // } fs.appendFileSync(filePath, section_str); for (var line_item in file_meta[file][script_section]) { if (file_meta[file][script_section].hasOwnProperty(line_item)) { var state_data = file_meta[file][script_section][line_item]; //creating front indentation var str; if (state_data.line_type === "LINE") { str = Array(state_data.indentation + 1).join(' '); if (!(line_item.indexOf("default_") > -1)) { str = str.concat(line_item.concat(":")); } //adding input or output replacing the newline chars if (state_data.input) { str = str.concat(state_data.input); } else { str = str.concat(state_data.output.split("\n").join("\\n")); } //adding the action. if (state_data.action && state_data.action.action !== "done") { str = str.concat(":", state_data.action.action); if (state_data.action.label != null) { str = str.concat(" ", state_data.action.label); } else if (state_data.action.section != null) { str = str.concat(" ", state_data.action.section); } } } else { str = ""; } str = str.concat(newLineChar); fs.appendFileSync(filePath, str); } } } } } } } module.exports.create = ScriptCreator;