4

The vinyl-ftp package has a function clean() but I'm not sure how to use it right. I need to:

  1. get all files from my build folder
  2. put them into the target folder on my ftp server
  3. clean files if they're not available locally

I have the following gulp task:

gulp.task('deploy', () => {
  let conn = ftp.create({host:host,user:user,password: password});
  return gulp.src('build/**', {base: './build/', buffer: false })
      .pipe(conn.newer('/path/on/my/server/')) // only upload newer files
      .pipe(conn.dest('/path/on/my/server/'))
      .pipe(conn.clean('build/**', './build/'));
});

1) and 2) is OK, but the clean() function does nothing

Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
Max Vorozhcov
  • 593
  • 1
  • 6
  • 22

2 Answers2

7

The vinyl-ftp docs have this to say:

conn.clean( globs, local[, options] )

Globs remote files, tests if they are locally available at <local>/<remote.relative> and removes them if not.

Note that globs expects a path for the remote files on your FTP server. Since your remote files are located in /path/on/my/server/ you have to specify that path as your glob:

  .pipe(conn.clean('/path/on/my/server/**', './build/'));
Community
  • 1
  • 1
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
4

Since I got a lot of struggle with this, here a working peace of code. It removes all files from the server that dont exist locally except the usage folder:

var connection = ftp.create({ ... });

connection.clean([
    '/*.*',
    '/!(usage)*',
    '/de/**',
    '/en/**',
    '/images/**',
    '/fonts/**',
    '/json/**',
    '/sounds/**'
], './dist', { base: '/' });

My files are locally on the ./dist folder and remote directly in the root directory (/) (of the used ftp user).

Thomas Kekeisen
  • 4,355
  • 4
  • 35
  • 54