_未定义自定义自动生成器

时间:2016-06-30 16:14:46

标签: yeoman yeoman-generator

我正在制作我的第一个定制Yeoman发电机并且遇到了障碍。当生成器创建package.json文件时,我收到_ is not defined的错误。错误是参考

    1| {
 >> 2|   "name": "<%= _.slugify(appName) %>",
    3|   "version": "0.0.1",
    4|   "description": "<%= appDescription %>",
    5|   "author": "<%= authorName %>",

这是我的index.js文件

'use strict';

var _ = require('underscore.string');
var generators = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

module.exports = generators.Base.extend({

    prompting: function () {
        var done = this.async();
        // Have Yeoman greet the user.
        this.log(yosay(
            'Welcome to the ' + chalk.red('\nSMS Boilerplate') + '\n generator!'
        ));
        this.log(chalk.green(
            'You\'ll also have the option to use Normalise-css and Modernizr.js \n'
        ));

        this.prompt([{
            type: 'input',
            name: 'appName',
            message: 'Your project name',
            default: 'sms-project',
            store: true
        }, {
            type: 'input',
            name: 'appDescription',
            message: 'Short description of the project...',
            default: 'A new SMS project',
            store: true
        }, {
            type: 'input',
            name: 'gitUsername',
            message: 'What\'s your Github username?',
            store: true
        }, {
            type: 'input',
            name: 'authorName',
            message: 'What\'s your name (the author)?',
            default: '',
            store: true
        }, {
            type: 'confirm',
            name: 'includeNormalize',
            message: 'Would you like to include Normalize.css?',
            default: true
        }]).then(function(answers) {
            this.props = answers;
            this.log('app name', answers.appName);
            done();
        }.bind(this));

    },

    writing: {
        // Copy the configuration files
        config: function() {
            this.fs.copyTpl(
                this.templatePath('_package.json'),
                this.destinationPath('package.json'),
                {
                    appName: _.slugify(this.props.appName),
                    appDescription : this.props.appDescription,
                    authorName : this.props.authorName
                }
            );
            this.fs.copyTpl(
                this.templatePath('_bower.json'),
                this.destinationPath('bower.json'),
                {
                    appName: this.props.appName,
                    appDescription : this.props.appDescription,
                    authorName : this.props.authorName,
                    includeNormalize : this.props.includeNormalize
                }
            );
            this.fs.copy(
                this.templatePath('bowerrc'),
                this.destinationPath('.bowerrc')
            );
        },
        // Copy Application Files
        app: function() {
            this.fs.copy(
                this.templatePath('scss/_style.scss'),
                this.destinationPath('scss/style.scss')
            );
            this.fs.copy(
                this.templatePath('css/_style.css'),
                this.destinationPath('css/style.css')
            );
            this.fs.copy(
                this.templatePath('js/_script.js'),
                this.destinationPath('js/script.js')
            );
            this.fs.copyTpl(
                this.templatePath('index.html'),
                this.destinationPath('index.html'),
                {
                    appName: this.props.appName,
                    appDescription : this.props.appDescription,
                    authorName : this.props.authorName
                }
            );
            this.fs.copy(
                this.templatePath('_Gruntfile.js'),
                this.destinationPath('Gruntfile.js')
            );
        },
    },

    //Install Dependencies
    install: function() {
        this.installDependencies({
            bower: true,
            npm: true,
            callback: function() {
                this.spawnCommand('grunt', ['bowerBuild']);
            }.bind(this)
        });
    },
});

我正在使用Yeoman Generator v 0.23.0和Node v 4.4.5 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

下划线未在模板中传递。因此,当您尝试访问它上面的功能时,它会告诉您它不存在。

我的建议是在生成器代码中预先格式化输入,并仅将字符串作为模板上下文传递。保持模板逻辑更少通常更好。

否则您可以手动传递having