Skip to content

Commit

Permalink
refactor(run): code cleanup (apache#1189)
Browse files Browse the repository at this point in the history
* refactor(run): merge functions startSim and iossimLaunch

* refactor(run): simplify deployToSim

* refactor(run): simplify deployToDevice
  • Loading branch information
raphinesse authored and gazben committed Aug 26, 2022
1 parent d7295c5 commit 8547eef
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions bin/templates/scripts/cordova/lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ function checkDeviceConnected () {
*/
function deployToDevice (appPath, target, extraArgs) {
events.emit('log', 'Deploying to device');
// Deploying to device...
const args = ['--justlaunch', '-d', '-b', appPath];
if (target) {
return spawn('ios-deploy', ['--justlaunch', '-d', '-b', appPath, '-i', target].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
args.push('-i', target);
} else {
return spawn('ios-deploy', ['--justlaunch', '--no-wifi', '-d', '-b', appPath].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
args.push('--no-wifi');
}
return spawn('ios-deploy', args.concat(extraArgs), { printCommand: true, stdio: 'inherit' });
}

/**
Expand All @@ -170,38 +171,26 @@ function deployToDevice (appPath, target, extraArgs) {
* @param {String} target Target device type
* @return {Promise} Resolves when deploy succeeds otherwise rejects
*/
function deployToSim (appPath, target) {
async function deployToSim (appPath, target) {
events.emit('log', 'Deploying to simulator');

if (!target) {
// Select target device for emulator
return require('./listEmulatorImages').run()
.then(emulators => {
if (emulators.length > 0) {
target = emulators[0];
}
emulators.forEach(emulator => {
if (emulator.indexOf('iPhone') === 0) {
target = emulator;
}
});
events.emit('log', `No target specified for emulator. Deploying to "${target}" simulator.`);
return startSim(appPath, target);
});
} else {
return startSim(appPath, target);
// Select target device for emulator (preferring iPhone Emulators)
const emulators = await require('./listEmulatorImages').run();
const iPhoneEmus = emulators.filter(emulator => emulator.startsWith('iPhone'));
target = iPhoneEmus.concat(emulators)[0];
events.emit('log', `No target specified for emulator. Deploying to "${target}" simulator.`);
}

return startSim(appPath, target);
}

function startSim (appPath, target) {
const logPath = path.join(cordovaPath, 'console.log');

return iossimLaunch(appPath, `com.apple.CoreSimulator.SimDeviceType.${target}`, logPath, '--exit');
}

function iossimLaunch (appPath, devicetypeid, log, exit) {
const deviceTypeId = `com.apple.CoreSimulator.SimDeviceType.${target}`;
return spawn(
require.resolve('ios-sim/bin/ios-sim'),
['launch', appPath, '--devicetypeid', devicetypeid, '--log', log, exit],
['launch', appPath, '--devicetypeid', deviceTypeId, '--log', logPath, '--exit'],
{ cwd: projectPath, printCommand: true }
).progress(stdio => {
if (stdio.stderr) {
Expand All @@ -211,7 +200,7 @@ function iossimLaunch (appPath, devicetypeid, log, exit) {
events.emit('log', `[ios-sim] ${stdio.stdout.trim()}`);
}
})
.then(result => {
.then(() => {
events.emit('log', 'Simulator successfully started via `ios-sim`.');
});
}
Expand Down

0 comments on commit 8547eef

Please sign in to comment.