123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
-
-
- var shell = require('shelljs');
- var path = require('path');
- var fs = require('fs');
- var util = require('util');
- var Tail = require('tail').Tail;
-
- var extensions = {
- start: function (deviceid) {
- var is_at_least_xcode_9 = false;
-
- var command = 'xcodebuild -version';
- var output = shell.exec(command, { silent: true }).output;
-
-
- var versionMatch = /Xcode (.*)/.exec(output);
- if (!versionMatch) {
- console.log('Unable to parse xcodebuild version.');
- return;
- } else {
- is_at_least_xcode_9 = (parseInt(versionMatch[1]) >= 9);
- }
-
- if (is_at_least_xcode_9) {
-
- command = util.format('xcrun simctl list -j');
- var res = shell.exec(command, { silent: true });
- if (res.code !== 0) {
- console.log('Unable to parse xcodebuild version.');
- return;
- }
- var list_output = JSON.parse(res.output);
- var device = Object.keys(list_output.devices)
- .reduce(function (acc, key) { return acc.concat(list_output.devices[key]); }, [])
- .find(function (el) { return el.udid === deviceid; });
-
- if (device.state === 'Booted') {
-
- console.log('Simulator already running.');
- return;
- }
- command = util.format('xcrun simctl boot "%s"', deviceid);
- shell.exec(command, { silent: true });
- command = 'open `xcode-select -p`/Applications/Simulator.app';
- return shell.exec(command, { silent: true });
- } else {
-
- command = util.format('xcrun instruments -w "%s"', deviceid);
- return shell.exec(command, { silent: true });
- }
- },
-
- log: function (deviceid, filepath) {
- var tail = new Tail(
- path.join(process.env.HOME, 'Library/Logs/CoreSimulator', deviceid, 'system.log')
- );
-
- tail.on('line', function (data) {
- if (filepath) {
- fs.appendFile(filepath, data + '\n', function (error) {
- if (error) {
- console.error('ERROR: ', error);
- throw error;
- }
- });
- } else {
- console.log(data);
- }
- });
-
- tail.on('error', function (error) {
- console.error('ERROR: ', error);
- });
-
- return tail;
- }
- };
-
- exports = module.exports = extensions;
|