description and source-codegulp-chug = function ( options, userCallback ) {
// Consider `options` the callback if it's a function
if ( _.isFunction( options ) ) {
userCallback = options;
options = {};
}
// Set default options
var opts = _.assign( {
nodeCmd: 'node',
tasks: [ 'default' ]
}, options );
// Set the callback to a noop if it's not a function
userCallback = _.isFunction( userCallback ) ? userCallback : _.noop
// Create a stream through which each file will pass
return through.obj( function ( file, enc, callback ) {
// Grab reference to this through object
var self = this;
// Since we're not modifying the gulpfile, always push it back on the
// stream.
self.push( file );
// Configure logging and errors
var say = function( msg, noNewLine ) {
if ( !noNewLine ) {
return console.log(
util.format( '[%s]', gutil.colors.green( PKG.name ) ), msg
);
}
process.stdout.write( util.format( '[%s]', gutil.colors.green( PKG.name ) ) + ' ' + msg )
};
var sayErr = function( errMsg ) {
self.emit( 'error', new PluginError( PKG.name, errMsg ) );
};
// Error if file contents is stream ( { buffer: false } in gulp.src )
// TODO: Add support for a streams
if ( file.isStream() ) {
sayErr( 'Streams are not supported yet. Pull requests welcome :)' );
return callback();
}
// Gather target gulpfile info
var gulpfile = {};
gulpfile.path = file.path;
gulpfile.relPath = path.relative( process.cwd(), gulpfile.path );
gulpfile.base = path.dirname( file.path );
gulpfile.relBase = path.relative( process.cwd(), gulpfile.base );
gulpfile.name = path.basename( gulpfile.path );
gulpfile.ext = path.extname( gulpfile.name );
// If file contents is null, { read: false }, just execute file as-is
// on disk
if( file.isNull() ){
say( util.format(
'Gulpfile, %s, contents is empty. Reading directly from disk...',
gulpfile.name
) );
}
// If file contents is a buffer, write a temp file and run that instead
if( file.isBuffer() ) {
say( 'File is a buffer. Need to write buffer to temp file...' );
var tmpGulpfileName = util.format(
'%s.tmp.%s%s',
path.basename( gulpfile.name, gulpfile.ext ),
new Date().getTime(),
gulpfile.ext
);
// Tweak gulpfile info to account for temp file
gulpfile.origPath = gulpfile.path;
gulpfile.path = path.join( gulpfile.base, tmpGulpfileName );
gulpfile.tmpPath = gulpfile.path;
gulpfile.origRelPath = gulpfile.relPath;
gulpfile.relPath = path.relative( process.cwd(), gulpfile.path );
gulpfile.name = tmpGulpfileName;
say( util.format(
'Writing buffer to %s...',
gutil.colors.magenta( gulpfile.relPath )
) );
// Write tmp file to disk
fs.writeFileSync( gulpfile.path, file.contents );
}
// Find local gulp cli script
var localGulpPackage = null;
var localGulpPackageBase = null;
var localGulpCliPath = null;
try {
localGulpPackageBase = path.dirname( resolve.sync( 'gulp', { basedir: gulpfile.base } ) );
localGulpPackage = require( path.join( localGulpPackageBase, 'package.json' ) );
localGulpCliPath = path.resolve( path.join( localGulpPackageBase, localGulpPackage.bin.gulp ) );
} catch( err ) {
sayErr( util.format(
'Problem finding locally-installed `gulp` for gulpfile %s. ' +
'(Try running `npm install gu ...