Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions lib/simctl-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,33 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

const shell = require('shelljs')
const path = require('path')
const fs = require('fs')
const util = require('util')
const Tail = require('tail').Tail
const { spawnSync } = require('child_process')
const { Tail } = require('tail')

const extensions = {
start: function (deviceid) {
let isAtLeastXcode9 = false

let command = 'xcodebuild -version'
const res = shell.exec(command, { silent: true })
const res = spawnSync('xcodebuild', ['-version'])

// parse output for Xcode version
const versionMatch = /Xcode (.*)/.exec(res.stdout)
if (res.code !== 0 || !versionMatch) {
if (res.status !== 0 || !versionMatch) {
console.error('Unable to parse xcodebuild version.')
return
} else {
isAtLeastXcode9 = (parseInt(versionMatch[1]) >= 9)
isAtLeastXcode9 = (parseInt(versionMatch[1], 10) >= 9)
}

if (isAtLeastXcode9) {
// Xcode 9 or greater
command = util.format('xcrun simctl list -j')
let res = shell.exec(command, { silent: true })
if (res.code !== 0) {
let res = spawnSync('xcrun', ['simctl', 'list', '--json'])
if (res.status !== 0) {
console.error('Could not get device list.')
return
}

const listOutput = JSON.parse(res.stdout)
const device = Object.keys(listOutput.devices)
.reduce(function (acc, key) { return acc.concat(listOutput.devices[key]) }, [])
Expand All @@ -62,26 +59,30 @@ const extensions = {
console.error('Simulator already running.')
return
}
command = util.format('xcrun simctl boot "%s"', deviceid)
res = shell.exec(command, { silent: true })

if (res.code !== 0) {
res = spawnSync('xcrun', ['simctl', 'boot', deviceid])
if (res.status !== 0) {
console.error(`Could not boot simulator ${deviceid}`)
return
}

command = 'open `xcode-select -p`/Applications/Simulator.app'
return shell.exec(command, { silent: true })
res = spawnSync('xcode-select', ['-p'])
if (res.status !== 0) {
console.error('Failed to get Xcode path')
return
}

const simulatorPath = path.join(res.stdout, 'Applications', 'Simulator.app')
return spawnSync('open', ['-a', simulatorPath])
} else {
// Xcode 8 or older
command = util.format('xcrun instruments -w "%s"', deviceid)
return shell.exec(command, { silent: true })
return spawnSync('xcrun', ['instruments', '-w', deviceid])
}
},

log: function (deviceid, filepath) {
const tail = new Tail(
path.join(process.env.HOME, 'Library/Logs/CoreSimulator', deviceid, 'system.log')
path.join(process.env.HOME, 'Library', 'Logs', 'CoreSimulator', deviceid, 'system.log')
)

tail.on('line', function (data) {
Expand Down
Loading