写入第一个Grunt插件 - 即使grunt.registerMultiTask()名称正确,也找不到任务

时间:2014-09-27 23:57:10

标签: javascript gruntjs

我写了第一个grunt插件,但由于某种原因,没有找到任务。我不知道为什么......因为这些名字与grunt.registerMultiTask是一对一的。我将任务的名称更改为apple,以便为调试它保持简单。

来自node_modules/grunt-testem-config-maker/tasks/apple.js

任务文件

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('apple', 'Purpose: None concatenated file use in testem with automated testem.json configuration generation. Reads index.html, scrapes <script> tags, and inserts filenames in src_files.', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      indexHtml: '/home/one/simple-load-balancer-service/lbs/app/static/index.html',
      punctuation: '.',
      separator: ', ',
      configFile: 'testem.json',
      launch_in_dev: ["PhantomJS"],
      launch_in_ci: ["PhantomJS"],
      url: "http://10.1.2.11:7357",
      src_files_ignore: "spec-bower-vendor/angular-core/"
    });

    readFile(options.indexHtml, 'utf8').done(function (indexHtml) {
      var fileList = getFileNames(indexHtml);
      dumpToFile(fileList);
    }, function (err) {
      console.log("Doh! Couldn't read index.html to get files for testem!")
    });
  });
};

var fs = require('fs');
var Q = require('q');
var cheerio = require('cheerio');

var readFile = Q.nfbind(fs.readFile);
var writeFile = Q.nfbind(fs.writeFile)

function dumpToFile(fileList) {
  var destFile = "file-list.txt";
  var output = 'module.exports = ' + JSON.stringify(fileList,null,'\t');
  writeFile(destFile, output)
    .done(function(err) {
      if (err) { 
        console.log(err);
      } else {
        console.log("saved");
      }
    });
}

function getFileNames(indexHtml) {
  var bower_files = [];
  var js_files = [];
  var BOWER_ROOT = 'app/';
  var JS_ROOT = 'app/scripts/';
  var BOWER_DIR = "bower_components";

  var $ = cheerio.load(indexHtml);
  var scriptTags = $('html').find('script');
  scriptTags.map(function(element) {
    if (element) {
      element = scriptTags[element].attribs.src;
      if (element.split('/')[1] === BOWER_DIR) {
        element = element.replace("../", BOWER_ROOT);
        bower_files.push(element);
      } else {
        element = JS_ROOT + element;
        js_files.push(element);
      }
    }
  });
  return {
    bower_files: bower_files,
    js_files: js_files
  };
}

我的Gruntfile.js文件:

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt);
    grunt.initConfig({
      apple: {
        options: {
          indexHtml: "sss"
        }
      }
    });
    grunt.registerTask('default', 'start server', function() {
        //grunt.task.run('express');
        grunt.task.run('apple');
    });
}

输出:

one@localhost ~/simple-load-balancer-service/lbs $ grunt apple
Warning: Task "apple" not found. Use --force to continue.

Aborted due to warnings.
one@localhost ~/simple-load-balancer-service/lbs $ 

堆栈和调试输出:

one@localhost ~/simple-load-balancer-service/lbs $ grunt --debug       
Running "default" task
[D] Task source: /home/one/simple-load-balancer-service/lbs/Gruntfile.js
Warning: Task "apple" not found. Use --force to continue.

Aborted due to warnings.


one@localhost ~/simple-load-balancer-service/lbs $ grunt --stack
Running "default" task
Warning: Task "apple" not found. Use --force to continue.
Error: Task "apple" not found.
    at Task.run (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/util/task.js:179:28)
    at Object.<anonymous> (/home/one/simple-load-balancer-service/lbs/Gruntfile.js:11:20)
    at Object.thisTask.fn (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/grunt/task.js:82:16)
    at Object.<anonymous> (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/util/task.js:301:30)
    at Task.runTaskFn (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/util/task.js:251:24)
    at Task.<anonymous> (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/util/task.js:300:12)
    at Task.start (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/util/task.js:309:5)
    at Object.grunt.tasks (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/grunt.js:164:8)
    at Object.module.exports [as cli] (/home/one/simple-load-balancer-service/lbs/node_modules/grunt/lib/grunt/cli.js:38:9)
    at Object.<anonymous> (/usr/lib64/node_modules/grunt-cli/bin/grunt:45:20)

Aborted due to warnings.
one@localhost ~/simple-load-balancer-service/lbs $ 

one@localhost ~/simple-load-balancer-service/lbs $  grunt --version
grunt-cli v0.1.13
grunt v0.4.5
one@localhost ~/simple-load-balancer-service/lbs $ 

1 个答案:

答案 0 :(得分:0)

我在node_modules目录中修改了我的任务文件。但是正在读取npm缓存目录而它没有反映所做的更改。