2017-07-27 193 views
0

我正在使用yeoman製作一個web應用程序,我被困在創建一個發電機。問題是它不會將文件複製到輸出文件夾。我yeoman發電機不會複製文件

這裏是我的代碼:

'use strict'; 
var fs = require('fs'); 
var path = require('path'); 
var yeoman = require('yeoman-generator'); 
var yosay = require('yosay'); 
var chalk = require('chalk'); 
var wiredep = require('wiredep'); 

module.exports=yeoman.extend({ 

    scaffoldFolders: function(){ 
     this.mkdir("app"); 
     this.mkdir("app/css"); 
     this.mkdir("app/sections"); 
     this.mkdir("build"); 
    }, 

    initializing: function(){ 
    this.pkg=require('../../package.json'); 
    }, 

    prompting: function() { 
    var done = this.async(); 

    this.log(yosay(
     'Welcome to the marvelous ' + chalk.red('generator-palmyra') + ' generator!' 
    )); 

    var prompts = [{ 
     type: 'checkbox', 
     name: 'mainframeworks', 
     message:'Would you like AngularJS or JQuery ?', 
     choices: [{ 
     name: 'Angular', 
     value: 'includeAngular', 
     checked: true 
     }, { 
     name: 'JQuery', 
     value: 'includeJQuery', 
     checked: true 
     }] 
     }, 
    { 
     type: 'checkbox', 
     name: 'features', 
     message:'What more front-end frameworks would you like ?', 
     choices: [{ 
     name: 'Sass', 
     value: 'includeSass', 
     checked: true 
    }, { 
     name: 'Bootstrap', 
     value: 'includeBootstrap', 
     checked: true 
    }, { 
     name: 'Modernizr', 
     value: 'includeModernizr', 
     checked: true 
     }] 
    } 
    ]; 


    this.prompt(prompts, function (answers) { 
    var features = answers.features; 
    var mainframeworks = answers.mainframeworks; 
    var hasFeature = function (feat) { 
    return features.indexOf(feat) !== -1; 
    }; 
    var hasMainframeworks = function (mainframework) { 
    return mainframeworks.indexOf(mainframework) !== -1; 
    }; 
// manually deal with the response, get back and store the results. 
    this.includeSass = hasFeature('includeSass'); 
    this.includeBootstrap = hasFeature('includeBootstrap'); 
    this.includeModernizr = hasFeature('includeModernizr'); 
    this.includeAngular = hasMainframeworks('includeAngular'); 
    this.includeJQuery = hasMainframeworks('includeJQuery'); 
    done(); 
}.bind(this)); 
}, 



    writing() { 

    gulpfile= function(){ 
    this.fs.copy(
     this.templatePath('gulpfile.js'), 
     this.destinationPath('gulpfile.js') 
       ); 
         }, 
    packageJSON= function() { 
     this.fs.copy(
    this.templatePath('_package.json'), 
    this.destinationPath('package.json') 
); 
           }, 
    git= function() { 
     this.fs.copy(
     this.templatePath('gitignore'), 
     this.destinationPath('.gitignore') 
        ); 
     this.fs.copy(
     this.templatePath('gitattributes'), 
     this.destinationPath('.gitattributes') 
        ); 
        }, 
     bower= function() { 
          var bower = { 
          name: this._.slugify(this.appname), 
          private: true, 
          dependencies: {} 
          }; 
          if (this.includeBootstrap) { 
          var bs = 'bootstrap' + (this.includeSass ? '-sass' : ''); 
          bower.dependencies[bs] = '~3.3.1'; 
          } 
          if (this.includeModernizr) { 
          bower.dependencies.modernizr = '~2.8.1'; 
          } 
         if (this.includeAngular) { 
         bower.dependencies.angular = '~1.3.15'; 
         } 
         if (this.includeJQuery) { 
         bower.dependencies.jquery = '~2.1.1'; 
         } 
         this.fs.copy(
          this.templatePath('bowerrc'), 
          this.destinationPath('.bowerrc') 
         ); 
          this.write('bower.json', JSON.stringify(bower, null, 2)); 
         }, 
     jshint= function() { 
     this.fs.copy(
     this.templatePath('jshintrc'), 
     this.destinationPath('.jshintrc') 
       ); 
          }, 
    mainStylesheet= function() { 
          var css = 'main'; 
          if (this.includeSass) { 
           css += '.scss'; 
          } else { 
           css += '.css'; 
          } 
         this.copy(css, 'app/styles/' + css); 
           }, 
    writeIndex= function() { 
           this.indexFile = this.src.read('index.html'); 
           this.indexFile = this.engine(this.indexFile, this); 
           // wire Bootstrap plugins 
           if (this.includeBootstrap) { 
            var bs = '/bower_components/'; 
            if (this.includeSass) { 
            bs += 'bootstrap-sass/assets/javascripts/bootstrap/'; 
            } else { 
            bs += 'bootstrap/js/'; 
            } 
            this.indexFile = this.appendScripts(this.indexFile, 'scripts/plugins.js', [ 
            bs + 'affix.js', 
            bs + 'alert.js', 
            bs + 'dropdown.js', 
            bs + 'tooltip.js', 
            bs + 'modal.js', 
            bs + 'transition.js', 
            bs + 'button.js', 
            bs + 'popover.js', 
            bs + 'carousel.js', 
            bs + 'scrollspy.js', 
            bs + 'collapse.js', 
            bs + 'tab.js' 
            ]); 
           } 
           this.indexFile = this.appendFiles({ 
            html: this.indexFile, 
            fileType: 'js', 
            optimizedPath: 'scripts/main.js', 
            sourceFileList: ['scripts/main.js'] 
           }); 
           this.write('app/index.html', this.indexFile); 
           }, 
           app= function() { 
           this.copy('main.js', 'app/scripts/main.js'); 
           } 
          }, 
    install: function() { 
    var howToInstall = 
     '\nAfter running ' + 
     chalk.yellow.bold('npm install & bower install') + 
     ', inject your' + 
     '\nfront end dependencies by running ' + 
     chalk.yellow.bold('gulp wiredep') + 
     '.'; 
    if (this.options['skip-install']) { 
     this.log(howToInstall); 
     return; 
    } 
    this.installDependencies(); 
    this.on('end', function() { 
     var bowerJson = this.dest.readJSON('bower.json'); 
     // wire Bower packages to .html 
     wiredep({ 
     bowerJson: bowerJson, 
     directory: 'bower_components', 
     exclude: ['bootstrap-sass', 'bootstrap.js'], 
     ignorePath: /^(\.\.\/)*\.\./, 
     src: 'app/index.html' 
     }); 
     if (this.includeSass) { 
     // wire Bower packages to .scss 
     wiredep({ 
      bowerJson: bowerJson, 
      directory: 'bower_components', 
      ignorePath: /^(\.\.\/)+/, 
      src: 'app/styles/*.scss' 
     }); 
     } 
    }.bind(this)); 
    } 
}); 

