用Grunt更改文件名



我需要在现有名称和文件扩展名之间输入我的任意名称,我的代码:

copy: {
dev: {
files: [{
expand: true,
src: ['**','.*'],
cwd: 'copy-test/',
dest: 'copy-test2/',   
rename: function(dest, matchedSrcPath) {
if (matchedSrcPath.substring(0,1)) {
return dest  + '_test' + matchedSrcPath ;
}
}
}]
}
},

rename函数是用于此类需求的正确功能。但是,您需要使用rename函数中定义的逻辑配置copy任务,如下所示:


Gruntfile.js

module.exports = function(grunt){
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
copy:{
dev: {
files: [{
expand: true,
cwd: 'copy-test/',
src: ['**'],
dest: 'copy-test2/',
/*dot: true,*/
/*extDot: 'last',*/
rename: function(dest, src) {
// String to insert into copied filename.
var str = '_test';
// Ensure `dest` path has a trailing forward slash.
dest = //$/.test(dest) ? dest : dest + '/';
// If option `dot: true` is specified any hidden file(s)
// are copied as is, i.e. the filename is not changed.
if (this.dot && /^./.test(src.split('/').pop())) {
return dest + src;
}
// The `extDot` option indicates where the period demarcating
// the file extension is located. The default value is 'first'.
// - When its value is 'first' the `str` value is inserted before
// the FIRST instance of a period.
// - When its value is 'last' the `str` value is inserted before
// the LAST instance of a period.
return this.extDot === 'last'
? dest + src.replace(/(.(?:[^.]*?)$)/, str + '$1')
: dest + src.replace(/(..*?$)/, str + '$1');
}
}]
}
}
});
grunt.registerTask('default', ['copy']);
}

解释

以下内容解释了rename函数中发生的情况,以及执行它的原因:

  1. 第一部分;var str = '_test';,是您应该指定要插入到文件名中的字符串的位置。当前字符串;将插入_test,因此您需要根据需要更改此值。

  2. 读:;

    dest = //$/.test(dest) ? dest : dest + '/';
    

    确保CCD_ 7路径总是以正斜杠(/(结束。例如,如果dest属性的值设置为dest: 'copy-test2'(请注意,没有尾随的正斜杠(,则会附加一个正斜杠(/(。这对于确保dest路径和新的src路径级联时它们形成有效路径是必要的。

    用于处理此问题的代码使用正则表达式/$test()方法,以及条件(三元(运算符;将尾部正斜杠附加到dest值上,或者使dest值保持原样(如果它已经以正斜杠结束(。

  3. 读的部分;

    if (this.dot && /^./.test(src.split('/').pop())) {
    return dest + src;
    }
    

    检查CCD_ 18选项是否设置为CCD_。如果dot选项值为true,grunt也将按原样复制隐藏的文件和/或文件夹,即文件名/文件夹名不会更改。任何以句点(.(开头的文件名和/或文件夹名(如.gitignore(都被识别为隐藏

    注意:您可能不想复制隐藏的文件/文件夹,但如果您这样做,则当dot选项设置为true时,存在正确处理这些文件/文件夹的逻辑

  4. 最后一部分是:

    return this.extDot === 'last'
    ? dest + src.replace(/(.(?:[^.]*?)$)/, str + '$1')
    : dest + src.replace(/(..*?$)/, str + '$1');
    

    是用于返回新目的地路径的逻辑发生的地方。这里发生了很多事情,如下所述:

    • 首先,grunt有一个extDot选项,我们使用它来确定字符串(例如'_test'(应该插入的位置。extDot选项在grunt文档中描述为:

      extDot用于指示指示扩展的周期所在的位置。可以采用'first'(扩展名从文件名中的第一个句点开始(或'last'(扩展名在最后一个句点之后开始(,默认设置为'first'。。。

      如果将extDot选项设置为'last',则grunt将从如下所示的路径中识别文件扩展名:

      '/path/to/filename.js'       //-->  '.js'
      '/path/to/filename.min.css'  //-->  '.css'
      

      注意:在第二个示例路径中,没有将.min.css标识为文件扩展名,而是将.css标识为

      如果将extDot选项设置为'last',则在给定上面显示的示例路径的情况下,恢复副本复制的文件名将重命名为filename_test.jsfilename.min_test.css

      但是,如果未指定extDot,则其值默认为'first',并且grunt将从如下所示的路径中识别文件扩展名:

      '/path/to/filename.js'       //-->  '.js'
      '/path/to/filename.min.css'  //-->  '.min.css'
      

      注意:这次grunt将.min.css标识为第二个示例路径中的文件扩展名

      当使用默认的extDot选项('first'(时,在给定上述示例路径的情况下,重新复制的文件名将重命名为filename_test.jsfilename_test.min.css

    • 您会注意到,我们在replace()方法中使用正则表达式来确定在哪里插入字符串(例如'_test'(。每个正则表达式的进一步解释可以在以下链接中找到:

      • (.(?:[^.]*?)$)-用于在LAST周期之前插入字符串(.(
      • (..*?$)-用于在FIRST周期之前插入字符串(.(

注意没有文件扩展名的文件不会更改其名称。

最新更新