Joe
Joe

Reputation: 326

Reading bower dependencies into grunt file list

I am using grunt, and I want to copy my bower dependencies when creating a production distribution

These dependencies already exist in ./components

I generate a production directory with index.html inside and want to copy only the dependencies from the bower.json file.

I thought this would be as simple as generating a list from the deps:

prodComponents = Object.keys(grunt.file.readJSON('./bower.json').dependencies)

(which produces from a simple console.log(prodComponents)

[ 'requirejs',
  'requirejs-text',
  'jquery',
  'underscore-amd',
  'backbone-amd',
  'backbone.wreqr',
  'backbone.babysitter',
  'marionette' ]

and then simply copying the matching files:

    copy:
        deps:
            files: [
                expand: true
                cwd: './components'
                src: ['./<%= prodComponents %>/*']
                dest: './dev/components'
            ]

This works, but copies ALL the components. i.e. my file spec is failing

Running "copy:deps" (copy) task
Created 15 directories

if I remove the ./ then it fails with:

Warning: Unable to read "components/Applications" file (Error code: ENOENT). Use --force to continue.

Can't help but think I'm either trying to be too clever, or nearly there with this.

What am I doing wrong with the specification of the file spec?

Thanks

Upvotes: 4

Views: 1853

Answers (1)

go-oleg
go-oleg

Reputation: 19480

I think you are close. I would save the directories with globbing patterns applied into prodComponents:

prodComponents = Object.keys(grunt.file.readJSON('./bower.json').dependencies).map(
    function(prodComponent) {
        return prodComponent + "/**/*";
    }
);

So prodComponents would contain:

["requirejs/**/*",
 "requirejs-text/**/*",
 "jquery/**/*",
 "underscore-amd/**/*",
 "backbone-amd/**/*",
 "backbone.wreqr/**/*",
 "backbone.babysitter/**/*",
 "marionette/**/*" ]

And the copy configuration would be:

copy:
    deps:
        files: [
            expand: true
            cwd: 'components'
            src: '<%= prodComponents %>'
            dest: 'dev/components'
        ]

Note that for you to be able to use prodComponents in a template in this way, it needs to be set in your grunt config.

Upvotes: 2

Related Questions