2016-02-02 73 views

回答

0

您可以使用gulp-rename package併爲其提供一個自定義函數,用於在文件通過時對其進行重命名。使用path包一些實用工具,創建一個構造函數生成計數器的一個新的哈希表,瞧:

'use strict'; 

var gulp = require('gulp'), 
    rename = require('gulp-rename'), 
    path = require('path'); 

gulp.task('default', [], function() { 
    var renamer = new Renamer(); 

    gulp.src('img/**/*.*') 
     .pipe(rename(renamer.folderToImgName)) 
     .pipe(gulp.dest('.build/images')); 
}); 

function Renamer() { 
    var countersHashTable = {}; 

    function folderToImgName(fileInfo) { 
     var baseName = path.basename(fileInfo.dirname); 

     countersHashTable[baseName] = countersHashTable[baseName] || 1; 

     fileInfo.basename = baseName + countersHashTable[baseName].toLocaleString(); 

     // If you want a flat folder structure: 
     //fileInfo.dirname = path.dirname(fileInfo.dirname); 

     countersHashTable[baseName]++; 
    } 

    this.folderToImgName = folderToImgName; 
} 

this answer by @OverZaelous部分混音。

相關問題