Skip to content

Commit 7da13cc

Browse files
authored
feat: add listTarget api (#1602)
* feat: add listTarget api * test: write Platform API target list specs
1 parent cb48147 commit 7da13cc

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

lib/Api.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ class Api {
277277
});
278278
}
279279

280+
listTargets (options) {
281+
return require('./check_reqs').check_android().then(() => {
282+
return require('./run').runListDevices.call(this, options);
283+
});
284+
}
285+
280286
/**
281287
* Cleans out the build artifacts from platform's directory, and also
282288
* cleans out the platform www directory if called without options specified.

lib/run.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,49 @@ module.exports.run = async function (runOptions = {}) {
8383

8484
return target.install(resolvedTarget, { manifest, buildResults, cordovaGradleConfigParser });
8585
};
86+
87+
module.exports.listDevices = async function () {
88+
events.emit('log', `\nAvailable ${this.platform} devices:`);
89+
90+
const { list } = require('./target');
91+
92+
await list().then(targets => {
93+
const deviceIds = targets
94+
.filter(({ type }) => type === 'device')
95+
.map(({ id }) => id);
96+
97+
console.log(deviceIds.join('\n'));
98+
}, function (err) {
99+
console.error('ERROR: ' + err);
100+
process.exit(2);
101+
});
102+
};
103+
104+
module.exports.listEmulators = async function () {
105+
events.emit('log', `\nAvailable ${this.platform} virtual devices:`);
106+
const emulators = require('./emulator');
107+
108+
await emulators.list_images().then(function (emulator_list) {
109+
emulator_list && emulator_list.forEach(function (emu) {
110+
console.log(emu.name);
111+
});
112+
}, function (err) {
113+
console.error('ERROR: ' + err);
114+
process.exit(2);
115+
});
116+
};
117+
118+
module.exports.runListDevices = async function (options = {}) {
119+
const { options: cliArgs = {} } = options;
120+
121+
if (cliArgs?.device) {
122+
await module.exports.listDevices.call(this);
123+
} else if (cliArgs?.emulator) {
124+
await module.exports.listEmulators.call(this);
125+
} else {
126+
await module.exports.listDevices.call(this);
127+
await module.exports.listEmulators.call(this);
128+
}
129+
130+
return true;
131+
};

spec/unit/Api.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const EventEmitter = require('events');
2424

2525
const Api = require('../../lib/Api');
2626
const AndroidProject = require('../../lib/AndroidProject');
27+
const check_reqs = require('../../lib/check_reqs');
28+
const run_mod = require('../../lib/run');
2729

2830
const PluginInfo = common.PluginInfo;
2931

@@ -60,4 +62,19 @@ describe('Api', () => {
6062
});
6163
});
6264
});
65+
66+
describe('listTargets', () => {
67+
let api;
68+
69+
beforeEach(() => {
70+
api = new Api('android', FAKE_PROJECT_DIR, new EventEmitter());
71+
spyOn(check_reqs, 'run').and.returnValue(Promise.resolve());
72+
});
73+
it('should call into lib/run module', () => {
74+
spyOn(run_mod, 'runListDevices');
75+
return api.listTargets().then(() => {
76+
expect(run_mod.runListDevices).toHaveBeenCalled();
77+
});
78+
});
79+
});
6380
});

spec/unit/run.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,32 @@ describe('run', () => {
106106
.toBeRejectedWithError(/Package type "bundle" is not supported/);
107107
});
108108
});
109+
110+
describe('--list option', () => {
111+
beforeEach(() => {
112+
spyOn(run, 'listDevices').and.returnValue(Promise.resolve());
113+
spyOn(run, 'listEmulators').and.returnValue(Promise.resolve());
114+
});
115+
116+
it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.device".', () => {
117+
return run.runListDevices({ options: { device: true } }).then(() => {
118+
expect(run.listDevices).toHaveBeenCalled();
119+
expect(run.listEmulators).not.toHaveBeenCalled();
120+
});
121+
});
122+
123+
it('should delegate to "listDevices" when the "runListDevices" method options param contains "options.emulator".', () => {
124+
return run.runListDevices({ options: { emulator: true } }).then(() => {
125+
expect(run.listDevices).not.toHaveBeenCalled();
126+
expect(run.listEmulators).toHaveBeenCalled();
127+
});
128+
});
129+
130+
it('should delegate to both "listEmulators" and "listDevices" when the "runListDevices" method does not contain "options.device" or "options.emulator".', () => {
131+
return run.runListDevices({ options: {} }).then(() => {
132+
expect(run.listDevices).toHaveBeenCalled();
133+
expect(run.listEmulators).toHaveBeenCalled();
134+
});
135+
});
136+
});
109137
});

0 commit comments

Comments
 (0)