Add initial typings for resin-cli-form

Change-type: patch
Signed-off-by: Thodoris Greasidis <thodoris@balena.io>
This commit is contained in:
Thodoris Greasidis 2019-05-31 21:22:40 +03:00
parent 2b264df41b
commit 051268168a
3 changed files with 43 additions and 5 deletions

View File

@ -141,7 +141,7 @@ export function confirm(
return true; return true;
} }
return getForm().ask({ return getForm().ask<boolean>({
message, message,
type: 'confirm', type: 'confirm',
default: false, default: false,
@ -185,7 +185,8 @@ export function selectOrCreateApplication() {
.hasAny() .hasAny()
.then(hasAnyApplications => { .then(hasAnyApplications => {
if (!hasAnyApplications) { if (!hasAnyApplications) {
return; // Just to make TS happy
return Promise.resolve(undefined);
} }
return balena.models.application.getAll().then(applications => { return balena.models.application.getAll().then(applications => {
@ -286,7 +287,7 @@ export function selectFromList<T>(
message: string, message: string,
choices: Array<T & { name: string }>, choices: Array<T & { name: string }>,
): Bluebird<T> { ): Bluebird<T> {
return getForm().ask({ return getForm().ask<T>({
message, message,
type: 'list', type: 'list',
choices: _.map(choices, s => ({ choices: _.map(choices, s => ({

View File

@ -376,7 +376,7 @@ async function generateApplicationConfig(
const opts = const opts =
manifest.options && manifest.options.filter(opt => opt.name !== 'network'); manifest.options && manifest.options.filter(opt => opt.name !== 'network');
const values = { const values = {
...(await form.run(opts)), ...(opts ? await form.run(opts) : {}),
...options, ...options,
}; };

View File

@ -15,4 +15,41 @@
* limitations under the License. * limitations under the License.
*/ */
declare module 'resin-cli-form'; declare module 'resin-cli-form' {
import Bluebird = require('bluebird');
type TypeOrPromiseLike<T> = T | PromiseLike<T>;
type Validate = (
input: any,
) => TypeOrPromiseLike<boolean | string | undefined>;
interface AskOptions {
message: string;
type?: string;
name?: string;
default?: T;
choices?: Array<{
name: string;
value: T;
}>;
validate?: Validate;
}
interface RunQuestion {
message: string;
name: string;
type?: string;
validate?: Validate;
}
const form: {
ask: <T = string>(options: AskOptions) => Bluebird<T>;
run: <T = any>(
questions: RunQuestion[],
extraOptions?: { override: object },
) => Bluebird<T>;
};
export = form;
}