gGen
A directory structure and file generator.
gGen allows you to easily create template directory structures and template files for your project.
You can generate any kind of files
- Java classes, interfaces...
- Golang files
- React Components
- Redux Reducers
- Redux Sagas
index.js
files based on directory content- CSS
- HTML
- And many more !
Install
# Local install npm i -D ggen
# Global install npm i -g ggen # In each project directory npm link ggen
Create default configuration:
# From your project directory ggen init
Usage
After you've edited your configuration:
ggen <command> [opts]
<command>
and [opts]
depend on your config files, see below.
Examples
Look in the .ggen
directory of this project to have more complete examples.
Configuration
By default, ggen
looks for its configuration in .ggen/
in the current working directory.
You can override this path with the GGEN_PATH
environnement variable.
.ggen
structure:
.ggen
├── templates/ -- Handlebars template files for file generation
└── config.js -- Main configuration file
File generation uses Handlebars templates located at <GGEN_PATH>/templates
.
Simple example
Let's say you want to automate the process of creating a nodejs module, using the following structure:
<module name>/
└── index.js
Where index.js
exports a single function.
Template and config files
-
.ggen/templates/index.js.hbs
module.exports = function {{ module_name }}() {} -
.ggen/config.js
const type =moduleexports =// adds the "module" command to the ggen climodule:params:// adds the --module_name option the ggen cli// adds a "module_name" variable to the template's contextmodule_name: type // function to parse the command line input// will create a directory named after the "module_name" cli option,// containing an index.js file based on the index.js.hbs templatetree:"{{module_name}}":"index.js": "index.js.hbs"
example
module
Generating an To generate the example
module, simply call:
ggen module --module_name example
It will create the following structure in your current working directory:
example/
└── index.js
example/index.js
:
module { }
Parameter types
NOTICE If you have installed
ggen
globally, you will need to link it to your project to use included type parsers.npm link ggen
Start your config file with:
const type = // type.String()// type.Array()// type.Map()// ...
type.String()
, type.Number()
, type.Boolean()
Parses the input into either a String, Number, or Boolean types
type.Array(separator = ",")
Parses the input into an array of strings.
// config.jsconst example = tree: // directory structure params: list: type
# generation command ggen example --list apple,banana,orange
// Resulting template context "list": "apple" "banana" "orange"
type.Map(entrySeparator = ",", keyValueSeparator ":")
Parses the input into an object.
// config.jsconst example = tree: // directory structure params: properties: type
# genaration command ggen example --properties email:null,isOk:true
// Resulting template context "properties": "email": "null" "isOk": "true"