[英]Node module to execute tasks grunt : “Gruntfile.js” not find
我目前正在嘗試開發一個模塊,該模塊將允許節點從命令行運行Grunt任務。 此Node模塊已全局安裝:
C:\Users\pcharpin\AppData\Roaming\npm\node_modules\task-app
目的是使“ Grunt”命令的使用對用戶透明。 為了更好地解釋我的方法,下面是一個使用我的節點模塊的簡單示例:
C:\Users\pcharpin\Desktop\demo-app> task-app copy
在此示例中,我的模塊將源目錄復制到目標目錄。
不幸的是,當我運行任務Grunt時,我的節點模塊向我指示目錄“ demo-app”中沒有文件“ Gruntfile.js”。 但是,該文件應由我的Node模塊在其自己的目錄中找到。
我的樹節點模塊:
我的task-app.js文件,代碼如下:
#!/Usr/bin/env node
var grunt = require('grunt');
var args = process.argv.splice(2)
checkArguments(args);
checkArguments function(args) {
[...]
runCopy();
}
runCopy = function() {
var spawn = require('child_process') spawn.
var exec = spawn('cmd', ['/ c', 'grunt copy']);
exec.stdout.on("data", function (data) {
console.log('' + data);
});
exec.stderr.on('data', function (data) {
console.log('' + data);
});
}
然后在我的“ Gruntfile.js”文件中,有執行源代碼到目標目錄的復制的代碼:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy {
srcWeb {
files: [{
cwd: '<%= pkg.srcWeb%>', //root to copy
src: '**/*', // copy all files and subfolders
dest: '<%= pkg.name%>/www/', // destination folder
expand: true // When required using cwd
}]
}
}
});
我不明白為什么在“ Gruntfile.js”文件中找不到“節點”模塊。
是否需要在該項目目錄中具有文件“ Gruntfile.js”? 還有其他解決方案嗎?
我也想知道,文件“ Gruntfile.js”是否可以讀取項目目錄中的文件“ package.json”? 例如,這是允許用戶配置此文件以更改源路徑。
編輯:
經過一些研究,我正在接近解決我的問題的方法。 要更改默認情況下當前目錄“ Gruntfile.js”的執行,我們可以在命令行中使用--gruntfile選項。
如源代碼grunt(grunt / lib / grunt / tasks.js)中所示:
//Get any local Gruntfile or tasks that might exist. Use --gruntfile override
//if specified, otherwise search the current directory or any parent.
如何為任何Windows用戶指定“ Gruntfile.js”(在全局安裝的節點模塊中找到)的路徑?
這是一個例子:
C:\\ Users \\用戶名\\ projects \\ demo-app>任務-應用程序副本
在我的源代碼中,我使用--gruntfile選項執行如下grunt任務:
runCopy = function() {
var spawn = require('child_process').spawn;
var pathGruntfile = 'C:\Users\username\AppData\Roaming\npm\node_module\task-app';
var exec = spawn('cmd', ['/ c', 'grunt --gruntfile' + pathGruntfile + ' Gruntfile.js']);
exec.stdout.on("data", function (data) {
console.log('' + data);
});
exec.stderr.on('data', function (data) {
console.log('' + data);
});
}
編輯2:
在等待響應之前,我找到了一個臨時解決方案,用於在我的Node模塊中獲取“ Gruntfiles.js”。 但是缺點是要在當前目錄中本地安裝所有Node模塊依賴關系和我的Node Module。
var pathDirCurrent = process.cwd();
var pathGruntfile = pathDirCurrent.concat('\\node_modules\\task-app\\Gruntfile.js');
var spawn = require('child_process').spawn;
var exec = spawn('cmd', ['/c', 'grunt --gruntfile ' + pathGruntfile + ' create']);
exec.stdout.on("data", function(data) {
console.log('' + data);
});
你從哪跑來的咕unt聲?
您的項目結構和grunt文件看起來不錯。 也許嘗試並包括一個基本任務:
grunt.registerTask('default', ['copy']);
最后,我了解了使用Grunt任務的邏輯。 當您有一個需要使用grunt任務的Web項目應用程序時,它必須在當前目錄中具有Gruntfile.js。
但是,就像我在“ edit 2”中所說的那樣,我們可以使用--- gruntfile選項指定Gruntfile.js。 因此,這並不是不利條件,因為Grunt工具可以在存在Web項目應用程序的當前目錄中與Gruntfile.js一起使用。
對於每個任務grunt,我將使用--gruntfile選項指定Gruntfile.js的路徑作為示例,來自“ edit 2”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.