Customizable code generator based on Yeoman Generator.
First install de Yeoman:
npm install -g yo
Install generator-app-gen
:
npm install -g generator-app-gen
Go into your project directory:
cd my-project
Create a file app-gen.[json|js]
(see above for more details):
vi app-gen.json
Run yo app-gen
to start code generation.
The app-gen.[json|js]
file contains information about code generation for your project. In this file going to describe the configurations for artifacts code generation. This file can be a json or a JavaScript module.
The artifact code generation is performed by 3 configurations: from
, in
and to
.
The configuration from
is the template source.
The configuration in
is the input source.
The configuration to
is the destination.
Below are the available drivers for each configuration.
In this example, the artifact "Sample1" is getting a template from a file, reading the inputs from JSON and will write resultant text on console and in a file.
{
"name": "Project Name",
"helper" : {
"filter": "/helpers/filter.js"
},
"artifacts": {
"Sample1": {
"from": {
"driver": "FILE",
"template": ["./templates/sample-from.js"]
},
"in": {
"driver": "JSON",
"config": {
"message": "Hello World!"
}
},
"to": [{
"driver": "CONSOLE"
}, {
"driver": "FILE",
"out" : "./out/sample-out.js"
}]
},
"Sample2": { ... },
...
"SampleN": { ... }
}
}
Read a template from file.
...
"from": {
"driver": "FILE",
"template": ["/tmeplate/path/goes/here.tmpl"]
}
...
Read a template from file.
...
"from": {
"driver": "JSON",
"template": ['template string <%=value%>']
}
...
Read the static supplied JSON config for template bindings.
...
"in": { # required
"driver": "JSON",
"config": {
"message": "Hello World!"
}
}
...
Similar to JSON, but reads the JSON of supplied files.
...
"in": { # required
"driver": "JSONFILE",
"config": ["file name.json"]
}
...
Request configuration with prompt api (see inquirer)
...
"in": {
"driver": "PROMPT",
"config": [{
"message": "Supply the message",
"name" : "message"
}]
}
...
Request configuration from database.
...
"in": {
"driver": "MYSQL",
"config": {
"host": "localhost",
"port" : "3306",
"user" : "root",
"password": "root",
"query": "SELECT NULL"
}
}
...
Request configuration from database.
...
"in": {
"driver": "POSTGRESQL",
"config": {
"host": "localhost",
"port" : 5432,
"database": "postgres"
"user" : "user",
"password": "password",
"query": "SELECT NULL"
}
}
...
Read the values from a execution of JavaScript file.
...
"in": {
"driver": "JS",
"config": { "filename" : "javascrip_file_name.js" }
}
...
JavaScript Sample:
module.exports = function(
generator, // Instance of Yo Generator
inValues, // Previous IN values
callback // Callback function
) {
// Your code goes here
var newValues = {
newValue: "newValue"
};
console.log('>>', Object.assign({}, inValues, newValues));
callback(null, Object.assign({}, inValues, newValues));
}
Writes the rendered template at supplied output file.
{
"driver": "FILE",
"out": ["path to destination file"],
// Optional - Replace ocurrences in file.
"replace" : {
"regex": "JS RegExp",
"flags": "JS RegExp flags."
}
}
Writes the rendered template at console output.
{
"driver": "CONSOLE"
}
Prints a string at console from a JSON template where the input is supplied by user.
app-gen.json
{
"name": "Examples",
"artifacts": {
"Example1": {
"from": {
"driver": "JSON",
"template": ["Hello <%=message%>"]
},
"in": {
"driver": "PROMPT",
"config": [{
"message": "Supply the message:",
"name": "message"
}]
},
"to": {
"driver": "CONSOLE"
}
}
}
}
Run app-gen and select the artifact "Example1". Will be prompted a message. If you supply "world", the output will be like this:
? Choose the artifact Example1
? Supply the message: world
------------- OUT -------------
Hello world
------------- END -------------