【发布时间】:2017-01-18 14:36:09
【问题描述】:
我正在尝试创建一个 yeoman 生成器来加快我的某些过程, 但是这样做有一些问题。
-
名称的输入是值
true,而不是提供的值 - 如果没有提供所有值,我会得到确认,但之后不会发生任何其他事情(仅在提供所有参数时)
-
scaffoldFolders 没有创建文件夹(编辑:搞定了)
有人知道我的问题的任何(甚至更好:所有)解决方案吗?
这是我正在使用的index.js:
'use strict';
var Generator = require('yeoman-generator');
var util = require('util')
var OptionOrPrompt = require('yeoman-option-or-prompt');
var mkdirp = require('mkdirp');
var _ = require('underscore.string');
var GlatGenerator = class extends Generator {
constructor(args, opts) {
// Calling the super constructor is important so our generator is correctly set up
super(args, opts);
this._optionOrPrompt = OptionOrPrompt;
this.props = {};
}
prompting() {
var done = this.async();
// Instead of calling prompt, call _optionOrPrompt to allow parameters to be passed as command line or composeWith options.
this._optionOrPrompt([{
type: 'input',
name: 'name',
message: 'Your component name',
default: 'hoogwerker',
store: true
}, {
type: 'confirm',
name: 'model',
message: 'Should we create a model for you?',
default: true,
store: true
}, {
type: 'confirm',
name: 'service',
message: 'Should we create a service for you?',
default: true,
store: true
}], function (answers) {
this.props.componentName = answers.name
this.props.createModel = answers.model
this.props.createService = answers.service
console.log("**********************");
console.log("***" + (JSON.stringify(answers)));
console.log("**********************");
done();
}.bind(this));
}
scaffoldFolders() {
console.log('scaffoldFolders');
var slugify = _.slugify(this.props.componentName);
var classify = _.classify(this.props.componentName);
var lowerName = _.decapitalize(_.classify(this.props.componentName));
mkdirp("src/components/" + lowerName);
mkdirp("src/components/" + lowerName + "/components");
if (this.props.createModel) {
mkdirp("src/components/" + lowerName + "/models");
}
if (this.props.createModel) {
mkdirp("src/components/" + lowerName + "/services");
}
}
copyMainFiles() {
console.log('copyMainFiles');
var slugify = _.slugify(this.props.componentName);
var classify = _.classify(this.props.componentName);
var lowerName = _.decapitalize(classify);
var dash = _.dasherize(lowerName);
var context = {
component_name: slugify,
component_name_camel: classify,
component_name_lower: lowerName,
component_name_dash: dash,
};
var base = "src/components/" + lowerName + "/";
this.fs.copyTpl(
this.templatePath('base-files/_component.html'),
this.destinationPath(base + lowerName + ".component.html"),
context
);
this.fs.copyTpl(
this.templatePath('base-files/_component.scss'),
this.destinationPath(base + lowerName + ".component.scss"),
context
);
this.fs.copyTpl(
this.templatePath('base-files/_component.ts'),
this.destinationPath(base + lowerName + ".component.ts"),
context
);
this.fs.copyTpl(
this.templatePath('base-files/_module.ts'),
this.destinationPath(base + lowerName + ".module.ts"),
context
);
this.fs.copyTpl(
this.templatePath('base-files/_routes.ts'),
this.destinationPath(base + lowerName + ".routes.ts"),
context
);
if (this.props.createModel) {
this.fs.copyTpl(
this.templatePath('model/_model.ts'),
this.destinationPath(base + "/models/" + classify + ".ts"),
context
);
}
if (this.props.createService) {
this.fs.copyTpl(
this.templatePath('service/_service.ts'),
this.destinationPath(base + "/services/" + lowerName + ".service.ts"),
context
);
}
}
};
module.exports = GlatGenerator;
// module.exports = base.extend({
// initializing: () => {},
// prompting: () => {},
// configuring: () => {},
// default: () => {},
// writing: () => {},
// conflicts: () => {},
// install: () => {},
// end: () => {}
// });
以及使用的命令:
yo glat:component --name="hoogwerker" --model --service
【问题讨论】: