[英]Grunt/Batch : how to execute shell commands within the gruntfile.js directory?
我知道這是一個常見問題,但我嘗試過的所有答案均無效。 奇怪的是,有位朋友在Windows上嘗試了此腳本並實際上獲得了當前目錄(包含gruntfile.js的目錄)。 我嘗試查看差異,但是也沒有找到任何差異。
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
shell: {
test: {
command: 'dir'
}
}
});
grunt.loadNpmTasks('grunt-shell');
};
這是我得到的:
D:\Websites\AUB>grunt shell:test
Running "shell:test" (shell) task
Volume in drive D is Data
Volume Serial Number is 6E7B-40AB
Directory of D:\Websites
04/22/2015 04:53 PM <DIR> .
04/22/2015 04:53 PM <DIR> ..
04/20/2015 11:04 AM <DIR> .sublime
03/30/2015 12:52 PM <DIR> .template
04/24/2015 07:55 AM <DIR> AUB
我試圖使用這篇文章中的技巧,但是它沒有用(我留在Websites目錄中):
command: 'set WORKING_DIRECTORY=%cd% && dir'
我還嘗試通過“獲取包含當前執行的批處理腳本的目錄”找到有用的東西,但是由於出現錯誤,所以我不知道如何使用它:
command: 'cd %~dp0 && dir'
返回:
D:\Websites\AUB>grunt shell:test
Running "shell:test" (shell) task
The system cannot find the path specified.
Warning: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "cd %~dp0 && dir"
The system cannot find the path specified.
Use --force to continue.
Aborted due to warnings.
附帶說明一下,我直接從Grunt使用的任何其他軟件包(清理,復制,重命名,uglify等)都可以正常工作,並且grunt-exec具有相同的行為。
這聽起來很奇怪,因為通常所有插件都相對於您的Gruntfile起作用:
http://gruntjs.com/api/grunt.file
注意:除非使用grunt.file.setBase或--base命令行選項更改了當前工作目錄,否則所有文件路徑都是相對於Gruntfile的。
您可以手動檢索當前工作:
var cwd process.cwd()
// Or to get a file inside the cwd
var pathOfGruntfile = require('path').resolve('./Gruntfile.js');
並在您的Gruntfile中使用它:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
cwd: process.cwd(),
shell: {
test: {
command: 'cd "<%= cwd %>";dir'
}
}
});
grunt.loadNpmTasks('grunt-shell');
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.