Skip to content

Instantly share code, notes, and snippets.

@AlloVince AlloVince/yeoman.md
Last active May 7, 2019

Embed
What would you like to do?
npm install -g reveal-md && wget -q -O yeoman.md https://gist.githubusercontent.com/AlloVince/8a53f1ff58748c719d15cbbe91ff5bd5/raw/yeoman.md && reveal-md yeoman.md

使用Yeoman生成项目脚手架

2018.8 @AlloVince


Yeoman

http://yeoman.io/


npm install -g yo generator-webapp
yo webapp

Dev a yeoman generator

npm install -g yo generator-generator
yo generator
npm link

├── generators/
│   └── app/
│       ├── index.js
│       └── templates/
│           └── dummyfile.txt
├── .editorconfig
├── .eslintignore
├── .gitattributes
├── .gitignore
├── .travis.yml
├── .yo-rc.json
├── LICENSE
├── README.md
├── package.json
└── __tests__/
    └── app.js

Basic

  • project root
  • run loop
  • destinationRoot / destinationPath
  • composeWith()

Events in a generator task

  • initializing
  • prompting
  • configuring
  • default
  • writing
  • conflicts
  • install
  • end

const Generator = require('yeoman-generator');
module.exports = class extends Generator {
  
  prompting() {
    const prompts = [
      {
        type: 'confirm',
        name: 'someAnswer',
        message: 'Would you like to enable this option?',
        default: true
      }
    ];
    return this.prompt(prompts).then(props => {
      this.props = props;
    });
  }
  
  writing() {
    this.fs.copy(
      this.templatePath('dummyfile.txt'),
      this.destinationPath('dummyfile.txt')
    );
  }
  
  install() {
    this.installDependencies();
  }
}

How to compose

class A extends Generator {
  prompting() {
    //doing A
  }
}

class B extends Generator {
  prompting() {
    this.composeWith(A)
    
    //doing B
  }
}

Core API

  • read / write / move / copy / exists
  • commit
  • extendJSON
  • copyTpl
  • npmInstall / bowerInstall / yarnInstall

Demo


yo typelibrary
输入项目名
选择LICENSE

yo evaengine
输入项目名
选择LICENSE
是否支持EvaQueue

yo android-app
输入项目名
是否支持离线包
是否支持RN
是否支持热更新
是否接入sentry
CI配置
Bugtags配置
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.