-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·94 lines (68 loc) · 2.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env node
const commander = require('commander')
const fs = require('fs-extra')
const { exec, spawn } = require('child_process')
commander
.version(require('./package.json').version)
.description('Quick React-Native Run-IOS')
.action(start)
commander
.option('-l --latest', 'run latest used device')
commander.parse(process.argv)
function start({ latest }) {
if (require('os').platform() == 'darwin') {
const inquirer = require('inquirer')
const getIOSDeviceProcess = exec('xcrun simctl list --json devices available')
let message = ''
getIOSDeviceProcess.stdout.on('data', jsonString => message += jsonString)
getIOSDeviceProcess.on('exit', () => {
const json = JSON.parse(message)
let choices = [ 'Physical Device' ]
for (const key of Object.keys(json.devices)) {
if (key.startsWith('com.apple.CoreSimulator.SimRuntime.iOS')) {
choices = choices.concat(json.devices[key].map(({ name }) => name))
}
}
const userPickHistoryFilePath = __dirname + '/user-pick-history.json'
let userPickHistory = []
try {
const lastUserPickHistory = require(userPickHistoryFilePath)
userPickHistory = lastUserPickHistory
} catch(err) { }
for (let index = userPickHistory.length - 1; index >= 0; index--) {
const item = userPickHistory[index]
const itemIndex = choices.indexOf(item)
if (itemIndex != -1) {
choices.splice(choices.indexOf(item), 1)
choices.unshift(item)
}
}
if (latest) {
runDevice(choices[0])
} else {
inquirer.prompt([
{
type: 'list',
name: 'device',
message: 'Quick React-Native Run-IOS',
choices
}
]).then(({device}) => {
const deviceIndex = userPickHistory.indexOf(device)
if (deviceIndex != -1) {
userPickHistory.splice(deviceIndex, 1)
}
userPickHistory.unshift(device)
fs.writeFileSync(userPickHistoryFilePath, JSON.stringify(userPickHistory, null, 2))
runDevice(device)
})
}
})
} else {
console.log('Sorry, run-ios is not supported with your operating system')
}
}
function runDevice(device) {
const deviceParameter = device == 'Physical Device' ? `--device` : `--simulator="${device}"`
spawn('npx', [`react-native`, `run-ios`, deviceParameter], {stdio: 'inherit', shell: true})
}