Coder Social home page Coder Social logo

grunt-galvanize's Introduction

grunt-galvanize

Run other grunt tasks multiple times with different options.

Notice of name change

Please be sure to install with grunt-galvanize, not grunt-galvenize. The master branch of this repo will henceforth be published to grunt-galvanize. I appologize for any inconvenience that this may cause.

Install

$ npm install --save-dev grunt-galvanize

Why?

Using grunt with static configurations has its limits. Perhaps your build process needs to read in a list of files from disk, then perform tasks on those files. This is made difficult by the fact that grunt.task.run simply queues a new task to be run. Thus the state of grunt.config may change between the time that the task was queued and when it is run. Galvanize allows you to define critical components of grunt.options and grunt.config, and ensure that those components are in the desired state when a sub-task is run. In practice, this enables calling grunt.task.run within a loop.

Usage

Create galvanizeConfig:

The galvanizeConfig is an array of objects with a configs and an options property. The configs and options will be used to set grunt config and grunt options before running each iteration of a task.

   grunt.option('galvanizeConfig', [
       { configs: {color: 'red'}, options: {size: small} },
       { configs: {color: 'green'}, options: {size: small}
   ]);

Run galvanize:

Run the galvanize task, specifying the task that you would like galvanize to iterate.

grunt.task.run(['galvanize:myTask']);

The above config will result in running myTask once with grunt.config.color = 'red', and once with grunt.config.color = 'green'.

Get fancy

For nested configs and options, use an array to specify the key and value:

    grunt.option('galvanizeConfig', [
        {
            configs: [
                { key: ['browserify', 'files'], value: '/mnt/foo/bar/**.js' },
                { key: ['browserify', 'dedup'], value: false }
            ]
        },
        {
            configs: [
                { key: ['browserify', 'files'], value: '/mnt/foo/baz/**.js' },
                { key: ['browserify', 'dedup'], value: true }
            ]
        }
    ]);

    grunt.task.run(['galvanize:browserify']);

The above config will run browserify once with grunt.config.browserify.files set to /mnt/foo/bar/**.js, and once with grunt.config.browserify.files set to /mnt/foo/baz/**.js.

The galvanizeConfig can be easily created programmatically within your build process.

In the wild:

Here's a snippet from our code base that takes advantage of Galvanize

/**
 * Run udb on each changelog in the master runlist
 * @param {string} db is the alias for the DB against which changelogs should be run.
 *   defaults to the db defined for the current branch in config.dbBranchMap.
 */
grunt.registerTask('execRunlist', '', function(db) {
    var runlistPath = config.get('masterRunlist');
    var runlist = grunt.file.readJSON(runlistPath);

    // verify that runlist is an array
    if (!_.isArray(runlist)) {
        throw new Error(
            'runlist is not an array: it is a ' + typeof runlist
        );
    }

    // map runlists to options object
    var galvanizeConfig = runlist.map(function(path) {
        return {options: {path: path}};
    });

    grunt.option('galvanizeConfig', galvanizeConfig);
    grunt.task.run([
        'galvanize:udb', //run udb for each path in galvanizeConfig
        'galvanize:dequeueFromRunlist' //remove each path in galvanizeConfig from runlist
    ]);
});

Contributing

Please submit any changes to this repo (including additions and subtractions from the lint config files) as pull requests.

grunt-galvanize's People

Contributors

dylancwood avatar nephrited avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grunt-galvanize's Issues

When will the latest changes be released to npm

The latest changes to the project are not included in the package that you get when installing with npm. More specific the changed handling of grunt task targets called by galvanize.

For example running this:
grunt.task.run(['galvanize:express:default:stop']);

npm version runs express:default`

github version runs express:default:stop correctly as expected

Is there any chance those changes will be published anytime soon? I'm asking because installing your module via github directly don't works, due to a typo in your package.json. There your project name is defined and installed as:

grunt-galvenize

not

grunt-galvanize

BTW: Really love your plugin, helped me tremendously in my current project. Thanks

grunt-galvanize not found

It would be nice if this conformed to the standard require(''grunt-').
Currently I have to do the following: require('./node_modules/grunt-galvanize/tasks/galvanize.js')(grunt);

Please fix.

Cant pull value from galvanize config

Hi, I have stumbled across your Grunt-Galvanize which seems to solve my issue (need to run a task multiple times with different values)

So I have galvanize in and working, in the fact that my task loops for the numbers of objects within the array of config objects. However I am unable to retrieve the value from the galvanize config when running the task.

I want to be able to use a value from the config as part of a path string so the task runs against different paths each time.

This is my grunt file:

/*global module:false*/
module.exports = function(grunt) {
    var LIVERELOAD_PORT = 35729;
    var config = [
            { configs: {color: 'red'}, options: {size: 'small'} },
            { configs: {color: 'green'}, options: {size: 'small'} }
        ];

    // Project configuration.
    grunt.initConfig({
        // Metadata.
        pkg: grunt.file.readJSON('package.json'),
        purifycss: {
            options: {
                minify: true,
            },
            target: {
                src: ['../src/components/article/templates/article_dev.mustache'],
                css: ['../src/components/lib/templates/' + grunt.config.color + '/index.mustache'],
                dest: '../src/components/lib/templates/' + grunt.config.color + '/index.mustache'
            }   
        },
    });

    grunt.loadNpmTasks('grunt-purifycss');
    grunt.loadNpmTasks('grunt-galvanize');

    grunt.registerTask('mytask', 'This is a task which runs taskName', function() {
        grunt.option('galvanizeConfig', config);
        console.log(grunt.config.color)

        grunt.task.run(['galvanize:purifycss']);
    });
};`

So inside the purifycss task you can see I have tried to access grunt.config.color this always returns undefined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.