2016-08-04 67 views
1

.SVG刪除我有一個咕嘟咕嘟任務在src文件夾中創建巴紐的.SVG文件回退:咕嘟咕嘟:刪除生成的從DEST png格式時,從SRC

gulp.task('svg2png', ['svg'], function() { 
    return gulp.src(src + '/svg/**/*.svg') 
    .pipe(plugins.newer(dest + '/svg')) 
    .pipe(plugins.svg2png()) 
    .pipe(gulp.dest(dest + '/svg')) 
    .pipe(plugins.notify({ 
    message: 'svg2png task complete', onLast: true 
    })); 
}); 

(該svg任務是svgmin)

我想通過刪除生成的文件(在DEST文件夾中)來整理自己的身後,認爲延伸如下表任務:

gulp.task('watch', function() { 
    var watchSvg = gulp.watch('src/svg/**/*.svg', ['svg2png']); 

    watchSvg.on('change', function(event) { 
    if (event.type === 'deleted') { 
     var filePathFromSrc = path.relative(path.resolve('src'), event.path); 
     var destFilePath = path.resolve('dist', filePathFromSrc); 
     del.sync(destFilePath); 
    } 
    }); 
}); 

這個作品在刪除.SVG但我希望阿爾斯o喜歡做的是刪除匹配的.png文件,所以沒有孤兒。我盲目地信任Handling the Delete Event on Watch recipe所以我已經超出了我的咕嘟咕嘟/ JS知識水平伴隨着這種實驗。

在此先感謝。

回答

0

假設.png文件被生成到相同的文件夾精縮.svg文件,下面應該工作:

watchSvg.on('change', function(event) { 
    if (event.type === 'deleted') { 
    var filePathFromSrc = path.relative(path.resolve('src'), event.path); 
    var destFilePath = path.resolve('dist', filePathFromSrc); 
    del.sync(destFilePath); 
    var pngDestFilePath = destFilePath.replace(/.svg$/, '.png'); 
    del.sync(pngDestFilePath); 
    } 
});