2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2019-10-31 01:46:14 +00:00
|
|
|
Copyright 2016-2019 Balena
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
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.
|
|
|
|
*/
|
2019-06-06 10:24:44 +00:00
|
|
|
import { BalenaApplicationNotFound } from 'balena-errors';
|
2020-02-27 14:55:30 +00:00
|
|
|
import * as BalenaSdk from 'balena-sdk';
|
2019-03-25 15:16:23 +00:00
|
|
|
import Bluebird = require('bluebird');
|
|
|
|
import { stripIndent } from 'common-tags';
|
2019-03-12 22:07:57 +00:00
|
|
|
import _ = require('lodash');
|
2019-01-11 17:52:06 +00:00
|
|
|
import _form = require('resin-cli-form');
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-04-18 01:31:13 +00:00
|
|
|
import { instanceOf, NotLoggedInError } from '../errors';
|
2020-02-28 15:25:12 +00:00
|
|
|
import { getBalenaSdk, getChalk, getVisuals } from './lazy';
|
2017-12-20 21:46:01 +00:00
|
|
|
import messages = require('./messages');
|
2019-03-12 22:07:57 +00:00
|
|
|
import validation = require('./validation');
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-01-11 17:52:06 +00:00
|
|
|
const getForm = _.once((): typeof _form => require('resin-cli-form'));
|
|
|
|
|
2019-03-25 15:16:23 +00:00
|
|
|
export function authenticate(options: {}): Bluebird<void> {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm()
|
2018-01-09 15:05:24 +00:00
|
|
|
.run(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
message: 'Email:',
|
|
|
|
name: 'email',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateEmail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
message: 'Password:',
|
|
|
|
name: 'password',
|
|
|
|
type: 'password',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ override: options },
|
|
|
|
)
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(balena.auth.login)
|
|
|
|
.then(balena.auth.twoFactor.isPassed)
|
2018-01-09 15:05:24 +00:00
|
|
|
.then((isTwoFactorAuthPassed: boolean) => {
|
|
|
|
if (isTwoFactorAuthPassed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm()
|
2018-01-09 15:05:24 +00:00
|
|
|
.ask({
|
|
|
|
message: 'Two factor auth challenge:',
|
|
|
|
name: 'code',
|
|
|
|
type: 'input',
|
|
|
|
})
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(balena.auth.twoFactor.challenge)
|
2018-01-09 15:05:24 +00:00
|
|
|
.catch((error: any) => {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.auth.logout().then(() => {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (
|
2018-10-19 14:38:50 +00:00
|
|
|
error.name === 'BalenaRequestError' &&
|
2018-01-09 15:05:24 +00:00
|
|
|
error.statusCode === 401
|
|
|
|
) {
|
|
|
|
throw new Error('Invalid two factor authentication code');
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
2018-01-04 16:17:43 +00:00
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Check if logged in, and throw `NotLoggedInError` if not.
|
|
|
|
* Note: `NotLoggedInError` is an `ExpectedError`.
|
|
|
|
*/
|
2019-10-31 01:40:57 +00:00
|
|
|
export async function checkLoggedIn(): Promise<void> {
|
2019-03-25 15:16:23 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
if (!(await balena.auth.isLoggedIn())) {
|
2019-10-31 01:40:57 +00:00
|
|
|
throw new NotLoggedInError(stripIndent`
|
2019-03-25 15:16:23 +00:00
|
|
|
You have to log in to continue
|
|
|
|
Run the following command to go through the login wizard:
|
|
|
|
$ balena login`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Check if logged in, and call `exitWithExpectedError()` if not.
|
|
|
|
* DEPRECATED: Use checkLoggedIn() instead.
|
|
|
|
*/
|
2019-10-31 01:40:57 +00:00
|
|
|
export async function exitIfNotLoggedIn(): Promise<void> {
|
|
|
|
try {
|
|
|
|
await checkLoggedIn();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof NotLoggedInError) {
|
|
|
|
exitWithExpectedError(error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
export function askLoginType() {
|
2020-02-05 17:55:29 +00:00
|
|
|
return getForm().ask<'web' | 'credentials' | 'token' | 'register'>({
|
2017-12-20 21:46:01 +00:00
|
|
|
message: 'How would you like to login?',
|
|
|
|
name: 'loginType',
|
|
|
|
type: 'list',
|
2018-01-09 15:05:24 +00:00
|
|
|
choices: [
|
|
|
|
{
|
|
|
|
name: 'Web authorization (recommended)',
|
|
|
|
value: 'web',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Credentials',
|
|
|
|
value: 'credentials',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Authentication token',
|
|
|
|
value: 'token',
|
|
|
|
},
|
|
|
|
{
|
2018-10-19 14:38:50 +00:00
|
|
|
name: "I don't have a balena account!",
|
2018-01-09 15:05:24 +00:00
|
|
|
value: 'register',
|
|
|
|
},
|
|
|
|
],
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function selectDeviceType() {
|
2019-01-12 21:20:19 +00:00
|
|
|
return getBalenaSdk()
|
|
|
|
.models.config.getDeviceTypes()
|
|
|
|
.then(deviceTypes => {
|
2019-11-14 09:53:38 +00:00
|
|
|
deviceTypes = _.sortBy(deviceTypes, 'name').filter(
|
|
|
|
dt => dt.state !== 'DISCONTINUED',
|
|
|
|
);
|
2019-01-12 21:20:19 +00:00
|
|
|
return getForm().ask({
|
|
|
|
message: 'Device Type',
|
|
|
|
type: 'list',
|
|
|
|
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
})),
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 15:05:24 +00:00
|
|
|
export function confirm(
|
2018-10-25 12:57:45 +00:00
|
|
|
yesOption: boolean,
|
2018-01-09 15:05:24 +00:00
|
|
|
message: string,
|
2018-10-25 12:57:45 +00:00
|
|
|
yesMessage?: string,
|
2019-09-30 19:56:57 +00:00
|
|
|
exitIfDeclined = false,
|
2018-01-09 15:05:24 +00:00
|
|
|
) {
|
2019-03-25 15:16:23 +00:00
|
|
|
return Bluebird.try(function() {
|
2017-12-20 21:46:01 +00:00
|
|
|
if (yesOption) {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (yesMessage) {
|
|
|
|
console.log(yesMessage);
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-31 18:22:40 +00:00
|
|
|
return getForm().ask<boolean>({
|
2017-12-20 21:46:01 +00:00
|
|
|
message,
|
|
|
|
type: 'confirm',
|
2018-01-04 14:07:55 +00:00
|
|
|
default: false,
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
|
|
|
}).then(function(confirmed) {
|
|
|
|
if (!confirmed) {
|
2019-09-30 19:56:57 +00:00
|
|
|
const err = new Error('Aborted');
|
|
|
|
if (exitIfDeclined) {
|
|
|
|
exitWithExpectedError(err);
|
|
|
|
}
|
|
|
|
throw err;
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:14:28 +00:00
|
|
|
export function selectApplication(
|
2020-04-25 10:47:23 +00:00
|
|
|
filter?: (app: BalenaSdk.Application) => boolean,
|
2018-10-19 11:14:28 +00:00
|
|
|
) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-01-09 15:05:24 +00:00
|
|
|
.hasAny()
|
|
|
|
.then(function(hasAnyApplications) {
|
|
|
|
if (!hasAnyApplications) {
|
|
|
|
throw new Error("You don't have any applications");
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application.getAll();
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.filter(filter || _.constant(true))
|
|
|
|
.then(applications => {
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
|
|
|
choices: _.map(applications, application => ({
|
2017-12-20 21:46:01 +00:00
|
|
|
name: `${application.app_name} (${application.device_type})`,
|
2018-01-04 14:07:55 +00:00
|
|
|
value: application.app_name,
|
2018-01-09 15:05:24 +00:00
|
|
|
})),
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function selectOrCreateApplication() {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-01-09 15:05:24 +00:00
|
|
|
.hasAny()
|
|
|
|
.then(hasAnyApplications => {
|
2019-03-12 22:07:57 +00:00
|
|
|
if (!hasAnyApplications) {
|
2019-05-31 18:22:40 +00:00
|
|
|
// Just to make TS happy
|
|
|
|
return Promise.resolve(undefined);
|
2019-03-12 22:07:57 +00:00
|
|
|
}
|
2018-01-09 15:05:24 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application.getAll().then(applications => {
|
2018-01-09 15:05:24 +00:00
|
|
|
const appOptions = _.map<
|
2018-10-19 14:38:50 +00:00
|
|
|
BalenaSdk.Application,
|
2018-01-09 15:05:24 +00:00
|
|
|
{ name: string; value: string | null }
|
|
|
|
>(applications, application => ({
|
|
|
|
name: `${application.app_name} (${application.device_type})`,
|
|
|
|
value: application.app_name,
|
|
|
|
}));
|
|
|
|
|
|
|
|
appOptions.unshift({
|
|
|
|
name: 'Create a new application',
|
|
|
|
value: null,
|
|
|
|
});
|
|
|
|
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
|
|
|
choices: appOptions,
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.then(application => {
|
|
|
|
if (application) {
|
|
|
|
return application;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Choose a Name for your new application',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateApplicationName,
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function awaitDevice(uuid: string) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device.getName(uuid).then(deviceName => {
|
2019-01-11 17:52:06 +00:00
|
|
|
const visuals = getVisuals();
|
2018-01-09 15:05:24 +00:00
|
|
|
const spinner = new visuals.Spinner(
|
|
|
|
`Waiting for ${deviceName} to come online`,
|
|
|
|
);
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-03-25 15:16:23 +00:00
|
|
|
const poll = (): Bluebird<void> => {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device.isOnline(uuid).then(function(isOnline) {
|
2017-12-20 21:46:01 +00:00
|
|
|
if (isOnline) {
|
|
|
|
spinner.stop();
|
|
|
|
console.info(`The device **${deviceName}** is online!`);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Spinner implementation is smart enough to
|
|
|
|
// not start again if it was already started
|
|
|
|
spinner.start();
|
|
|
|
|
2019-03-25 15:16:23 +00:00
|
|
|
return Bluebird.delay(3000).then(poll);
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
};
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
console.info(`Waiting for ${deviceName} to connect to balena...`);
|
2017-12-20 21:46:01 +00:00
|
|
|
return poll().return(uuid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:08:15 +00:00
|
|
|
export function awaitDeviceOsUpdate(uuid: string, targetOsVersion: string) {
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
|
|
|
return balena.models.device.getName(uuid).then(deviceName => {
|
|
|
|
const visuals = getVisuals();
|
|
|
|
const progressBar = new visuals.Progress(
|
|
|
|
`Updating the OS of ${deviceName} to v${targetOsVersion}`,
|
|
|
|
);
|
|
|
|
progressBar.update({ percentage: 0 });
|
|
|
|
|
|
|
|
const poll = (): Bluebird<void> => {
|
|
|
|
return Bluebird.all([
|
|
|
|
balena.models.device.getOsUpdateStatus(uuid),
|
2020-04-13 15:16:09 +00:00
|
|
|
balena.models.device.get(uuid, { $select: 'overall_progress' }),
|
|
|
|
]).then(([osUpdateStatus, { overall_progress: osUpdateProgress }]) => {
|
2020-04-15 10:39:27 +00:00
|
|
|
if (osUpdateStatus.status === 'done') {
|
2019-06-11 12:08:15 +00:00
|
|
|
console.info(
|
|
|
|
`The device ${deviceName} has been updated to v${targetOsVersion} and will restart shortly!`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osUpdateStatus.error) {
|
|
|
|
console.error(
|
|
|
|
`Failed to complete Host OS update on device ${deviceName}!`,
|
|
|
|
);
|
|
|
|
exitWithExpectedError(osUpdateStatus.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 10:39:27 +00:00
|
|
|
if (osUpdateProgress !== null) {
|
|
|
|
// Avoid resetting to 0% at end of process when device goes offline.
|
|
|
|
progressBar.update({ percentage: osUpdateProgress });
|
|
|
|
}
|
2019-06-11 12:08:15 +00:00
|
|
|
|
|
|
|
return Bluebird.delay(3000).then(poll);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return poll().return(uuid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
export function inferOrSelectDevice(preferredUuid: string) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device
|
2018-01-09 15:05:24 +00:00
|
|
|
.getAll()
|
2018-10-19 14:38:50 +00:00
|
|
|
.filter<BalenaSdk.Device>(device => device.is_online)
|
2018-01-09 15:05:24 +00:00
|
|
|
.then(onlineDevices => {
|
|
|
|
if (_.isEmpty(onlineDevices)) {
|
|
|
|
throw new Error("You don't have any devices online");
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-16 10:25:37 +00:00
|
|
|
const defaultUuid = _(onlineDevices)
|
|
|
|
.map('uuid')
|
|
|
|
.includes(preferredUuid)
|
2018-01-09 15:05:24 +00:00
|
|
|
? preferredUuid
|
|
|
|
: onlineDevices[0].uuid;
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-01-11 17:52:06 +00:00
|
|
|
return getForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select a device',
|
|
|
|
type: 'list',
|
|
|
|
default: defaultUuid,
|
|
|
|
choices: _.map(onlineDevices, device => ({
|
2018-10-19 11:14:28 +00:00
|
|
|
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
|
|
|
|
0,
|
|
|
|
7,
|
|
|
|
)})`,
|
2018-01-09 15:05:24 +00:00
|
|
|
value: device.uuid,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 10:24:44 +00:00
|
|
|
export async function getOnlineTargetUuid(
|
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
applicationOrDevice: string,
|
|
|
|
) {
|
2019-06-26 09:51:05 +00:00
|
|
|
const Logger = await import('../utils/logger');
|
2019-09-11 18:34:43 +00:00
|
|
|
const logger = Logger.getLogger();
|
2019-06-06 10:24:44 +00:00
|
|
|
const appTest = validation.validateApplicationName(applicationOrDevice);
|
|
|
|
const uuidTest = validation.validateUuid(applicationOrDevice);
|
|
|
|
|
|
|
|
if (!appTest && !uuidTest) {
|
|
|
|
throw new Error(`Device or application not found: ${applicationOrDevice}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a definite device UUID...
|
|
|
|
if (uuidTest && !appTest) {
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching device by UUID ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2020-01-20 21:21:05 +00:00
|
|
|
return (
|
|
|
|
await sdk.models.device.get(applicationOrDevice, {
|
|
|
|
$select: ['uuid'],
|
|
|
|
$filter: { is_online: true },
|
|
|
|
})
|
|
|
|
).uuid;
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, it may be a device OR an application...
|
|
|
|
try {
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching application by name ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2019-06-06 10:24:44 +00:00
|
|
|
const app = await sdk.models.application.get(applicationOrDevice);
|
|
|
|
const devices = await sdk.models.device.getAllByApplication(app.id, {
|
|
|
|
$filter: { is_online: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (_.isEmpty(devices)) {
|
|
|
|
throw new Error('No accessible devices are online');
|
|
|
|
}
|
|
|
|
|
|
|
|
return await getForm().ask({
|
|
|
|
message: 'Select a device',
|
|
|
|
type: 'list',
|
|
|
|
default: devices[0].uuid,
|
|
|
|
choices: _.map(devices, device => ({
|
|
|
|
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
|
|
|
|
0,
|
|
|
|
7,
|
|
|
|
)})`,
|
|
|
|
value: device.uuid,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2020-04-18 01:31:13 +00:00
|
|
|
if (!instanceOf(err, BalenaApplicationNotFound)) {
|
2019-06-06 10:24:44 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(`Application not found`);
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// it wasn't an application, maybe it's a device...
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching device by UUID ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2020-01-20 21:21:05 +00:00
|
|
|
return (
|
|
|
|
await sdk.models.device.get(applicationOrDevice, {
|
|
|
|
$select: ['uuid'],
|
|
|
|
$filter: { is_online: true },
|
|
|
|
})
|
|
|
|
).uuid;
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
export function selectFromList<T>(
|
|
|
|
message: string,
|
|
|
|
choices: Array<T & { name: string }>,
|
2019-03-25 15:16:23 +00:00
|
|
|
): Bluebird<T> {
|
2019-05-31 18:22:40 +00:00
|
|
|
return getForm().ask<T>({
|
2018-04-25 14:20:07 +00:00
|
|
|
message,
|
|
|
|
type: 'list',
|
2018-05-18 20:31:08 +00:00
|
|
|
choices: _.map(choices, s => ({
|
2018-04-25 14:20:07 +00:00
|
|
|
name: s.name,
|
|
|
|
value: s,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
export function printErrorMessage(message: string) {
|
2020-02-28 15:25:12 +00:00
|
|
|
const chalk = getChalk();
|
2017-12-20 21:46:01 +00:00
|
|
|
console.error(chalk.red(message));
|
|
|
|
console.error(chalk.red(`\n${messages.getHelp}\n`));
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2019-02-07 15:10:16 +00:00
|
|
|
/**
|
|
|
|
* Print a friendly error message and exit the CLI with an error code, BYPASSING
|
|
|
|
* error reporting through Sentry.io's platform (raven.Raven.captureException).
|
|
|
|
* Note that lib/errors.ts provides top-level error handling code to catch any
|
|
|
|
* otherwise uncaught errors, AND to report them through Sentry.io. But many
|
|
|
|
* "expected" errors (say, a JSON parsing error in a file provided by the user)
|
|
|
|
* don't warrant reporting through Sentry.io. For such mundane errors, catch
|
|
|
|
* them and call this function.
|
2020-04-15 10:39:27 +00:00
|
|
|
*
|
|
|
|
* DEPRECATED: Use `throw new ExpectedError(<message>)` instead.
|
2019-02-07 15:10:16 +00:00
|
|
|
*/
|
2018-10-25 12:57:45 +00:00
|
|
|
export function exitWithExpectedError(message: string | Error): never {
|
2017-12-20 21:46:01 +00:00
|
|
|
if (message instanceof Error) {
|
|
|
|
({ message } = message);
|
|
|
|
}
|
|
|
|
|
|
|
|
printErrorMessage(message);
|
2018-12-13 21:02:27 +00:00
|
|
|
process.exit(1);
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|