简体   繁体   English

使用grunt加载grunt-configs和bower.install()

[英]load-grunt-configs and bower.install() using grunt

I am trying to devide my existing Gruntfile into more smaller ones to make having an overview easier. 我正在尝试将现有的Gruntfile划分为更小的文件,以使概述更容易。 However I ran into a bump. 但是我碰到了颠簸。 I am using bower.install to install my project dependencies. 我正在使用bower.install安装我的项目依赖项。 My folder structure looks something like this: 我的文件夹结构如下所示:

package.json
bower.json
config
--install.js
--default.js //Empty for now
gruntfile.js

My Gruntfile right now looks like this: 我的Gruntfile现在看起来像这样:

'use strict';
module.exports = function(grunt) {
  var npmDependencies = require('./package.json').devDependencies;
  require('load-grunt-tasks')(grunt);

  var configs = {
    config : {
      paths : {
        "build" : ["grunts/*.js*", "package.json", "bower.json", "Gruntfile.js"],
      }
    }
  };

  var loadConfigs = require( 'load-grunt-configs' );
  configs = loadConfigs( grunt, configs );
  // Project configuration.
  grunt.initConfig( configs );
}

My install.js looks like so: 我的install.js看起来像这样:

module.exports = function(grunt) {
  var done = grunt.async();
  var bower = require('bower').commands;
  bower.install().on('end', function(data) {
    done();
  }).on('data', function(data) {
    console.log(data);
  }).on('error', function(err) {
    console.error(err);
    done();
  });
}

However this does not work as I am receiving the error: 但是,这不起作用,因为我收到错误消息:

>> TypeError: Object #<Object> has no method 'async'

If I remove all that async-clutter (which I ultimately want to keep), so that the file looks a lot simpler: 如果删除所有的async-clutter(最终希望保留),那么文件看起来会简单很多:

module.exports = function(grunt) {
    var bower = require('bower').commands;
    bower.install();
}

I still receive the following error: 我仍然收到以下错误:

>> TypeError: Cannot call method 'hasOwnProperty' of undefined

I am fairly new to this topic and so far (having everything in one file) it worked fine. 我对这个主题还很陌生,到目前为止(将所有内容都保存在一个文件中)效果很好。 Big question now: How do I get back to that point ;) 现在的大问题:如何回到这一点;)

There are a couple of issues with install.js : install.js有几个问题:

1) The grunt object does not expose the async function, this is the reason for the first error. 1) grunt对象没有公开异步功能,这是第一个错误的原因。 The async function is available for grunt tasks so it should be called from within a task. 异步功能可用于艰苦的任务,因此应从任务中调用它。
2) When using node modules for configuration , load-grunt-configs expects a function which returns an object, for example: 2)在使用节点模块进行配置时load-grunt-configs需要一个返回对象的函数,例如:

//config/jshint.js
module.exports = function(grunt, options){
     return {
         gruntfile       : {
             src : "Gruntfile.js"
         }
     }
 } 

In your case the function return nothing and load-grunt-configs fails with the Cannot call method 'hasOwnProperty' of undefined error. 在您的情况下,该函数不返回任何内容,并且load-grunt-configs失败, Cannot call method 'hasOwnProperty' of undefined错误的Cannot call method 'hasOwnProperty' of undefined

The following should work: 以下应该工作:

module.exports = function(grunt) {
  return grunt.registerTask("bower", function() {
      var done = this.async();
      var bower = require('bower').commands;
      bower.install().on('end', function(data) {
        done();
      }).on('data', function(data) {
        console.log(data);
      }).on('error', function(err) {
        console.error(err);
        done();
      });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM