Search code examples
pathgulpradixcwd

Gulp loosing relative path for files from gulp.src(Array) to gulp.dest


I want to use gulp to copy and paste all HTML from a collection of src that are from a parent directory to a child directory, but keep their path

Project
+-- /Development
|   +- gulpfile.js
|
+-- /Source
    +- /ComponentA
    |  +- /DirA
    |     +- fileA.html
    |
    +- /ComponentB
    |  +- /DirB
    |     +- fileB.html
    |
    +- /ComponentC
       +- /DirC
          +- fileC.html

I need gulp to copy all HTML files to the relative paths into Development/public_html

Project
+-- /Development
    +- gulpfile.js
    |
    +- /public_html 
        +- /ComponentA
        |  +- /DirA
        |  +- fileA.html
        |
        +- /ComponentC
           +- /DirC
              +- fileC.html

My gulp task

gulp.task('copyHTMLwrong', function() {
    return gulp.src([
        '../Source/ComponentA/**/*.html',
        '../Source/ComponentC/**/*.html'
    ])
    .pipe(gulp.dest('public_html'));
});

But I get (loose path):

Project
+-- /Development
    +- gulpfile.js
    |
    +- /public_html 
        +- fileA.html
        +- fileC.html

PS: I know if I use 'Source/**/*.html', will copy all files correctly and I'm sure I can also remove ComponentC using !, but i need to define each Component on my gulp.src, so I can compile for each website a group of files.

gulp.task('copyHTMLnotUseful', function() {
    return gulp.src([
        '../Source/**.*.html',
        '!../Source/ComponentB/**/*.html'
    ])
    .pipe(gulp.dest('public_html'));
});

I've tried set cwd or base, but didn't work either.

Thanks


Solution

  • I figured out how to use base to fix my Issue.

    It's simple, just add a base with a __dirname, like so. Just make sure you set inside path.resolve():

    gulp.task('copyHTML', function() {
        return gulp.src([
            '../Source/ComponentA/**/*.html',
            '../Source/ComponentC/**/*.html'
        ],
        {base: path.resolve(__dirname + '/../Source')})
        .pipe(gulp.dest('public_html'));
    });