Skip to content

Commit

Permalink
feat: Infer prompt answers (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
fratzinger committed Jul 9, 2023
1 parent 4d052fa commit a20827a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
27 changes: 27 additions & 0 deletions packages/pinion/src/declarations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Question, QuestionCollection } from 'inquirer'

type InferAnswerType<Q extends Question> = Q extends { type: 'input' }
? string
: Q extends { type: 'list' }
? string
: Q extends { type: 'rawlist' }
? string
: Q extends { type: 'number' }
? number
: Q extends { type: 'password' }
? string
: Q extends { type: 'confirm' }
? boolean
: Q extends { type: 'checkbox' }
? string[]
: Q extends { type: 'editor' }
? string
: unknown

export type InferAnswerTypes<Q extends QuestionCollection> = Q extends ReadonlyArray<
Question & { name: string }
>
? { [K in Q[number] as K['name']]: InferAnswerType<K> }
: {
[K in keyof Q]: InferAnswerType<Q[K]>
}
2 changes: 1 addition & 1 deletion packages/pinion/src/gpt/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const checkLogin = async <C extends PinionContext>(ctx: C) => {
user
}
} catch (error) {
const { loginConfirm } = await prompt<C & { loginConfirm?: boolean }>([
const { loginConfirm } = await prompt([
{
type: 'confirm',
name: 'loginConfirm',
Expand Down
1 change: 1 addition & 0 deletions packages/pinion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './core'
export * from './ops/index'
export * from './cli/index'
export * from './gpt/index'
export * from './declarations'
6 changes: 3 additions & 3 deletions packages/pinion/src/ops/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QuestionCollection } from 'inquirer'

import { PinionContext, Callable, getCallable } from '../core'
import { InferAnswerTypes } from '../declarations'
import { addTrace } from './helpers'

/**
Expand All @@ -10,13 +10,13 @@ import { addTrace } from './helpers'
* @returns The generator context updated with the prompt results
*/
export const prompt =
<C extends PinionContext, R extends PinionContext = C>(prompts: Callable<QuestionCollection, C>) =>
<C extends PinionContext, Q extends QuestionCollection = QuestionCollection>(prompts: Callable<Q, C>) =>
async <T extends C>(ctx: T) => {
const answers = await ctx.pinion.prompt(await getCallable(prompts, ctx))
const result = {
...ctx,
...answers
} as R
} as C & InferAnswerTypes<Q>

return addTrace(result, 'prompt', answers)
}
4 changes: 2 additions & 2 deletions packages/pinion/test/templates/pinion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const generate = (ctx: GeneratorArguments) =>
.then(inject('<!-- Prepended -->', prepend(), toHelloMd))
.then(inject('<!-- Appended -->', append(), toHelloMd))
.then(
prompt<GeneratorArguments, GeneratorContext>((ctx) => [
prompt<GeneratorArguments>((ctx) => [
{
type: 'input',
name: 'name',
Expand All @@ -53,7 +53,7 @@ export const generate = (ctx: GeneratorArguments) =>
)
.then(copyFiles(fromFile(__dirname), toFile('tmp', 'copy')))
.then(runGenerators(__dirname))
.then((ctx: GeneratorContext) => ({
.then((ctx) => ({
...ctx,
finalized: true
}))

0 comments on commit a20827a

Please sign in to comment.