F
F
FlapJalc2018-12-28 16:49:42
gulp.js
FlapJalc, 2018-12-28 16:49:42

How to determine the directory through the task?

There is a folder with resources (resources) where I store static files favicons, robots.txt and other configuration files. I also have several directories there. So directories with their contents should be copied to the directory dist/assets, and files that lie outside these directories (in the resourses root) should be copied to dist. I managed to implement it in the way below.

gulp.task("copy", function () {
  gulp.src(['src/resources/**', '!src/resources/{{fonts,files},{fonts,files}/**}'])
  .pipe(gulp.dest("dist"))
  gulp.src("src/resources/{fonts,files}/**")
  .pipe(gulp.dest("dist/assets"))
});

But if I create another directory with files in resources, I will need to add it to the task. Is it possible to make the task itself determine all the folders in resources and send them to the path specified in the tax.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RAX7, 2018-12-28
@FlapJalc

Separate files and folders using fs.lstatSync(name).isDirectory() and copy what and where you need.
Something like this:

const fs = require('fs');

gulp.task('copy', function(done) {
  const allFiles = fs.readdirSync('./');
  const allStats = allFiles.reduce((stats, name) => {
    fs.lstatSync(name).isDirectory() ? stats.dirs.push(name) : stats.files.push(name);
    return stats;
  }, {files: [], dirs: []});
  
  gulp.src(allStats.files)
    .pipe(gulp.dest("dist"));
  
  gulp.src(allStats.dirs)
    .pipe(gulp.dest("dist/assets"));

  done();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question