From c000a1b5d9ec587f64d78219167abde4bb21a1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A8=E3=83=AA=E3=82=B9?= Date: Mon, 8 May 2023 14:47:08 +0900 Subject: [PATCH] feat: add listTarget api & revert original bin file location (#1320) * feat: add listTarget api & revert original bin file location * test: write Platform API target list specs --- lib/Api.js | 5 +++++ lib/run.js | 15 +++++++++++++ package.json | 2 +- {lib => templates/cordova/lib}/list-devices | 2 +- .../cordova/lib}/list-emulator-images | 2 +- tests/spec/unit/Api.spec.js | 12 +++++++++++ tests/spec/unit/lib/run.spec.js | 21 +++++++++++++++++++ 7 files changed, 56 insertions(+), 3 deletions(-) rename {lib => templates/cordova/lib}/list-devices (94%) rename {lib => templates/cordova/lib}/list-emulator-images (93%) diff --git a/lib/Api.js b/lib/Api.js index e282e8ec5..d37adf809 100644 --- a/lib/Api.js +++ b/lib/Api.js @@ -665,6 +665,11 @@ class Api { .then(() => require('./run').run.call(this, runOptions)); } + listTargets (options) { + return check_reqs.run() + .then(() => require('./run').runListDevices.call(this, options)); + } + /** * Cleans out the build artifacts from platform's directory. * diff --git a/lib/run.js b/lib/run.js index 678bd130b..11ddb8e48 100644 --- a/lib/run.js +++ b/lib/run.js @@ -228,3 +228,18 @@ function listEmulators () { }); }); } + +module.exports.runListDevices = async function (options = {}) { + const { options: cliArgs = {} } = options; + + if (cliArgs?.device) { + await module.exports.listDevices.call(this); + } else if (cliArgs?.emulator) { + await module.exports.listEmulators.call(this); + } else { + await module.exports.listDevices.call(this); + await module.exports.listEmulators.call(this); + } + + return true; +}; diff --git a/package.json b/package.json index e9efc07a8..54bfc443c 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "xcodebuild": "xcodebuild -quiet test -workspace tests/cordova-ios.xcworkspace -destination \"platform=iOS Simulator,name=iPhone 8\" CONFIGURATION_BUILD_DIR=\"`mktemp -d 2>/dev/null || mktemp -d -t 'cordova-ios'`\"", "preobjc-tests": "killall Simulator || true", "unit-tests": "jasmine --config=tests/spec/unit.json", - "lint": "eslint . \"lib/**/!(*.*)\"" + "lint": "eslint . \"templates/cordova/lib/!(*.*)\"" }, "author": "Apache Software Foundation", "license": "Apache-2.0", diff --git a/lib/list-devices b/templates/cordova/lib/list-devices similarity index 94% rename from lib/list-devices rename to templates/cordova/lib/list-devices index 5f89e21b2..81c3e3d77 100755 --- a/lib/list-devices +++ b/templates/cordova/lib/list-devices @@ -19,7 +19,7 @@ under the License. */ -const { run } = require('./listDevices'); +const { run } = require('cordova-ios/lib/listDevices'); run().then(devices => { devices.forEach(device => { diff --git a/lib/list-emulator-images b/templates/cordova/lib/list-emulator-images similarity index 93% rename from lib/list-emulator-images rename to templates/cordova/lib/list-emulator-images index db9b72af0..b422269c7 100755 --- a/lib/list-emulator-images +++ b/templates/cordova/lib/list-emulator-images @@ -19,7 +19,7 @@ under the License. */ -const { run } = require('./listEmulatorImages'); +const { run } = require('cordova-ios/lib/listEmulatorImages'); run().then(names => { names.forEach(name => { diff --git a/tests/spec/unit/Api.spec.js b/tests/spec/unit/Api.spec.js index 269e9053d..f7c48b910 100644 --- a/tests/spec/unit/Api.spec.js +++ b/tests/spec/unit/Api.spec.js @@ -92,6 +92,18 @@ describe('Platform Api', () => { }); }); }); + + describe('listTargets', () => { + beforeEach(() => { + spyOn(check_reqs, 'run').and.returnValue(Promise.resolve()); + }); + it('should call into lib/run module', () => { + spyOn(run_mod, 'runListDevices'); + return api.listTargets().then(() => { + expect(run_mod.runListDevices).toHaveBeenCalled(); + }); + }); + }); } describe('addPlugin', () => { diff --git a/tests/spec/unit/lib/run.spec.js b/tests/spec/unit/lib/run.spec.js index a08ce31b7..160c965f1 100644 --- a/tests/spec/unit/lib/run.spec.js +++ b/tests/spec/unit/lib/run.spec.js @@ -48,6 +48,27 @@ if (process.platform === 'darwin') { expect(run.listEmulators).toHaveBeenCalled(); }); }); + + it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.device".', () => { + return run.runListDevices({ options: { device: true } }).then(() => { + expect(run.listDevices).toHaveBeenCalled(); + expect(run.listEmulators).not.toHaveBeenCalled(); + }); + }); + + it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.emulator".', () => { + return run.runListDevices({ options: { emulator: true } }).then(() => { + expect(run.listDevices).not.toHaveBeenCalled(); + expect(run.listEmulators).toHaveBeenCalled(); + }); + }); + + it('should delegate to both "listEmulators" and "listDevices" when the "runListDevices" method does not contain "options.device" or "options.emulator".', () => { + return run.runListDevices({ options: {} }).then(() => { + expect(run.listDevices).toHaveBeenCalled(); + expect(run.listEmulators).toHaveBeenCalled(); + }); + }); }); }); }