mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-07 11:31:15 +00:00
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:
commit
49449e42be
@ -1,6 +1,5 @@
|
|||||||
Promise = require('bluebird')
|
Promise = require('bluebird')
|
||||||
_ = require('lodash')
|
_ = require('lodash')
|
||||||
form = require('resin-cli-form')
|
|
||||||
chalk = require('chalk')
|
chalk = require('chalk')
|
||||||
|
|
||||||
dockerUtils = require('../../utils/docker')
|
dockerUtils = require('../../utils/docker')
|
||||||
@ -15,6 +14,7 @@ exports.filterOutSupervisorContainer = filterOutSupervisorContainer = (container
|
|||||||
return true
|
return true
|
||||||
|
|
||||||
exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) ->
|
exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor = false) ->
|
||||||
|
form = require('resin-cli-form')
|
||||||
docker = dockerUtils.createClient(host: deviceIp, port: dockerPort, timeout: dockerTimeout)
|
docker = dockerUtils.createClient(host: deviceIp, port: dockerPort, timeout: dockerTimeout)
|
||||||
|
|
||||||
# List all containers, including those not running
|
# List all containers, including those not running
|
||||||
|
@ -19,8 +19,6 @@ import chalk from 'chalk';
|
|||||||
import { stripIndent } from 'common-tags';
|
import { stripIndent } from 'common-tags';
|
||||||
import * as sdk from 'etcher-sdk';
|
import * as sdk from 'etcher-sdk';
|
||||||
|
|
||||||
import { DriveList } from '../../utils/visuals/drive-list';
|
|
||||||
|
|
||||||
async function getDrive(options: {
|
async function getDrive(options: {
|
||||||
drive?: string;
|
drive?: string;
|
||||||
}): Promise<sdk.sourceDestination.BlockDevice> {
|
}): Promise<sdk.sourceDestination.BlockDevice> {
|
||||||
@ -37,6 +35,7 @@ async function getDrive(options: {
|
|||||||
}
|
}
|
||||||
drive = d;
|
drive = d;
|
||||||
} else {
|
} else {
|
||||||
|
const { DriveList } = await import('../../utils/visuals/drive-list');
|
||||||
const driveList = new DriveList(scanner);
|
const driveList = new DriveList(scanner);
|
||||||
drive = await driveList.run();
|
drive = await driveList.run();
|
||||||
}
|
}
|
||||||
|
@ -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
|
See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
import _form = require('resin-cli-form');
|
||||||
|
import _visuals = require('resin-cli-visuals');
|
||||||
|
|
||||||
import _ = require('lodash');
|
import _ = require('lodash');
|
||||||
import Promise = require('bluebird');
|
import Promise = require('bluebird');
|
||||||
import form = require('resin-cli-form');
|
|
||||||
import visuals = require('resin-cli-visuals');
|
|
||||||
import BalenaSdk = require('balena-sdk');
|
import BalenaSdk = require('balena-sdk');
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import validation = require('./validation');
|
import validation = require('./validation');
|
||||||
@ -25,8 +25,11 @@ import messages = require('./messages');
|
|||||||
|
|
||||||
const balena = BalenaSdk.fromSharedOptions();
|
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> {
|
export function authenticate(options: {}): Promise<void> {
|
||||||
return form
|
return getForm()
|
||||||
.run(
|
.run(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@ -50,7 +53,7 @@ export function authenticate(options: {}): Promise<void> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return form
|
return getForm()
|
||||||
.ask({
|
.ask({
|
||||||
message: 'Two factor auth challenge:',
|
message: 'Two factor auth challenge:',
|
||||||
name: 'code',
|
name: 'code',
|
||||||
@ -72,7 +75,7 @@ export function authenticate(options: {}): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function askLoginType() {
|
export function askLoginType() {
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'How would you like to login?',
|
message: 'How would you like to login?',
|
||||||
name: 'loginType',
|
name: 'loginType',
|
||||||
type: 'list',
|
type: 'list',
|
||||||
@ -100,7 +103,7 @@ export function askLoginType() {
|
|||||||
export function selectDeviceType() {
|
export function selectDeviceType() {
|
||||||
return balena.models.config.getDeviceTypes().then(deviceTypes => {
|
return balena.models.config.getDeviceTypes().then(deviceTypes => {
|
||||||
deviceTypes = _.sortBy(deviceTypes, 'name');
|
deviceTypes = _.sortBy(deviceTypes, 'name');
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'Device Type',
|
message: 'Device Type',
|
||||||
type: 'list',
|
type: 'list',
|
||||||
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
|
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
|
||||||
@ -124,7 +127,7 @@ export function confirm(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message,
|
message,
|
||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
default: false,
|
default: false,
|
||||||
@ -150,7 +153,7 @@ export function selectApplication(
|
|||||||
})
|
})
|
||||||
.filter(filter || _.constant(true))
|
.filter(filter || _.constant(true))
|
||||||
.then(applications => {
|
.then(applications => {
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'Select an application',
|
message: 'Select an application',
|
||||||
type: 'list',
|
type: 'list',
|
||||||
choices: _.map(applications, application => ({
|
choices: _.map(applications, application => ({
|
||||||
@ -181,7 +184,7 @@ export function selectOrCreateApplication() {
|
|||||||
value: null,
|
value: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'Select an application',
|
message: 'Select an application',
|
||||||
type: 'list',
|
type: 'list',
|
||||||
choices: appOptions,
|
choices: appOptions,
|
||||||
@ -193,7 +196,7 @@ export function selectOrCreateApplication() {
|
|||||||
return application;
|
return application;
|
||||||
}
|
}
|
||||||
|
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'Choose a Name for your new application',
|
message: 'Choose a Name for your new application',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
validate: validation.validateApplicationName,
|
validate: validation.validateApplicationName,
|
||||||
@ -203,6 +206,7 @@ export function selectOrCreateApplication() {
|
|||||||
|
|
||||||
export function awaitDevice(uuid: string) {
|
export function awaitDevice(uuid: string) {
|
||||||
return balena.models.device.getName(uuid).then(deviceName => {
|
return balena.models.device.getName(uuid).then(deviceName => {
|
||||||
|
const visuals = getVisuals();
|
||||||
const spinner = new visuals.Spinner(
|
const spinner = new visuals.Spinner(
|
||||||
`Waiting for ${deviceName} to come online`,
|
`Waiting for ${deviceName} to come online`,
|
||||||
);
|
);
|
||||||
@ -243,7 +247,7 @@ export function inferOrSelectDevice(preferredUuid: string) {
|
|||||||
? preferredUuid
|
? preferredUuid
|
||||||
: onlineDevices[0].uuid;
|
: onlineDevices[0].uuid;
|
||||||
|
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message: 'Select a device',
|
message: 'Select a device',
|
||||||
type: 'list',
|
type: 'list',
|
||||||
default: defaultUuid,
|
default: defaultUuid,
|
||||||
@ -262,7 +266,7 @@ export function selectFromList<T>(
|
|||||||
message: string,
|
message: string,
|
||||||
choices: Array<T & { name: string }>,
|
choices: Array<T & { name: string }>,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
return form.ask({
|
return getForm().ask({
|
||||||
message,
|
message,
|
||||||
type: 'list',
|
type: 'list',
|
||||||
choices: _.map(choices, s => ({
|
choices: _.map(choices, s => ({
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import * as sdk from 'etcher-sdk';
|
import * as _sdk from 'etcher-sdk';
|
||||||
|
|
||||||
import { CustomDynamicList } from './custom-dynamic-list';
|
import { CustomDynamicList } from './custom-dynamic-list';
|
||||||
|
|
||||||
export class DriveList extends CustomDynamicList<
|
export class DriveList extends CustomDynamicList<
|
||||||
sdk.sourceDestination.BlockDevice
|
_sdk.sourceDestination.BlockDevice
|
||||||
> {
|
> {
|
||||||
constructor(private scanner: sdk.scanner.Scanner) {
|
constructor(private scanner: _sdk.scanner.Scanner) {
|
||||||
super(
|
super(
|
||||||
'Select a drive',
|
'Select a drive',
|
||||||
`${chalk.red('x')} No available drives were detected, plug one in!`,
|
`${chalk.red('x')} No available drives were detected, plug one in!`,
|
||||||
@ -17,6 +17,7 @@ export class DriveList extends CustomDynamicList<
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected *getThings() {
|
protected *getThings() {
|
||||||
|
const sdk: typeof _sdk = require('etcher-sdk')
|
||||||
for (const drive of this.scanner.drives) {
|
for (const drive of this.scanner.drives) {
|
||||||
if (drive instanceof sdk.sourceDestination.BlockDevice) {
|
if (drive instanceof sdk.sourceDestination.BlockDevice) {
|
||||||
yield drive;
|
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;
|
const size = drive.size / 1e9;
|
||||||
return `${drive.device} (${size.toFixed(1)} GB) - ${drive.description}`;
|
return `${drive.device} (${size.toFixed(1)} GB) - ${drive.description}`;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user