You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2025/07/25 04:35:00 UTC

(cordova-lib) branch master updated: fix(emulator): Support listing emulators with --list (#944)

This is an automated email from the ASF dual-hosted git repository.

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-lib.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ab9819d fix(emulator): Support listing emulators with --list (#944)
0ab9819d is described below

commit 0ab9819df22f2d467bd34831e2633879a86bf0f0
Author: Darryl Pogue <da...@dpogue.ca>
AuthorDate: Thu Jul 24 21:34:56 2025 -0700

    fix(emulator): Support listing emulators with --list (#944)
---
 spec/cordova/emulate.spec.js | 49 ++++++++++++++++++++++++++++++++++++---
 spec/cordova/run.spec.js     | 55 +++++++++++++++++++++++++++++++++++++++++---
 src/cordova/emulate.js       | 35 +++++++++++++++++++++++++++-
 3 files changed, 132 insertions(+), 7 deletions(-)

diff --git a/spec/cordova/emulate.spec.js b/spec/cordova/emulate.spec.js
index fe089372..1cfaff58 100644
--- a/spec/cordova/emulate.spec.js
+++ b/spec/cordova/emulate.spec.js
@@ -17,6 +17,7 @@
     under the License.
 */
 const rewire = require('rewire');
+const { events } = require('cordova-common');
 const platforms = require('../../src/platforms/platforms');
 const HooksRunner = require('../../src/hooks/HooksRunner');
 const util = require('../../src/cordova/util');
@@ -25,21 +26,24 @@ const supported_platforms = Object.keys(platforms);
 
 describe('emulate command', function () {
     const project_dir = '/some/path';
-    let cordovaEmulate, cordovaPrepare, platformApi, getPlatformApi;
+    let cordovaEmulate, cordovaPrepare, platformApi, getPlatformApi, targets;
 
     beforeEach(function () {
         spyOn(util, 'isCordova').and.returnValue(project_dir);
         spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
         spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
         spyOn(HooksRunner.prototype, 'fire').and.returnValue(Promise.resolve());
+        spyOn(events, 'emit');
 
         cordovaEmulate = rewire('../../src/cordova/emulate');
         cordovaPrepare = jasmine.createSpy('cordovaPrepare').and.returnValue(Promise.resolve());
-        cordovaEmulate.__set__({ cordovaPrepare });
+        targets = jasmine.createSpy('targets').and.returnValue(Promise.resolve());
+        cordovaEmulate.__set__({ cordovaPrepare, targets });
 
         platformApi = {
             run: jasmine.createSpy('run').and.returnValue(Promise.resolve()),
-            build: jasmine.createSpy('build').and.returnValue(Promise.resolve())
+            build: jasmine.createSpy('build').and.returnValue(Promise.resolve()),
+            listTargets: jasmine.createSpy('listTargets').and.returnValue(Promise.resolve())
         };
         getPlatformApi = spyOn(platforms, 'getPlatformApi').and.returnValue(platformApi);
     });
@@ -60,6 +64,35 @@ describe('emulate command', function () {
         });
     });
 
+    describe('list', function () {
+        it('should warn if platforms are not specified', function () {
+            const result = cordovaEmulate({ platforms: [], options: { list: true } });
+
+            expect(result).toBeFalsy();
+            expect(events.emit).toHaveBeenCalledWith('warn', 'A platform must be provided when using the "--list" flag.');
+        });
+
+        it('should try to use the Platform API to list emulator targets', function () {
+            return cordovaEmulate({ platforms: ['ios'], options: { list: true } })
+                .then(function () {
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(platformApi.listTargets).toHaveBeenCalledWith(jasmine.objectContaining({ options: jasmine.objectContaining({ device: false, emulator: true }) }));
+                });
+        });
+
+        it('should fall back to the pre-Platform API targets', function () {
+            delete platformApi.listTargets;
+
+            return cordovaEmulate({ platforms: ['ios'], options: { list: true } })
+                .then(function () {
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(targets).toHaveBeenCalledWith(jasmine.objectContaining({ options: jasmine.objectContaining({ device: false, emulator: true }) }));
+
+                    expect(events.emit).toHaveBeenCalledWith('warn', 'Please update to the latest platform release to ensure uninterrupted fetching of target lists.');
+                });
+        });
+    });
+
     describe('success', function () {
         it('Test 003 : should run inside a Cordova-based project with at least one added platform and call prepare and shell out to the emulate script', function () {
             return cordovaEmulate(['android', 'ios'])
@@ -81,6 +114,16 @@ describe('emulate command', function () {
                 });
         });
 
+        it('Test 005 : should skip preparing if --noprepare is passed', function () {
+            return cordovaEmulate({ platforms: ['ios'], options: { noprepare: true } })
+                .then(function () {
+                    expect(cordovaPrepare).not.toHaveBeenCalledWith(jasmine.objectContaining({ platforms: ['ios'] }));
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(platformApi.build).toHaveBeenCalledWith({ device: false, emulator: true, noprepare: true });
+                    expect(platformApi.run).toHaveBeenCalledWith({ device: false, emulator: true, noprepare: true, nobuild: true });
+                });
+        });
+
         describe('run parameters should not be altered by intermediate build command', function () {
             beforeEach(function () {
                 platformApi.build.and.callFake(opts => {
diff --git a/spec/cordova/run.spec.js b/spec/cordova/run.spec.js
index f046bfa8..30515528 100644
--- a/spec/cordova/run.spec.js
+++ b/spec/cordova/run.spec.js
@@ -17,6 +17,7 @@
     under the License.
 */
 const rewire = require('rewire');
+const { events } = require('cordova-common');
 const platforms = require('../../src/platforms/platforms');
 const HooksRunner = require('../../src/hooks/HooksRunner');
 const util = require('../../src/cordova/util');
@@ -25,21 +26,24 @@ const supported_platforms = Object.keys(platforms).filter(function (p) { return
 
 describe('run command', function () {
     const project_dir = '/some/path';
-    let cordovaRun, cordovaPrepare, platformApi, getPlatformApi;
+    let cordovaRun, cordovaPrepare, platformApi, getPlatformApi, targets;
 
     beforeEach(function () {
         spyOn(util, 'isCordova').and.returnValue(project_dir);
         spyOn(util, 'cdProjectRoot').and.returnValue(project_dir);
         spyOn(util, 'listPlatforms').and.returnValue(supported_platforms);
         spyOn(HooksRunner.prototype, 'fire').and.returnValue(Promise.resolve());
+        spyOn(events, 'emit');
 
         cordovaRun = rewire('../../src/cordova/run');
         cordovaPrepare = jasmine.createSpy('cordovaPrepare').and.returnValue(Promise.resolve());
-        cordovaRun.__set__({ cordovaPrepare });
+        targets = jasmine.createSpy('targets').and.returnValue(Promise.resolve());
+        cordovaRun.__set__({ cordovaPrepare, targets });
 
         platformApi = {
             run: jasmine.createSpy('run').and.returnValue(Promise.resolve()),
-            build: jasmine.createSpy('build').and.returnValue(Promise.resolve())
+            build: jasmine.createSpy('build').and.returnValue(Promise.resolve()),
+            listTargets: jasmine.createSpy('listTargets').and.returnValue(Promise.resolve())
         };
         getPlatformApi = spyOn(platforms, 'getPlatformApi').and.returnValue(platformApi);
     });
@@ -62,6 +66,43 @@ describe('run command', function () {
         });
     });
 
+    describe('list', function () {
+        it('should warn if platforms are not specified', function () {
+            const result = cordovaRun({ platforms: [], options: { list: true } });
+
+            expect(result).toBeFalsy();
+            expect(events.emit).toHaveBeenCalledWith('warn', 'A platform must be provided when using the "--list" flag.');
+        });
+
+        it('should try to use the Platform API to list emulator targets', function () {
+            return cordovaRun({ platforms: ['ios'], options: { list: true } })
+                .then(function () {
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(platformApi.listTargets).toHaveBeenCalledWith(jasmine.objectContaining({ options: jasmine.objectContaining({ list: true }) }));
+                });
+        });
+
+        it('should pass options down', function () {
+            return cordovaRun({ platforms: ['ios'], options: { list: true, device: true } })
+                .then(function () {
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(platformApi.listTargets).toHaveBeenCalledWith(jasmine.objectContaining({ options: jasmine.objectContaining({ device: true }) }));
+                });
+        });
+
+        it('should fall back to the pre-Platform API targets', function () {
+            delete platformApi.listTargets;
+
+            return cordovaRun({ platforms: ['ios'], options: { list: true } })
+                .then(function () {
+                    expect(getPlatformApi).toHaveBeenCalledWith('ios');
+                    expect(targets).toHaveBeenCalledWith(jasmine.objectContaining({ options: jasmine.objectContaining({ list: true }) }));
+
+                    expect(events.emit).toHaveBeenCalledWith('warn', 'Please update to the latest platform release to ensure uninterrupted fetching of target lists.');
+                });
+        });
+    });
+
     describe('success', function () {
         it('Test 003 : should call prepare before actually run platform ', function () {
             return cordovaRun(['android', 'ios']).then(function () {
@@ -84,6 +125,14 @@ describe('run command', function () {
             });
         });
 
+        it('Test 006 : should skip preparing if --noprepare is passed', function () {
+            return cordovaRun({ platforms: ['blackberry10'], options: { noprepare: true } }).then(function () {
+                expect(cordovaPrepare).not.toHaveBeenCalledWith(jasmine.objectContaining({ platforms: ['blackberry10'] }));
+                expect(platformApi.build).toHaveBeenCalledWith({ noprepare: true });
+                expect(platformApi.run).toHaveBeenCalledWith({ noprepare: true, nobuild: true });
+            });
+        });
+
         it('Test 007 : should call platform\'s build method', function () {
             return cordovaRun({ platforms: ['blackberry10'] })
                 .then(function () {
diff --git a/src/cordova/emulate.js b/src/cordova/emulate.js
index 4ca7963e..95ccdc8c 100644
--- a/src/cordova/emulate.js
+++ b/src/cordova/emulate.js
@@ -18,12 +18,45 @@
 */
 
 const cordova_util = require('./util');
+const { events } = require('cordova-common');
 const HooksRunner = require('../hooks/HooksRunner');
 const platform_lib = require('../platforms/platforms');
 const cordovaPrepare = require('./prepare');
 
+// Support for listing targets.
+const targets = require('./targets');
+
 // Returns a promise.
-module.exports = function emulate (options) {
+module.exports = function emulate (options = {}) {
+    const { options: cliArgs } = options;
+
+    if (cliArgs?.list) {
+        const { platforms } = options;
+
+        if (platforms.length <= 0) {
+            events.emit('warn', 'A platform must be provided when using the "--list" flag.');
+            return false;
+        }
+
+        cordova_util.cdProjectRoot();
+        options = cordova_util.preProcessOptions(options);
+        options.options.device = false;
+        options.options.emulator = true;
+
+        return Promise.resolve(platforms.map(function (platform) {
+            const platformApi = platform_lib.getPlatformApi(platform);
+
+            if (platformApi?.listTargets) {
+                // Use Platform's API to fetch target list when available
+                return platformApi.listTargets(options);
+            } else {
+                events.emit('warn', 'Please update to the latest platform release to ensure uninterrupted fetching of target lists.');
+                // fallback to original method.
+                return targets(options);
+            }
+        }));
+    }
+
     return Promise.resolve().then(function () {
         const projectRoot = cordova_util.cdProjectRoot();
         options = cordova_util.preProcessOptions(options);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org