run.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env node
  2. /*
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. var path = require('path');
  19. var emulator = require('./emulator');
  20. var device = require('./device');
  21. var Q = require('q');
  22. var PackageType = require('./PackageType');
  23. var events = require('cordova-common').events;
  24. function getInstallTarget (runOptions) {
  25. var install_target;
  26. if (runOptions.target) {
  27. install_target = runOptions.target;
  28. } else if (runOptions.device) {
  29. install_target = '--device';
  30. } else if (runOptions.emulator) {
  31. install_target = '--emulator';
  32. }
  33. return install_target;
  34. }
  35. /**
  36. * Runs the application on a device if available. If no device is found, it will
  37. * use a started emulator. If no started emulators are found it will attempt
  38. * to start an avd. If no avds are found it will error out.
  39. *
  40. * @param {Object} runOptions various run/build options. See Api.js build/run
  41. * methods for reference.
  42. *
  43. * @return {Promise}
  44. */
  45. module.exports.run = function (runOptions) {
  46. runOptions = runOptions || {};
  47. var self = this;
  48. var install_target = getInstallTarget(runOptions);
  49. return Q().then(function () {
  50. if (!install_target) {
  51. // no target given, deploy to device if available, otherwise use the emulator.
  52. return device.list().then(function (device_list) {
  53. if (device_list.length > 0) {
  54. events.emit('warn', 'No target specified, deploying to device \'' + device_list[0] + '\'.');
  55. install_target = device_list[0];
  56. } else {
  57. events.emit('warn', 'No target specified and no devices found, deploying to emulator');
  58. install_target = '--emulator';
  59. }
  60. });
  61. }
  62. }).then(function () {
  63. if (install_target === '--device') {
  64. return device.resolveTarget(null);
  65. } else if (install_target === '--emulator') {
  66. // Give preference to any already started emulators. Else, start one.
  67. return emulator.list_started().then(function (started) {
  68. return started && started.length > 0 ? started[0] : emulator.start();
  69. }).then(function (emulatorId) {
  70. return emulator.resolveTarget(emulatorId);
  71. });
  72. }
  73. // They specified a specific device/emulator ID.
  74. return device.list().then(function (devices) {
  75. if (devices.indexOf(install_target) > -1) {
  76. return device.resolveTarget(install_target);
  77. }
  78. return emulator.list_started().then(function (started_emulators) {
  79. if (started_emulators.indexOf(install_target) > -1) {
  80. return emulator.resolveTarget(install_target);
  81. }
  82. return emulator.list_images().then(function (avds) {
  83. // if target emulator isn't started, then start it.
  84. for (var avd in avds) {
  85. if (avds[avd].name === install_target) {
  86. return emulator.start(install_target).then(function (emulatorId) {
  87. return emulator.resolveTarget(emulatorId);
  88. });
  89. }
  90. }
  91. return Q.reject('Target \'' + install_target + '\' not found, unable to run project');
  92. });
  93. });
  94. });
  95. }).then(function (resolvedTarget) {
  96. return new Promise((resolve) => {
  97. const builder = require('./builders/builders').getBuilder();
  98. const buildOptions = require('./build').parseBuildOptions(runOptions, null, self.root);
  99. // Android app bundles cannot be deployed directly to the device
  100. if (buildOptions.packageType === PackageType.BUNDLE) {
  101. const packageTypeErrorMessage = 'Package type "bundle" is not supported during cordova run.';
  102. events.emit('error', packageTypeErrorMessage);
  103. throw packageTypeErrorMessage;
  104. }
  105. resolve(builder.fetchBuildResults(buildOptions.buildType, buildOptions.arch));
  106. }).then(function (buildResults) {
  107. if (resolvedTarget && resolvedTarget.isEmulator) {
  108. return emulator.wait_for_boot(resolvedTarget.target).then(function () {
  109. return emulator.install(resolvedTarget, buildResults);
  110. });
  111. }
  112. return device.install(resolvedTarget, buildResults);
  113. });
  114. });
  115. };
  116. module.exports.help = function () {
  117. console.log('Usage: ' + path.relative(process.cwd(), process.argv[1]) + ' [options]');
  118. console.log('Build options :');
  119. console.log(' --debug : Builds project in debug mode');
  120. console.log(' --release : Builds project in release mode');
  121. console.log(' --nobuild : Runs the currently built project without recompiling');
  122. console.log('Deploy options :');
  123. console.log(' --device : Will deploy the built project to a device');
  124. console.log(' --emulator : Will deploy the built project to an emulator if one exists');
  125. console.log(' --target=<target_id> : Installs to the target with the specified id.');
  126. process.exit(0);
  127. };