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
2 changes: 2 additions & 0 deletions lib/subcommands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import spawnCommands from './spawn';
import terminateCommands from './terminate';
import uiCommands from './ui';
import uninstallCommands from './uninstall';
import locationCommands from './location';

// xcrun simctl --help
const subcommands = Object.assign({},
Expand Down Expand Up @@ -49,6 +50,7 @@ const subcommands = Object.assign({},
terminateCommands,
uiCommands,
uninstallCommands,
locationCommands,
);

export default subcommands;
46 changes: 46 additions & 0 deletions lib/subcommands/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const commands = {};

/**
* Formats the given location argument for simctl usage
*
* @param {string} name Argument name
* @param {string|number} value Location argument value
* @returns {string} Formatted value, for example -73.768254
*/
function formatArg (name, value) {
const flt = parseFloat(value);
if (isNaN(flt)) {
throw new TypeError(`${name} must be a valid number, got '${value}' instead`);
}
return flt.toFixed(7);
}

/**
* Set the Simulator location to a specific latitude and longitude.
* This functionality is only available since Xcode 14.
*
* @param {string|number} latitude Location latitude value
* @param {string|number} longitude Location longitude value
* @throws {Error} If the corresponding simctl subcommand command
* returns non-zero return code.
* @throws {TypeError} If any of the arguments is not a valid value.
*/
commands.setLocation = async function setLocation (latitude, longitude) {
const lat = formatArg('latitude', latitude);
const lon = formatArg('longitude', longitude);
await this.exec('location', {
args: [this.requireUdid('location'), 'set', `${lat},${lon}`],
});
};

/**
* Stop any running scenario and clear any simulated location.
* This functionality is only available since Xcode 14.
*/
commands.clearLocation = async function clearLocation () {
await this.exec('location', {
args: [this.requireUdid('location'), 'clear'],
});
};

export default commands;