2017-11-04 141 views
0

我的目录结构与此类似:咕嘟咕嘟部分删除目录结构

app/ 
    directory1/ 
    assets/ 
     images/ 
     js/ 
    directory2/ 
    assets/ 
     images/ 
dist/ 
    assets/ 
    images/ 
    js/ 

我尝试使用咕嘟咕嘟是“收集”,从目录1,2,资产...,并将它们放到了实现什么DIST /资产/所以我写了这个:

gulp.task('gather-assets', function() { 
    gulp.src('app/*/assets/**').pipe(gulp.dest('dist/assets/')); 
}); 

的问题是,运行此功能后,它会创建这样一个路径:

dist/assets/directory1/assets/images 

继从this question的建议,我试图用一口,重命名,但我的情况是不同的,如果我使用一饮而尽,重命名是这样的:

gulp.task('gather-assets', function() { 
    gulp.src('app/*/assets/**').pipe(rename({dirname: ''})).pipe(gulp.dest('dist/assets/')); 
}); 

它必将在*星号的地方删除不必要的路径,但它也会删除**路径。因此,来自images /和js /的文件将被复制到assets /没有子目录。我在这种情况下可以使用哪种解决方案?

回答

1

Gulp-flatten将为您工作。

var flatten = require('gulp-flatten'); 

gulp.task('gather-assets', function() { 

    return gulp.src('app/*/assets/**') 
    // .pipe(rename({ dirname: '' })) 

    // -2 will keep the last two parents : assets/images or assets/js 
    .pipe(flatten({ includeParents: -2 })) 

    .pipe(gulp.dest('dist')); 
}); 

如果你想使用一口,重命名:既咕嘟咕嘟,扁平化和吞掉,重命名只是在做每个文件的目录结构的字符串操作]

// .pipe(flatten({ includeParents: -2 })) 

.pipe(rename(function (file) { 

    let tempArray = file.dirname.split(path.sep); 

    // remove the first array item : directory1 or directory2 
    // rejoin the remaining array items into a directory string 

    let temp = tempArray.slice(1).join(path.sep); 

    file.dirname = temp; 
    })) 
+0

谢谢,马克! – Mike