Skip to content

Instantly share code, notes, and snippets.

@wswebcreation
Created November 29, 2017 06:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wswebcreation/f4c82f5207c3fc145c028e8f00a0322b to your computer and use it in GitHub Desktop.
Save wswebcreation/f4c82f5207c3fc145c028e8f00a0322b to your computer and use it in GitHub Desktop.
Brain fart to start an iOS simulator from the command line including starting a test
const { execFileSync, spawnSync } = require('child_process');
const inquirer = require('inquirer');
console.log(`
============================
iOS Simulator CLI Helper
============================
`);
console.log('📲 Retrieve all 📱 📱');
const simulators = execFileSync('xcrun', ['instruments', '-s'], { encoding: 'utf8' })
.replace(/\n$/, '').split('\n')
.filter(simulator => simulator.includes('iPhone') && !simulator.includes('Watch'))
.map((simulator) => {
const device = simulator.match(/(.*?) \((.*?)\) \[(.*?)\]/);
const name = device[1];
const version = device[2];
const udid = device[3];
return { udid, name, version };
});
const formatedSimulatorNameList = simulators
.map(simulator => `${simulator.name} (${simulator.version})`);
console.log('✅ All simulators retrieved\n');
inquirer.prompt([
{
type: 'list',
name: 'simulator',
message: 'On which simulator do you want to run your test?',
choices: formatedSimulatorNameList,
},
])
.then((answer) => {
const devices = JSON.parse(execFileSync('xcrun', ['simctl', 'list', '--json', 'devices'], { encoding: 'utf8' })).devices;
for (const version in devices) {
if (version.indexOf('iOS') !== 0) {
continue;
}
for (const device in devices[version]) {
const simulator = devices[version][device];
const simulatorName = `${simulator.name} (${version.replace(/iOS|\s+/g, '')})`;
if (simulator.availability !== '(available)') {
continue;
}
if (simulator.state === 'Booted' && simulatorName !== answer.simulator) {
console.log(`
=========================================================
☝🏻 The ${simulatorName} is already opened.
It will be closed.
=========================================================
`);
execFileSync('xcrun', ['simctl', 'shutdown', 'all'], { encoding: 'utf8' });
console.log(`✅ ${simulatorName} has been shut down.\n`);
}
}
}
const simulator = answer.simulator.split('(');
const iphone = simulator[0].replace(/\s+$/g, '');
const os = simulator[1].replace(/\s+$|\(|\)/g, '').split('.');
console.log(`📱 ${answer.simulator} wil be opened and tests will be run.`);
const runOnSimulator = spawnSync('npm',
['run', 'e2e.ios', '--', `--iphone="${iphone}"`, `--os=${os[0]}.${os[1]}`]
);
console.log(runOnSimulator.stdout.toString());
if (runOnSimulator.status !== 0) {
throw new Error(`Tests on the ${answer.simulator} did not run properly.`);
}
process.exit(0);
});
@ArjunReddyD
Copy link

Great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment