Lazy-load resin-cli-form and resin-cli-visuals to speed up startup

Change-type: patch
This commit is contained in:
Pagan Gazzard 2019-01-11 17:52:06 +00:00
parent e0bd6b9d4e
commit f780d47198
2 changed files with 17 additions and 13 deletions

View File

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

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 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 => ({