如何复制多个文件并使用Gulp保持文件夹结构?

时间:2021-09-04 23:11:43

I am trying to copy files from one folder to another folder using gulp

我正在尝试使用gulp将文件从一个文件夹复制到另一个文件夹

gulp.task('move-css',function(){
  return gulp.src([
    './source/css/one.css',
    './source/other/css/two.css'

    ]).pipe(gulp.dest('./public/assets/css/'));
});

Above code is copying one.css & two.css to public/assets/css folder

上面的代码是将one.css和two.css复制到public / assets / css文件夹

And if I use gulp.src('./source/css/*.css') it will copy all CSS files to public/assets/css folder which is not what I want

如果我使用gulp.src('。/ source / css / * .css'),它会将所有CSS文件复制到public / assets / css文件夹,这不是我想要的

My question is how to select multiple files and keep the folder structure?

我的问题是如何选择多个文件并保留文件夹结构?

2 个解决方案

#1


To achieve this please specify base.

为此,请指定基数。

¶ base - Specify the folder relative to the cwd. Default is where the glob begins. This is used to determine the file names when saving in .dest()

¶base - 指定相对于cwd的文件夹。默认是glob开始的地方。保存在.dest()时用于确定文件名


In your case it would be:

在你的情况下,它将是:

gulp.task('move-css',function(){
  return gulp.src([
      './source/css/one.css',
      './source/other/css/two.css'
  ],  {base: './source/'}) 
  .pipe(gulp.dest('./public/assets/'));
});

Folder structure :

文件夹结构:

|-gulpfile.js
|-source
|   |-css
|   |-other
|      |-css
|-public
|   |-assets

#2


I use gulp-flatten and use this configuration.

我使用gulp-flatten并使用此配置。

var gulp = require('gulp'),
    gulpFlatten = require('gulp-flatten');

var routeSources = {
  dist:  './public/',
  app: './app/',
  html_views: {
    path: 'app/views/**/*.*',
    dist: 'public/views/'
  }
};

gulp.task('copy-html-views', task_Copy_html_views);
function task_Copy_html_views() {
  return gulp.src([routeSources.html_views.path])
      .pipe(gulpFlatten({ includeParents: 1 }))
      .pipe(gulp.dest(routeSources.html_views.dist));
}

And there you can see the documentations about gulp-flatten: Link.

在那里你可以看到关于gulp-flatten的文档:链接。

#1


To achieve this please specify base.

为此,请指定基数。

¶ base - Specify the folder relative to the cwd. Default is where the glob begins. This is used to determine the file names when saving in .dest()

¶base - 指定相对于cwd的文件夹。默认是glob开始的地方。保存在.dest()时用于确定文件名


In your case it would be:

在你的情况下,它将是:

gulp.task('move-css',function(){
  return gulp.src([
      './source/css/one.css',
      './source/other/css/two.css'
  ],  {base: './source/'}) 
  .pipe(gulp.dest('./public/assets/'));
});

Folder structure :

文件夹结构:

|-gulpfile.js
|-source
|   |-css
|   |-other
|      |-css
|-public
|   |-assets

#2


I use gulp-flatten and use this configuration.

我使用gulp-flatten并使用此配置。

var gulp = require('gulp'),
    gulpFlatten = require('gulp-flatten');

var routeSources = {
  dist:  './public/',
  app: './app/',
  html_views: {
    path: 'app/views/**/*.*',
    dist: 'public/views/'
  }
};

gulp.task('copy-html-views', task_Copy_html_views);
function task_Copy_html_views() {
  return gulp.src([routeSources.html_views.path])
      .pipe(gulpFlatten({ includeParents: 1 }))
      .pipe(gulp.dest(routeSources.html_views.dist));
}

And there you can see the documentations about gulp-flatten: Link.

在那里你可以看到关于gulp-flatten的文档:链接。