Skip to content

Solutions

Scott Johnson edited this page Jan 25, 2017 · 2 revisions

Multiple Project Use

This example assumes that the main project has one or more sub-projects with build files located in a directory called modules.

Example project structure.

.
|-- modules/
|   |-- module1/
|   |    `-- bobfile.js
|   |-- module2/
|   |    `-- bobfile.js
|   |-- module3/
|   |    `-- bobfile.js
|-- src/
`-- bobfile.js
// ./bobfile.js
var glob = require( 'promise-file-glob' );
var resolve = require( 'promise-resolve-path' );
var bob = require( 'builder-bob' );

var builder = module.exports = function( bob ){
    
    // Use glob to quickly locate all of the sub-module bobfiles.
    return glob( './modules/**/bobfile.js' )

    // Resolve all of the paths to absolute paths.
    .then( resolve )

    // Create your jobs.
    .then(function( aBobfiles ){

        // Create the "build" job.
        var buildJob = bob.createJob( 'build' );

        buildJob.addTask( 'build-modules', function(){
            var i, l = aResolved.length;
            var oBatch, aProm = [];

            // Loop over each absolute bobfile path.
            for( i = 0; i < l; i++ ) {

                // Get the module batch object.
                oBatch = require( aResolved[ i ] )( bob );

                // Run the module builder.
                aProm.push( oBatch.getJob( 'build' ).run() );

            }// /for()

            // https://github.com/kriskowal/q/wiki/API-Reference#promise-for-array-methods
            return Q.all( aProm );

        });

        // Add another task.
        buildJob.addTask( 'concat', function(){ ...

        // Create a "watch" job.
        var watchJob = bob.createJob( 'watch' )...
    });


    // Always return bob. :)
    return bob;

};// /export()

builder( bob ).getJob( 'build' ).run();

Each module has a bobfile that looks like this...

// ./modules/module1/bobfile.js
var builder = module.exports = function( bob ){
    // Create a new batch.
    var batch = bob.createBatch( 'module1' );

    // Create the "build" job.
    var buildJob = batch.createJob( 'build', function(){
        
        console.log( 'Put module build stuff here!' );

    });

    // Add another task to the build job.
    buildJob.addTask( 'another-task', function(){
        
        console.log( 'This is another task!' );

    });

    // Return the batch instance instead of bob. :\
    return batch;

};// /export()

Set All Tasks at Once

// ./bobfile.js
var bob = require( 'builder-bob' );

var builder = module.exports = function( bob ){

        // Create the "build" job.
        bob.createJob( 'build' )
        .setTasks([

        // Do this first.
        {
            task: 'empty',
            do: function(){
                // Create promises here.
            }
        },

        // Do this second.
        {
            task: 'copy',
            do: function(){
                // Create promises here.
            }
        }
        
    ]);

    // Always return bob. :)
    return bob;

};// /export()

builder( bob ).getJob( 'build' ).run();

Task Without Promise

Sometimes you don't want to have to wrap code in a promise.

// ./bobfile.js
var bob = require( '../builder-bob' );

var builder = module.exports = function( bob ){

        // Create the "build" job.
        var job = bob.createJob( 'build', function( result, next ){
            console.log( 'No promises here!' );
            next( 'foo' );
        });

        job.addTask( 'another-task', function( result, next ){
            console.log( result ); // outputs: foo
            next();
        });

        job.addTask( 'complete', function( result, next ){
            console.log( result ); // outputs: undefined
            console.log( 'complete!' );
            next();
        });

    // Always return bob. :)
    return bob;

};// /export()

builder( bob ).getJob( 'build' ).run();

Clone this wiki locally