Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-16153] Fixed pre-hook callback function signature and function ho... #94

Merged
merged 2 commits into from
Jan 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions hooks/tisdk3fixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@

var appc = require('node-appc');

exports.cliVersion = '>=3.X';
exports.cliVersion = '>=3.2';

exports.init = function (logger, config, cli) {
cli.on('cli:post-validate', function (data, finished) {
cli.on('cli:go', function () {
var sdk = (cli.sdk && cli.sdk.name) || (cli.manifest && cli.manifest.version);

if (sdk && appc.version.eq(sdk, '3.2.0')) {
cli._fireHookCallback = function (callback, err, data) {
if (err) {
callback(err);
} else {
callback(err, {}, data.result.shift());
}
};
}
});

cli.on('cli:post-validate', function (data) {
var sdk = (cli.sdk && cli.sdk.name) || (cli.manifest && cli.manifest.version);

if (sdk && appc.version.gte(sdk, '3.0.0') && appc.version.lt(sdk, '3.2.0') && data.command.platform && /^ios|iphone$/.test(data.command.platform.name)) {
Expand All @@ -30,6 +44,5 @@ exports.init = function (logger, config, cli) {
data.command.platform.options['deploy-type'].values = ['production'];
}
}
finished();
});
};
25 changes: 18 additions & 7 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,18 @@ CLI.prototype.initCompletion = function initCompletion(program) {
* 5. executeCommand()
*
* @param {Function} [done] - Callback when the function finishes
* @emits CLI#cli:go
*/
CLI.prototype.go = function (done) {
series(this, [
'loadCommand',
'processCompletion',
'login',
'validate',
'executeCommand'
], done || function(){});
this.emit('cli:go', { cli: this }, function () {
series(this, [
'loadCommand',
'processCompletion',
'login',
'validate',
'executeCommand'
], done || function(){});
}.bind(this));
};

/**
Expand Down Expand Up @@ -350,6 +353,7 @@ CLI.prototype.loadCommand = function loadCommand(next) {
this.logger.log(err.stack.trim().grey + '\n');
process.exit(1);
}

this.emit('cli:command-loaded', { cli: this, command: command }, next);
}.bind(this));
};
Expand Down Expand Up @@ -1056,6 +1060,13 @@ CLI.prototype.prompt = function prompt(items, done) {

/* Events */

/**
* Fired before the CLI begins the main process flow.
* @event CLI#cli:go
* @type {object}
* @property {Object} cli - The CLI instance.
*/

/**
* Fired after the command file has been require()'d.
* @event CLI#cli:command-loaded
Expand Down
43 changes: 31 additions & 12 deletions lib/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ Hook.prototype.fireHook = Hook.prototype.emit;
*/
Hook.prototype.createHook = function createHook(name, ctx, fn) {
var dataPayload = {},
hooks = this.hooks;
hooks = this.hooks,
_t = this;

if (typeof ctx == 'function') {
fn = ctx;
Expand All @@ -186,32 +187,42 @@ Hook.prototype.createHook = function createHook(name, ctx, fn) {
async.series(pres.map(function (pre) {
return function (cb) {
if (pre.length >= 2) {
pre.call(ctx, data, function (e) {
e && (data = e);
cb();
pre.call(ctx, data, function (err, _data) {
_data && (data = _data);
cb(err);
});
} else {
pre.call(ctx, data);
cb();
}
};
}), function () {
var next = function (result) {
data.result = result;
}), function (preErr) {
if (preErr) {
callback && callback(preErr);
return;
}

function next() {
data.result = Array.prototype.slice.call(arguments);
// call all post filters
async.series(posts.map(function (post) {
return function (cb) {
if (post.length >= 2) {
post.call(ctx, data, function (err, _data) {
_data && (data = _data);
cb(err, data);
if (!err && _data && typeof _data == 'object' && _data.type) {
data = _data;
}
cb(err);
});
} else {
cb(post.call(ctx, data), data);
post.call(ctx, data);
cb();
}
};
}), function (err, results) {
callback && callback(err, results, data.result);
}), function (postErr) {
if (callback) {
_t._fireHookCallback(callback, postErr, data);
}
});
};

Expand All @@ -226,3 +237,11 @@ Hook.prototype.createHook = function createHook(name, ctx, fn) {
});
};
};

Hook.prototype._fireHookCallback = function _fireHookCallback(callback, err, data) {
if (err) {
callback(err);
} else {
callback.apply(null, data.result);
}
};