Reduce lodash usage in oclif actions

Change-type: patch
This commit is contained in:
Pagan Gazzard 2020-07-08 17:34:53 +01:00
parent 6082771aad
commit 5242510481
7 changed files with 18 additions and 26 deletions

View File

@ -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,
);

View File

@ -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,
]),
);
}

View File

@ -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`,
);

View File

@ -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[];

View File

@ -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) {

View File

@ -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<T = Dictionary<any>>(
varArray: T[],
fields: string[],
): string {
const transformed = _.map(varArray, (o: Dictionary<any>) =>
const transformed = varArray.map((o: Dictionary<any>) =>
_.transform(
o,
(result, value, key) => {

View File

@ -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)');
}