基于generator-vuecli-3-mobile 为例
导读 面对复杂的 Web 应用的开发,良好的流程和工具支持是必不可少的,可以让日常的开发工作更加顺畅。Yeoman 作为一个流行的工具集,在整合了 Yo、Grunt 和 Bower 等工具的基础上,定义了一个更加完备和清晰的工作流程。通过把一些最佳实践引入到 Web 应用中,有助于创建高质量和可维护的应用。
环境 需要安装Nodejs
全局安装yo
安装generator-generator ,生成自己需要的generator的基础框架
1 npm install -g generator-generator
运行命令开始生成基础代码
设置之后
检查一下生成的目录结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 . ├── generators/ │ └── app/ │ ├── index.js // 设置项目模板文件 │ └── templates/ // 存放模板文件 │ └── dummyfile.txt ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .eslintrc ├── .travis.yml ├── .yo-rc.json // 存储项目配置 ├── package.json ├── gulpfile.js ├── README.md ├── LICENSE
将模板文件放入到 generators/app/templates文件夹后,我们开始编辑 index.js 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 'use strict' ;const Generator = require ('yeoman-generator' );const chalk = require ('chalk' );const yosay = require ('yosay' );const glob = require ('globby' );const path = require ('path' );module .exports = class extends Generator { prompting() { this .log( yosay(`Welcome to the phenomenal ${chalk.red('generator-test-generator' )} generator!` ) ); const prompts = [ { type: 'confirm' , name: 'someAnswer' , message: 'Would you like to enable this option?' , default : true }, { type: 'input' , name: 'project-version' , message: '项目版本号' , default : '0.0.1' } ... ]; return this .prompt(prompts).then(props => { this .props = props; }); } writing() { this ._writeRootFiles(); this ._writeFolders(); } _writeFolders() { ['public' , 'src' ].forEach(floder => { this .fs.copy( this .templatePath(floder), this .destinationPath(floder) ) }); } _writeRootFiles() { glob .sync([this .templatePath('*' ), this .templatePath('package.json' )]) .forEach(file => { this .fs.copy( this .templatePath(file), this .destinationPath(path.basename(file).replace(/^_/ , '' )) ) }); this .fs.copyTpl( this .templatePath('package.json' ), this .destinationPath('package.json' ), this .props ); } install() { this .installDependencies({ bower : false }); } };
提交到git仓库 1 2 3 4 5 git init git add . git commit -m 'first commit' git remote add origin https: git push -u origin master
发布到npm
登录or注册账号
1 2 3 4 5 npm publish --access=public + generator-vuecli-3 -mobile@0.0 .4
如何使用
升级脚手架 实际开发中我们难免会多次修改项目,如何升级脚手架呢?
1.修改项目中package.json里的version版本号
2.重新发布
3.重新安装全局脚手架
1 2 3 4 sudo npm i vuecli-3 -mobile@1.0 .1 -g sudo npm uninstall vuecli-3 -mobile -g sudo npm install vuecli-3 -mobile -g