gulpjs / gulp

A toolkit to automate & enhance your workflow
https://gulpjs.com
MIT License
32.99k stars 4.22k forks source link

gulp.src and gulp.watch glob patterns are not working the same. #2687

Closed ve3 closed 2 years ago

ve3 commented 2 years ago

Before you open this issue, please complete the following tasks:

What were you expecting to happen?

The glob pattern use in gulp.watch should work the same way with gulp.src.

What actually happened?

The negative character (!pattern) should filtered out the path. They did in gulp.src but did not in gulp.watch even they both have the same options.

For example: I filtered out tests folder.

Folder structure for D:/wwwroot/_test/gulp-test

Please give us a sample of your gulpfile

'use strict';

const {series, parallel, src, dest, watch} = require('gulp');
const print = require('gulp-print').default;

const patterns = [
    './**',
    '!./node_modules',
    '!./node_modules/**',
    '!./tests',
    '!./tests/**',
];
const srcPath = 'D:/wwwroot/_test/gulp-test';
const destPath = 'D:/wwwroot/_test/gulp-dest';

function doCopy(cb) {
    return src(patterns, {
        allowEmpty: true,
        base: srcPath,
        cwd: srcPath
    })
    .pipe(print())
    .pipe(dest(destPath))
}

function doWatch(cb) {
    const watcher = watch(patterns, {
        base: srcPath,
        cwd: srcPath,
        events: 'all'
    }/*, series(
        // can't `copyPath` here because some functional required.
    )*/);
    watcher.on('all', (event, filepath) => {
        copyPath(event, filepath);
    });
    cb();
}

async function copyPath(event, filepath) {
    let command;
    if (event.toLowerCase().indexOf('unlink') !== -1) {
        // if matched unlink (file), unlinkDir (folder)
        command = 'delete';
    } else {
        // if matched add, addDir, change
        command = null;
    }

    if (command === 'delete') {
        const del = require('del');
        await del(filepath, {cwd: destPath});
    } else {
        return src(filepath, {
            allowEmpty: true,
            base: srcPath,
            cwd: srcPath
        })
        .pipe(print())
        .pipe(dest(destPath, {cwd: destPath}));
    }
}

// exports ==================================
exports.default = series(
    doCopy
);

exports.watch = series(doWatch);

Terminal output / screenshots

$ gulp watch

Please provide the following information:

Additional information

Use dot prefix or without dot prefix (!./tests, !tests) as described here don't change anything.

This seems to be resolved in https://github.com/gulpjs/gulp/issues/2340 but it doesn't. Because from my paths.json file, the source path is using / as directory separator but still not working.

Packages required for testing: gulp, del, gulp-print.

phated commented 2 years ago

This example is too complex. Please provide a simplified test case that we can check. Ideally this will be a repository that can be cloned.

ve3 commented 2 years ago

This example is too complex. Please provide a simplified test case that we can check. Ideally this will be a repository that can be cloned.

I've updated the content.

phated commented 2 years ago

Can you please update the example to be idiomatic gulp code? Your copyPath function is async but also tries to return a stream, which is entirely invalid. Additionally, the function is being called inside an event handler instead of the gulp task system. We don't support this type of non-idiomatic code. If you need help writing idiomatic gulp, I can move this to Discussions where a community member might be able to help out.

ve3 commented 2 years ago

It is async because I use with await del(). The problem is patterns in src() work diffently with watch() that's. The problem is not in copyPath(). Even if you use more simple like series() after watch() it is still not work the same with src() that is the main problem.

I think this is not discussion type, it is the problem that your src() and watch() work differently with the same patterns. If you don't think that this is the problem it's okay to close the issue.