【问题标题】:_ is not defined custom yeoman generator_ 未定义自定义 yeoman 生成器
【发布时间】:2016-06-30 16:14:46
【问题描述】:

我正在开发我的第一个自定义 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 感谢您的帮助。

【问题讨论】:

    标签: yeoman yeoman-generator


    【解决方案1】:

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

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

    否则你可以手动传递this.fs.copyTpl(from, to, {_: _, ...etc})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多