No Description

simctl-extensions.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Shazron Abdullah.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. var shell = require('shelljs');
  21. var path = require('path');
  22. var fs = require('fs');
  23. var util = require('util');
  24. var Tail = require('tail').Tail;
  25. var extensions = {
  26. start: function (deviceid) {
  27. var is_at_least_xcode_9 = false;
  28. var command = 'xcodebuild -version';
  29. var output = shell.exec(command, { silent: true }).output;
  30. // parse output for Xcode version
  31. var versionMatch = /Xcode (.*)/.exec(output);
  32. if (!versionMatch) {
  33. console.log('Unable to parse xcodebuild version.');
  34. return;
  35. } else {
  36. is_at_least_xcode_9 = (parseInt(versionMatch[1]) >= 9);
  37. }
  38. if (is_at_least_xcode_9) {
  39. // Xcode 9 or greater
  40. command = util.format('xcrun simctl list -j');
  41. var res = shell.exec(command, { silent: true });
  42. if (res.code !== 0) {
  43. console.log('Unable to parse xcodebuild version.');
  44. return;
  45. }
  46. var list_output = JSON.parse(res.output);
  47. var device = Object.keys(list_output.devices)
  48. .reduce(function (acc, key) { return acc.concat(list_output.devices[key]); }, [])
  49. .find(function (el) { return el.udid === deviceid; });
  50. if (device.state === 'Booted') {
  51. // no need to launch the emulator, it is already running
  52. console.log('Simulator already running.');
  53. return;
  54. }
  55. command = util.format('xcrun simctl boot "%s"', deviceid);
  56. shell.exec(command, { silent: true });
  57. command = 'open `xcode-select -p`/Applications/Simulator.app';
  58. return shell.exec(command, { silent: true });
  59. } else {
  60. // Xcode 8 or older
  61. command = util.format('xcrun instruments -w "%s"', deviceid);
  62. return shell.exec(command, { silent: true });
  63. }
  64. },
  65. log: function (deviceid, filepath) {
  66. var tail = new Tail(
  67. path.join(process.env.HOME, 'Library/Logs/CoreSimulator', deviceid, 'system.log')
  68. );
  69. tail.on('line', function (data) {
  70. if (filepath) {
  71. fs.appendFile(filepath, data + '\n', function (error) {
  72. if (error) {
  73. console.error('ERROR: ', error);
  74. throw error;
  75. }
  76. });
  77. } else {
  78. console.log(data);
  79. }
  80. });
  81. tail.on('error', function (error) {
  82. console.error('ERROR: ', error);
  83. });
  84. return tail;
  85. }
  86. };
  87. exports = module.exports = extensions;