Yeoman, code generator

Every day the architectures become more complex, and the creation of a base application for the architecture that fits best in your project gets more difficult, so there are some tools that help us to make this process more simple and faster. One of them is Yeoman, that is a simple code generator, written in nodejs, with a long list of code generators for several architectures, that will help us start our project and keep our architecture as clean as possible, using the sub generators for each module.

Installation

The installation is very simple, we just need to have nodejs and npm installed and in the path, and we run the following command:

npm install -g yo

And now, we have yeoman installed globally and we can use it.

Using generators

Yeoman has a long list of generators, we can see the official ones here, but we can also create our own generator, or use different generators that can be on npm repositories or directly on github.

Example

We will use as example the generator we use the most, it is angular-fullstack. This generator has a MEAN.JS architecture (MongoDB, ExpreesJS, AngularJS and NodeJS), and it provides a backend with ExpressJS for a restful api, and Mongoose for data modeling, and a frontend with AngularJS.

The installation is simple with this command:

npm install -g yo grunt-cli bower generator-angular-fullstack

It uses Yeoman as generator, npm as package manager for the backend, bower as package manager for the frontend and grunt for the tasks.

When we have already installed, we need to create an application, so we create a directory and we generate the base:

mkdir fullstackapp1

yo angular-fullstack fullstackapp1

It will prompt all the selectable options, and now we choose what we want or what we need for our project.

Once the base generation is finished, we need to install all the dependencies with the commands:

npm install

bower install

When it is finished, to run it is just the following command:

grunt serve

And once the process is finished, the browser will be opened with the home page of our application.

Conclusions and tips

Yeoman is helpful to create base applications and to have order and a good structure in the applications, but sometimes it is necessary to know the used generator, because sometimes they have some particularities that can confuse us in the beginning, but knowing better the generator, we can do magic with it.

 

Thanks for reading, and if you have any question, please fell free to leave a comment.




Leave a Reply

Your email address will not be published. Required fields are marked *

Posts de Prensa Recientes