Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

run.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. var Q = require('q');
  18. var path = require('path');
  19. var build = require('./build');
  20. var shell = require('shelljs');
  21. var superspawn = require('cordova-common').superspawn;
  22. var check_reqs = require('./check_reqs');
  23. var fs = require('fs-extra');
  24. var events = require('cordova-common').events;
  25. var cordovaPath = path.join(__dirname, '..');
  26. var projectPath = path.join(__dirname, '..', '..');
  27. module.exports.run = function (runOptions) {
  28. // Validate args
  29. if (runOptions.device && runOptions.emulator) {
  30. return Q.reject('Only one of "device"/"emulator" options should be specified');
  31. }
  32. // support for CB-8168 `cordova/run --list`
  33. if (runOptions.list) {
  34. if (runOptions.device) return module.exports.listDevices();
  35. if (runOptions.emulator) return module.exports.listEmulators();
  36. // if no --device or --emulator flag is specified, list both devices and emulators
  37. return module.exports.listDevices().then(function () {
  38. return module.exports.listEmulators();
  39. });
  40. }
  41. var useDevice = !!runOptions.device;
  42. return require('./list-devices').run()
  43. .then(function (devices) {
  44. if (devices.length > 0 && !(runOptions.emulator)) {
  45. useDevice = true;
  46. // we also explicitly set device flag in options as we pass
  47. // those parameters to other api (build as an example)
  48. runOptions.device = true;
  49. return check_reqs.check_ios_deploy();
  50. }
  51. }).then(function () {
  52. if (!runOptions.nobuild) {
  53. return build.run(runOptions);
  54. } else {
  55. return Q.resolve();
  56. }
  57. }).then(function () {
  58. return build.findXCodeProjectIn(projectPath);
  59. }).then(function (projectName) {
  60. var appPath = path.join(projectPath, 'build', 'emulator', projectName + '.app');
  61. var buildOutputDir = path.join(projectPath, 'build', 'device');
  62. // select command to run and arguments depending whether
  63. // we're running on device/emulator
  64. if (useDevice) {
  65. return module.exports.checkDeviceConnected()
  66. .then(function () {
  67. // Unpack IPA
  68. var ipafile = path.join(buildOutputDir, projectName + '.ipa');
  69. // unpack the existing platform/ios/build/device/appname.ipa (zipfile), will create a Payload folder
  70. return superspawn.spawn('unzip', [ '-o', '-qq', ipafile ], { cwd: buildOutputDir, printCommand: true, stdio: 'inherit' });
  71. })
  72. .then(function () {
  73. // Uncompress IPA (zip file)
  74. var appFileInflated = path.join(buildOutputDir, 'Payload', projectName + '.app');
  75. var appFile = path.join(buildOutputDir, projectName + '.app');
  76. var payloadFolder = path.join(buildOutputDir, 'Payload');
  77. // delete the existing platform/ios/build/device/appname.app
  78. fs.removeSync(appFile);
  79. // move the platform/ios/build/device/Payload/appname.app to parent
  80. shell.mv('-f', appFileInflated, buildOutputDir);
  81. // delete the platform/ios/build/device/Payload folder
  82. shell.rm('-rf', payloadFolder);
  83. return null;
  84. })
  85. .then(function () {
  86. appPath = path.join(projectPath, 'build', 'device', projectName + '.app');
  87. var extraArgs = [];
  88. if (runOptions.argv) {
  89. // argv.slice(2) removes node and run.js, filterSupportedArgs removes the run.js args
  90. extraArgs = module.exports.filterSupportedArgs(runOptions.argv.slice(2));
  91. }
  92. return module.exports.deployToDevice(appPath, runOptions.target, extraArgs);
  93. }, function () {
  94. // if device connection check failed use emulator then
  95. return module.exports.deployToSim(appPath, runOptions.target);
  96. });
  97. } else {
  98. return module.exports.deployToSim(appPath, runOptions.target);
  99. }
  100. });
  101. };
  102. module.exports.filterSupportedArgs = filterSupportedArgs;
  103. module.exports.checkDeviceConnected = checkDeviceConnected;
  104. module.exports.deployToDevice = deployToDevice;
  105. module.exports.deployToSim = deployToSim;
  106. module.exports.startSim = startSim;
  107. module.exports.listDevices = listDevices;
  108. module.exports.listEmulators = listEmulators;
  109. /**
  110. * Filters the args array and removes supported args for the 'run' command.
  111. *
  112. * @return {Array} array with unsupported args for the 'run' command
  113. */
  114. function filterSupportedArgs (args) {
  115. var filtered = [];
  116. var sargs = ['--device', '--emulator', '--nobuild', '--list', '--target', '--debug', '--release'];
  117. var re = new RegExp(sargs.join('|'));
  118. args.forEach(function (element) {
  119. // supported args not found, we add
  120. // we do a regex search because --target can be "--target=XXX"
  121. if (element.search(re) === -1) {
  122. filtered.push(element);
  123. }
  124. }, this);
  125. return filtered;
  126. }
  127. /**
  128. * Checks if any iOS device is connected
  129. * @return {Promise} Fullfilled when any device is connected, rejected otherwise
  130. */
  131. function checkDeviceConnected () {
  132. return superspawn.spawn('ios-deploy', ['-c', '-t', '1'], { printCommand: true, stdio: 'inherit' });
  133. }
  134. /**
  135. * Deploy specified app package to connected device
  136. * using ios-deploy command
  137. * @param {String} appPath Path to application package
  138. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  139. */
  140. function deployToDevice (appPath, target, extraArgs) {
  141. events.emit('log', 'Deploying to device');
  142. // Deploying to device...
  143. if (target) {
  144. return superspawn.spawn('ios-deploy', ['--justlaunch', '-d', '-b', appPath, '-i', target].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
  145. } else {
  146. return superspawn.spawn('ios-deploy', ['--justlaunch', '--no-wifi', '-d', '-b', appPath].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
  147. }
  148. }
  149. /**
  150. * Deploy specified app package to ios-sim simulator
  151. * @param {String} appPath Path to application package
  152. * @param {String} target Target device type
  153. * @return {Promise} Resolves when deploy succeeds otherwise rejects
  154. */
  155. function deployToSim (appPath, target) {
  156. events.emit('log', 'Deploying to simulator');
  157. if (!target) {
  158. // Select target device for emulator
  159. return require('./list-emulator-images').run()
  160. .then(function (emulators) {
  161. if (emulators.length > 0) {
  162. target = emulators[0];
  163. }
  164. emulators.forEach(function (emulator) {
  165. if (emulator.indexOf('iPhone') === 0) {
  166. target = emulator;
  167. }
  168. });
  169. events.emit('log', `No target specified for emulator. Deploying to "${target}" simulator.`);
  170. return startSim(appPath, target);
  171. });
  172. } else {
  173. return startSim(appPath, target);
  174. }
  175. }
  176. function startSim (appPath, target) {
  177. var logPath = path.join(cordovaPath, 'console.log');
  178. return iossimLaunch(appPath, 'com.apple.CoreSimulator.SimDeviceType.' + target, logPath, '--exit');
  179. }
  180. function iossimLaunch (appPath, devicetypeid, log, exit) {
  181. var f = path.resolve(path.dirname(require.resolve('ios-sim')), 'bin', 'ios-sim');
  182. var params = ['launch', appPath, '--devicetypeid', devicetypeid, '--log', log, exit];
  183. return superspawn.spawn(f, params, { cwd: projectPath, printCommand: true })
  184. .progress(function (stdio) {
  185. if (stdio.stderr) {
  186. events.emit('error', `[ios-sim] ${stdio.stderr}`);
  187. }
  188. if (stdio.stdout) {
  189. events.emit('log', `[ios-sim] ${stdio.stdout.trim()}`);
  190. }
  191. })
  192. .then(function (result) {
  193. events.emit('log', 'Simulator successfully started via `ios-sim`.');
  194. });
  195. }
  196. function listDevices () {
  197. return require('./list-devices').run()
  198. .then(function (devices) {
  199. events.emit('log', 'Available iOS Devices:');
  200. devices.forEach(function (device) {
  201. events.emit('log', '\t' + device);
  202. });
  203. });
  204. }
  205. function listEmulators () {
  206. return require('./list-emulator-images').run()
  207. .then(function (emulators) {
  208. events.emit('log', 'Available iOS Simulators:');
  209. emulators.forEach(function (emulator) {
  210. events.emit('log', '\t' + emulator);
  211. });
  212. });
  213. }
  214. module.exports.help = function () {
  215. console.log('\nUsage: run [ --device | [ --emulator [ --target=<id> ] ] ] [ --debug | --release | --nobuild ]');
  216. // TODO: add support for building different archs
  217. // console.log(" [ --archs=\"<list of target architectures>\" ] ");
  218. console.log(' --device : Deploys and runs the project on the connected device.');
  219. console.log(' --emulator : Deploys and runs the project on an emulator.');
  220. console.log(' --target=<id> : Deploys and runs the project on the specified target.');
  221. console.log(' --debug : Builds project in debug mode. (Passed down to build command, if necessary)');
  222. console.log(' --release : Builds project in release mode. (Passed down to build command, if necessary)');
  223. console.log(' --nobuild : Uses pre-built package, or errors if project is not built.');
  224. // TODO: add support for building different archs
  225. // console.log(" --archs : Specific chip architectures (`anycpu`, `arm`, `x86`, `x64`).");
  226. console.log('');
  227. console.log('Examples:');
  228. console.log(' run');
  229. console.log(' run --device');
  230. console.log(' run --emulator --target=\"iPhone-6-Plus\"'); /* eslint no-useless-escape : 0 */
  231. console.log(' run --device --release');
  232. console.log(' run --emulator --debug');
  233. console.log('');
  234. process.exit(0);
  235. };