Merge pull request #2646 from balena-io/reduce-lodash-usage

Reduce lodash usage in common user interaction patterns
This commit is contained in:
flowzone-app[bot] 2023-07-10 17:15:14 +00:00 committed by GitHub
commit 4ee55b049f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 23 deletions

View File

@ -22,7 +22,6 @@ import type {
PineOptions,
PineTypedResult,
} from 'balena-sdk';
import _ = require('lodash');
import { instanceOf, NotLoggedInError, ExpectedError } from '../errors';
import { getBalenaSdk, getVisuals, stripIndent, getCliForm } from './lazy';
@ -126,7 +125,7 @@ export async function selectDeviceType() {
return getCliForm().ask({
message: 'Device Type',
type: 'list',
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
choices: deviceTypes.map(({ slug: value, name }) => ({
name,
value,
})),
@ -183,7 +182,7 @@ export async function selectApplication(
errorOnEmptySelection = false,
) {
const balena = getBalenaSdk();
const apps = (await balena.models.application.getAllDirectlyAccessible(
let apps = (await balena.models.application.getAllDirectlyAccessible(
selectApplicationPineOptions,
)) as SelectApplicationResult[];
@ -191,15 +190,17 @@ export async function selectApplication(
throw new ExpectedError('No fleets found');
}
const applications = filter ? apps.filter(filter) : apps;
if (filter != null) {
apps = apps.filter(filter);
}
if (errorOnEmptySelection && applications.length === 0) {
if (errorOnEmptySelection && apps.length === 0) {
throw new ExpectedError('No suitable fleets found for selection');
}
return getCliForm().ask({
message: 'Select an application',
type: 'list',
choices: _.map(applications, (application) => ({
choices: apps.map((application) => ({
name: `${application.app_name} (${application.slug}) [${application.is_for__device_type[0].slug}]`,
value: application,
})),
@ -338,7 +339,7 @@ export async function getOnlineTargetDeviceUuid(
const devices = application.owns__device;
// Throw if no devices online
if (_.isEmpty(devices)) {
if (!devices.length) {
throw new ExpectedError(
`Fleet ${application.slug} found, but has no devices online.`,
);
@ -349,7 +350,7 @@ export async function getOnlineTargetDeviceUuid(
message: `Select a device on fleet ${application.slug}`,
type: 'list',
default: devices[0].uuid,
choices: _.map(devices, (device) => ({
choices: devices.map((device) => ({
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(0, 7)})`,
value: device.uuid,
})),
@ -363,7 +364,7 @@ export function selectFromList<T>(
return getCliForm().ask<T>({
message,
type: 'list',
choices: _.map(choices, (s) => ({
choices: choices.map((s) => ({
name: s.name,
value: s,
})),

28
npm-shrinkwrap.json generated
View File

@ -1802,16 +1802,16 @@
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg=="
},
"node_modules/@oclif/command": {
"version": "1.8.30",
"resolved": "https://registry.npmjs.org/@oclif/command/-/command-1.8.30.tgz",
"integrity": "sha512-1l8t77foQJErqveIJwWIXLr/EtWSqQkEcSmHgVBZAVjUaJWvnLZKgohmugJcIp9aR+wYCowRrCL3rLxEMzpq4A==",
"version": "1.8.31",
"resolved": "https://registry.npmjs.org/@oclif/command/-/command-1.8.31.tgz",
"integrity": "sha512-5GLT2l8ccxTqog4UBIX6DqdvDXkpDWBIU7tz8Bx+N1CACpY9cXqG6luUqQzLshKaHXx9b/Y4/KF6SvRTg9FN5A==",
"dependencies": {
"@oclif/config": "^1.18.2",
"@oclif/errors": "^1.3.6",
"@oclif/help": "^1.0.1",
"@oclif/parser": "^3.8.13",
"debug": "^4.1.1",
"semver": "^7.5.3"
"semver": "^7.5.4"
},
"engines": {
"node": ">=12.0.0"
@ -17202,9 +17202,9 @@
}
},
"node_modules/semver": {
"version": "7.5.3",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz",
"integrity": "sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==",
"version": "7.5.4",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz",
"integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==",
"dependencies": {
"lru-cache": "^6.0.0"
},
@ -23562,16 +23562,16 @@
}
},
"@oclif/command": {
"version": "1.8.30",
"resolved": "https://registry.npmjs.org/@oclif/command/-/command-1.8.30.tgz",
"integrity": "sha512-1l8t77foQJErqveIJwWIXLr/EtWSqQkEcSmHgVBZAVjUaJWvnLZKgohmugJcIp9aR+wYCowRrCL3rLxEMzpq4A==",
"version": "1.8.31",
"resolved": "https://registry.npmjs.org/@oclif/command/-/command-1.8.31.tgz",
"integrity": "sha512-5GLT2l8ccxTqog4UBIX6DqdvDXkpDWBIU7tz8Bx+N1CACpY9cXqG6luUqQzLshKaHXx9b/Y4/KF6SvRTg9FN5A==",
"requires": {
"@oclif/config": "^1.18.2",
"@oclif/errors": "^1.3.6",
"@oclif/help": "^1.0.1",
"@oclif/parser": "^3.8.13",
"debug": "^4.1.1",
"semver": "^7.5.3"
"semver": "^7.5.4"
}
},
"@oclif/config": {
@ -35827,9 +35827,9 @@
}
},
"semver": {
"version": "7.5.3",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz",
"integrity": "sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==",
"version": "7.5.4",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz",
"integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==",
"requires": {
"lru-cache": "^6.0.0"
}