kokoi
kokoi

Reputation: 35

How to dynamically set destination paths in gulp?

I have a simple gulp task that moves a folder tree from node-modules to a specific destination keeping the folder structure like this:

function libraries(){

    let libPaths = [
        './node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
        './node_modules/bootstrap-icons/**/*.*' // Note: copying the module
    ]

    return gulp.src(libPaths, {base: './node_modules/'})
        .pipe(gulp.dest(
                (vinyl) => {
                    // Something here?
                    return '/destination/'
                }
            )
        );
    }

Result:

destination
├── bootstrap-icons
│   ├── LICENSE.md
│   ├── README.md
│   ├── bootstrap-icons.svg
│   ├── font
│   ├── icons
│   └── package.json
└── jquery
    └── dist            // <- problem
        ├── jquery.js
        ├── jquery.min.js
        ├── jquery.min.map
        ├── jquery.slim.js
        ├── jquery.slim.min.js
        └── jquery.slim.min.map

Expecting:

destination
├── bootstrap-icons
│   ├── LICENSE.md
│   ├── README.md
│   ├── bootstrap-icons.svg
│   ├── font
│   ├── icons
│   └── package.json
└── jquery
    ├── jquery.js
    ├── jquery.min.js
    ├── jquery.min.map
    ├── jquery.slim.js
    ├── jquery.slim.min.js
    └── jquery.slim.min.map

How would I detect when the source file is within a dist/ e.g. ./node_modules/jquery/dist/jquery.js to set the destination output to /destination/jquery/jquery.js -- and not within /destination/jquery/dist/jquery.js.

Upvotes: 0

Views: 67

Answers (1)

kokoi
kokoi

Reputation: 35

I made it work like this:

function libraries(){

    let libPaths = [
        './node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
        './node_modules/bootstrap-icons/**/*.*' // Note: copying the module
    ]

    return gulp.src(libPaths, {base: './node_modules/'})
        .pipe(gulp.dest(
                (vinyl) => {
                    vinyl.path = path.join(
                        vinyl.cwd, vinyl.base,
                        vinyl.relative.replace('dist\\', ''));
                    return '/destination/'
                }
            )
        );
    }

Upvotes: 0

Related Questions