From e11b39b248675978f3841acf2f9d09331bf2e8ff Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sat, 16 Jul 2022 17:27:42 +0200 Subject: [PATCH 1/2] feat: Add location simctl subcommand --- lib/subcommands/index.js | 2 ++ lib/subcommands/location.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/subcommands/location.js diff --git a/lib/subcommands/index.js b/lib/subcommands/index.js index 79ae0eb..1fb2c8b 100644 --- a/lib/subcommands/index.js +++ b/lib/subcommands/index.js @@ -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({}, @@ -49,6 +50,7 @@ const subcommands = Object.assign({}, terminateCommands, uiCommands, uninstallCommands, + locationCommands, ); export default subcommands; diff --git a/lib/subcommands/location.js b/lib/subcommands/location.js new file mode 100644 index 0000000..384ec78 --- /dev/null +++ b/lib/subcommands/location.js @@ -0,0 +1,36 @@ +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}`], + }); +}; + +export default commands; From 6e58ef6d4b46c5d7a9cc3d001cb67d4f7918b3b9 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sat, 16 Jul 2022 17:43:02 +0200 Subject: [PATCH 2/2] Add clear location --- lib/subcommands/location.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/subcommands/location.js b/lib/subcommands/location.js index 384ec78..23885b2 100644 --- a/lib/subcommands/location.js +++ b/lib/subcommands/location.js @@ -33,4 +33,14 @@ commands.setLocation = async function setLocation (latitude, longitude) { }); }; +/** + * 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;