Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Separate CLI invocation

  • Loading branch information...
commit 2b383ff2dc6538c89115ebc88fb9a84dada19d82 1 parent cb05476
mde authored
Showing with 85 additions and 81 deletions.
  1. +2 −70 bin/cli.js
  2. +76 −5 lib/jake.js
  3. +7 −6 lib/program.js
View
72 bin/cli.js
@@ -18,74 +18,6 @@
*/
var args = process.argv.slice(2)
- , libPath = __dirname + '/../lib'
- , fs = require('fs')
- , jake = require(libPath + '/jake')
- , api = require(libPath + '/api')
- , utils = require(libPath + '/utils')
- , Program = require(libPath + '/program').Program
- , program = new Program()
- , Loader = require(libPath + '/loader').Loader
- , loader = new Loader()
- , pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json').toString())
- , opts
- , envVars
- , taskNames
- , rootTask;
-
-jake.version = pkg.version;
-
-global.jake = jake;
-
-process.addListener('uncaughtException', function (err) {
- program.handleErr(err);
-});
-
-program.parseArgs(args);
-
-if (!program.preemptiveOption()) {
- opts = program.opts
- envVars = program.envVars;
-
- // Globalize top-level API methods (e.g., `task`, `desc`)
- for (var p in api) {
- global[p] = api[p];
- }
-
- // Convenience aliases
- jake.program = program;
- utils.mixin(jake, utils);
- // File utils should be aliased directly on the base namespace
- utils.mixin(jake, utils.file);
- jake.FileList = require(libPath + '/file_list').FileList;
- jake.PackageTask = require(libPath + '/package_task').PackageTask;
- jake.NpmPublishTask = require(libPath + '/npm_publish_task').NpmPublishTask;
- jake.TestTask = require(libPath + '/test_task').TestTask;
-
- // Enhance env with any env vars passed in
- for (var p in envVars) { process.env[p] = envVars[p]; }
-
- loader.load(opts);
-
- // Set working dir
- var dirname = opts.directory;
- if (dirname) {
- process.chdir(dirname);
- }
-
- taskNames = program.taskNames;
- taskNames = taskNames.length ? taskNames : ['default'];
- task('__root__', taskNames, function () {});
-
- if (opts.tasks) {
- jake.showAllTaskDescriptions(opts.tasks);
- }
- else {
- rootTask = jake.Task['__root__'];
- rootTask.once('complete', function () {
- jake.emit('complete');
- });
- rootTask.invoke();
- }
-}
+ , jake = require('../lib/jake');
+jake.run(args);
View
81 lib/jake.js
@@ -20,11 +20,16 @@ var jake
, EventEmitter = require('events').EventEmitter
, fs = require('fs')
, path = require('path')
- , utils = require('utilities')
- , task = require('./task')
- , Task = task.Task
- , FileTask = task.FileTask
- , DirectoryTask = task.DirectoryTask;
+ , taskNs = require('./task')
+ , Task = taskNs.Task
+ , FileTask = taskNs.FileTask
+ , DirectoryTask = taskNs.DirectoryTask
+ , api = require('./api')
+ , utils = require('./utils')
+ , Program = require('./program').Program
+ , Loader = require('./loader').Loader
+ , loader = new Loader()
+ , pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json').toString());
var Namespace = function (name, parentNamespace) {
this.name = name;
@@ -57,6 +62,9 @@ var Invocation = function (taskName, args) {
* The main namespace for Jake
*/
jake = new EventEmitter();
+
+global.jake = jake;
+
utils.mixin(jake, new (function () {
this._invocationChain = [];
@@ -68,6 +76,8 @@ utils.mixin(jake, new (function () {
// Public properties
// =================
+ this.version = pkg.version;
+
this.errorCode = undefined;
// Name/value map of all the various tasks defined in a Jakefile.
// Non-namespaced tasks are placed into 'default.'
@@ -219,6 +229,67 @@ utils.mixin(jake, new (function () {
return task;
};
+ jake.run = function (args) {
+ var program = new Program()
+ , opts
+ , envVars
+ , taskNames
+ , rootTask;
+
+ process.addListener('uncaughtException', function (err) {
+ program.handleErr(err);
+ });
+
+ program.parseArgs(args);
+
+ if (!program.preemptiveOption()) {
+ opts = program.opts
+ envVars = program.envVars;
+
+ // Globalize top-level API methods (e.g., `task`, `desc`)
+ for (var p in api) {
+ global[p] = api[p];
+ }
+
+ // Convenience aliases
+ // ======
+ jake.program = program;
+ utils.mixin(jake, utils);
+ // File utils should be aliased directly on the base namespace
+ utils.mixin(jake, utils.file);
+ jake.FileList = require('./file_list').FileList;
+ jake.PackageTask = require('./package_task').PackageTask;
+ jake.NpmPublishTask = require('./npm_publish_task').NpmPublishTask;
+ jake.TestTask = require('./test_task').TestTask;
+
+ // Enhance env with any env vars passed in
+ for (var p in envVars) { process.env[p] = envVars[p]; }
+
+ loader.load(opts);
+
+ // Set working dir
+ var dirname = opts.directory;
+ if (dirname) {
+ process.chdir(dirname);
+ }
+
+ taskNames = program.taskNames;
+ taskNames = taskNames.length ? taskNames : ['default'];
+ task('__root__', taskNames, function () {});
+
+ if (opts.tasks) {
+ jake.showAllTaskDescriptions(opts.tasks);
+ }
+ else {
+ rootTask = jake.Task['__root__'];
+ rootTask.once('complete', function () {
+ jake.emit('complete');
+ });
+ rootTask.invoke();
+ }
+ }
+ };
+
})());
jake.Task = Task;
View
13 lib/program.js
@@ -16,7 +16,8 @@
*
*/
-var parseargs = require('../lib/parseargs')
+var parseargs = require('./parseargs')
+ , utils = require('./utils')
, Program
, optsReg
, preempts
@@ -117,18 +118,18 @@ Program.prototype = new (function () {
this.handleErr = function (err) {
var msg;
- jake.logger.error('jake aborted.');
+ utils.logger.error('jake aborted.');
if (this.opts.trace && err.stack) {
- jake.logger.error(err.stack);
+ utils.logger.error(err.stack);
}
else {
if (err.stack) {
msg = err.stack.split('\n').slice(0, 2).join('\n');
- jake.logger.error(msg);
- jake.logger.error('(See full trace by running task with --trace)');
+ utils.logger.error(msg);
+ utils.logger.error('(See full trace by running task with --trace)');
}
else {
- jake.logger.error(err.message);
+ utils.logger.error(err.message);
}
}
process.exit(jake.errorCode || 1);
Please sign in to comment.
Something went wrong with that request. Please try again.