grunt.file


本文整理自网络,侵删。

grunt.file

这里提供了很多用于读写文件、遍历文件系统和通过模式匹配查找文件的方法。其中很多方法都是Node.js中的文件操作函数的封装,但是提供了额外的错误处理、日志记录和字符编码转换。

注意:所有的文件路径都是参照 Gruntfile 文件的相对路径,除非通过 grunt.file.setBase 函数或在命令行中指定 --base 参数改变当前工作目录。

字符编码

grunt.file.defaultEncoding

设置此属性可以改变所有 grunt.file 方法的默认编码。默认是 'utf8'。如果必须改变这个值,建议你在Gruntfile文件中尽可能早改变。

grunt.file.defaultEncoding = 'utf8';

grunt.file.preserveBOM

添加于 0.4.2 版本

是否在 file.read 时保留字节顺序标记(BOM)。

grunt.file.preserveBOM = false;

读写文件

grunt.file.read

读取并返回文件的内容。返回值为一个字符串,如果 options.encoding 为 null ,则返回一个 Buffer。

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
};

grunt.file.readJSON

读取一个文件的内容,将其按照JSON格式解析,返回结果。参见 grunt.file.read 获取其所支持的参数列表。

grunt.file.readJSON(filepath [, options])

grunt.file.readYAML

读取一个文件的内容,将其按照YAML格式解析,返回结果。参见 grunt.file.read 获取其所支持的参数列表。

grunt.file.readYAML(filepath [, options])

grunt.file.write

将指定的内容写入文件中,如果需要,将创建文件路径中所有不存在的目录。字符串将按照指定的字符编码进行编码,Buffers 将会按照指定的方式写入磁盘。

如果指定了 --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
};

grunt.file.copy

将原文件拷贝到指定路径,如果需要,将创建文件路径中所有不存在的目录

如果指定了 --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
};

grunt.file.delete

删除指定的文件。文件和目录会被依次递归删除。

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
};

目录操作

grunt.file.mkdir

工作方式类似 mkdir -p。创建一个目录和所有的中间目录。如果没有指定 mode ,默认是0777 & (~process.umask()).

如果没有 --no-write 命令行参数,目录不会被真正创建。

grunt.file.mkdir(dirpath [, mode])

grunt.file.recurse

递归遍历整个目录,对每个文件都执行 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通过内置的node-glob 库支持文件名 expansion (或者叫做 globbing) 。

参见 配置任务 指南中的 "Globbing patterns" 章节以获取 globbing pattern 实例。

grunt.file.expand

返回包含匹配给定通配符模式的文件或者目录路径的特殊数组。这个方法接收一个逗号分割的匹配模式或者一个匹配模式数组。如果路径匹配模式以!开头,它会从返回的数组排除所匹配的项。模式是按指定的顺序进行处理的, 因此包含和排除文件的顺序是很重要的。

grunt.file.expand([options, ] patterns)

文件路径都是参照 Gruntfile 文件的相对路径,除非通过 grunt.file.setBase 或 --base 命令行参数修改了当前工作目录。

阅读剩余部分

相关阅读 >>

Grunt 配置任务

Grunt.option

Grunt 创建插件

Grunt_Grunt 教程

Grunt 项目脚手架

Grunt.config

Grunt 快速入门

Grunt 深入任务内幕

Grunt 退出码

Grunt.task

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




打赏

取消

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

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

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

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

评论

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