Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grunt.file.expand() equivalent in gulp #386

Closed
niki4810 opened this issue Mar 31, 2014 · 9 comments
Closed

grunt.file.expand() equivalent in gulp #386

niki4810 opened this issue Mar 31, 2014 · 9 comments

Comments

@niki4810
Copy link

Hi,
I am just getting started with gulp and need help with the following:

//if you have a  directory structure as follows
-foo/
      foo1.js
      foo2.js
-bar/
      bar1.js
      bar2.js

//running grunt.file.expand()
var paths = grunt.file.expand(_.map(['foo','bar'], function (pa) {
            return path.join("../", proj, "**/*.js");
          }));
console.log(paths);
//outputs
['../foo/foo1.js','../foo/foo2.js','../bar/bar1.js','../bar/bar2.js']

I was trying to achieve the same using gulp, i.e. get an array of all the .js files that match a pattern, what is the easiest way to achieve this in gulp?

Thanks

@yocontra
Copy link
Member

@niki4810 You can pass globs into src, there is no need to "expand".

For your example:

gulp.src(['foo/**/*.js', 'bar/**/*.js'])

or if you still needed to do it programmatically for some reason, here is an exact copy of what your stuff is doing

var path = require('path');

var folders = ['foo', 'bar'];
var globs = folders.map(function(folder){
  return path.join('../', folder, '**/*.js');
});

gulp.src(globs).pipe(stuff())

@niki4810
Copy link
Author

@contra : Here is a exact scenario on why I need to expand the file paths. When compile stylus files, I have a case where each stylus file goes to a separate destination folder. To do this, currently I am doing something like this :

var path = require('path'),
      _ = require("lodash"),
      mainSrcPath = 'src/main/',
      distPath = 'src/dist/',
      folders = ['foo', 'bar'];

var globs = folders.map(function(folder){
  return path.join('../',mainSrcPath, folder, '**/*.styl');
});

_.forEach(globs, function(source) {
   var destination = src.replace(mainSrcPath, distPath)
        .replace(/\.styl$/, ".css"); 
   gulp.src(source)
      .pipe(stylus())
      .pipe(gulp.dest(destination));
  });

In the above case, each source file needs to map to a specific destination. if each path in the globs array is not expanded, the destination path is set incorrectly.

Also, not sure what I did is an anti-pattern in gulp, i.e. using _.forEach to run stylus on each source path.

@yocontra
Copy link
Member

@niki4810 I'm convinced you can do it all in one .src run

Give this a try and see if it does what you need

var path = require('path');
var mainSrcPath = 'src/main';
var distPath = 'src/dist';
var folders = ['foo', 'bar'];

var glob = '../' + mainSrcPath + '/{' + folders.join(',') + '}/**/*.styl';

gulp.src(glob)
    .pipe(stylus())
    .pipe(gulp.dest(distPath));

@yocontra
Copy link
Member

If every folder beneath src/main is a folder containing stylus folders you dont even have to do anything fancy

gulp.src('src/main/**/*.styl')
    .pipe(stylus())
    .pipe(gulp.dest('src/dist'));

@niki4810
Copy link
Author

@contra: Thanks for your solution, it did work, but its still does not fit my use case. Basically if my directory structure is :

- foo/
  - src/
    -main/
      - foo/
         - foo1.styl
         - foo2.styl
    - gen/
- bar/
  - src/
    - main/
      - bar/
         - bar1.styl
         - bar2.styl
    - gen/

I wanted my generated styles per module to be with in

 - foo/
    - src/
       - main/
       - gen/
            - foo/
               - foo1.css
               - foo2.css 
 - bar/
   - src/
      - main/
      - gen/
         - bar/
            - bar1.css
            - bar2.css

So for every module, the styl file under <module>/src/main/<module>/foo.styl the generated css file should be under <module>/src/gen/<module>/foo.css .

The reason I was looping over the paths was because my dest is different per each source module.
Sorry about all the confusion with this :).

@laurelnaiad
Copy link
Contributor

Seems like a gulp-stylus feature request. I think the more typical use-case is for the styl files to be combined.

@yocontra
Copy link
Member

gulp.src('./*/src/main/**/*.styl')
    .pipe(stylus())
    .pipe(rename(function(path){
      path.dirname = path.dirname.replace('src/main', 'src/gen');
    })
    .pipe(gulp.dest('.'));

See if that does it?

@niki4810
Copy link
Author

niki4810 commented Apr 1, 2014

@contra : That solution worked for me.. Thanks a lot for all your help 👍

@niki4810 niki4810 closed this as completed Apr 1, 2014
@eguneys
Copy link

eguneys commented Jul 16, 2014

Can you take a look at this thanks: http://stackoverflow.com/questions/24767080/package-files-by-folder-with-gulp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants