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

run.js 11KB

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