grunt.file


当前第2页 返回上一页

options 对象支持所有 minimatch library 的参数,也支持额外的一些,如下:

  • filter E接受一个有效的 fs.Stats 方法名 或者一个已经通过了src文件路径匹配的函数,这个函数会返回truefalse
  • nonull 会保留src匹配模式,即使文件匹配失败。结合Grunt的-verbose标志,这个选项有助于文件路径问题的调试。
  • matchBase 不带斜线的模式只会匹配基本的名称部分。例如,这会使*.js就像**/*.js一样。
  • cwd 会让模式相对于当前路径进行模式匹配,所有返回的文件路径也是相对于当前路径的。

grunt.file.expandMapping

返回一个src-dest文件映射对象的数组。通过所指定的模式来匹配每一个源文件,然后将匹配的文件路径加入指定的dest中(dest存放匹配的文件路径)。这个文件路径会按照指定的选项加工或者重命名过。 查看grunt.file.expand方法文档可以了解如何指定patternsoptions

grunt.file.expandMapping(patterns, dest [, options])

注意:这个方法可以用于以编程的方式针对多任务的情况生成一个files数组,它会优先使用在配置任务指南中"动态构建文件对象"一节所描述的语法。

除了支持那些grunt.file.expand方法之外,options对象还支持下面这些属性:

var options = {
  // The directory from which patterns are matched. Any string specified as
  // cwd is effectively stripped from the beginning of all matched paths.
  cwd: String,
  // Remove the path component from all matched src files. The src file path
  // is still joined to the specified dest.
  flatten: Boolean,
  // Remove anything after (and including) either the first or last "." in the 
  // destination path (indicated by options.extDot), then append this value.
  ext: String,
  // *Added in 0.4.3*
  // Indicates where the period demarcating the extension is located. Can take:
  // - 'first' (extension begins after the first period in the file name) 
  // - 'last' (extension begins after the last period)
  // Default: 'first'
  extDot: String,
  // If specified, this function will be responsible for returning the final
  // dest filepath. By default, it joins dest and matchedSrcPath like so:
  rename: function(dest, matchedSrcPath, options) {
    return path.join(dest, matchedSrcPath);
  }
};

grunt.file.match

针对一个或者多个文件路径来匹配一个或者多个匹配模式。返回一个特殊的数组,这个数组包含与指定的通配符模式任意匹配的所有文件路径。patternsfilepaths参数可以是一个单一的字符串或者也可以是一个字符串数组.如果匹配模式以!开头,就会从返回的数组从排除模式匹配的路径。模式会按指定的顺序进行处理,因此包含和排除文件的顺序是重要的。

grunt.file.match([options, ] patterns, filepaths)

options对象也支持minimatch库提供的所有选项。例如:如果options.matchBase为true,即使模式中不带斜线,这个模式也会匹配包含斜线的基本名称。例如:*.js模式将匹配path/to/file.js文件路径。

grunt.file.isMatch

这个方法与grunt.file.match方法包含同样的签名和逻辑,但是如果它匹配任意文件,就会简单的返回ture,否则返回false

判断文件类型

grunt.file.exists

检测给定的路径是否存在,返回boolean类型的值。

和Node.js 中的 path.join 方法一样,此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.exists(path1 [, path2 [, ...]])

grunt.file.isLink

给定的路径是否是符号链接,返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isLink(path1 [, path2 [, ...]])

如果路径不存在则返回false。

grunt.file.isDir

指定的路径是否是一个目录?返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isDir(path1 [, path2 [, ...]])

如果路径不存在它也会返回false。

grunt.file.isFile

指定的路径是否是一个文件? 返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isFile(path1 [, path2 [, ...]])

如果路径不存在将返回false。

路径

grunt.file.isPathAbsolute

指定的文件路径是否是绝对路径? 返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isPathAbsolute(path1 [, path2 [, ...]])

grunt.file.arePathsEquivalent

所有给出的路径是否都是同一个路径?返回boolean类型的值。

grunt.file.arePathsEquivalent(path1 [, path2 [, ...]])

grunt.file.doesPathContain

所有descendant路径是否全部包含在指定的ancestor路径中?返回boolean类型的值。

注意:不需要检查路径是否真的存在。

grunt.file.doesPathContain(ancestorPath, descendantPath1 [, descendantPath2 [, ...]])

grunt.file.isPathCwd

指定的文件路径是否是CWD?返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isPathCwd(path1 [, path2 [, ...]])

grunt.file.isPathInCwd

指定的文件路径是否在在CWD中?注意:CWD不在CWD 。返回boolean类型的值。

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

grunt.file.isPathInCwd(path1 [, path2 [, ...]])

grunt.file.setBase

改变Grunt的当前工作目录(CWD)。默认情况下,所有文件路径都是参照 Gruntfile 文件的相对路径。此函数和 --base 命令行参数的工作方式一致。

grunt.file.setBase(path1 [, path2 [, ...]])

和 Node.js 中的 path.join 方法一样,此方法此方法将所有参数连接在一起,并对结果做规范化。

外部工具库

不建议使用

下面列出的所有外部工具库已经不再建议使用了。

请使用 npm 管理项目中对第三方工具库的依赖。

例如,如果你需要使用 Lo-Dash,首先通过 npm install lodash 安装,然后在 Gruntfile 文件中使用即可: var _ = require('lodash');

grunt.file.glob

不建议使用

glob - File globbing utility.

grunt.file.minimatch

不建议使用

minimatch - File pattern matching utility.

grunt.file.findup

不建议使用

findup-sync - Search upwards for matching file patterns.


标签:Grunt

返回前面的内容

相关阅读 >>

Grunt.file

Grunt 概述

Grunt.config

Grunt.event

Grunt 常见问题

Grunt 快速入门

Grunt.util

Grunt 配置任务

Grunt 深入任务内幕

Grunt.template

更多相关阅读请进入《Grunt》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...