我認爲這個問題是在寫作方法。我也想問下一步該去哪裏?或者我通過了一個基本步驟去學習web dev

+0

您是否收到任何錯誤?如果是這樣,請分享它們。 –

+0

沒有錯誤...當我與yeoman一起運行時...提示方法工作原理:詢問我是否想使用角度或jquery ...填充選項後...它只是在那裏結束 –

回答

0

如果你格式化你的代碼,你會看到writing函數沒有做任何事情。它聲明瞭一堆子函數,但不運行任何人。

我認爲問題是你想要一個對象,而是寫了一個函數:writing() {}而不是writing: {}

我做了一個代碼的快速修復,但沒有測試出來。所以有可能是進一步的語法問題,但它應該大致是這樣的:

'use strict'; 
 
var fs = require('fs'); 
 
var path = require('path'); 
 
var yeoman = require('yeoman-generator'); 
 
var yosay = require('yosay'); 
 
var chalk = require('chalk'); 
 
var wiredep = require('wiredep'); 
 

 
module.exports = yeoman.extend({ 
 
    scaffoldFolders: function() { 
 
     this.mkdir('app'); 
 
     this.mkdir('app/css'); 
 
     this.mkdir('app/sections'); 
 
     this.mkdir('build'); 
 
    }, 
 

 
    initializing: function() { 
 
     this.pkg = require('../../package.json'); 
 
    }, 
 

 
    prompting: function() { 
 
     var done = this.async(); 
 

 
     this.log(
 
      yosay(
 
       'Welcome to the marvelous ' + 
 
        chalk.red('generator-palmyra') + 
 
        ' generator!' 
 
      ) 
 
     ); 
 

 
     var prompts = [ 
 
      { 
 
       type: 'checkbox', 
 
       name: 'mainframeworks', 
 
       message: 'Would you like AngularJS or JQuery ?', 
 
       choices: [ 
 
        { 
 
         name: 'Angular', 
 
         value: 'includeAngular', 
 
         checked: true, 
 
        }, 
 
        { 
 
         name: 'JQuery', 
 
         value: 'includeJQuery', 
 
         checked: true, 
 
        }, 
 
       ], 
 
      }, 
 
      { 
 
       type: 'checkbox', 
 
       name: 'features', 
 
       message: 'What more front-end frameworks would you like ?', 
 
       choices: [ 
 
        { 
 
         name: 'Sass', 
 
         value: 'includeSass', 
 
         checked: true, 
 
        }, 
 
        { 
 
         name: 'Bootstrap', 
 
         value: 'includeBootstrap', 
 
         checked: true, 
 
        }, 
 
        { 
 
         name: 'Modernizr', 
 
         value: 'includeModernizr', 
 
         checked: true, 
 
        }, 
 
       ], 
 
      }, 
 
     ]; 
 

 
     this.prompt(
 
      prompts, 
 
      function(answers) { 
 
       var features = answers.features; 
 
       var mainframeworks = answers.mainframeworks; 
 
       var hasFeature = function(feat) { 
 
        return features.indexOf(feat) !== -1; 
 
       }; 
 
       var hasMainframeworks = function(mainframework) { 
 
        return mainframeworks.indexOf(mainframework) !== -1; 
 
       }; 
 
       // manually deal with the response, get back and store the results. 
 
       this.includeSass = hasFeature('includeSass'); 
 
       this.includeBootstrap = hasFeature('includeBootstrap'); 
 
       this.includeModernizr = hasFeature('includeModernizr'); 
 
       this.includeAngular = hasMainframeworks('includeAngular'); 
 
       this.includeJQuery = hasMainframeworks('includeJQuery'); 
 
       done(); 
 
      }.bind(this) 
 
     ); 
 
    }, 
 

 
    writing: { 
 
     gulpfile: function() { 
 
      this.fs.copy(
 
       this.templatePath('gulpfile.js'), 
 
       this.destinationPath('gulpfile.js') 
 
      ); 
 
     }, 
 
     packageJSON: function() { 
 
      this.fs.copy(
 
       this.templatePath('_package.json'), 
 
       this.destinationPath('package.json') 
 
      ); 
 
     }, 
 
     git: function() { 
 
      this.fs.copy(
 
       this.templatePath('gitignore'), 
 
       this.destinationPath('.gitignore') 
 
      ); 
 
      this.fs.copy(
 
       this.templatePath('gitattributes'), 
 
       this.destinationPath('.gitattributes') 
 
      ); 
 
     }, 
 
     bower: function() { 
 
      var bower = { 
 
       name: this._.slugify(this.appname), 
 
       private: true, 
 
       dependencies: {}, 
 
      }; 
 
      if (this.includeBootstrap) { 
 
       var bs = 'bootstrap' + (this.includeSass ? '-sass' : ''); 
 
       bower.dependencies[bs] = '~3.3.1'; 
 
      } 
 
      if (this.includeModernizr) { 
 
       bower.dependencies.modernizr = '~2.8.1'; 
 
      } 
 
      if (this.includeAngular) { 
 
       bower.dependencies.angular = '~1.3.15'; 
 
      } 
 
      if (this.includeJQuery) { 
 
       bower.dependencies.jquery = '~2.1.1'; 
 
      } 
 
      this.fs.copy(this.templatePath('bowerrc'), this.destinationPath('.bowerrc')); 
 
      this.write('bower.json', JSON.stringify(bower, null, 2)); 
 
     }, 
 
     jshint: function() { 
 
      this.fs.copy(
 
       this.templatePath('jshintrc'), 
 
       this.destinationPath('.jshintrc') 
 
      ); 
 
     }, 
 
     mainStylesheet: function() { 
 
      var css = 'main'; 
 
      if (this.includeSass) { 
 
       css += '.scss'; 
 
      } else { 
 
       css += '.css'; 
 
      } 
 
      this.copy(css, 'app/styles/' + css); 
 
     }, 
 
     writeIndex: function() { 
 
      this.indexFile = this.src.read('index.html'); 
 
      this.indexFile = this.engine(this.indexFile, this); 
 
      // wire Bootstrap plugins 
 
      if (this.includeBootstrap) { 
 
       var bs = '/bower_components/'; 
 
       if (this.includeSass) { 
 
        bs += 'bootstrap-sass/assets/javascripts/bootstrap/'; 
 
       } else { 
 
        bs += 'bootstrap/js/'; 
 
       } 
 
       this.indexFile = this.appendScripts(
 
        this.indexFile, 
 
        'scripts/plugins.js', 
 
        [ 
 
         bs + 'affix.js', 
 
         bs + 'alert.js', 
 
         bs + 'dropdown.js', 
 
         bs + 'tooltip.js', 
 
         bs + 'modal.js', 
 
         bs + 'transition.js', 
 
         bs + 'button.js', 
 
         bs + 'popover.js', 
 
         bs + 'carousel.js', 
 
         bs + 'scrollspy.js', 
 
         bs + 'collapse.js', 
 
         bs + 'tab.js', 
 
        ] 
 
       ); 
 
      } 
 
      this.indexFile = this.appendFiles({ 
 
       html: this.indexFile, 
 
       fileType: 'js', 
 
       optimizedPath: 'scripts/main.js', 
 
       sourceFileList: ['scripts/main.js'], 
 
      }); 
 
      this.write('app/index.html', this.indexFile); 
 
     }, 
 
     app: function() { 
 
      this.copy('main.js', 'app/scripts/main.js'); 
 
     }, 
 
    }, 
 

 
    install: function() { 
 
     var howToInstall = 
 
      '\nAfter running ' + 
 
      chalk.yellow.bold('npm install & bower install') + 
 
      ', inject your' + 
 
      '\nfront end dependencies by running ' + 
 
      chalk.yellow.bold('gulp wiredep') + 
 
      '.'; 
 
     if (this.options['skip-install']) { 
 
      this.log(howToInstall); 
 
      return; 
 
     } 
 
     this.installDependencies(); 
 
     this.on(
 
      'end', 
 
      function() { 
 
       var bowerJson = this.dest.readJSON('bower.json'); 
 
       // wire Bower packages to .html 
 
       wiredep({ 
 
        bowerJson: bowerJson, 
 
        directory: 'bower_components', 
 
        exclude: ['bootstrap-sass', 'bootstrap.js'], 
 
        ignorePath: /^(\.\.\/)*\.\./, 
 
        src: 'app/index.html', 
 
       }); 
 
       if (this.includeSass) { 
 
        // wire Bower packages to .scss 
 
        wiredep({ 
 
         bowerJson: bowerJson, 
 
         directory: 'bower_components', 
 
         ignorePath: /^(\.\.\/)+/, 
 
         src: 'app/styles/*.scss', 
 
        }); 
 
       } 
 
      }.bind(this) 
 
     ); 
 
    }, 
 
});

+0

我做出了這些更改。 ..這是同一個問題......文件不會複製到新目錄中 –

0

我刪除了var做=異步()和替換「this.prompt」被「退貨。提示「 大部分文件被複制...但仍有更多無效代碼::