Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Re-written inquirer package with new prompts #1173

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@
}
},
{
"files": ["packages/inquirer/test/**", "packages/**/*.test.*"],
"files": [
"packages/inquirer/test/**",
"packages/**/*.test.*",
"canary/**/*.test.*"
],
"rules": {
"n/no-extraneous-import": [
"error",
Expand All @@ -79,6 +83,24 @@
}
]
}
},
{
"files": ["packages/**/*.test-d.*", "canary/**/*.test-d.*"],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"n/no-extraneous-import": [
"error",
{
"allowModules": ["vitest"]
}
],
"n/no-extraneous-require": [
"error",
{
"allowModules": ["vitest"]
}
]
}
}
]
}
14 changes: 14 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ jobs:
run: yarn turbo tsc
- name: Integration tests
run: yarn node --test integration/

TypesTest:
name: Typing validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- run: yarn install --immutable --immutable-cache
- name: Typescript
run: yarn tsc --project tsconfig.test.json
5 changes: 5 additions & 0 deletions canary/inquirer/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-await-in-loop": "off"
}
}
35 changes: 35 additions & 0 deletions canary/inquirer/demo.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import inquirer from './src/index.mjs';

const answers = await inquirer.prompt([
{
type: 'input',
name: 'first_name',
message: "What's your first name",
},
{
type: 'select',
name: 'ask_last_name',
message: 'Are you willing to share your last name',
choices: [
{ value: '1', name: 'Yes' },
{ value: '', name: 'No' },
],
},
{
type: 'input',
name: 'last_name',
when: (currentAnswers) => Boolean(currentAnswers.ask_last_name),
message: "What's your last name",
},
{
type: 'input',
name: 'phone',
message: "What's your phone number?",
filter(answer: string): string {
// TODO: What's the multi match flag again?

Check warning on line 29 in canary/inquirer/demo.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: What's the multi match flag again?'

Check warning on line 29 in canary/inquirer/demo.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: What's the multi match flag again?'
return answer.replace(/[^\d]+/, '');
},
},
]);

