当前位置:K88软件开发文章中心编程工具Grunt → 文章内容

grunt.file

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-24 10:34:16

由 小路依依 创建, 最后一次修改 2016-10-05 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 命令行参数修改了当前工作目录。options 对象支持所有 minimatch library 的参数,也支持额外的一些,如下:filter E接受一个有效的 fs.Stats 方法名 或者一个已经通过了src文件路径匹配的函数,这个函数会返回true或false。nonull 会保留src匹配模式,即使文件匹配失败。结合Grunt的-verbose标志,这个选项有助于文件路径问题的调试。matchBase 不带斜线的模式只会匹配基本的名称部分。例如,这会使*.js就像**/*.js一样。cwd 会让模式相对于当前路径进行模式匹配,所有返回的

[1] [2]  下一页


grunt.file