Yeoman WordPress 生成器使用教程
1、项目的目录结构及介绍
Yeoman WordPress 生成器的目录结构如下:
yeoman-wordpress/
├── app/
│ ├── templates/
│ │ ├── theme/
│ │ └── plugin/
│ ├── index.js
│ └── utils.js
├── test/
│ ├── app.js
│ └── utils.js
├── .gitignore
├── .jshintrc
├── .npmignore
├── LICENSE
├── README.md
├── Rakefile
├── config.js
├── package.json
└── yo-generator.json
目录介绍:
app/
:包含生成器的主要逻辑和模板文件。templates/
:包含主题和插件的模板文件。index.js
:生成器的主入口文件。utils.js
:工具函数文件。
test/
:包含生成器的测试文件。.gitignore
:Git 忽略文件配置。.jshintrc
:JSHint 配置文件。.npmignore
:NPM 忽略文件配置。LICENSE
:项目许可证文件。README.md
:项目说明文档。Rakefile
:Rake 任务文件。config.js
:生成器的配置文件。package.json
:项目的依赖和脚本配置。yo-generator.json
:Yeoman 生成器的配置文件。
2、项目的启动文件介绍
index.js
index.js
是 Yeoman WordPress 生成器的主入口文件,负责初始化和配置生成器。以下是 index.js
的主要内容:
'use strict';
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');
module.exports = yeoman.Base.extend({
prompting: function () {
// 欢迎信息
this.log(yosay(
'Welcome to the ' + chalk.red('Yeoman WordPress') + ' generator!'
));
// 用户输入提示
var prompts = [
{
type: 'input',
name: 'projectName',
message: 'Your project name',
default: this.appname
},
{
type: 'list',
name: 'projectType',
message: 'What do you want to generate?',
choices: ['Theme', 'Plugin']
}
];
return this.prompt(prompts).then(function (props) {
this.props = props;
}.bind(this));
},
writing: function () {
// 根据用户选择生成文件
if (this.props.projectType === 'Theme') {
this.fs.copyTpl(
this.templatePath('theme'),
this.destinationPath(this.props.projectName)
);
} else {
this.fs.copyTpl(
this.templatePath('plugin'),
this.destinationPath(this.props.projectName)
);
}
},
install: function () {
// 安装依赖
this.installDependencies();
}
});
主要功能:
- 显示欢迎信息。
- 提示用户输入项目名称和项目类型(主题或插件)。
- 根据用户选择生成相应的文件和目录。
- 安装项目依赖。
3、项目的配置文件介绍
config.js
config.js
是 Yeoman WordPress 生成器的配置文件,包含生成器的默认配置和选项。以下是 config.js
的主要内容:
module.exports = {
defaultProjectName: 'my-wordpress-project',
defaultProjectType: 'Theme',
supportedProjectTypes: ['Theme', 'Plugin'],
themeTemplates: ['starter-theme', 'custom-theme'],
pluginTemplates: ['boilerplate-plugin', 'custom-plugin']
};
主要配置项:
defaultProjectName
:默认的项目名称。defaultProjectType
:默认的项目类型(主题或插件)。supportedProjectTypes
:支持的项目类型。