加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

grunt.file教程

发布时间:2023-05-15 14:02:31 所属栏目:教程 来源:
导读:这里提供了很多用于读写文件、遍历文件系统和通过模式匹配查找文件的方法。其中很多方法都是Node.js中的文件操作函数的封装,但是提供了额外的错误处理、日志记录和字符编码转换。

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

读写文件
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.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
删除指定的文件。文件和目录会被依次递归删除。

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) 。

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 会让模式相对于当前路径进行模式匹配,所有返回的文件路径也是相对于当前路径的。

grunt.file.expandMapping
返回一个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);
  }
};

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

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

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

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章