A template based, language agnostic, node scriptable code generator for developers .
To install the the ravi-codegen run the following cmd from terminal
npm i -g ravi-codegen
Run cmd from terminal
codegen init
It will create a folder .codegen
where all your templates reside
Every folder in .codegen
directory considered as a template.
Every template consist of at least two files
- Template script (template.js)
- Template (template.ejs)
Template script file is the entry point of your template script. Every template script must expose a default function that returns one or more Template models.
Every template model can have following fields.
- fileName -
Template output file name, default: <template-name>
- directory -
Output directory relative path from current directory, default: ., eg. ../entities
- overideFile -
Should override existing file if true, default: false
- model -
any data which is required by the template, default: null
- templateFile -
template to be used for this template model, default: template.ejs
So, a very simple script will look like as following
module.exports = async function script (args) { // args, provided while running the script
let templateModels = []; // for every model in templateModels codegen execute and generate some file defined by the model rules.
// create template model
const templateModel = {
fileName: '', // Template output file name, default: <template-name>
directory: '.', // Output directory relative path from current directory, default: ., eg. ../entities
templateFile: '', // Template name to use, default: template.ejs
model: {
// add model properties here, any property added will be available in ejs template used.
msg: 'lets start generating your code',
},
overideFile: false, // Should override if file exists, default false
};
templateModels.push(templateModel);
return templateModels;
}
Script Examples
ravi-codegen
uses the EJS template to generate the files. Take a look at EJS understand text templating language.
Run cmd from terminal
codegen new <template-name>
The above command will create a folder in .codegen
directory with two files, namely template.js
and template.ejs
Run cmd from terminal
codegen run <template-name> [args]
The above command will execute and create code files according to your templates.