[code]npm install -g generator-ngtimo[/code]安装完成后即可使用yo命令来进行构建:
[code]yo ngtimo[/code]然后顺利的话yeoman会像下面这样询问一系列构建的配置,这里笔者选择输入项目名称为ng-test剩下的一路敲回车: [img]http://files.jb51.net/file_images/article/201708/2017081407540016.png[/img] 顺利的话如下图这样的项目结构就诞生了,可以 cd 到项目目录下(自动执行的 npm install 失败的话再手动 npm install 一下),并运行 npm run server 启动项目查看效果。 [img]http://files.jb51.net/file_images/article/201708/2017081407540017.png[/img] 如果只是想要使用yeoman来快速进行项目搭建的话,只需要找到一个喜欢的generator,像上文这样全局安装然后yo它就可以了!不过只是使用别人的generator会有些不自由而且考验对方的维护能力,就像笔者这个一时兴起的ngtimo就才刚刚有了一个主模板而已,还需要做很多改进和迭代。 如果想要自己来编写一个generator其实难度也非常小,yeoman官方甚至给出了一个generator-generator来帮助我们创建一个generator,笔者这个不成器的ngtimo也是yo generator给yo出来然后加以养成的 :)。 [b]yeoman generator基本项目结构[/b] 不想自己从零开始写一个generator的话强烈推荐使用yeoman官方的generator-generator先把基本结构构建出来:
[code]npm install -g generator-generator yo generator[/code]yeoman的generator说白也只是一个npm包,主要依赖yeoman-generator包来制定构建规则,这里给出ngtimo的基本目录结构: [img]http://files.jb51.net/file_images/article/201708/2017081407540018.png[/img] 构建规则 现在主要着眼于generators/app/index.js,此文件是最主要的角色,定义了ngtimo要如何处理模板,如何与用户交互等。笔者目前也只是照着比较成熟的generator在使用,存在片面之处还请包涵。 [b]总览index.js[/b] 整个index.js将导出一个扩展了yeoman-generator的类,就像这样:
const Generator = require('yeoman-generator');
// ...
module.exports = class extends Generator {
prompting() {
// ...
}
default() {
// ...
}
writing() {
// ...
}
install() {
// ...
}
}
const yosay = require('yosay');
// ...
this.log(yosay(
'Welcome to the astonishing ' + chalk.red('generator-ngtimo') + ' generator!'
));
const prompts = [{
type: 'input',
name: 'appName',
message: 'Your project name(你的项目名称)',
default: this.appname
}, {
type: 'confirm',
name: 'addCommon',
message: 'Would you like to add some common code(include CoreModule, SharedModule, Router)?\n(要自动创建额外的常用内容吗, 包含了核心模块、共享模块和路由能力)',
default: true
}];
return this.prompt(prompts).then(props => {
// 用户交互完成后把得到的配置设置到参数中
this.props = props;
});
const path = require('path');
const mkdirp = require('mkdirp');
// ...
default() {
if (path.basename(this.destinationPath()) !== this.props.appName) {
this.log(
'发现你不是在 ' + this.props.appName + ' 目录下构建\n' +
'我将主动创建此目录.(created folder with app name)'
);
mkdirp(this.props.appName);
this.destinationRoot(this.destinationPath(this.props.appName));
}
}
this.fs.copy(
this.templatePath('模板位置'),
this.destinationPath('目标位置')
);
this.fs.copyTpl(
this.templatePath('模板位置'),
this.destinationPath('目标位置'),
{
参数名: 值
}
);
install() {
this.installDependencies({bower: false});
}
npm link yo ngtimo
npm publish
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有