Code Monkey home page Code Monkey logo

generator-app-gen's Introduction

app-gen

Join the chat at https://gitter.im/tarcisiojr/generator-app-gen Build Status Coverage Status npm version Codacy Badge Code Climate NPM

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

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%>']
}
...

In

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));
}

To

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"
}

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 -------------

generator-app-gen's People

Contributors

tarcisiojr avatar tarcisio-softbox avatar thyagoluciano avatar gitter-badger avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.