diff --git a/src/cmd.js b/src/cmd.js index 1fdf046..c30bbdc 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -1,11 +1,15 @@ let jsste = require("./index"); let myargs = process.argv.slice(2); -let path = require("path"); -const { fstat } = require("fs"); let output = ""; let pagefile, tempfile; +let path = require("path"); let fs = require("fs"); +/** + * this function filters the prefix and call the callback with the param + * @param {prefix, args } settings + * @param {*} callback + */ let addCommand = ({prefix,args = myargs}, callback) => { myargs.forEach((elem) =>{ @@ -17,23 +21,77 @@ let addCommand = ({prefix,args = myargs}, callback) => }) } +/** + * -log | show jsste log + */ +addCommand({prefix:"-log"},(arg) =>{ + jsste.setStateFunction((state) =>{ + console.log(state); + }) +}) + +/** + * -Jsconfig | set jsste config as json + */ addCommand({prefix:"-Jsconfig="},(arg) =>{ jsste.__config.setConfig(arg); }) +/** + * -pageFile | set pageFile path + */ +addCommand({prefix:"-pageFile="},(arg) =>{ + pagefile = JSON.parse(fs.readFileSync(arg, "utf8")); + pagefile["_SELFPATH_"] = path.dirname(arg); +}) + +/** + * -pageFile | set pageFile as json + */ addCommand({prefix:"-page="},(arg) =>{ - - pagefile = JSON.parse(fs.readFileSync(arg,"utf-8").toString()) - console.log(pagefile) + + pagefile = JSON.parse(arg); + pagefile["_SELFPATH_"] = path.dirname(arg); }) +/** + * -tempFile | set tempFile path + */ +addCommand({prefix:"-tempFile="},(arg) =>{ + tempfile = fs.readFileSync(arg, "utf8"); +}) + +/** + * -pageFile | set pageFile as code + */ addCommand({prefix:"-temp="},(arg) =>{ - tempfile = fs.readFileSync(arg, "utf-8").toString() - console.log(tempfile) + tempfile = arg }) +/** + * rendering + */ output = jsste.render(pagefile || undefined, tempfile || undefined) -addCommand({prefix:"-out"},(arg) =>{ - console.log("output\n", output ) +/** + * -info | show jsste.info + */ +addCommand({prefix:"-info"},(arg) =>{ + + if(arg == "?json") + { + console.log(JSON.stringify(jsste.info)) + } + else{ + Object.keys(jsste.info).forEach((key)=>{ + console.log(key + " : " + jsste.info[key] ) + }) + } +}) + +/** + * -out | write rendered code into the commandling + */ +addCommand({prefix:"-out"},(arg) =>{ + console.log(output) }) diff --git a/src/engine.js b/src/engine.js index 37c5885..c4593f5 100644 --- a/src/engine.js +++ b/src/engine.js @@ -56,7 +56,14 @@ app.render = function (pagecode, templatecode) { pagecode = JSON.parse(pagecode); app.setState({ status: 0, statusMSG: "Parse Pagecode" }); } else { - app.setState({ status: 1, statusMSG: "Pagecode is undefined" }); + if(typeof pagecode === "object") + { + app.setState({ status: 0, statusMSG: "Pagecode is an object" }); + } + else{ + app.setState({ status: 1, statusMSG: "Pagecode is undefined" }); + } + } if (!templatecode) { diff --git a/src/index.js b/src/index.js index b882f48..ca40acf 100644 --- a/src/index.js +++ b/src/index.js @@ -6,19 +6,27 @@ var app = {}; app.render = engine.render; app.CONST = engine.CONST; + +let pj = require("../package.json") + +app.info = {}; +app.info["version"] = pj.version +app.info["license"] = pj.license /** * Render File * @param {*} filePath * @returns */ -app.renderFile = (filePath) => { +app.renderFile = (filePath, templatePath) => { app.setState({ status: 0, statusMSG: "read file" }); let file = fs.readFileSync(filePath, "utf8"); app.setState({ status: 0, statusMSG: "parse file" }); file = JSON.parse(file); + app.setState({ status: 0, statusMSG: "read template" }); + let temp = fs.readFileSync(templatePath, "utf8"); app.setState({ status: 0, statusMSG: "set Selfpath" }); file["_SELFPATH_"] = path.dirname(filePath); - return engine.render(file); + return engine.render(file,temp); }; /** diff --git a/src/scriptExecuter.js b/src/scriptExecuter.js index 82c466d..1b1f261 100644 --- a/src/scriptExecuter.js +++ b/src/scriptExecuter.js @@ -3,6 +3,16 @@ */ function Added() { let outString = ""; + + //[jsste_info] + + function jsste_info() + { + let jssteinfo = jsste_input_file + result = `\tINFOS\n\t${Object.keys(jssteinfo).map((key) => key + " : " + jssteinfo[key] ).join("\n\t") }` + + return result; + } function out(arg) { outString += arg; @@ -26,6 +36,8 @@ function exec(script) { script ); + script = script.replace("//[jsste_info]",`let jsste_input_file = ${JSON.stringify(require("./index").info)}`), + script += "\n return outString;"; try {