2016-06-30 10 views
1

私は最初のカスタムYeoman Generatorを使用しています。ジェネレータが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) 
     }); 
    }, 
}); 

を提出された私はヨーマンジェネレータV 0.23.0およびノー​​ドV 4.4.5 を使用しています任意の助けてくれてありがとう。

答えて

2

アンダースコアがテンプレート内で渡されませんでした。だから、あなたがその上の機能にアクセスしようとすると、そこにはないことが伝えられます。

私の提案は、入力をジェネレータコード内で事前フォーマットし、文字列をテンプレートコンテキストとして渡すことだけです。テンプレートのロジックを少なくする方がよいでしょう。

それ以外の場合は、手動で渡すことができます。this.fs.copyTpl(from, to, {_: _, ...etc})

関連する問題