From 77196746b3071cf3f27bc8108d4a69c364b4d64b Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Sat, 12 Jan 2019 21:20:19 +0000 Subject: [PATCH] Lazy load the sdk as much as possible Change-type: patch --- lib/app.coffee | 3 +-- lib/events.ts | 9 +++++---- lib/utils/patterns.ts | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/app.coffee b/lib/app.coffee index 84aa8700..3dca1e67 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -71,8 +71,6 @@ BalenaSdk.setSharedOptions( retries: 2 ) -balena = BalenaSdk.fromSharedOptions() - actions = require('./actions') errors = require('./errors') events = require('./events') @@ -86,6 +84,7 @@ update = require('./utils/update') require('any-promise/register/bluebird') capitano.permission 'user', (done) -> + balena = BalenaSdk.fromSharedOptions() balena.auth.isLoggedIn().then (isLoggedIn) -> if not isLoggedIn exitWithExpectedError(''' diff --git a/lib/events.ts b/lib/events.ts index aff7d26d..2b78b7eb 100644 --- a/lib/events.ts +++ b/lib/events.ts @@ -7,16 +7,17 @@ import Promise = require('bluebird'); import BalenaSdk = require('balena-sdk'); import packageJSON = require('../package.json'); -const balena = BalenaSdk.fromSharedOptions(); +const getBalenaSdk = _.once(() => BalenaSdk.fromSharedOptions()); const getMatchCommandAsync = Promise.promisify(Capitano.state.getMatchCommand); -const getMixpanel = _.memoize(() => - balena.models.config - .getAll() +const getMixpanel = _.once(() => + getBalenaSdk() + .models.config.getAll() .get('mixpanelToken') .then(Mixpanel.init), ); export function trackCommand(capitanoCli: Capitano.Cli) { + const balena = getBalenaSdk(); return Promise.props({ balenaUrl: balena.settings.get('balenaUrl'), username: balena.auth.whoami().catchReturn(undefined), diff --git a/lib/utils/patterns.ts b/lib/utils/patterns.ts index 1dab1210..e3a3b29e 100644 --- a/lib/utils/patterns.ts +++ b/lib/utils/patterns.ts @@ -23,12 +23,13 @@ import chalk from 'chalk'; import validation = require('./validation'); import messages = require('./messages'); -const balena = BalenaSdk.fromSharedOptions(); +const getBalenaSdk = _.once(() => BalenaSdk.fromSharedOptions()); const getForm = _.once((): typeof _form => require('resin-cli-form')); const getVisuals = _.once((): typeof _visuals => require('resin-cli-visuals')); export function authenticate(options: {}): Promise { + const balena = getBalenaSdk(); return getForm() .run( [ @@ -101,17 +102,19 @@ export function askLoginType() { } export function selectDeviceType() { - return balena.models.config.getDeviceTypes().then(deviceTypes => { - deviceTypes = _.sortBy(deviceTypes, 'name'); - return getForm().ask({ - message: 'Device Type', - type: 'list', - choices: _.map(deviceTypes, ({ slug: value, name }) => ({ - name, - value, - })), + return getBalenaSdk() + .models.config.getDeviceTypes() + .then(deviceTypes => { + deviceTypes = _.sortBy(deviceTypes, 'name'); + return getForm().ask({ + message: 'Device Type', + type: 'list', + choices: _.map(deviceTypes, ({ slug: value, name }) => ({ + name, + value, + })), + }); }); - }); } export function confirm( @@ -142,6 +145,7 @@ export function confirm( export function selectApplication( filter: (app: BalenaSdk.Application) => boolean, ) { + const balena = getBalenaSdk(); return balena.models.application .hasAny() .then(function(hasAnyApplications) { @@ -165,6 +169,7 @@ export function selectApplication( } export function selectOrCreateApplication() { + const balena = getBalenaSdk(); return balena.models.application .hasAny() .then(hasAnyApplications => { @@ -205,6 +210,7 @@ export function selectOrCreateApplication() { } export function awaitDevice(uuid: string) { + const balena = getBalenaSdk(); return balena.models.device.getName(uuid).then(deviceName => { const visuals = getVisuals(); const spinner = new visuals.Spinner( @@ -233,6 +239,7 @@ export function awaitDevice(uuid: string) { } export function inferOrSelectDevice(preferredUuid: string) { + const balena = getBalenaSdk(); return balena.models.device .getAll() .filter(device => device.is_online)