2014-09-27 8 views
1

我寫了第一個grunt插件,但由於某種原因未找到任務。我不知道爲什麼......因爲名字與grunt.registerMultiTask是一對一的。我將任務的名稱更改爲apple,以便簡化調試。寫入第一個Grunt插件 - 即使grunt.registerMultiTask()名稱正確,也找不到任務

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

輸出:

[email protected] ~/simple-load-balancer-service/lbs $ grunt apple 
Warning: Task "apple" not found. Use --force to continue. 

Aborted due to warnings. 
[email protected] ~/simple-load-balancer-service/lbs $ 

棧和調試輸出:

[email protected] ~/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. 


[email protected] ~/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. 
[email protected] ~/simple-load-balancer-service/lbs $ 

[email protected] ~/simple-load-balancer-service/lbs $ grunt --version 
grunt-cli v0.1.13 
grunt v0.4.5 
[email protected] ~/simple-load-balancer-service/lbs $ 
+1

你的Gruntfile似乎沒有[加載](http://gruntjs.com/api/grunt#grunt.loadtasks)任務? – Whymarrh 2014-09-28 00:08:52

+0

這是正確的......我得到了一個'apple'找不到的'task'。只是要注意,這不是「目標」未找到。 – dman 2014-09-28 01:11:03

+1

您可以發佈運行'grunt'命令時得到的錯誤(即運行'grunt'的輸出)。 – Whymarrh 2014-09-28 10:10:10

回答

0

我修改我的任務文件中的node_modules目錄。但npm緩存目錄正在被讀取,並沒有反映所做的更改。

+1

這是一個解決方案,還是應該對問題進行編輯? – Whymarrh 2014-09-28 22:36:56

+0

好吧,它似乎是解決方案...雖然沒有多大作爲,因爲我不是100%相信任務直接從npm緩存運行。 – dman 2014-09-29 01:51:31

相關問題