我的自动生成器不会复制文件

时间:2017-07-27 14:47:32

标签: javascript node.js yeoman yeoman-generator

我正在使用自耕农制作一个网络应用程序而且我一直在创建一个生成器。问题是它不会将文件复制到输出文件夹。

这是我的代码:

'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开发的基本步骤

2 个答案:

答案 0 :(得分: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)
        );
    },
});




答案 1 :(得分:0)

我删除了var done = async()并将“this.prompt”替换为“return this.prompt” 大多数文件都被复制了......但是还有更多的无效代码::

相关问题