Skip to content

Commit 90a8db4

Browse files
ikeyanwing328
andauthored
chore(deps): replace inquirer with @inquirer/select (#1136)
* chore(deps): replace inquirer with `@inquirer/select` * chore(deps): downgrade @inquirer/select to ^1.3.3 * chore: fix unit test version-manager.controller.spec.ts --------- Co-authored-by: William Cheng <wing328hk@gmail.com>
1 parent c4146b5 commit 90a8db4

File tree

5 files changed

+122
-139
lines changed

5 files changed

+122
-139
lines changed

apps/generator-cli/src/app/controllers/version-manager.controller.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ describe('VersionManagerController', () => {
149149
expect(uiServiceMock.table).toHaveBeenNthCalledWith(1, {
150150
printColNum: false,
151151
message: 'The following releases are available:',
152-
name: 'version',
153152
rows: [
154153
{
155154
value: versions[0],

apps/generator-cli/src/app/controllers/version-manager.controller.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class VersionManagerController {
2626
@Inject(LOGGER) private readonly logger: LOGGER,
2727
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
2828
private readonly ui: UIService,
29-
private readonly service: VersionManagerService
29+
private readonly service: VersionManagerService,
3030
) {}
3131

3232
private list = async (versionTags: string[]) => {
@@ -43,8 +43,12 @@ export class VersionManagerController {
4343
}
4444

4545
const { version, installed } = await this.table(versions);
46-
const isSelected = await this.service.isSelectedVersion(version);
47-
const choice = (name: string, cb = () => null, color = (v) => v) => ({
46+
const isSelected = this.service.isSelectedVersion(version);
47+
const choice = (
48+
name: string,
49+
cb: () => Promise<unknown> = () => Promise.resolve(),
50+
color = (v: string) => v,
51+
) => ({
4852
name: color(name),
4953
value: cb,
5054
});
@@ -53,11 +57,11 @@ export class VersionManagerController {
5357

5458
if (!installed) {
5559
choices.unshift(
56-
choice('download', () => this.service.download(version), chalk.yellow)
60+
choice('download', () => this.service.download(version), chalk.yellow),
5761
);
5862
} else if (!isSelected) {
5963
choices.unshift(
60-
choice('remove', () => this.service.remove(version), chalk.red)
64+
choice('remove', () => this.service.remove(version), chalk.red),
6165
);
6266
}
6367

@@ -66,13 +70,13 @@ export class VersionManagerController {
6670
choice(
6771
'use',
6872
() => this.service.setSelectedVersion(version),
69-
chalk.green
70-
)
73+
chalk.green,
74+
),
7175
);
7276
}
7377

7478
await (
75-
await this.ui.list({ name: 'next', message: 'Whats next?', choices })
79+
await this.ui.list({ message: 'Whats next?', choices })
7680
)();
7781
};
7882

@@ -86,21 +90,20 @@ export class VersionManagerController {
8690

8791
this.logger.log(
8892
chalk.red(
89-
`Unable to find version matching criteria "${versionTags.join(' ')}"`
90-
)
93+
`Unable to find version matching criteria "${versionTags.join(' ')}"`,
94+
),
9195
);
9296
};
9397

9498
private table = (versions: Version[]) =>
9599
this.ui.table({
96100
printColNum: false,
97101
message: 'The following releases are available:',
98-
name: 'version',
99102
rows: versions.map((version) => {
100103
const stable = version.versionTags.includes('stable');
101104
const selected = this.service.isSelectedVersion(version.version);
102105
const versionTags = version.versionTags.map((t) =>
103-
t === 'latest' ? chalk.green(t) : t
106+
t === 'latest' ? chalk.green(t) : t,
104107
);
105108

106109
return {
Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,57 @@
11
// 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 { dim } from 'chalk';
56

67
@Injectable()
78
export class UIService {
8-
99
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 }>;
1413
}): Promise<T> {
14+
const table: string = getTable(
15+
config.rows.map(({ row }, index: number) => {
16+
return config.printColNum === false ? row : { '#': index + 1, ...row };
17+
}),
18+
);
1519

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')
20+
const [header, separator, ...rows] = table.trim().split('\n');
2221
return this.list({
23-
name: config.name,
2422
message: config.message,
2523
choices: [
26-
new Separator(header),
27-
new Separator(separator),
28-
...rows.map((name: string, index: number) => ({
24+
new Separator(dim(header)),
25+
new Separator(dim(separator)),
26+
...rows.map((name, index) => ({
2927
name,
3028
short: config.rows[index].short,
3129
value: config.rows[index].value,
3230
})),
33-
new Separator(separator),
34-
new Separator(' '.repeat(separator.length)),
31+
new Separator(dim(separator)),
32+
new Separator(dim(' '.repeat(separator.length))),
3533
],
36-
})
34+
});
3735
}
3836

3937
public async list<T>(config: {
40-
name: string,
41-
message: string,
42-
choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>,
38+
message: string;
39+
choices: Array<{ name: string; short?: string; value: T } | Separator>;
4340
}): 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-
41+
const pageSize = Math.max(1, process.stdout.rows - 2);
42+
try {
43+
const res = await select({
44+
pageSize,
45+
message: config.message,
46+
choices: config.choices,
47+
});
48+
49+
return res;
50+
} catch (err) {
51+
if (err instanceof Error && err.message.startsWith('User force closed the prompt with')) {
52+
process.exit(0);
53+
}
54+
throw err;
55+
}
6056
}
61-
6257
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
"console.table": "0.10.0",
9393
"fs-extra": "11.3.3",
9494
"glob": "13.x",
95-
"inquirer": "8.2.7",
9695
"jsonpath": "1.2.1",
9796
"proxy-agent": "^6.4.0",
9897
"reflect-metadata": "^0.2.2",
@@ -102,6 +101,7 @@
102101
"devDependencies": {
103102
"@commitlint/cli": "20.4.2",
104103
"@commitlint/config-conventional": "20.4.2",
104+
"@inquirer/select": "^1.3.3",
105105
"@nestjs/schematics": "11.0.9",
106106
"@nestjs/testing": "^11.0.16",
107107
"@nx/eslint": "22.5.2",
@@ -114,7 +114,6 @@
114114
"@nx/workspace": "22.5.2",
115115
"@semantic-release/changelog": "6.0.3",
116116
"@types/fs-extra": "11.0.4",
117-
"@types/inquirer": "8.2.12",
118117
"@types/jest": "30.0.0",
119118
"@types/jsonpath": "0.2.4",
120119
"@types/node": "20.19.33",

0 commit comments

Comments
 (0)