Skip to content

Conversation

@huancz
Copy link

@huancz huancz commented Mar 20, 2019

I was trying to make use of gulp-newy, and it seems to have trouble on Windows. First, my reduced test gulpfile:

var gulp = require('gulp'), plumber = require('gulp-plumber'), newy = require('gulp-newy');

gulp.task('test', function () {
    var step = 0;

    return gulp.src([
            'c:\\windows\\system32\\xcopy.exe', 'c:\\windows\\system32\\cmd.exe', 'c:\\windows\\system32\\powercfg.exe', 'C:\\windows\\notepad.exe',
            '/bin/ls', '/bin/sh', '/bin/true'
        ], {allowEmpty: true})
        .pipe(plumber())
        .pipe(newy(function(projectRoot, srcName, absoluteSrcName) {
            console.log(projectRoot, srcName, absoluteSrcName);
            if (process.platform === 'linux') {
                switch (step++) {
                    case 0: // warning: missing directory /tmp/foo -> OK
                        return '/tmp/foo/bar/test.txt';
                    case 1: // warning: missing directory /tmp/foo -> OK
                        return '//tmp////foo/bar/test.txt';
                    case 2: // (silent)                            -> OK
                        return '/tmp/test.txt';
                }
            } else { // WINDOWS
                switch (step++) {
                    case 0: // warning: missing directory \c\TEMP -> not OK but impossible to fix
                        // this syntax is special for mingw environment and node's statSync will not recognize it as valid,
                        // but users would have to go out of their way to obtain it from projectRoot or absoluteSrcName.
                        return '/c/TEMP/test.txt';
                    case 1: // warning: missing directory \c: -> not OK but at least warning is warranted here, some dirs ARE missing
                        return 'c:\\TEMP\\\\foo\\bar\\test.txt';
                    case 2: // warning: missing directory \c:\TEMP\foo\bar -> same as previous, right output would be that "C:\TEMP\foo" is missing
                        return 'c:///TEMP//foo/bar/test.txt';
                    case 3: // warning: missing directory \c: -> this is *REALLY* not OK, every dir in path does exist
                        return 'C:\\TEMP\\test.txt';
                }
            }
        }));
});

All input parameters to the callback function are in the form similar to C:\TEMP\foo\bar, which your test for missing dir can't handle properly. Every missing output file will print warning about "missing directory: \C:", even if the directories are there. I found out about it when I wrote task to pipe about 60 files though imagemin - resulting output looks like it's end of the world, full of red.

I rewrote your test for missing dir from the other end. path.dirname just removes last component of the path, it doesn't care if it is dir or file - so I let it just works backwards from full path until if finds a dir that does exist. This seems to work on both systems.

Second commit just limits the warning to one per each missing directory, I think it makes more sense, but feel free to ignore it.

@dman777
Copy link
Owner

dman777 commented Apr 22, 2019

Hello, thank you for the contribution! I never intended for Windows support but since you wrote it in, that is great. Please allow me review it this weekend when I have some time off from work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants