generator-app-gen
v0.0.11
Published
Code generator based on Yeoman Generators.
Downloads
28
Maintainers
Readme
app-gen
Customizable code generator based on Yeoman Generator.
Usage
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.
Configuration (app-gen.[json|js])
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.
From
In
To
Configuration Structure
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": { ... }
}
}
From
In
...
"in": { # required
"driver": "JSON",
"config": {
"message": "Hello World!"
}
}
...
...
"in": { # required
"driver": "JSONFILE",
"config": ["file name.json"]
}
...
...
"in": {
"driver": "PROMPT",
"config": [{
"message": "Supply the message",
"name" : "message"
}]
}
...
...
"in": {
"driver": "MYSQL",
"config": {
"host": "localhost",
"port" : "3306",
"user" : "root",
"password": "root",
"query": "SELECT NULL"
}
}
...
...
"in": {
"driver": "POSTGRESQL",
"config": {
"host": "localhost",
"port" : 5432,
"database": "postgres"
"user" : "user",
"password": "password",
"query": "SELECT NULL"
}
}
...
...
"in": {
"driver": "JS",
"config": "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));
}
To
// Optional - Replace ocurrences in file.
"replace" : {
"regex": "JS RegExp",
"flags": "JS RegExp flags."
}
}
<a name="plugin-console" />
### CONSOLE
Writes the rendered template at console output.
```js
{
"driver": "CONSOLE"
}
Examples
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 -------------