grunt.file
这里提供了很多用于读写、遍历系统和通过模式匹配的。其中很多都是Node.js中的操作的封装,但是提供了额外的处理、日志记录和字符编码转换。
注意:所有的路径都是参照 Gruntfile 的相对路径,除非通过 grunt.file.setBase 或在命令行中指定 --base 参数改变当前工作目录。
字符编码
设置此可以改变所有 grunt.file 的认编码。认是 'utf8'。如果必须改变这个值,建议你在Gruntfile中尽可能早改变。
grunt.file.defaultEncoding = 'utf8';
于 0.4.2 版本
是否在 file.read 时保留字节顺序(BOM)。
grunt.file.preserveBOM = false;
读写
读取并返回的。返回值为字符串,如果 options.encoding 为 null ,则返回 。
grunt.file.read(filepath [, options])
options 对象可以设置以下:
var options = { // If an encoding is not specified, default to grunt.file.defaultEncoding. // If specified as null, returns a non-decoded Buffer instead of a string. encoding: encodingName };
读取的,将其按照JSON格式解析,返回结果。参见 grunt.file.read 其所的参数列表。
grunt.file.readJSON(filepath [, options])
读取的,将其按照YAML格式解析,返回结果。参见 grunt.file.read 其所的参数列表。
grunt.file.readYAML(filepath [, options])
将指定的写入中,如果需要,将路径中所有不存在的目录。字符串将按照指定的字符编码进行编码, 将会按照指定的方式写入磁盘。
如果指定了 --no-write 命令行参数,将不会真正写入。
grunt.file.write(filepath, contents [, options])
options 对象可设置以下:
var options = { // If an encoding is not specified, default to grunt.file.defaultEncoding. // If `contents` is a Buffer, encoding is ignored. encoding: encodingName };
将原到指定路径,如果需要,将路径中所有不存在的目录
如果指定了 --no-write 命令行参数,将不会真正写入。
grunt.file.copy(srcpath, destpath [, options])
options 对象可设置以下:
var options = { // If an encoding is not specified, default to grunt.file.defaultEncoding. // If null, the `process` function will receive a Buffer instead of String. encoding: encodingName, // The source file contents, source file path, and destination file path // are passed into this function, whose return value will be used as the // destination file's contents. If this function returns `false`, the file // copy will be aborted. process: processFunction, // These optional globbing patterns will be matched against the filepath // (not the filename) using grunt.file.isMatch. If any specified globbing // pattern matches, the file won't be processed via the `process` function. // If `true` is specified, processing will be prevented. noProcess: globbingPatterns };
指定的。和目录会被依次递归。
Will not delete the current working directory or files outside the current working directory unless the--force command-line option is specified.
如果指定了 --no-write 命令行参数,那么,路径将不会真的被。
grunt.file.delete(filepath [, options])
options 对象只可以设置以下:
var options = { // Enable deleting outside the current working directory. This option may // be overridden by the --force command-line option. force: true };
目录操作
工作方式类似 mkdir -p。创建目录和所有的中间目录。如果没有指定 mode ,认是0777 & (~process.umask()).
如果没有 --no-write 命令行参数,目录不会被真正创建。
grunt.file.mkdir(dirpath [, mode])
递归遍历整个目录,对每个都执行 callback 。
grunt.file.recurse(rootdir, callback)
callback 接收以下参数:
function callback(abspath, rootdir, subdir, filename) { // The full path to the current file, which is nothing more than // the rootdir + subdir + filename arguments, joined. abspath // The root director, as originally specified. rootdir // The current file's directory, relative to rootdir. subdir // The filename of the current file, without any directory parts. filename }
模式匹配
有时单独指定所有原始路径是不现实的,因此,Grunt通过内置的 库名 expansion (或者叫做 globbing) 。
参见 配置任务 指南中的 "Globbing patterns" 章节以 globbing pattern 实例。
返回包含匹配给定符模式的或者的特殊数组。这个接收逗号分割的匹配模式或者匹配模式数组。如果路径匹配模式以!开头,它会从返回的数组排除所匹配的项。模式是按指定的顺序进行处理的, 因此包含和排除的顺序是很重要的。
grunt.file.expand([options, ] patterns)
路径都是参照 Gruntfile 的相对路径,除非通过 grunt.file.setBase 或 --base 命令行参数了当前工作目录。
options 对象所有 的参数,也额外的一些,如下:
filter E接受有效的 或者已经通过了src路径匹配的,这个会返回true或false。
nonull 会保留src匹配模式,即使匹配失败。结合Grunt的-verbose标志,这个选项有助于路径问题的调试。
matchBase 不带斜线的模式只会匹配基本的部分。例如,这会使*.js就像**/*.js一样。
cwd 会让模式相对于当前路径进行模式匹配,所有返回的路径也是相对于当前路径的。
返回src-dest映射对象的数组。通过所指定的模式来匹配每源,然后将匹配的路径加入指定的dest中(dest存放匹配的路径)。这个路径会按照指定的选项加工或者过。 查看grunt.file.expand文档可以了解如何指定patterns和options
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); } };
针对或者多个路径来匹配或者多个匹配模式。返回特殊的数组,这个数组包含与指定的符模式任意匹配的所有路径。patterns和filepaths参数可以是单一的字符串或者也可以是字符串数组.如果匹配模式以!开头,就会从返回的数组从排除模式匹配的路径。模式会按指定的顺序进行处理,因此包含和排除的顺序是重要的。
grunt.file.match([options, ] patterns, filepaths)
options对象也提供的所有选项。例如:如果options.matchBase为true,即使模式中不带斜线,这个模式也会匹配包含斜线的基本。例如:*.js模式将匹配path/to/file.js路径。
这个与grunt.file.match包含同样的签名和逻辑,但是如果它匹配任意,就会简单的返回ture,否则返回false。
判断类型
检测给定的路径是否存在,返回boolean类型的值。
和Node.js 中的 一样,此将所有参数连接在一起,并对结果做规范化。
grunt.file.exists(path1 [, path2 [, ...]])
给定的路径是否是符号,返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isLink(path1 [, path2 [, ...]])
如果路径不存在则返回false。
指定的路径是否是目录?返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isDir(path1 [, path2 [, ...]])
如果路径不存在它也会返回false。
指定的路径是否是? 返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isFile(path1 [, path2 [, ...]])
如果路径不存在将返回false。
路径
指定的路径是否是? 返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isPathAbsolute(path1 [, path2 [, ...]])
所有给出的路径是否都是同路径?返回boolean类型的值。
grunt.file.arePathsEquivalent(path1 [, path2 [, ...]])
所有descendant路径是否全部包含在指定的ancestor路径中?返回boolean类型的值。
注意:不需要检查路径是否真的存在。
grunt.file.doesPathContain(ancestorPath, descendantPath1 [, descendantPath2 [, ...]])
指定的路径是否是CWD?返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isPathCwd(path1 [, path2 [, ...]])
指定的路径是否在在CWD中?注意:CWD不在CWD中 。返回boolean类型的值。
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
grunt.file.isPathInCwd(path1 [, path2 [, ...]])
改变Grunt的当前工作目录(CWD)。认情况下,所有路径都是参照 Gruntfile 的相对路径。此和 --base 命令行参数的工作方式一致。
grunt.file.setBase(path1 [, path2 [, ...]])
和 Node.js 中的 一样,此此将所有参数连接在一起,并对结果做规范化。
外部工具库
不建议使用
下面列出的所有外部工具库已经不再建议使用了。
请使用 npm 管理项目中对第三方工具库的依赖。
例如,如果你需要使用 ,首先通过 npm install lodash 安装,然后在 Gruntfile 中使用即可: var _ = require('lodash');
不建议使用
- 全局处理实用程序。
不建议使用
- 模式匹配实用程序。
不建议使用
- 向上匹配的模式。