From 051268168a67bcf54cb0d95fc3b45d07f12eb4f6 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Fri, 31 May 2019 21:22:40 +0300 Subject: [PATCH] Add initial typings for resin-cli-form Change-type: patch Signed-off-by: Thodoris Greasidis --- lib/utils/patterns.ts | 7 +++--- lib/utils/promote.ts | 2 +- typings/resin-cli-form/index.d.ts | 39 ++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/utils/patterns.ts b/lib/utils/patterns.ts index f999f30c..7815b9fd 100644 --- a/lib/utils/patterns.ts +++ b/lib/utils/patterns.ts @@ -141,7 +141,7 @@ export function confirm( return true; } - return getForm().ask({ + return getForm().ask({ message, type: 'confirm', default: false, @@ -185,7 +185,8 @@ export function selectOrCreateApplication() { .hasAny() .then(hasAnyApplications => { if (!hasAnyApplications) { - return; + // Just to make TS happy + return Promise.resolve(undefined); } return balena.models.application.getAll().then(applications => { @@ -286,7 +287,7 @@ export function selectFromList( message: string, choices: Array, ): Bluebird { - return getForm().ask({ + return getForm().ask({ message, type: 'list', choices: _.map(choices, s => ({ diff --git a/lib/utils/promote.ts b/lib/utils/promote.ts index 37bfc58f..9406f2b5 100644 --- a/lib/utils/promote.ts +++ b/lib/utils/promote.ts @@ -376,7 +376,7 @@ async function generateApplicationConfig( const opts = manifest.options && manifest.options.filter(opt => opt.name !== 'network'); const values = { - ...(await form.run(opts)), + ...(opts ? await form.run(opts) : {}), ...options, }; diff --git a/typings/resin-cli-form/index.d.ts b/typings/resin-cli-form/index.d.ts index 4fcdef7e..e8877934 100644 --- a/typings/resin-cli-form/index.d.ts +++ b/typings/resin-cli-form/index.d.ts @@ -15,4 +15,41 @@ * limitations under the License. */ -declare module 'resin-cli-form'; +declare module 'resin-cli-form' { + import Bluebird = require('bluebird'); + + type TypeOrPromiseLike = T | PromiseLike; + + type Validate = ( + input: any, + ) => TypeOrPromiseLike; + + 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: (options: AskOptions) => Bluebird; + run: ( + questions: RunQuestion[], + extraOptions?: { override: object }, + ) => Bluebird; + }; + + export = form; +}