console.log(answers);
92 changes: 92 additions & 0 deletions canary/inquirer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"name": "@inquirer/canary",
"version": "0.0.0",
"description": "Inquirer input text prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.ts",
"files": [
"dist/**/*"
],
"repository": {
"type": "git",
"url": "https://github.com/SBoudrias/Inquirer.js.git"
},
"keywords": [
"answer",
"answers",
"ask",
"base",
"cli",
"command",
"command-line",
"confirm",
"enquirer",
"generate",
"generator",
"hyper",
"input",
"inquire",
"inquirer",
"interface",
"iterm",
"javascript",
"menu",
"node",
"nodejs",
"prompt",
"promptly",
"prompts",
"question",
"readline",
"scaffold",
"scaffolder",
"scaffolding",
"stdin",
"stdout",
"terminal",
"tty",
"ui",
"yeoman",
"yo",
"zsh"
],
"author": "Simon Boudrias <admin@simonboudrias.com>",
"license": "MIT",
"homepage": "https://github.com/SBoudrias/Inquirer.js",
"dependencies": {
"@inquirer/checkbox": "^2.1.0",
"@inquirer/confirm": "^3.0.0",
"@inquirer/core": "^7.0.0",
"@inquirer/editor": "^2.0.0",
"@inquirer/expand": "^2.0.0",
"@inquirer/input": "^2.0.0",
"@inquirer/password": "^2.0.0",
"@inquirer/rawlist": "^2.0.0",
"@inquirer/select": "^2.0.0",
"@inquirer/type": "^1.0.5",
"lodash": "^4.17.21"
},
"scripts": {
"tsc": "yarn run tsc:esm && yarn run tsc:cjs",
"tsc:esm": "rm -rf dist/esm && tsc -p ./tsconfig.json",
"tsc:cjs": "rm -rf dist/cjs && tsc -p ./tsconfig.cjs.json && node ../../tools/fix-ext.mjs"
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=18"
},
"exports": {
".": {
"import": {
"types": "./dist/esm/types/index.d.mts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
}
}
116 changes: 116 additions & 0 deletions canary/inquirer/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import checkbox from '@inquirer/checkbox';
import confirm from '@inquirer/confirm';
import editor from '@inquirer/editor';
import expand from '@inquirer/expand';
import input from '@inquirer/input';
import password from '@inquirer/password';
import rawlist from '@inquirer/rawlist';
import select from '@inquirer/select';

import type { Prompt, Context } from '@inquirer/type';

type AsyncValue<Value> = Value | Promise<Value>;

function createPromptModule() {
const promptStore = {
checkbox,
confirm,
editor,
expand,
input,
password,
rawlist,
select,
};

type GetPrompt<U extends keyof typeof promptStore> = (typeof promptStore)[U];
type GetAnswerType<U extends keyof typeof promptStore> =
GetPrompt<U> extends Prompt<infer Answer, any> ? Answer : never;
type GetPromptFnConfig<U> = U extends any
? U extends Prompt<any, infer Config>
? Config
: never
: never;
type GetPromptConfig<U extends keyof typeof promptStore> = U extends any
? GetPromptFnConfig<GetPrompt<U>>
: never;

type Answers = Record<string, unknown>;
type ControlFlowConfig<U extends keyof typeof promptStore> = U extends any
? {
name: string;
when?: (answers: Answers) => boolean | Promise<boolean>;
filter?: (
answer: GetAnswerType<U>,
answers: Answers,
) => AsyncValue<GetAnswerType<U>>;
}
: never;
type PromptNameToFullConfig<U extends keyof typeof promptStore> = U extends any
? ControlFlowConfig<U> & GetPromptConfig<U> & { type: U }
: never;

async function prompt(
config:
| PromptNameToFullConfig<keyof typeof promptStore>
| PromptNameToFullConfig<keyof typeof promptStore>[],
context?: Context,
) {
const answers: Answers = {};
const promptSeries = Array.isArray(config) ? config : [config];

for (const promptConfig of promptSeries) {
promptConfig as PromptNameToFullConfig<typeof promptConfig.type>;
const { type, name, when, filter, ...configRest } = promptConfig;

const promptFn = promptStore[type];

if (when != null && !(await when(answers))) {
continue;
}

const answer = await promptFn(configRest as any, context);
answers[name] = filter ? await (filter as any)(answer, answers) : answer;
}

return answers;
}

function registerPrompt(name: string, promptFn: Prompt<any, any>) {
// @ts-ignore: To fix later
promptStore[name] = promptFn;
}

function restoreDefaultPrompts() {
registerPrompt('checkbox', checkbox);
registerPrompt('confirm', confirm);
registerPrompt('editor', editor);
registerPrompt('expand', expand);
registerPrompt('input', input);
registerPrompt('password', password);
registerPrompt('rawlist', rawlist);
registerPrompt('select', select);
}

prompt.prompt = prompt;
prompt.registerPrompt = registerPrompt;
prompt.createPromptModule = createPromptModule;
prompt.restoreDefaultPrompts = restoreDefaultPrompts;

return prompt;
}

const inquirer = createPromptModule();
export default inquirer;

export {
createPromptModule,
checkbox,
confirm,
editor,
expand,
input,
password,
rawlist,
select,
};
11 changes: 11 additions & 0 deletions canary/inquirer/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": ["ES6"],
"target": "es6",
"module": "commonjs",
"moduleResolution": "node10",
"outDir": "dist/cjs",
"declarationDir": "dist/cjs/types"
}
}
13 changes: 13 additions & 0 deletions canary/inquirer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"exclude": ["**/*.test.mts"],
"compilerOptions": {
"lib": ["ESNext"],
"target": "es2022",
"module": "NodeNext",
"moduleResolution": "nodenext",
"outDir": "dist/esm",
"declarationDir": "dist/esm/types"
}
}