jhstest v1.0.0
jhs
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 jhs
Getting Started
1 - Create new template: Template file will be created at current dir/.jhsTemplates/create/class.js
jhs -n create class
2 - Edit new template current dir/.jhsTemplates/create/class.js
<#
//any NodeJS here
var className = "HelloWorld";
#>
class <#=className#>{
}
3 - Execute template
jhs create class
Will generate
class HelloWorld{
}
Examples
Create new template file
jhs -n create class
New template file will be created at current dir/.jhsTemplates/create/class.js.
Execute template file
jhs create class
Execute template file: current dir/.jhsTemplates/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>/.jhsTemplates/create/class.js
Template path directory: <YOUR_LOCATION>/.jhsTemplates/create
Change output Name
<#
outputFile = "myNewFile.js";
#>
Change output location
<#
outputFile = "../class.js";
#>
Overwite file without asking -rf option overwrite existing files without asking. Example
<#
jhs create class -rf
#>
Create global template A global template can executed in any folder/path. To creat global template instead local template add "-g" after "-n":
jhs -n -g create class
First jhs will look for the template at "./.jhsTemplates" folder, if not found it will look search in the global template path.
Global template path Get
jhs config template location
Set
jhs config template location C:/Desenv/Templates
Require Modules Install your modules at ".jhsTemplates" folder, to see where ".jhsTemplates" is, execute command "jhs config template location". Any module installed at your ".jhsTemplates" 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", [], []);
The second parameter is an array of input arguments. The third parameter is an arrays of named arguments.
Input Args Aditional parameters will became arguments inside the template:
jhs create class MyClassName calculateAge
class <#=inputArgs[0]#>{
<#=inputArgs[1]#>(){
}
}
Named Args Passing named parameters.
jhs create class -methodName calculateAge -className MyClassName
class <#=namedArgs["className"]#>{
<#=namedArgs["methodName"]#>(){
}
}
3 years ago