jsht v1.0.4
jsht
Program to generate templates files using NodeJS code, using tags <# #> and <#= #> to print values. With all capabilities NodeJS has to offer.
Prerequisites
NodeJS installed.
Installing
npm i -g jsht
Getting Started
1 - Create new template: Template file will be created at current dir/.jshtTemplates/create/class.js
jsht -n create class
2 - Edit new template current dir/.jshtTemplates/create/class.js
<#
//any NodeJS here
var className = "HelloWorld";
#>
class <#=className#>{
}
3 - Execute template
jsht create class
Will generate
class HelloWorld{
}
Examples
Create new template file
jsht -n create class
New template file will be created at current dir/.jshtTemplates/create/class.js.
Execute template file
jsht create class
Execute template file: current dir/.jshtTemplates/create/class.js.
Hello World
<#
//any NodeJS here
var className = "HelloWorld";
#>
class <#=className#>{
}
Will generate
class HelloWorld{
}
Print text
print text: <#
printText("printed text");
#>
short form: <#="printed text"#>
Will generate
print text: printed text
short form: printed text
IF
<#if(5 > 6){#>
var biggerVar;
<#}else{#>
var smallerVar;
<#}#>
Will generate
var smallerVar;
FOR
<#for(var i = 0; i < 10; i++){#>
let var<#=i#>;
<#}#>
Will generate
let var0;
let var1;
let var2;
let var3;
let var4;
let var5;
let var6;
let var7;
let var8;
let var9;
Location variables
Outputfile: <#=outputFile#>
Outputfile directory: <#=currentDir#>
Template name: <#=templateName#>
Template path: <#=templatePath#>
Template path directory: <#=templateDir#>
Will generate
Outputfile: <YOUR_LOCATION>/class.js
Outputfile directory: <YOUR_LOCATION>
Template name: class
Template path: <YOUR_LOCATION>/.jshtTemplates/create/class.js
Template path directory: <YOUR_LOCATION>/.jshtTemplates/create
Change output Name
<#
outputFile = "myNewFile.js";
#>
Change output location
<#
outputFile = "../class.js";
#>
not generating output location
<#
outputFile = "";
#>
or
<#
outputFile = null;
#>
Overwite file without asking -rf option overwrite existing files without asking. Example
jsht create class -rf
Output generated template in console\ -outputTemplate \ Example
jsht create class -outputTemplate
Output generated JS Code in console for debug\ -outputJSCode \ Example
jsht create class -outputJSCode
Create global template A global template can executed in any folder/path. To creat global template instead local template add "-g" after "-n":
jsht -n -g create class
First jsht will look for the template at "./.jshtTemplates" folder, if not found it will look search in the global template path.
Global template path Get
jsht config template location
Set
jsht config template location C:/Desenv/Templates
Require Modules Install your modules at ".jshtTemplates" folder, to see where ".jshtTemplates" is, execute command "jsht config template location". Any module installed at your ".jshtTemplates" folder will be accessible by require method in your template.
Require Files A js file can be called using require, by the relative path of the template file. Example:
require("./folder/fileToBeCalled");
Call Template from template
callTemplate("./otherTemplate.js", [], []);
or through command:
execShell("jsht otherTemplate");
The second parameter is an array of input arguments. The third parameter is an arrays of named arguments.
Build in functions
- execShell - execute command in the system \ params: \ command - command to be executed \ path - optional, folder to execute the command, if not provided it will be executed on the current folder. \ Example:
execShell("ls");
execShell("cd ..");
Input Args Aditional parameters will became arguments inside the template:
jsht create class MyClassName calculateAge
class <#=inputArgs[0]#>{
<#=inputArgs[1]#>(){
}
}
Named Args Passing named parameters.
jsht create class -methodName calculateAge -className MyClassName
class <#=namedArgs["className"]#>{
<#=namedArgs["methodName"]#>(){
}
}