Noitidart
Noitidart

Reputation: 37268

Modify path of gulp.dest

My goal is to find the binaries in here, and elevate them to the first subdir in exe dir.

My simplified (in reality it has more junk, more subdirs) folder structure is this:

src
├── exe
│   └── nix
│       └── binary.out
│       └── blah
│   └── mac
│       └── binary.out
│       └── blah
│   └── win
│       └── src
│           └── trigger
│                └── trigger
│                    └── trigger
│                        └── binary.exe
│                    └── Debug

My goal is to copy just the binary files into dist/exe like this:

dist
├── exe
│   └── nix
│       └── binary.out
│   └── mac
│       └── binary.out
│   └── win
│       └── binary.exe

This is the code I use right now:

var path = require('path');

gulp.src(['src/exe/**/*.out', 'src/exe/**/*.exe'])
  .pipe(gulp.dest(function(file) {
    var os = file.path.match(/(?:win|mac|nix)/)[0];
    return 'dist/exe/' + os + '/' + path.basename(file.path);
  }));

However this is still copying the structure even after the os name. It's like it appends the **/ stuff after my returned string. Is there any way to make it not do this?

Upvotes: 2

Views: 2280

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30574

It like appends the **/ stuff after my returned string. Is there anyway to make it not do this

Yep, that's exactly what it does and it's intentional. Anything before the ** is treated as the base path, anything after is the path that is used when writing the file to the destination directory. (See this answer if you want to know more).

Normally you could use the base option in gulp.src() to change this, but this doesn't really work in your case since you would need one base path for nix/mac and another one for win.

On top of all that you're using gulp.dest() wrong. gulp.dest() specifies the destination directory. You're trying to use it to specify a destination file. That's not possible.

The easiest solution for you is to use gulp-rename:

var path = require('path');
var rename = require('gulp-rename');

gulp.src(['src/exe/**/binary.{exe,out}'])
  .pipe(rename(function(file) {
    file.dirname = file.dirname.split(path.sep)[0];
  }))
  .pipe(gulp.dest('dist/exe'));

Upvotes: 6

Related Questions