简体   繁体   English

用于src的gulp搜索文件夹并保存到父文件夹

[英]gulp search folder for src and save to parent folder

I am trying to make a gulp task go through a given directory, search in all subdirectories (recursively inside them also going through all possible subdirectories) for folders with a specific name (src) and have the destination to the parent folder. 我试图让一个gulp任务通过一个给定的目录,搜索所有子目录(在它们内部递归也通过所有可能的子目录)搜索具有特定名称(src)的文件夹,并将目标指向父文件夹。

So every folder which has a folder inside named SRC will be the destination for the images inside that SRC folder. 因此,每个名为SRC的文件夹内的文件夹都将成为该SRC文件夹内图像的目标。 Files will be saved with the same name 文件将以相同名称保存

here is what I have so far: 这是我到目前为止:

 gulp.task('img', function() {

      gulp.src('../../../uploads/slider-images/**/src/*.jpg')

      .pipe(plumber(plumberErrorHandler))

      .pipe(imagemin({

       optimizationLevel: 7,

       progressive: true

    }))

    .pipe(gulp.dest(function (file) {
        var destPath = file.base.split("/").pop().join('/');
        return destPath;
    }));

});

It's not working tho, stays with 'compressing image' at 0, and I have to exit the process. 它不起作用,保持'压缩图像'为0,我必须退出该过程。

any help appreciated 任何帮助赞赏

EDIT 编辑

Example folder structure 示例文件夹结构

--gulpFolder

--testimages

    --headers

        --src

        --funky

             --src

Right now I got to this piece of code: 现在我得到了这段代码:

gulp.task('img', function () {
    let plumberErrorHandler = (err) => {
        console.log('Plumber Err', err);
    };
    var sourcePath = '../testimages/';
    var endPath;
    return gulp.src(sourcePath + '**/src/**/*.jpg')
        .pipe(plumber(plumberErrorHandler))

        .pipe(imagemin({
            optimizationLevel: 6,
            progressive: true
        }))
        .pipe(rename(function(file) {
            let dir = file.dirname.split('\\');
            dir.pop();
            file.dirname = dir.join('/');
            endPath = sourcePath + file.dirname + '/';
            gutil.log(file);
        }))
        .pipe(gulp.dest(function(file) {
            return endPath;
        }));
});

It logs the right properties for the files, but I don't know why It always re-creates a folder named 'headers' and saves the images there. 它记录了文件的正确属性,但我不知道为什么它总是重新创建一个名为“headers”的文件夹并将图像保存在那里。 In the example case, it creates a folder named headers inside the 'headers' folder and another one inside the 'funky' folder and saves the images inside them. 在示例中,它在'headers'文件夹中创建一个名为headers的文件夹,在'funky'文件夹中创建另一个文件夹,并将图像保存在其中。

I think it is just not setting the proper path (.pop() is behaving different that you expect), tried it with gulp-imagemin and setting destination. 我认为它不是设置正确的路径(.pop()表现出与您期望的不同),尝试使用gulp-imagemin和设置目标。 If I understood your use case correct: 如果我理解你的用例是正确的:

# before
../../images/one/src/img.jpg
../../images/two/src/img.jpg
# after run
../../images/two/img.jpg
../../images/one/src/img.jpg
../../images/two/img.jpg
../../images/one/src/img.jpg

For that case, this would be the implementation: 对于这种情况,这将是实施:

const path = require('path');
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');

gulp.task('img', function () {
    let plumberErrorHandler = (err) => {
        console.log('Plumber Err', err);
    };
    const sourcePath = '...//testimages';
    return gulp.src(path.join(sourcePath, '/**/src/**/*.jpg'))
        .pipe(plumber(plumberErrorHandler))

        .pipe(imagemin({
            optimizationLevel: 0,
            progressive: true
        }))
        .pipe(rename(function(file) {
            let dir = file.dirname.split('/');
            dir.pop(); // remark - pop removes the src in this case
            file.dirname = path.join(sourcePath, dir.join('/'));
        }))
        .pipe(gulp.dest(function(file) {
            return sourcePath;
        }));

});

I was using wrong PATH var. 我使用了错误的PATH var。

right task code: 正确的任务代码:

const path = require('path');

gulp.task('img', function () {
    let plumberErrorHandler = (err) => {
        console.log('Plumber Err', err);
    };
    var sourcePath = '../testimages/';
    var endPath;
    return gulp.src(sourcePath + '**/src/**/*.jpg') 
        .pipe(plumber(plumberErrorHandler))

        .pipe(imagemin({
            optimizationLevel: 6,
            progressive: true
        }))
        .pipe(rename(function(file) {
            file.dirname = path.dirname(file.dirname);
            file.basename = file.basename;
            return file;
        }))
        .pipe(gulp.dest(sourcePath))
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM