From 5242510481bd7aa6c233e22a6029affb10f0954a Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Wed, 8 Jul 2020 17:34:53 +0100 Subject: [PATCH] Reduce lodash usage in oclif actions Change-type: patch --- lib/actions-oclif/apps.ts | 2 +- lib/actions-oclif/device/move.ts | 8 +++----- lib/actions-oclif/device/os-update.ts | 3 +-- lib/actions-oclif/devices/index.ts | 3 +-- lib/actions-oclif/env/add.ts | 7 +++---- lib/actions-oclif/envs.ts | 16 +++++++--------- lib/actions-oclif/note.ts | 5 ++--- 7 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/actions-oclif/apps.ts b/lib/actions-oclif/apps.ts index 11c3243d..8fe74900 100644 --- a/lib/actions-oclif/apps.ts +++ b/lib/actions-oclif/apps.ts @@ -74,7 +74,7 @@ export default class AppsCmd extends Command { // Add extended properties applications.forEach((application) => { - application.device_count = _.size(application.owns__device); + application.device_count = application.owns__device?.length ?? 0; application.online_devices = _.sumBy(application.owns__device, (d) => d.is_online === true ? 1 : 0, ); diff --git a/lib/actions-oclif/device/move.ts b/lib/actions-oclif/device/move.ts index 2573b53f..2accb885 100644 --- a/lib/actions-oclif/device/move.ts +++ b/lib/actions-oclif/device/move.ts @@ -18,7 +18,6 @@ import { flags } from '@oclif/command'; import type { IArg } from '@oclif/parser/lib/args'; import type { Application, Device } from 'balena-sdk'; -import * as _ from 'lodash'; import Command from '../../command'; import * as cf from '../../utils/common-flags'; import { expandForAppName } from '../../utils/helpers'; @@ -114,12 +113,11 @@ export default class DeviceMoveCmd extends Command { dt.state !== 'DISCONTINUED', ); - application = await patterns.selectApplication((app: Application) => - _.every([ - _.some(compatibleDeviceTypes, (dt) => dt.slug === app.device_type), + application = await patterns.selectApplication( + (app: Application) => + compatibleDeviceTypes.some((dt) => dt.slug === app.device_type) && // @ts-ignore using the extended device object prop device.application_name !== app.app_name, - ]), ); } diff --git a/lib/actions-oclif/device/os-update.ts b/lib/actions-oclif/device/os-update.ts index 6c473a99..e7f9b4c5 100644 --- a/lib/actions-oclif/device/os-update.ts +++ b/lib/actions-oclif/device/os-update.ts @@ -76,7 +76,6 @@ export default class DeviceOsUpdateCmd extends Command { DeviceOsUpdateCmd, ); - const _ = await import('lodash'); const sdk = getBalenaSdk(); const patterns = await import('../../utils/patterns'); const form = await import('resin-cli-form'); @@ -116,7 +115,7 @@ export default class DeviceOsUpdateCmd extends Command { // Get target OS version let targetOsVersion = options.version; if (targetOsVersion != null) { - if (!_.includes(hupVersionInfo.versions, targetOsVersion)) { + if (!hupVersionInfo.versions.includes(targetOsVersion)) { throw new ExpectedError( `The provided version ${targetOsVersion} is not in the Host OS update targets for this device`, ); diff --git a/lib/actions-oclif/devices/index.ts b/lib/actions-oclif/devices/index.ts index 7b7e2c79..0e0fe2dc 100644 --- a/lib/actions-oclif/devices/index.ts +++ b/lib/actions-oclif/devices/index.ts @@ -16,7 +16,6 @@ */ import { flags } from '@oclif/command'; -import * as _ from 'lodash'; import Command from '../../command'; import * as cf from '../../utils/common-flags'; import { expandForAppName } from '../../utils/helpers'; @@ -82,7 +81,7 @@ export default class DevicesCmd extends Command { devices = await balena.models.device.getAll(expandForAppName); } - devices = _.map(devices, function (device) { + devices = devices.map(function (device) { device.dashboard_url = balena.models.device.getDashboardUrl(device.uuid); const belongsToApplication = device.belongs_to__application as Application[]; diff --git a/lib/actions-oclif/env/add.ts b/lib/actions-oclif/env/add.ts index 8dbadc1a..46df60c1 100644 --- a/lib/actions-oclif/env/add.ts +++ b/lib/actions-oclif/env/add.ts @@ -17,7 +17,6 @@ import { flags } from '@oclif/command'; import type * as BalenaSdk from 'balena-sdk'; -import * as _ from 'lodash'; import Command from '../../command'; import { ExpectedError } from '../../errors'; @@ -129,8 +128,8 @@ export default class EnvAddCmd extends Command { const balena = getBalenaSdk(); const reservedPrefixes = await getReservedPrefixes(balena); - const isConfigVar = _.some(reservedPrefixes, (prefix) => - _.startsWith(params.name, prefix), + const isConfigVar = reservedPrefixes.some((prefix) => + params.name.startsWith(prefix), ); if (options.service) { @@ -212,7 +211,7 @@ async function getServiceIdForApp( const services = await sdk.models.service.getAllByApplication(appName, { $filter: { service_name: serviceName }, }); - if (!_.isEmpty(services)) { + if (services.length > 0) { serviceId = services[0].id; } if (serviceId === undefined) { diff --git a/lib/actions-oclif/envs.ts b/lib/actions-oclif/envs.ts index 7cac7dc0..50742099 100644 --- a/lib/actions-oclif/envs.ts +++ b/lib/actions-oclif/envs.ts @@ -229,7 +229,7 @@ export default class EnvsCmd extends Command { ...(await getDeviceVars(balena, fullUUID, appName, options)), ); } - if (!options.json && _.isEmpty(variables)) { + if (!options.json && variables.length === 0) { const target = (options.service ? `service "${options.service}" of ` : '') + (options.application @@ -249,7 +249,7 @@ export default class EnvsCmd extends Command { if (options.all) { // Replace undefined app names with 'N/A' or null - varArray = _.map(varArray, (i: EnvironmentVariableInfo) => { + varArray = varArray.map((i: EnvironmentVariableInfo) => { i.appName = i.appName || (options.json ? null : 'N/A'); return i; }); @@ -286,7 +286,7 @@ async function validateServiceName( const services = await sdk.models.service.getAllByApplication(appName, { $filter: { service_name: serviceName }, }); - if (_.isEmpty(services)) { + if (services.length === 0) { throw new ExpectedError( `Service "${serviceName}" not found for application "${appName}"`, ); @@ -407,13 +407,11 @@ function fillInInfoFields( for (const envVar of varArray) { if ('service' in envVar) { // envVar is of type ServiceEnvironmentVariableInfo - envVar.serviceName = _.at(envVar as any, 'service[0].service_name')[0]; + envVar.serviceName = (envVar.service as SDK.Service[])[0]?.service_name; } else if ('service_install' in envVar) { // envVar is of type DeviceServiceEnvironmentVariableInfo - envVar.serviceName = _.at( - envVar as any, - 'service_install[0].installs__service[0].service_name', - )[0]; + envVar.serviceName = ((envVar.service_install as SDK.ServiceInstall[])[0] + ?.installs__service as SDK.Service[])[0]?.service_name; } envVar.appName = appName; envVar.serviceName = envVar.serviceName || '*'; @@ -429,7 +427,7 @@ function stringifyVarArray>( varArray: T[], fields: string[], ): string { - const transformed = _.map(varArray, (o: Dictionary) => + const transformed = varArray.map((o: Dictionary) => _.transform( o, (result, value, key) => { diff --git a/lib/actions-oclif/note.ts b/lib/actions-oclif/note.ts index 263dd95d..f7d57081 100644 --- a/lib/actions-oclif/note.ts +++ b/lib/actions-oclif/note.ts @@ -16,7 +16,6 @@ */ import { flags } from '@oclif/command'; -import * as _ from 'lodash'; import Command from '../command'; import { ExpectedError } from '../errors'; import * as cf from '../utils/common-flags'; @@ -75,14 +74,14 @@ export default class NoteCmd extends Command { params.note = params.note || this.stdin; - if (_.isEmpty(params.note)) { + if (params.note.length === 0) { throw new ExpectedError('Missing note content'); } options.device = options.device || options.dev; delete options.dev; - if (_.isEmpty(options.device)) { + if (options.device == null || options.device.length === 0) { throw new ExpectedError('Missing device UUID (--device)'); }