|
1 | 1 | // import ora from 'ora' |
2 | | -import {Injectable} from '@nestjs/common'; |
3 | | -import {prompt, Separator} from 'inquirer'; |
4 | | -import {getTable} from 'console.table' |
| 2 | +import { Injectable } from '@nestjs/common'; |
| 3 | +import select, { Separator } from '@inquirer/select'; |
| 4 | +import { getTable } from 'console.table'; |
| 5 | +import { styleText } from 'util'; |
5 | 6 |
|
6 | 7 | @Injectable() |
7 | 8 | export class UIService { |
8 | | - |
9 | 9 | public async table<T>(config: { |
10 | | - name: string, |
11 | | - message: string, |
12 | | - printColNum?: boolean, |
13 | | - rows: Array<{ row: Record<string, unknown>, short: string, value: T }>, |
| 10 | + message: string; |
| 11 | + printColNum?: boolean; |
| 12 | + rows: Array<{ row: Record<string, string>; short: string; value: T }>; |
14 | 13 | }): Promise<T> { |
15 | | - |
16 | | - |
17 | | - const table = getTable(config.rows.map(({row}, index: number) => { |
18 | | - return config.printColNum === false ? row : ({'#': index + 1, ...row}); |
19 | | - })) |
20 | | - |
21 | | - const [header, separator, ...rows] = table.trim().split('\n') |
| 14 | + const table: string = getTable( |
| 15 | + config.rows.map(({ row }, index: number) => { |
| 16 | + return config.printColNum === false ? row : { '#': index + 1, ...row }; |
| 17 | + }), |
| 18 | + ); |
| 19 | + |
| 20 | + const [header, separator, ...rows] = table.trim().split('\n'); |
| 21 | + const dim = (text: string) => styleText('dim', text); |
22 | 22 | return this.list({ |
23 | | - name: config.name, |
24 | 23 | message: config.message, |
25 | 24 | choices: [ |
26 | | - new Separator(header), |
27 | | - new Separator(separator), |
28 | | - ...rows.map((name: string, index: number) => ({ |
| 25 | + new Separator(dim(header)), |
| 26 | + new Separator(dim(separator)), |
| 27 | + ...rows.map((name, index) => ({ |
29 | 28 | name, |
30 | 29 | short: config.rows[index].short, |
31 | 30 | value: config.rows[index].value, |
32 | 31 | })), |
33 | | - new Separator(separator), |
34 | | - new Separator(' '.repeat(separator.length)), |
| 32 | + new Separator(dim(separator)), |
| 33 | + new Separator(dim(' '.repeat(separator.length))), |
35 | 34 | ], |
36 | | - }) |
| 35 | + }); |
37 | 36 | } |
38 | 37 |
|
39 | 38 | public async list<T>(config: { |
40 | | - name: string, |
41 | | - message: string, |
42 | | - choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>, |
| 39 | + message: string; |
| 40 | + choices: Array<{ name: string; short?: string; value: T } | Separator>; |
43 | 41 | }): Promise<T> { |
44 | | - |
45 | | - const separatorCount = config |
46 | | - .choices |
47 | | - .filter((c) => c instanceof Separator) |
48 | | - .length |
49 | | - |
50 | | - const res = await prompt([{ |
51 | | - type: 'list', |
52 | | - name: config.name, |
53 | | - pageSize: process.stdout.rows - separatorCount - 1, |
54 | | - message: config.message, |
55 | | - choices: config.choices, |
56 | | - }]) |
57 | | - |
58 | | - return res[config.name] as T |
59 | | - |
| 42 | + const pageSize = Math.max(1, process.stdout.rows - 3); |
| 43 | + try { |
| 44 | + const res = await select({ |
| 45 | + pageSize, |
| 46 | + message: config.message, |
| 47 | + choices: config.choices, |
| 48 | + }); |
| 49 | + |
| 50 | + return res; |
| 51 | + } catch (err) { |
| 52 | + if (err instanceof Error && err.name === 'ExitPromptError') { |
| 53 | + process.exit(0); |
| 54 | + } |
| 55 | + throw err; |
| 56 | + } |
60 | 57 | } |
61 | | - |
62 | 58 | } |
0 commit comments