Lazy load the sdk as much as possible

Change-type: patch
This commit is contained in:
Pagan Gazzard 2019-01-12 21:20:19 +00:00
parent 99650ab732
commit 77196746b3
3 changed files with 24 additions and 17 deletions

View File

@ -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('''

View File

@ -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<any>(() =>
balena.models.config
.getAll()
const getMixpanel = _.once<any>(() =>
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),

View File

@ -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<void> {
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<BalenaSdk.Device>(device => device.is_online)