Remove extra request when filling the application selection list

Saves one request of about 450ms on the
init and move commands.

Change-type: patch
This commit is contained in:
Thodoris Greasidis 2023-05-19 20:59:49 +03:00
parent b6c7fb82c3
commit 46aa08c953

View File

@ -157,40 +157,36 @@ export async function confirm(
} }
} }
export function selectApplication( export async function selectApplication(
filter?: (app: ApplicationWithDeviceType) => boolean, filter?: (app: ApplicationWithDeviceType) => boolean,
errorOnEmptySelection = false, errorOnEmptySelection = false,
) { ) {
const balena = getBalenaSdk(); const balena = getBalenaSdk();
return balena.models.application const apps = (await balena.models.application.getAllDirectlyAccessible({
.hasAny() $expand: {
.then(async (hasAnyApplications) => { is_for__device_type: {
if (!hasAnyApplications) { $select: 'slug',
throw new ExpectedError('No fleets found'); },
} },
})) as ApplicationWithDeviceType[];
const apps = (await balena.models.application.getAllDirectlyAccessible({ if (!apps.length) {
$expand: { throw new ExpectedError('No fleets found');
is_for__device_type: { }
$select: 'slug',
}, const applications = filter ? apps.filter(filter) : apps;
},
})) as ApplicationWithDeviceType[]; if (errorOnEmptySelection && applications.length === 0) {
return apps.filter(filter || _.constant(true)); throw new ExpectedError('No suitable fleets found for selection');
}) }
.then((applications) => { return getCliForm().ask({
if (errorOnEmptySelection && applications.length === 0) { message: 'Select an application',
throw new ExpectedError('No suitable fleets found for selection'); type: 'list',
} choices: _.map(applications, (application) => ({
return getCliForm().ask({ name: `${application.app_name} (${application.slug}) [${application.is_for__device_type[0].slug}]`,
message: 'Select an application', value: application,
type: 'list', })),
choices: _.map(applications, (application) => ({ });
name: `${application.app_name} (${application.slug}) [${application.is_for__device_type[0].slug}]`,
value: application,
})),
});
});
} }
export async function selectOrganization( export async function selectOrganization(