Merge pull request #1072 from balena-io/lazy-load-patterns

Lazy-load resin-cli-form and resin-cli-visuals to speed up startup
This commit is contained in:
Page- 2019-01-11 18:48:05 +00:00 committed by GitHub
commit 49449e42be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 19 deletions

View File

@ -1,6 +1,5 @@
Promise = require('bluebird')
_ = require('lodash')
form = require('resin-cli-form')
chalk = require('chalk')
dockerUtils = require('../../utils/docker')
@ -15,6 +14,7 @@ exports.filterOutSupervisorContainer = filterOutSupervisorContainer = (container
return true
exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) ->
form = require('resin-cli-form')
docker = dockerUtils.createClient(host: deviceIp, port: dockerPort, timeout: dockerTimeout)
# List all containers, including those not running

View File

@ -19,8 +19,6 @@ import chalk from 'chalk';
import { stripIndent } from 'common-tags';
import * as sdk from 'etcher-sdk';
import { DriveList } from '../../utils/visuals/drive-list';
async function getDrive(options: {
drive?: string;
}): Promise<sdk.sourceDestination.BlockDevice> {
@ -37,6 +35,7 @@ async function getDrive(options: {
}
drive = d;
} else {
const { DriveList } = await import('../../utils/visuals/drive-list');
const driveList = new DriveList(scanner);
drive = await driveList.run();
}

View File

@ -13,11 +13,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import _form = require('resin-cli-form');
import _visuals = require('resin-cli-visuals');
import _ = require('lodash');
import Promise = require('bluebird');
import form = require('resin-cli-form');
import visuals = require('resin-cli-visuals');
import BalenaSdk = require('balena-sdk');
import chalk from 'chalk';
import validation = require('./validation');
@ -25,8 +25,11 @@ import messages = require('./messages');
const balena = 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> {
return form
return getForm()
.run(
[
{
@ -50,7 +53,7 @@ export function authenticate(options: {}): Promise<void> {
return;
}
return form
return getForm()
.ask({
message: 'Two factor auth challenge:',
name: 'code',
@ -72,7 +75,7 @@ export function authenticate(options: {}): Promise<void> {
}
export function askLoginType() {
return form.ask({
return getForm().ask({
message: 'How would you like to login?',
name: 'loginType',
type: 'list',
@ -100,7 +103,7 @@ export function askLoginType() {
export function selectDeviceType() {
return balena.models.config.getDeviceTypes().then(deviceTypes => {
deviceTypes = _.sortBy(deviceTypes, 'name');
return form.ask({
return getForm().ask({
message: 'Device Type',
type: 'list',
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
@ -124,7 +127,7 @@ export function confirm(
return true;
}
return form.ask({
return getForm().ask({
message,
type: 'confirm',
default: false,
@ -150,7 +153,7 @@ export function selectApplication(
})
.filter(filter || _.constant(true))
.then(applications => {
return form.ask({
return getForm().ask({
message: 'Select an application',
type: 'list',
choices: _.map(applications, application => ({
@ -181,7 +184,7 @@ export function selectOrCreateApplication() {
value: null,
});
return form.ask({
return getForm().ask({
message: 'Select an application',
type: 'list',
choices: appOptions,
@ -193,7 +196,7 @@ export function selectOrCreateApplication() {
return application;
}
return form.ask({
return getForm().ask({
message: 'Choose a Name for your new application',
type: 'input',
validate: validation.validateApplicationName,
@ -203,6 +206,7 @@ export function selectOrCreateApplication() {
export function awaitDevice(uuid: string) {
return balena.models.device.getName(uuid).then(deviceName => {
const visuals = getVisuals();
const spinner = new visuals.Spinner(
`Waiting for ${deviceName} to come online`,
);
@ -243,7 +247,7 @@ export function inferOrSelectDevice(preferredUuid: string) {
? preferredUuid
: onlineDevices[0].uuid;
return form.ask({
return getForm().ask({
message: 'Select a device',
type: 'list',
default: defaultUuid,
@ -262,7 +266,7 @@ export function selectFromList<T>(
message: string,
choices: Array<T & { name: string }>,
): Promise<T> {
return form.ask({
return getForm().ask({
message,
type: 'list',
choices: _.map(choices, s => ({

View File

@ -1,12 +1,12 @@
import chalk from 'chalk';
import * as sdk from 'etcher-sdk';
import * as _sdk from 'etcher-sdk';
import { CustomDynamicList } from './custom-dynamic-list';
export class DriveList extends CustomDynamicList<
sdk.sourceDestination.BlockDevice
_sdk.sourceDestination.BlockDevice
> {
constructor(private scanner: sdk.scanner.Scanner) {
constructor(private scanner: _sdk.scanner.Scanner) {
super(
'Select a drive',
`${chalk.red('x')} No available drives were detected, plug one in!`,
@ -17,6 +17,7 @@ export class DriveList extends CustomDynamicList<
}
protected *getThings() {
const sdk: typeof _sdk = require('etcher-sdk')
for (const drive of this.scanner.drives) {
if (drive instanceof sdk.sourceDestination.BlockDevice) {
yield drive;
@ -24,7 +25,7 @@ export class DriveList extends CustomDynamicList<
}
}
protected format(drive: sdk.sourceDestination.BlockDevice) {
protected format(drive: _sdk.sourceDestination.BlockDevice) {
const size = drive.size / 1e9;
return `${drive.device} (${size.toFixed(1)} GB) - ${drive.description}`;
}