Search code examples
javascriptnode.jscmdgulpgulp-notify

Display project folder path in the terminal with Gulp-Notify instead of entire path of project on hard drive


I am using gulp-notify to display extra information in the terminal when a task is run. Currently I can only get the full path on my HD and file name. I would prefer to only display the project folders as it is cleaner.

function copyVideo (done) {
   // Locate files
   return gulp.src('./src/assets/video/*')
   // Copy the files to the dist folder
   .pipe(gulp.dest('./dist/assets/video'))
   // Notify the files copied in the terminal
   .pipe(notify('Copied <%= file.relative %> to <%= file.path %>')),
 done();
}

Terminal view

enter image description here

I would like the terminal to simply say *Copied quick-scope-for-6.mp4 to \dist\assets\video*

I have tried <%= folder.path %> and <%= directory.path %>


Solution

  • With the form of notify(Function) (as documented here), you can use the built-in path.relative method to get the destination path relative to your project folder.

    var path = require('path');
    // ...
    
    function copyVideo (done) {
      // Locate files
      return gulp.src('./src/assets/video/*')
      // Copy the files to the dist folder
      .pipe(gulp.dest('./dist/assets/video'))
      // Notify the files copied in the terminal
      .pipe(notify(file => {
        var destFolder = path.dirname(file.path);
        var projectFolder = path.dirname(module.id); // Also available as `module.path`
        return `Copied ${file.relative} to ${path.relative(projectFolder, destFolder)}`;
      })),
      done();
    }