diff --git a/bin/create b/bin/create index 2052a27e..b1e4d5a8 100755 --- a/bin/create +++ b/bin/create @@ -28,7 +28,7 @@ var argv = require('nopt')({ 'shared' : Boolean, 'link' : Boolean, 'activity-name' : [String, undefined] -}); +}, { 'd' : '--verbose' }); if (argv.help || argv.argv.remain.length === 0) { console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'create')) + ' [] [--activity-name ] [--link]'); @@ -53,4 +53,6 @@ var options = { activityName: argv['activity-name'] }; +require('./templates/cordova/loggingHelper').adjustLoggerLevel(argv); + Api.createPlatform(argv.argv.remain[0], config, options).done(); diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js index ad6f71c6..0a207e4d 100644 --- a/bin/templates/cordova/Api.js +++ b/bin/templates/cordova/Api.js @@ -29,11 +29,25 @@ var AndroidProject = require('./lib/AndroidProject'); var PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger; var PluginInfoProvider = require('cordova-common').PluginInfoProvider; -var ConsoleLogger = require('./lib/ConsoleLogger'); var pluginHandlers = require('./lib/pluginHandlers'); +var CordovaLogger = require('cordova-common').CordovaLogger; +var selfEvents = require('cordova-common').events; var PLATFORM = 'android'; +function setupEvents(externalEventEmitter) { + if (externalEventEmitter) { + // This will make the platform internal events visible outside + selfEvents.forwardEventsTo(externalEventEmitter); + return externalEventEmitter; + } + + // There is no logger if external emitter is not present, + // so attach a console logger + CordovaLogger.get().subscribe(selfEvents); + return selfEvents; +} + /** * Class, that acts as abstraction over particular platform. Encapsulates the * platform's properties and methods. @@ -48,9 +62,8 @@ var PLATFORM = 'android'; function Api(platform, platformRootDir, events) { this.platform = PLATFORM; this.root = path.resolve(__dirname, '..'); - this.events = events || ConsoleLogger.get(); - // NOTE: trick to share one EventEmitter instance across all js code - require('cordova-common').events = this.events; + + setupEvents(events); this._platformJson = PlatformJson.load(this.root, platform); this._pluginInfoProvider = new PluginInfoProvider(); @@ -91,8 +104,10 @@ function Api(platform, platformRootDir, events) { * instance or rejected with CordovaError. */ Api.createPlatform = function (destination, config, options, events) { + events = setupEvents(events); + return require('../../lib/create') - .create(destination, config, options, events || ConsoleLogger.get()) + .create(destination, config, options, events) .then(function (destination) { var PlatformApi = require(path.resolve(destination, 'cordova/Api')); return new PlatformApi(PLATFORM, destination, events); @@ -116,8 +131,10 @@ Api.createPlatform = function (destination, config, options, events) { * instance or rejected with CordovaError. */ Api.updatePlatform = function (destination, options, events) { + events = setupEvents(events); + return require('../../lib/create') - .update(destination, options, events || ConsoleLogger.get()) + .update(destination, options, events) .then(function (destination) { var PlatformApi = require(path.resolve(destination, 'cordova/Api')); return new PlatformApi('android', destination, events); @@ -220,7 +237,7 @@ Api.prototype.addPlugin = function (plugin, installOptions) { .save_all(); if (plugin.getFrameworks(self.platform).length > 0) { - self.events.emit('verbose', 'Updating build files since android plugin contained '); + selfEvents.emit('verbose', 'Updating build files since android plugin contained '); require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); } @@ -278,7 +295,7 @@ Api.prototype.removePlugin = function (plugin, uninstallOptions) { .save_all(); if (plugin.getFrameworks(self.platform).length > 0) { - self.events.emit('verbose', 'Updating build files since android plugin contained '); + selfEvents.emit('verbose', 'Updating build files since android plugin contained '); require('./lib/builders/builders').getBuilder('gradle').prepBuildFiles(); } diff --git a/bin/templates/cordova/build b/bin/templates/cordova/build index de86a36a..222e84a0 100755 --- a/bin/templates/cordova/build +++ b/bin/templates/cordova/build @@ -41,6 +41,8 @@ var buildOpts = nopt({ // Make buildOptions compatible with PlatformApi build method spec buildOpts.argv = buildOpts.argv.original; +require('./loggingHelper').adjustLoggerLevel(buildOpts); + new Api().build(buildOpts) .catch(function(err) { console.error(err.stack); diff --git a/bin/templates/cordova/clean b/bin/templates/cordova/clean index 1f4a53d2..dd858495 100755 --- a/bin/templates/cordova/clean +++ b/bin/templates/cordova/clean @@ -21,6 +21,7 @@ var Api = require('./Api'); var path = require('path'); +var nopt = require('nopt'); // Support basic help commands if(['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) >= 0) { @@ -29,7 +30,18 @@ if(['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) >= process.exit(0); } -new Api().clean({argv: process.argv.slice(2)}) +// Do some basic argument parsing +var opts = nopt({ + 'verbose' : Boolean, + 'silent' : Boolean +}, { 'd' : '--verbose' }); + +// Make buildOptions compatible with PlatformApi clean method spec +opts.argv = opts.argv.original; + +require('./loggingHelper').adjustLoggerLevel(opts); + +new Api().clean(opts) .catch(function(err) { console.error(err.stack); process.exit(2); diff --git a/bin/templates/cordova/lib/ConsoleLogger.js b/bin/templates/cordova/lib/ConsoleLogger.js deleted file mode 100644 index cee2dc10..00000000 --- a/bin/templates/cordova/lib/ConsoleLogger.js +++ /dev/null @@ -1,75 +0,0 @@ -/** - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ - -var loggerInstance; -var util = require('util'); -var EventEmitter = require('events').EventEmitter; -var CordovaError = require('cordova-common').CordovaError; - -/** - * @class ConsoleLogger - * @extends EventEmitter - * - * Implementing basic logging for platform. Inherits regular NodeJS - * EventEmitter. All events, emitted on this class instance are immediately - * logged to console. - * - * Also attaches handler to process' uncaught exceptions, so these exceptions - * logged to console similar to regular error events. - */ -function ConsoleLogger() { - EventEmitter.call(this); - - var isVerbose = process.argv.indexOf('-d') >= 0 || process.argv.indexOf('--verbose') >= 0; - // For CordovaError print only the message without stack trace unless we - // are in a verbose mode. - process.on('uncaughtException', function(err){ - if ((err instanceof CordovaError) && isVerbose) { - console.error(err.stack); - } else { - console.error(err.message); - } - process.exit(1); - }); - - this.on('results', console.log); - this.on('verbose', function () { - if (isVerbose) - console.log.apply(console, arguments); - }); - this.on('info', console.log); - this.on('log', console.log); - this.on('warn', console.warn); -} -util.inherits(ConsoleLogger, EventEmitter); - -/** - * Returns already instantiated/newly created instance of ConsoleLogger class. - * This method should be used instead of creating ConsoleLogger directly, - * otherwise we'll get multiple handlers attached to process' - * uncaughtException - * - * @return {ConsoleLogger} New or already created instance of ConsoleLogger - */ -ConsoleLogger.get = function () { - loggerInstance = loggerInstance || new ConsoleLogger(); - return loggerInstance; -}; - -module.exports = ConsoleLogger; diff --git a/bin/templates/cordova/lib/build.js b/bin/templates/cordova/lib/build.js index e1263ef7..1abaa525 100644 --- a/bin/templates/cordova/lib/build.js +++ b/bin/templates/cordova/lib/build.js @@ -149,17 +149,16 @@ module.exports.runClean = function(options) { module.exports.run = function(options, optResolvedTarget) { var opts = parseOpts(options, optResolvedTarget, this.root); var builder = builders.getBuilder(opts.buildMethod); - var self = this; return builder.prepEnv(opts) .then(function() { if (opts.prepEnv) { - self.events.emit('verbose', 'Build file successfully prepared.'); + events.emit('verbose', 'Build file successfully prepared.'); return; } return builder.build(opts) .then(function() { var apkPaths = builder.findOutputApks(opts.buildType, opts.arch); - self.events.emit('log', 'Built the following apk(s): \n\t' + apkPaths.join('\n\t')); + events.emit('log', 'Built the following apk(s): \n\t' + apkPaths.join('\n\t')); return { apkPaths: apkPaths, buildType: opts.buildType, diff --git a/bin/templates/cordova/lib/prepare.js b/bin/templates/cordova/lib/prepare.js index 3fba9fdb..474ffd62 100644 --- a/bin/templates/cordova/lib/prepare.js +++ b/bin/templates/cordova/lib/prepare.js @@ -45,7 +45,7 @@ module.exports.prepare = function (cordovaProject) { handleSplashes(cordovaProject.projectConfig, self.root); }) .then(function () { - self.events.emit('verbose', 'updated project successfully'); + events.emit('verbose', 'updated project successfully'); }); }; diff --git a/bin/templates/cordova/lib/run.js b/bin/templates/cordova/lib/run.js index 3cc5c0d5..027a482f 100644 --- a/bin/templates/cordova/lib/run.js +++ b/bin/templates/cordova/lib/run.js @@ -25,7 +25,8 @@ var path = require('path'), build = require('./build'), emulator = require('./emulator'), device = require('./device'), - Q = require('q'); + Q = require('q'), + events = require('cordova-common').events; function getInstallTarget(runOptions) { var install_target; @@ -62,10 +63,10 @@ function getInstallTarget(runOptions) { return device.list() .then(function(device_list) { if (device_list.length > 0) { - self.events.emit('warn', 'No target specified, deploying to device \'' + device_list[0] + '\'.'); + events.emit('warn', 'No target specified, deploying to device \'' + device_list[0] + '\'.'); install_target = device_list[0]; } else { - self.events.emit('warn', 'No target specified, deploying to emulator'); + events.emit('warn', 'No target specified, deploying to emulator'); install_target = '--emulator'; } }); diff --git a/bin/templates/cordova/loggingHelper.js b/bin/templates/cordova/loggingHelper.js new file mode 100644 index 00000000..32b2ee0a --- /dev/null +++ b/bin/templates/cordova/loggingHelper.js @@ -0,0 +1,18 @@ +var CordovaLogger = require('cordova-common').CordovaLogger; + +module.exports = { + adjustLoggerLevel: function (opts) { + if (opts instanceof Array) { + opts.silent = opts.indexOf('--silent') !== -1; + opts.verbose = opts.indexOf('--verbose') !== -1; + } + + if (opts.silent) { + CordovaLogger.get().setLevel('error'); + } + + if (opts.verbose) { + CordovaLogger.get().setLevel('verbose'); + } + } +}; diff --git a/bin/templates/cordova/run b/bin/templates/cordova/run index 1a7835ef..9544c1dc 100755 --- a/bin/templates/cordova/run +++ b/bin/templates/cordova/run @@ -44,6 +44,8 @@ var runOpts = nopt({ // Make runOptions compatible with PlatformApi run method spec runOpts.argv = runOpts.argv.remain; +require('./loggingHelper').adjustLoggerLevel(runOpts); + new Api().run(runOpts) .catch(function(err) { console.error(err, err.stack); diff --git a/bin/update b/bin/update index 861c8d04..c51b7ff2 100755 --- a/bin/update +++ b/bin/update @@ -24,7 +24,7 @@ var args = require('nopt')({ 'link': Boolean, 'shared': Boolean, 'help': Boolean -}); +}, { 'd' : '--verbose' }); if (args.help || args.argv.remain.length === 0) { console.log('Usage: ' + path.relative(process.cwd(), path.join(__dirname, 'update')) + ' [--link]'); @@ -32,4 +32,6 @@ if (args.help || args.argv.remain.length === 0) { process.exit(1); } +require('./templates/cordova/loggingHelper').adjustLoggerLevel(args); + Api.updatePlatform(args.argv.remain[0], {link: (args.link || args.shared)}).done();