简体   繁体   English

Grunt文件对象格式:紧靠顶级目录下的所有常规文件文件

[英]Grunt Files Object Format: All regular files files immediately under top level directory

I can't figure out for the life of me how to get this work. 我无法弄清楚我的生活如何得到这项工作。 The grunt configuration for a module I'm using ( grunt-sloc ) requires the Files Object Format . 我正在使用的模块( grunt-sloc )的grunt配置需要Files Object Format Using this, I want to be able to match all regular files (ie, non-directories) immediately under the top level directory. 使用这个,我希望能够匹配顶级目录下的所有常规文件(即非目录)。

For example, say this is my directory structure, where + signifies a directory and - signifies a regular file: 例如,假设这是我的目录结构,其中+表示目录, -表示常规文件:

repo
  + dir
    - unwanted.js
  - server.js

How do I use Grunt's Files Object Format to match server.js but not dir or dir/unwanted.js ? 如何使用Grunt的文件对象格式来匹配server.js而不是dirdir/unwanted.js

This is how it would look in the Compact Fomat : 这就是Compact Fomat的外观

mine: {
  src: [
    '*'
  ]
}

Or, in bash, you would get them like this: 或者,在bash中,你会得到这样的:

ls -p | grep -v /;

Here's what I've tried with the Files Object Format : 这是我尝试使用Files对象格式的内容

  • This does not work: 这不起作用:

     mine: { files: { '.': ['*'] } } 
  • Neither does this: 这也不是:

     mine: { files: { './': ['*'] } } 
  • Not even this: 甚至不是这样的:

     mine: { files: { './': ['*/server.js'] } } 
  • Nor this: 这不是:

     mine: { files: { './': ['server.js'] } } 
  • This works, but it runs recursively, which I do not want: 这是有效的,但它以递归方式运行,我不想要:

     mine: { files: { './': ['**/server.js'] } } 

After doing a bunch of testing and code reading, I've verified that it is in fact the minimatch package (a depencency of Grunt), which is not returning the matches I'm looking for. 在做了一堆测试和代码阅读之后,我已经确认它实际上是minimatch包(Grunt的依赖 ),它没有返回我正在寻找的匹配项。 So it is not the grunt module I am using; 所以它不是我使用的咕噜模块; it's my grunt configuration. 这是我的笨拙配置。

Since Grunt is so popular and this seems like a very common use case, I'm guessing that there's a way to do this. 由于Grunt如此受欢迎,这似乎是一个非常常见的用例,我猜测有一种方法可以做到这一点。 Does anyone know what that is? 有谁知道那是什么?

Update 更新

As RobC pointed out, my last example does NOT work. 作为RobC指出的那样,我的最后一个例子行不通的。 It was picking up server.js 's in my node_modules directory, which made me think it was working. 它正在我的node_modules目录中获取server.js ,这让我觉得它正在运行。

Firstly FWIW, your examples listed using the Files Object Format method also did not work for me, including your last one: 首先使用文件对象格式方法列出的示例FWIW对我来说也不起作用,包括你的最后一个:

 // Although this worked for you, this failed for me...
 mine: {
     files: {
         './': ['**/server.js']
     }
 }

The only way I could get this to work was to match everything and then negate the top level directories using Grunt globbing patterns. 我能让它工作的唯一方法是匹配所有内容,然后使用Grunt globbing模式否定顶级目录。

Whilst the following gists demonstrates what worked for me, it does require knowing/configuring the names of the top-level directories you wish to exclude: 虽然以下要点演示了对我有用的内容,但它确实需要知道/配置您要排除的顶级目录的名称:

Directory Structure 目录结构

Given a directory set up as follows: 给定目录设置如下:

repo
│
├─── dir
│   │
│   └─── unwanted.js
│
├─── foo.js
│
├─── Gruntfile.js
│
├─── node_modules
│   │
│   └─── ...
│
├─── package.json
│
└─── server.js

Gruntfile.js Gruntfile.js

...and a Gruntfile.js configured as follows: ...和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'
    ]);
};

Sloc results Sloc结果

Running grunt via the CLI correctly resulted in grunt-sloc reporting statistics for two files only, namely foo.js and server.js . 通过CLI正确运行grunt导致仅两个文件的grunt-sloc报告统计信息,即foo.jsserver.js

As expected, stats for package.json were omitted due to JSON being a non-supported language. 正如所料,由于JSON是不受支持的语言,因此省略了package.json统计信息。

Additional note: 附加说明:

I found the answer to this post fairly informative in explaining how patterns that begin with ! 我发现这篇文章的答案在解释如何开始的模式方面提供了相当丰富的信息! are excluded from the initial returned array. 从初始返回的数组中排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM