Skip to content

Commit

Permalink
use local gulp version, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Oct 30, 2013
1 parent 1881df4 commit c6a89f7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
76 changes: 65 additions & 11 deletions bin/gulp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,84 @@ var path = require('path');
var fs = require('fs');

var argv = require('optimist').argv;
var chalk = require('chalk');
var resolve = require('resolve');

var gulp = require('../');
var gulpFilename = getGulpFile();
var tasks = argv._;
var gulpFile = getGulpFile(process.cwd());
var cliPkg = require('../package.json');
var cliGulp = require('../');

if (!gulpFilename) {
throw new Error("Missing Gulpfile");
if (!gulpFile) {
cliGulp.log(chalk.red('No Gulpfile found'));
return;
}

// Expose CLI flags to tasks or plugins
gulp.env = tasks;
// find the local gulp
var localGulp = findLocalGulp(gulpFile);
var localPkg = findLocalGulpPackage(gulpFile);

var theGulpfile = require(gulpFilename);
// print some versions and shit
if (argv.v || argv.version) {
cliGulp.log('CLI version', cliPkg.version);
if (localGulp) {
cliGulp.log('Local version', localPkg.version);
}
return;
}

if (!localGulp) {
cliGulp.log(chalk.red('No local gulp install found in'), getLocalBase(gulpFile));
cliGulp.log(chalk.red('You need to npm install it first'));
return;
}

// Mix CLI flags into gulp
localGulp.env = argv;

// Load their gulpfile and run it
cliGulp.log('Using file', chalk.magenta(gulpFile));
loadGulpFile(localGulp, gulpFile, tasks);

function getLocalBase(gulpFile) {
return path.resolve(path.dirname(gulpFile));
}

gulp.run.apply(gulp, tasks);
function findLocalGulp(gulpFile){
var localBase = getLocalBase(gulpFile);
try {
return require(resolve.sync('gulp', {basedir: localBase}));
} catch(e) {}
return;
}

function findLocalGulpPackage(gulpFile){
var localBase = getLocalBase(gulpFile);
var packageBase;
try {
packageBase = path.dirname(resolve.sync('gulp', {basedir: localBase}));
return require(path.join(packageBase, 'package.json'));
} catch(e) {}
return;
}

function loadGulpFile(localGulp, gulpFile, tasks){
var theGulpfile = require(gulpFile);
// just for good measure
process.nextTick(function(){
localGulp.run.apply(localGulp, tasks);
});
return theGulpfile;
}

function getGulpFile() {
function getGulpFile(baseDir) {
var extensions = Object.keys(require.extensions);
var Gulpfiles = extensions.map(function(ext){
return path.join(process.cwd(), "Gulpfile" + ext);
return path.join(baseDir, "Gulpfile" + ext);
});

var gulpFiles = extensions.map(function(ext){
return path.join(process.cwd(), "gulpfile" + ext);
return path.join(baseDir, "gulpfile" + ext);
});

return gulpFiles.concat(Gulpfiles)
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

var util = require('util');
var Orchestrator = require('orchestrator');
var chalk = require('chalk');

function Gulp(){
Orchestrator.call(this);
this.env = {};
this.on('task_start', function(e){
gulp.log('Running', "'"+chalk.cyan(e.task)+"'...");
});
}
util.inherits(Gulp, Orchestrator);

Gulp.prototype.log = function(){
if (this.env.silent) return;
var sig = '['+chalk.green('gulp')+']';
var args = [].slice.call(arguments);
args.unshift(sig);
console.log.apply(console, args);
return this;
};

Gulp.prototype.taskQueue = Gulp.prototype.seq;
Gulp.prototype.task = Gulp.prototype.add;
Gulp.prototype.run = Gulp.prototype.start;
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"gulp",
"description":"The streaming build system",
"version":"1.2.1",
"version":"2.0.0",
"homepage":"http://github.com/wearefractal/gulp",
"repository":"git://github.com/wearefractal/gulp.git",
"author":"Fractal <[email protected]> (http://wearefractal.com/)",
Expand All @@ -18,7 +18,9 @@
"optimist":"0.6.0",
"gulp-util":"1.0.0",
"gaze":"0.4.2",
"orchestrator": "0.0.6"
"orchestrator": "0.0.6",
"chalk":"0.3.0",
"resolve":"0.5.1"
},
"devDependencies":{
"mocha":"*",
Expand Down

0 comments on commit c6a89f7

Please sign in to comment.