Yeoman – Getting Started with ‘Yo’ Automation via Terminal
Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.
It allows you to type the yo
command in terminal, then answer few questions and do amazing stuff automatically.
It’s essentially a workflow tool/plugin built on node.js. Which provides a cool user interface inside your terminal and can automate tasks.
Understanding the application lifecycle
Yeoman uses a run loop to prioritize the tasks to automate. It means that each one of those methods will be invoke by their order – you need to take this to your advantage.
The methods priority is (in running order):
initializing
– Your initialization methods (checking current project state, getting configs, etc)prompting
– Where you prompt users for options (where you’d callthis.prompt()
)configuring
– Saving configurations and configure the project (creating.editorconfig
files and other metadata files)default
– If the method name doesn’t match a priority, it will be pushed to this group.writing
– Where you write the generator specific files (routes, controllers, etc)conflicts
– Where conflicts are handled (used internally)install
– Where installations are run (npm, bower)end
– Called last, cleanup, say good bye, etc
Follow these priorities guidelines and your generator will play nice with others.
Quick Getting Started :
Assuming you already have node
and npm
installed.
STEP 1
- Install
yo
usingnpm install -g yo
STEP 2
- Install the boiler-plate generator to create a generator.
- Use the following command
npm install -g yo generator-generator
STEP 3
- Run the following command on your terminal:
yo generator
Name your generator this will be the only required input and then follow the questions answer as you wish – we will be able to change those settings anyway.
This will simply created the basic structure of a generator for us.
STEP 4
- Run the following command to open the project folder in VS Code.
code generator-<your-generator-name>
Lets step into the package.json file – you can replace it or make it look like this for the base.
{ "name": "generator-sfdx", "version": "0.0.1", "description": "sample generator for sfdx yeoman", "author": { "name": "Your Name" }, "license": "MIT", "files": [ "generators/app" ], "keywords": [ "yeoman-generator" ], "main": "generators/app/index.js", "dependencies": { "chalk": "^2.3.0", "generator-sfdx": "^0.1.11", "github-username": "^4.1.0", "inquirer": "^7.0.0", "shelljs": "^0.7.8", "yeoman-generator": "^1.1.1", "yosay": "^2.0.1" }, "repository": "https://github.com" }
STEP 5
Go to generators/app/index.js => this is where we can configure your questions and actions.
'use strict'; const Generator = require('yeoman-generator'); const chalk = require('chalk'); const yosay = require('yosay'); module.exports = class extends Generator { initializing() { this.log( yosay(`Welcome to my ${chalk.red('Cool App ')} generator!` )); } prompting() { const prompts = [ { type: 'confirm', name: 'someAnswer', message: 'Would you like to enable this option?', default: true } ]; return this.prompt(prompts).then(props => { // To access props later use this.props.someAnswer; this.props = props; }); } };
STEP 6
- Link all your dependencies to your project with
npm link
STEP 7
You ready to run your generator with the word yo-<your-generator-name>