run.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /*jshint node: true*/
  18. var Q = require('q'),
  19. nopt = require('nopt'),
  20. path = require('path'),
  21. build = require('./build'),
  22. spawn = require('./spawn'),
  23. check_reqs = require('./check_reqs');
  24. var cordovaPath = path.join(__dirname, '..');
  25. var projectPath = path.join(__dirname, '..', '..');
  26. module.exports.run = function (argv) {
  27. // parse args here
  28. // --debug and --release args not parsed here
  29. // but still valid since they can be passed down to build command
  30. var args = nopt({
  31. // "archs": String, // TODO: add support for building different archs
  32. 'list': Boolean,
  33. 'nobuild': Boolean,
  34. 'device': Boolean, 'emulator': Boolean, 'target': String
  35. }, {}, argv);
  36. // Validate args
  37. if (args.device && args.emulator) {
  38. return Q.reject('Only one of "device"/"emulator" options should be specified');
  39. }
  40. // validate target device for ios-sim
  41. // Valid values for "--target" (case sensitive):
  42. var validTargets = ['iPhone-4s', 'iPhone-5', 'iPhone-5s', 'iPhone-6-Plus', 'iPhone-6',
  43. 'iPad-2', 'iPad-Retina', 'iPad-Air', 'Resizable-iPhone', 'Resizable-iPad'];
  44. if (!(args.device) && args.target && validTargets.indexOf(args.target.split(',')[0]) < 0 ) {
  45. return Q.reject(args.target + ' is not a valid target for emulator');
  46. }
  47. // support for CB-8168 `cordova/run --list`
  48. if (args.list) {
  49. if (args.device) return listDevices();
  50. if (args.emulator) return listEmulators();
  51. // if no --device or --emulator flag is specified, list both devices and emulators
  52. return listDevices().then(function () {
  53. return listEmulators();
  54. });
  55. }
  56. // check for either ios-sim or ios-deploy is available
  57. // depending on arguments provided
  58. var checkTools = args.device ? check_reqs.check_ios_deploy() : check_reqs.check_ios_sim();
  59. return checkTools.then(function () {
  60. // if --nobuild isn't specified then build app first
  61. if (!args.nobuild) {
  62. return build.run(argv);
  63. }
  64. }).then(function () {
  65. return build.findXCodeProjectIn(projectPath);
  66. }).then(function (projectName) {
  67. var appPath = path.join(projectPath, 'build', (args.device ? 'device' : 'emulator'), projectName + '.app');
  68. // select command to run and arguments depending whether
  69. // we're running on device/emulator
  70. if (args.device) {
  71. return checkDeviceConnected().then(function () {
  72. return deployToDevice(appPath);
  73. }, function () {
  74. // if device connection check failed use emulator then
  75. return deployToSim(appPath, args.target);
  76. });
  77. } else {
  78. return deployToSim(appPath, args.target);
  79. }
  80. });
  81. };
  82. /**
  83. * Checks if any iOS device is connected
  84. * @return {Promise} Fullfilled when any device is connected, rejected otherwise
  85. */
  86. function checkDeviceConnected() {
  87. return spawn('ios-deploy', ['-c']);
  88. }
  89. /**
  90. * Deploy specified app package to connected device
  91. * using ios-deploy command
  92. * @param {String} appPath Path to application package
  93. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  94. */
  95. function deployToDevice(appPath) {
  96. // Deploying to device...
  97. return spawn('ios-deploy', ['-d', '-b', appPath]);
  98. }
  99. /**
  100. * Deploy specified app package to ios-sim simulator
  101. * @param {String} appPath Path to application package
  102. * @param {String} target Target device type
  103. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  104. */
  105. function deployToSim(appPath, target) {
  106. // Select target device for emulator. Default is 'iPhone-6'
  107. if (!target) {
  108. target = 'iPhone-6';
  109. console.log('No target specified for emulator. Deploying to ' + target + ' simulator');
  110. }
  111. var logPath = path.join(cordovaPath, 'console.log');
  112. var simArgs = ['launch', appPath,
  113. '--devicetypeid', 'com.apple.CoreSimulator.SimDeviceType.' + target,
  114. // We need to redirect simulator output here to use cordova/log command
  115. // TODO: Is there any other way to get emulator's output to use in log command?
  116. '--stderr', logPath, '--stdout', logPath,
  117. '--exit'];
  118. return spawn('ios-sim', simArgs);
  119. }
  120. function listDevices() {
  121. return require('./list-devices').run()
  122. .then(function (devices) {
  123. console.log('Available iOS Devices:');
  124. devices.forEach(function (device) {
  125. console.log('\t' + device);
  126. });
  127. });
  128. }
  129. function listEmulators() {
  130. return require('./list-emulator-images').run()
  131. .then(function (emulators) {
  132. console.log('Available iOS Virtual Devices:');
  133. emulators.forEach(function (emulator) {
  134. console.log('\t' + emulator);
  135. });
  136. });
  137. }
  138. module.exports.help = function () {
  139. console.log('\nUsage: run [ --device | [ --emulator [ --target=<id> ] ] ] [ --debug | --release | --nobuild ]');
  140. // TODO: add support for building different archs
  141. // console.log(" [ --archs=\"<list of target architectures>\" ] ");
  142. console.log(' --device : Deploys and runs the project on the connected device.');
  143. console.log(' --emulator : Deploys and runs the project on an emulator.');
  144. console.log(' --target=<id> : Deploys and runs the project on the specified target.');
  145. console.log(' --debug : Builds project in debug mode. (Passed down to build command, if necessary)');
  146. console.log(' --release : Builds project in release mode. (Passed down to build command, if necessary)');
  147. console.log(' --nobuild : Uses pre-built package, or errors if project is not built.');
  148. // TODO: add support for building different archs
  149. // console.log(" --archs : Specific chip architectures (`anycpu`, `arm`, `x86`, `x64`).");
  150. console.log('');
  151. console.log('Examples:');
  152. console.log(' run');
  153. console.log(' run --device');
  154. console.log(' run --emulator --target=\"iPhone-6-Plus\"');
  155. console.log(' run --device --release');
  156. console.log(' run --emulator --debug');
  157. console.log('');
  158. process.exit(0);
  159. };