From 21c704eaff3587e53be01b67beae7e4a4e718d4b Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 20 Nov 2022 19:27:34 +0100 Subject: [PATCH 1/2] add jsste as cli tool --- package.json | 3 ++ src/cmd.js | 104 +++++++++++++++++++++++++------------------------- src/engine.js | 23 ++++++----- 3 files changed, 69 insertions(+), 61 deletions(-) diff --git a/package.json b/package.json index bdc009b..1e94f9a 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "directories": { "test": "test" }, + "bin": { + "jsste": "./src/cmd.js" +}, "files": [ "src", "package.json", diff --git a/src/cmd.js b/src/cmd.js index c30bbdc..0fbe241 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node let jsste = require("./index"); let myargs = process.argv.slice(2); let output = ""; @@ -5,93 +6,94 @@ let pagefile, tempfile; let path = require("path"); let fs = require("fs"); +var currentPath = process.cwd(); + +jsste.__config.set("templatePath",currentPath) +jsste.__config.set("pagePath",currentPath) + /** * this function filters the prefix and call the callback with the param - * @param {prefix, args } settings - * @param {*} callback + * @param {prefix, args } settings + * @param {*} callback */ -let addCommand = ({prefix,args = myargs}, callback) => -{ - myargs.forEach((elem) =>{ - if(elem.startsWith(prefix)) - { - elem = elem.replace(prefix,"") - callback(elem); - } - }) -} +let addCommand = ({ prefix, args = myargs }, callback) => { + myargs.forEach((elem) => { + if (elem.startsWith(prefix)) { + elem = elem.replace(prefix, ""); + callback(elem); + } + }); +}; /** * -log | show jsste log */ -addCommand({prefix:"-log"},(arg) =>{ - jsste.setStateFunction((state) =>{ - console.log(state); - }) -}) +addCommand({ prefix: "-log" }, (arg) => { + jsste.setStateFunction((state) => { + console.log(state); + }); +}); /** * -Jsconfig | set jsste config as json */ -addCommand({prefix:"-Jsconfig="},(arg) =>{ - jsste.__config.setConfig(arg); -}) +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); -}) +addCommand({ prefix: "-pageFile=" }, (arg) => { + jsste.setState({ status: 4, statusMSG: currentPath }) + pagefile = JSON.parse(fs.readFileSync(path.join(currentPath, arg), "utf8")); + + pagefile["_SELFPATH_"] = path.dirname(arg); +}); /** * -pageFile | set pageFile as json */ -addCommand({prefix:"-page="},(arg) =>{ - - pagefile = JSON.parse(arg); - pagefile["_SELFPATH_"] = path.dirname(arg); -}) +addCommand({ prefix: "-page=" }, (arg) => { + pagefile = JSON.parse(arg); + pagefile["_SELFPATH_"] = path.dirname(arg); +}); /** * -tempFile | set tempFile path */ -addCommand({prefix:"-tempFile="},(arg) =>{ - tempfile = fs.readFileSync(arg, "utf8"); -}) +addCommand({ prefix: "-tempFile=" }, (arg) => { + tempfile = fs.readFileSync(arg, "utf8"); +}); /** * -pageFile | set pageFile as code */ -addCommand({prefix:"-temp="},(arg) =>{ - tempfile = arg -}) +addCommand({ prefix: "-temp=" }, (arg) => { + tempfile = arg; +}); /** * rendering */ -output = jsste.render(pagefile || undefined, tempfile || undefined) +output = jsste.render(pagefile || undefined, tempfile || undefined); /** * -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] ) - }) - } -}) +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) -}) +addCommand({ prefix: "-out" }, (arg) => { + console.log(output); +}); diff --git a/src/engine.js b/src/engine.js index c4593f5..3b6e9dd 100644 --- a/src/engine.js +++ b/src/engine.js @@ -27,7 +27,7 @@ function replaceAll(str, find, replace) { } /** - * function to handle consts + * function to handle consts * @param {*} pagecode * @param {*} constant * @param {*} callback @@ -56,20 +56,23 @@ app.render = function (pagecode, templatecode) { pagecode = JSON.parse(pagecode); app.setState({ status: 0, statusMSG: "Parse Pagecode" }); } else { - if(typeof pagecode === "object") - { + if (typeof pagecode === "object") { app.setState({ status: 0, statusMSG: "Pagecode is an object" }); - } - else{ + } else { app.setState({ status: 1, statusMSG: "Pagecode is undefined" }); - } - + } } if (!templatecode) { try { app.setState({ status: 0, statusMSG: "Load Templatecode" }); - console.log(app.config.templatePath) + app.setState({ + status: 4, + statusMSG: path.join( + app.config.templatePath, + pagecode["_TEMPLATE_"] + ".tjsste" + ), + }); templatecode = fs.readFileSync( path.join(app.config.templatePath, pagecode["_TEMPLATE_"] + ".tjsste"), "utf-8" @@ -124,10 +127,10 @@ app.render = function (pagecode, templatecode) { pagecode = currentPagecode; }; - // Handle _IMPORTS_ const + // Handle _IMPORTS_ const app.CONST(pagecode, "_IMPORTS_", DissolveImports); - // Handle _STYLES_ const + // Handle _STYLES_ const app.CONST(pagecode, "_STYLES_", (pagecode, value) => { app.setState({ status: 0, statusMSG: "Import Styles" }); let rex = /(.|\n|\t|\r)*?<\/head>/; From 99234c865ce23d45703840e461644e85701c5e9f Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 20 Nov 2022 19:28:47 +0100 Subject: [PATCH 2/2] rename cmd in cli --- package.json | 4 +-- src/cmd.js | 99 ---------------------------------------------------- 2 files changed, 2 insertions(+), 101 deletions(-) delete mode 100644 src/cmd.js diff --git a/package.json b/package.json index 1e94f9a..cf8f30f 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "test" }, "bin": { - "jsste": "./src/cmd.js" + "jsste": "./src/cli.js" }, "files": [ "src", @@ -18,7 +18,7 @@ "scripts": { "test": "mocha ./test/test.test.js", "publish": "npm publish --access publish", - "cmd": "node ./src/cmd.js" + "cmd": "node ./src/cli.js" }, "repository": { "type": "git", diff --git a/src/cmd.js b/src/cmd.js deleted file mode 100644 index 0fbe241..0000000 --- a/src/cmd.js +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/env node -let jsste = require("./index"); -let myargs = process.argv.slice(2); -let output = ""; -let pagefile, tempfile; -let path = require("path"); -let fs = require("fs"); - -var currentPath = process.cwd(); - -jsste.__config.set("templatePath",currentPath) -jsste.__config.set("pagePath",currentPath) - -/** - * 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) => { - if (elem.startsWith(prefix)) { - elem = elem.replace(prefix, ""); - callback(elem); - } - }); -}; - -/** - * -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) => { - jsste.setState({ status: 4, statusMSG: currentPath }) - pagefile = JSON.parse(fs.readFileSync(path.join(currentPath, arg), "utf8")); - - pagefile["_SELFPATH_"] = path.dirname(arg); -}); - -/** - * -pageFile | set pageFile as json - */ -addCommand({ prefix: "-page=" }, (arg) => { - 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 = arg; -}); - -/** - * rendering - */ -output = jsste.render(pagefile || undefined, tempfile || undefined); - -/** - * -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); -});