gulp

Copy file tree using gulp


Can't move a source folder tree to another location using gulp.

After years of manually transforming my source code into production I finally decided to switch to using gulp. As a starter I thought I'd do something simple - moving my source tree to another location but have failed :(

The source tree looks like this (omitting the actual files):

src
    css
    lib
        components
    services
    views
        auth
    call
        home
           home-page.js
           SectionCard.js
        journal
           journal-update.js
    portal.html
    routes.js

After some research I came up with this gulp file:

var gulp = require('gulp')

function defaultTask(cb) {
    console.log("Moving all folders/files in src folder");
    gulp.src(["src/**.*"])
      .pipe(gulp.dest('../server/public'));
    cb();
  }
  
  exports.default = defaultTask
$ gulp
[13:18:03] Using gulpfile F:\backup\Z\portal\gulpfile.js
[13:18:03] Starting 'default'...
Moving all files in site folder
[13:18:03] Finished 'default' after 5.09 ms

When I ran this it moved only the 2 files in the src folder (portal.html, routes.js).

I've searched for other answers (like 'article') which seem to suggest that this is how it should be done. I'm getting a bit frustrated with it as I thought this would be a simple enough task to start with :(


Solution

  • It seems like your Gulp task is not capturing the directory structure correctly. Let's adjust your Gulp task to include all files and directories within the src folder:

    var gulp = require('gulp');
    
    function moveSource(cb) {
        console.log("Moving all folders/files in src folder");
        gulp.src(["src/**/*"]) // This will include all files and subdirectories within src
          .pipe(gulp.dest('../server/public'));
        cb();
    }
    
    exports.default = moveSource;
    

    With this adjustment, the src/**/* pattern will match all files and subdirectories within the src folder. So, it should move the entire directory tree to the specified destination.

    Give this a try and see if it resolves your issue. If you encounter any further problems or have any questions, feel free to ask!