【问题标题】:npmignore .sh files in all but one directorynpmignore .sh 文件在除一个目录之外的所有目录中
【发布时间】:2017-03-06 09:24:30
【问题描述】:

我想忽略项目中的所有 .sh 文件,除了位于特定目录中的文件,我们将目录称为 foo。

所以我们有:

project/
   bar.sh
   baz.sh
   foo/
     a.sh
     b.sh

我不想将 bar.shbaz.sh 发布到 NPM,但我确实想将 a.shb.sh 发布到 NPM。原来我在 foo 目录之外有许多 .sh 文件,一举忽略所有这些文件会更方便。

我可以在我的.npmignore 文件中添加什么来完成此操作?

【问题讨论】:

    标签: node.js npm gitignore npmignore


    【解决方案1】:

    .npmignore 的文档中指出:

    .npmignore 文件跟随same pattern rules.gitignore 文件:

    在这种情况下,您将忽略所有 .sh 文件(即 *.sh),然后使用撇号 ! 否定该模式并指定要包含的文件夹名称。例如:

    示例 1

    # ignore all .sh files
    *.sh
    
    # include .sh files in the folder foo
    !foo/*.sh
    

    使用此配置,(在下面显示的示例文件夹结构的上下文中),所有这些文件都将被忽略:a.shb.she.shf.sh p>

    包含/发布了以下文件:c.shd.sh

    注意:e.shf.sh 在使用此配置时也会被忽略,因为它们位于子文件夹中。


    示例 2

    还要在文件夹foo 的任何子文件夹中包含/发布.sh 文件,然后按如下方式配置您的.npmignore

    # ignore all .sh files
    *.sh
    
    # include .sh files in the folder foo and any inside its sub folders
    !foo/**/*.sh
    

    使用此配置,(在下面显示的示例文件夹结构的上下文中),只有文件:a.shb.sh 被忽略。所有其他 .sh 文件均已发布。


    示例文件夹结构

    project
    ├── a.sh
    ├── b.sh
    └── foo
        ├── baz
        │   ├── e.sh
        │   └── f.sh
        ├── c.sh
        └── d.sh
    

    【讨论】:

      最近更新 更多