From b2d932afabb9c1ca131c0839397d0492f2f1a272 Mon Sep 17 00:00:00 2001 From: Paulo Castro Date: Fri, 24 Dec 2021 15:02:59 +0000 Subject: [PATCH] os versions, os download: Replace deprecated version fields Replace deprecated `rawVersion` and `formattedVersion` fields and use alternative overload of `getAvailableOsVersions`. As a result, the word 'recommended' is no longer printed next to any OS versions. Change-type: patch --- lib/commands/os/versions.ts | 8 ++++--- lib/utils/cloud.ts | 44 ++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/commands/os/versions.ts b/lib/commands/os/versions.ts index 6e3a42ac..9130e372 100644 --- a/lib/commands/os/versions.ts +++ b/lib/commands/os/versions.ts @@ -65,9 +65,11 @@ export default class OsVersionsCmd extends Command { OsVersionsCmd, ); - const { getFormattedOsVersions } = await import('../../utils/cloud'); - const vs = await getFormattedOsVersions(params.type, !!options.esr); + const { formatOsVersion, getOsVersions } = await import( + '../../utils/cloud' + ); + const vs = await getOsVersions(params.type, !!options.esr); - console.log(vs.map((v) => v.formattedVersion).join('\n')); + console.log(vs.map((v) => formatOsVersion(v)).join('\n')); } } diff --git a/lib/utils/cloud.ts b/lib/utils/cloud.ts index c4111c9d..540a258b 100644 --- a/lib/utils/cloud.ts +++ b/lib/utils/cloud.ts @@ -225,53 +225,45 @@ async function selectOSVersionFromMenu( deviceType: string, esr: boolean, ): Promise { - const vs = await getFormattedOsVersions(deviceType, esr); + const vs = await getOsVersions(deviceType, esr); const choices = vs.map((v) => ({ - value: v.rawVersion, - name: v.formattedVersion, + value: v.raw_version, + name: formatOsVersion(v), })); return getCliForm().ask({ message: 'Select the OS version:', type: 'list', choices, - default: (vs.find((v) => v.isRecommended) ?? vs[0])?.rawVersion, + default: vs[0]?.raw_version, }); } /** - * Return the output of sdk.models.hostapp.getAvailableOsVersions(), filtered - * regarding ESR or non-ESR versions, and having the `formattedVersion` field - * reformatted for compatibility with the pre-existing output format of the - * `os versions` and `os download` commands. + * Return the output of sdk.models.os.getAvailableOsVersions(), resolving + * device type aliases and filtering with regard to ESR versions. */ -export async function getFormattedOsVersions( +export async function getOsVersions( deviceType: string, esr: boolean, ): Promise { const sdk = getBalenaSdk(); let slug = deviceType; - let versionsByDT: SDK.OsVersionsByDeviceType = - await sdk.models.os.getAvailableOsVersions([slug]); + let versions: SDK.OsVersion[] = await sdk.models.os.getAvailableOsVersions( + slug, + ); // if slug is an alias, fetch the real slug - if (!versionsByDT[slug]?.length) { + if (!versions.length) { // unaliasDeviceType() produces a nice error msg if slug is invalid slug = await unaliasDeviceType(sdk, slug); if (slug !== deviceType) { - versionsByDT = await sdk.models.os.getAvailableOsVersions([slug]); + versions = await sdk.models.os.getAvailableOsVersions(slug); } } - const versions: SDK.OsVersion[] = (versionsByDT[slug] || []) - .filter((v: SDK.OsVersion) => v.osType === (esr ? 'esr' : 'default')) - .map((v: SDK.OsVersion) => { - const i = v.formattedVersion.indexOf(' '); - v.formattedVersion = - i < 0 - ? `v${v.rawVersion}` - : `v${v.rawVersion}${v.formattedVersion.substring(i)}`; - return v; - }); + versions = versions.filter( + (v: SDK.OsVersion) => v.osType === (esr ? 'esr' : 'default'), + ); if (!versions.length) { const vType = esr ? 'ESR versions' : 'versions'; throw new ExpectedError( @@ -280,3 +272,9 @@ export async function getFormattedOsVersions( } return versions; } + +export function formatOsVersion(osVersion: SDK.OsVersion): string { + return osVersion.line + ? `v${osVersion.raw_version} (${osVersion.line})` + : `v${osVersion.raw_version}`; +}