UNPKG

@bash_candy/candy-js

Version:

Web Development for Dummies

265 lines (233 loc) 8.69 kB
'use scrict'; const fs = require('fs'); const readline = require('readline'); const copydir = require('copy-dir'); const candyPath = require("global-modules-path").getPath("@bash_candy/candy-js"); const pageGenerator = require('./pageGenerator.js'); const scripter = require('./scripter.js'); const colors = require('colors'); function getElementWithId(id, json){ for(var x in json){ if(json.hasOwnProperty(x)){ if(x == id){ return json[x]; }else if(typeof json[x] == "object"){ let aux = getElementWithId(id, json[x]); if(aux){ return aux; } } } } } function printTreeRec(json, level){ for(var x in json){ if(x != 'meta'){ xAux = json[x].tag + '-' + x; for(let i = 0; i < level; i++){ xAux = '--' + xAux; } console.log(level % 2 == 0 ? xAux.green : xAux.yellow); if(json[x].tag == 'div' || json[x].tag == 'nav' || json[x].tag == 'ul'){ printTreeRec(json[x].children, level + 1); } } } } function appendToJSON(newElem, id){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); let jsonAux = json; if(json["meta"].current != ""){ jsonAux = getElementWithId(json["meta"].current, json).children; } jsonAux[id] = newElem; fs.writeFileSync('data.json', JSON.stringify(json)); } function appendChild(newElem, id, parentId){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); let jsonAux = getElementWithId(parentId, json).children; jsonAux[id] = newElem; fs.writeFileSync('data.json', JSON.stringify(json)); } module.exports = { init: function () { fs.writeFileSync('index.html', ''); fs.writeFileSync('data.json','{"meta": {"current":"", "theme":"red-candy"}}'); copydir.sync(candyPath + "/src", "./src"); }, defaulttheme: function(theme){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); json["meta"].theme = theme; fs.writeFileSync('data.json', JSON.stringify(json)); }, createbox: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Theme: ', function(theme){ appendToJSON({"tag": "div", "class" : theme, "children" : {}}, id); rl.close(); }); }); }, createboxScript: function(id, theme){ appendToJSON({"tag": "div", "class" : theme, "children" : {}}, id); }, createtext: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Text: ', function(text){ appendToJSON({"tag": "p", "text" : text}, id); rl.close(); }); }); }, createtextScript: function(id, text){ appendToJSON({"tag": "p", "text" : text}, id); }, createinput: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Text: ', function(text){ appendToJSON({"tag": "input", "type":"text", "placeholder" : text}, id); rl.close(); }); }); }, createinputScript: function(id, text){ appendToJSON({"tag": "input", "type":"text", "placeholder" : text}, id); }, createbutton: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Text: ', function(text){ rl.question('Action: ', function(action){ if(action && action != ""){ rl.question('Text-box #1: ', function(textbox1){ rl.question('Text-box #2: ', function(textbox2){ appendToJSON({"tag": "input", "type":"submit", "value" : text, "action": action, "txt1": textbox1, "txt2": textbox2}, id); rl.close(); }) }) }else{ appendToJSON({"tag": "input", "type":"submit", "value" : text}, id); rl.close(); } }) }) }); }, createbuttonScriptAction: function(id, text, action, textbox1, textbox2){ appendToJSON({"tag": "input", "type":"submit", "value" : text, "action": action, "txt1": textbox1, "txt2": textbox2}, id); }, createbuttonScript: function(id, text){ appendToJSON({"tag": "input", "type":"submit", "value" : text}, id); }, createimage: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Image name: ', function(path){ appendToJSON({"tag": "img", "src" : "./src/img/" + path}, id); rl.close(); }); }); }, createimageScript: function(id, path){ appendToJSON({"tag": "img", "src" : "./src/img/" + path}, id); }, createnavbar: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Theme: ', function(theme){ appendToJSON({"tag": "nav", "class" : "navbar navbar-fixed-top", "children":{}}, id); appendChild({"tag": "ul", "children":{}}, "ul_" + id, id); rl.close(); }); }); }, createnavbarScript: function(id, theme){ appendToJSON({"tag": "nav", "class" : "navbar navbar-fixed-top", "children":{}}, id); appendChild({"tag": "ul", "children":{}}, "ul_" + id, id); }, createnavitem: function(){ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Id: ', function(id) { rl.question('Text: ', function(text){ rl.question('Link: ', function(link){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); let selId = json["meta"].current; if(selId != "" && getElementWithId(selId,json).tag == 'nav'){ appendChild({"tag": "li","href" : "#" + link, "text":text}, id,"ul_" + selId); }else{ console.log('You cannot add an navbar item to a box'.red); } rl.close(); }); }); }); }, createnavitemScript: function(id,text,link){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); let selId = json["meta"].current; if(selId != "" && getElementWithId(selId,json).tag == 'nav'){ appendChild({"tag": "li","href" : "#" + link, "text":text}, id,"ul_" + selId); }else{ console.log('You cannot add an navbar item to a box'.red); } }, use: function(id){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); const elem = getElementWithId(id, json); if(elem && (elem.tag == "div" || elem.tag == "nav")){ json["meta"].current = id; fs.writeFileSync('data.json', JSON.stringify(json)); }else{ console.log('No such container'.red); } }, root: function(){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); json["meta"].current = ""; fs.writeFileSync('data.json', JSON.stringify(json)); }, printTree: function(){ let data = fs.readFileSync('data.json','utf8'); let json = JSON.parse(data); printTreeRec(json,0); }, build: function(){ let data = fs.readFileSync('data.json','utf8'); pageGenerator.createHtml(JSON.parse(data)); }, source: function(path){ let data = fs.readFileSync(path,'utf8'); scripter.processScript(data); } };