[英]Grunt Files Object Format: All regular files files immediately under top level directory
我無法弄清楚我的生活如何得到這項工作。 我正在使用的模塊( grunt-sloc )的grunt配置需要Files Object Format 。 使用這個,我希望能夠匹配頂級目錄下的所有常規文件(即非目錄)。
例如,假設這是我的目錄結構,其中+
表示目錄, -
表示常規文件:
repo
+ dir
- unwanted.js
- server.js
如何使用Grunt的文件對象格式來匹配server.js
而不是dir
或dir/unwanted.js
?
這就是Compact Fomat的外觀 :
mine: {
src: [
'*'
]
}
或者,在bash中,你會得到這樣的:
ls -p | grep -v /;
這是我嘗試使用Files對象格式的內容 :
這不起作用:
mine: { files: { '.': ['*'] } }
這也不是:
mine: { files: { './': ['*'] } }
甚至不是這樣的:
mine: { files: { './': ['*/server.js'] } }
這不是:
mine: { files: { './': ['server.js'] } }
這是有效的,但它以遞歸方式運行,我不想要:
mine: { files: { './': ['**/server.js'] } }
在做了一堆測試和代碼閱讀之后,我已經確認它實際上是minimatch包(Grunt的依賴 ),它沒有返回我正在尋找的匹配項。 所以它不是我使用的咕嚕模塊; 這是我的笨拙配置。
由於Grunt如此受歡迎,這似乎是一個非常常見的用例,我猜測有一種方法可以做到這一點。 有誰知道那是什么?
作為RobC
指出的那樣,我的最后一個例子是行不通的。 它正在我的node_modules
目錄中獲取server.js
,這讓我覺得它正在運行。
首先使用文件對象格式方法列出的示例FWIW對我來說也不起作用,包括你的最后一個:
// Although this worked for you, this failed for me...
mine: {
files: {
'./': ['**/server.js']
}
}
我能讓它工作的唯一方法是匹配所有內容,然后使用Grunt globbing模式否定頂級目錄。
雖然以下要點演示了對我有用的內容,但它確實需要知道/配置您要排除的頂級目錄的名稱:
給定目錄設置如下:
repo
│
├─── dir
│ │
│ └─── unwanted.js
│
├─── foo.js
│
├─── Gruntfile.js
│
├─── node_modules
│ │
│ └─── ...
│
├─── package.json
│
└─── server.js
...和Gruntfile.js
配置如下:
module.exports = function(grunt) {
grunt.initConfig({
sloc: {
mine: {
files: {
'.': [
'**', // Match everything and add to the results set.
// Explicitly state which directories under the top
// level directory to negate from the results set.
'!**/node_modules/**',
'!**/dir/**',
// Files can be negated from the results set too.
'!**/Gruntfile.js' //
]
}
}
}
});
grunt.loadNpmTasks('grunt-sloc');
grunt.registerTask('default', [
'sloc:mine'
]);
};
通過CLI正確運行grunt
導致僅兩個文件的grunt-sloc報告統計信息,即foo.js
和server.js
。
正如所料,由於JSON是不受支持的語言,因此省略了package.json
統計信息。
我發現這篇文章的答案在解釋如何開始的模式方面提供了相當豐富的信息!
從初始返回的數組中排除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.