2017-07-27 4 views
0

Je fais une application web en utilisant yeoman et je suis coincé dans la création d'un générateur. Le problème est qu'il ne copiera pas les fichiers dans le dossier de sortie.mon générateur yeoman ne va pas copier les fichiers

Voici mon code:

'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)); 
    } 
}); 

Je pense que le problème est dans la méthode d'écriture. Je voulais aussi demander où aller ensuite? Ou ai-je franchi une étape fondamentale vers l'apprentissage du développement Web

+0

Avez-vous des erreurs? Si oui, merci de les partager. –

+0

pas d'erreur ... quand je l'exécute avec yeoman ... la méthode rapide fonctionne: me demandant si je veux utiliser angulaire ou jquery ... après avoir rempli les options ... il se termine juste là –

Répondre

0

Si vous formatez votre code, vous verrez que la fonction writing ne fait rien. Il déclare un tas de sous-fonctions, mais ne gère personne.

Je pense que le problème est que vous voulez un objet, mais écrit à la place une fonction: writing() {} au lieu de writing: {}. J'ai fait une correction rapide du code, mais je ne l'ai pas testé. Donc, il pourrait y avoir autre problème syntaxe, mais il faut à peu près ressembler à ceci:

'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

j'ai fait ces changements. .. c'est le même problème ... les fichiers ne seront pas copiés dans le nouveau répertoire –

0

i enlevé le var = fait async() et remplacé "this.prompt" par « retourner ce. invite " la plupart des fichiers sont copiés ... mais il y a encore plus de code invalide ::