gulp icon indicating copy to clipboard operation
gulp copied to clipboard

copy and keep original folder structure

Open pajooh opened this issue 12 years ago • 38 comments

i have a list of files to copy:

    gulp.src([list of js files])
      .pipe(gulp.dest(build_dir))

the files are copied, but flattened. how can i reserve original directory structure? i can't find an option for that

pajooh avatar Jan 14 '14 16:01 pajooh

Can you paste the globs you are using?

yocontra avatar Jan 14 '14 17:01 yocontra

i use external config.json:

{
  "build_dir": "build",
  "vendor_files": {
    "js": [
      "vendor/angular/angular.js",
      "vendor/angular-animate/angular-animate.js",
      "vendor/angular-bootstrap/dist/ui-bootstrap-tpls-0.7.0.min.js",
      "vendor/angular-ui-router/release/angular-ui-router.js",
      "vendor/angular-ui-utils/modules/route/route.js",
      "vendor/restangular/dist/restangular.js",
      "vendor/underscore/underscore.js",
      "vendor/angular-underscore/angular-underscore.js"
    ],
    "css": [
    ],
    "assets": [
    ]
  }
}

and gulpfile.js

var config = require('./build.config.json');
gulp.task('scripts', function(){
    gulp.src(config.vendor_files.js)
      .pipe(gulp.dest(config.build_dir + '/scripts/vendor'))
});

pajooh avatar Jan 14 '14 17:01 pajooh

@pajooh what do you want the files coming out to look like?

yocontra avatar Jan 14 '14 17:01 yocontra

i want the original paths, i.e. "vendor/angular/angular.js", "vendor/angular-animate/angular-animate.js", "vendor/angular-bootstrap/dist/ui-bootstrap-tpls-0.7.0.min.js", "vendor/angular-ui-router/release/angular-ui-router.js", "vendor/angular-ui-utils/modules/route/route.js", "vendor/restangular/dist/restangular.js", "vendor/underscore/underscore.js", "vendor/angular-underscore/angular-underscore.js" under build directory, now all js files are coppied to the top level directory

pajooh avatar Jan 14 '14 17:01 pajooh

It seems that gulp only preserves the directory structure of globs. @Contra is this correct?

@pajooh is there a reason globs aren't being used?

phated avatar Jan 15 '14 08:01 phated

i have some bower installed packages, with a bunch of js files in them. i don't need them all, but some specific files(e.g. just minified versions). i have to list them one by one, globs can not help here

pajooh avatar Jan 15 '14 08:01 pajooh

@pajooh you can still use globs

An example being:

{
  "build_dir": "build",
  "vendor_files": {
    "js": [
      "**/angular.js",
      "**/angular-animate.js",
      "**/ui-bootstrap-tpls-0.7.0.min.js",
      "**/angular-ui-router.js",
      "**/route.js",
      "**/restangular.js",
      "**/underscore.js",
      "**/angular-underscore.js"
    ],
    "css": [
    ],
    "assets": [
    ]
  }
}

but you could make it more precise. It seems like such a pain to type out the whole path and keep that up-to-date.

phated avatar Jan 15 '14 08:01 phated

yes, but the glob approach is not generally the best way. think if some js exist in many folders with the same name, glob approach may get worse than typing out the whole path

pajooh avatar Jan 15 '14 08:01 pajooh

@pajooh that's why I said you can get more specific.

The best approach would be to not expect that directory structure in your output, since that is vendor code anyway.

phated avatar Jan 15 '14 08:01 phated

lets see an example i want to use jquery.js from jquery package, if i use **/jquery.js, its too general, because some other packages may already has that, the proper glob seems to be jquery/**/jquery.js, it will do the job, but the folder structure does not contain top jquery folder! so i think there is a need for gulp option to preserves the directory structure of non globs

pajooh avatar Jan 15 '14 09:01 pajooh

temporary solution that worked for me just fine:

{
  "build_dir": "build",
  "vendor_files": {
    "js": [
      "angular/angular.js",
      "angular-animate/angular-animate.js",
      "angular-bootstrap/dist/ui-bootstrap-tpls-0.7.0.min.js",
      "angular-ui-router/release/angular-ui-router.js",
      "angular-ui-utils/modules/route/route.js",
      "restangular/dist/restangular.js",
      "underscore/underscore.js",
      "angular-underscore/angular-underscore.js"
    ],
    "css": [
    ],
    "assets": [
    ]
  }
}
// gulpfile
var config = require('./build.config.json');
gulp.task('scripts', function(){
    gulp.src(config.vendor_files.js, { cwd : 'vendor/**' })
      .pipe(gulp.dest(config.build_dir + '/scripts/vendor'))
});

It seems that gulp only preserves the directory structure of globs

@phaet seems like it - is that unexpected?

stryju avatar Jan 15 '14 11:01 stryju

@stryju very good, your temporary solution worked for me i used:

// gulpfile
var config = require('./build.config.json');
gulp.task('scripts', function(){
    gulp.src(config.vendor_files.js, { cwd : '**/vendor' })
      .pipe(gulp.dest(config.build_dir + '/scripts'))
});

pajooh avatar Jan 15 '14 11:01 pajooh

@pajooh glad i could help

now, the question remains - is that glob behavior a desired behavior? the only difference is the base prop on the streams

stryju avatar Jan 15 '14 12:01 stryju

Yup - gulp gets the output file structure from the relativity between the base path (where the glob ended) and the cwd

yocontra avatar Jan 15 '14 20:01 yocontra

When you give it a direct file it won't maintain the structure. To keep the structure you can pass in a base arg to src or you can use gulp-rename, gulp-flatten, etc. to mess with the structure

yocontra avatar Jan 15 '14 20:01 yocontra

I made a example for side by side compilation. Maybe useful as recipe: https://gist.github.com/steida/8592161.

steida avatar Jan 24 '14 04:01 steida

The solutions here didn't work in my case (specifically copying only the angular source map to a flat folder) so I wrote this simple plugin. Hope it can help someone out: https://gist.github.com/dwelch2344/8969706

dwelch2344 avatar Feb 13 '14 04:02 dwelch2344

@dwelch2344 Isn't that basically what gulp-flatten does?

yocontra avatar Feb 13 '14 06:02 yocontra

Ah, indeed it does, though not nearly as pretty. We'll be switching to this. Thanks for the heads up!— Sent from Mailbox for iPhone

On Wed, Feb 12, 2014 at 11:40 PM, Eric Schoffstall [email protected] wrote:

@dwelch2344 Isn't that basically what gulp-flatten does?

Reply to this email directly or view it on GitHub: https://github.com/gulpjs/gulp/issues/151#issuecomment-34952423

dwelch2344 avatar Feb 13 '14 14:02 dwelch2344

Long thread here for a very simple solution.

Just set the files' file.base so that their file.relative resolves to the path you want for each file relative to the build dir (dest). That's all.

In any such situation, if you can't set the base with config, you can effect such a transformation with a through-like stream right in your task, instead of using a gulp- module. Like

// [ ... ]
.pipe(through(function(file) { file.base = yourBase; this.emit('data', file); }))
// [ ... ]

All if this, by the way, is behavior directly from require('vinyl-fs').src and require('vinyl-fs').dest, because require('gulp').src and require('gulp').dest are identically those objects.

dminkovsky avatar Apr 03 '14 15:04 dminkovsky