Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ describe('VersionManagerController', () => {
expect(uiServiceMock.table).toHaveBeenNthCalledWith(1, {
printColNum: false,
message: 'The following releases are available:',
name: 'version',
rows: [
{
value: versions[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class VersionManagerController {
@Inject(LOGGER) private readonly logger: LOGGER,
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
private readonly ui: UIService,
private readonly service: VersionManagerService
private readonly service: VersionManagerService,
) {}

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

const { version, installed } = await this.table(versions);
const isSelected = await this.service.isSelectedVersion(version);
const choice = (name: string, cb = () => null, color = (v) => v) => ({
const isSelected = this.service.isSelectedVersion(version);
const choice = (
name: string,
cb: () => Promise<unknown> = () => Promise.resolve(),
color = (v: string) => v,
) => ({
name: color(name),
value: cb,
});
Expand All @@ -53,11 +57,11 @@ export class VersionManagerController {

if (!installed) {
choices.unshift(
choice('download', () => this.service.download(version), chalk.yellow)
choice('download', () => this.service.download(version), chalk.yellow),
);
} else if (!isSelected) {
choices.unshift(
choice('remove', () => this.service.remove(version), chalk.red)
choice('remove', () => this.service.remove(version), chalk.red),
);
}

Expand All @@ -66,13 +70,13 @@ export class VersionManagerController {
choice(
'use',
() => this.service.setSelectedVersion(version),
chalk.green
)
chalk.green,
),
);
}

await (
await this.ui.list({ name: 'next', message: 'Whats next?', choices })
await this.ui.list({ message: 'Whats next?', choices })
)();
};

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

this.logger.log(
chalk.red(
`Unable to find version matching criteria "${versionTags.join(' ')}"`
)
`Unable to find version matching criteria "${versionTags.join(' ')}"`,
),
);
};

private table = (versions: Version[]) =>
this.ui.table({
printColNum: false,
message: 'The following releases are available:',
name: 'version',
rows: versions.map((version) => {
const stable = version.versionTags.includes('stable');
const selected = this.service.isSelectedVersion(version.version);
const versionTags = version.versionTags.map((t) =>
t === 'latest' ? chalk.green(t) : t
t === 'latest' ? chalk.green(t) : t,
);

return {
Expand Down
77 changes: 36 additions & 41 deletions apps/generator-cli/src/app/services/ui.service.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,57 @@
// import ora from 'ora'
import {Injectable} from '@nestjs/common';
import {prompt, Separator} from 'inquirer';
import {getTable} from 'console.table'
import { Injectable } from '@nestjs/common';
import select, { Separator } from '@inquirer/select';
import { getTable } from 'console.table';
import { dim } from 'chalk';

@Injectable()
export class UIService {

public async table<T>(config: {
name: string,
message: string,
printColNum?: boolean,
rows: Array<{ row: Record<string, unknown>, short: string, value: T }>,
message: string;
printColNum?: boolean;
rows: Array<{ row: Record<string, string>; short: string; value: T }>;
}): Promise<T> {
const table: string = getTable(
config.rows.map(({ row }, index: number) => {
return config.printColNum === false ? row : { '#': index + 1, ...row };
}),
);


const table = getTable(config.rows.map(({row}, index: number) => {
return config.printColNum === false ? row : ({'#': index + 1, ...row});
}))

const [header, separator, ...rows] = table.trim().split('\n')
const [header, separator, ...rows] = table.trim().split('\n');
return this.list({
name: config.name,
message: config.message,
choices: [
new Separator(header),
new Separator(separator),
...rows.map((name: string, index: number) => ({
new Separator(dim(header)),
new Separator(dim(separator)),
...rows.map((name, index) => ({
name,
short: config.rows[index].short,
value: config.rows[index].value,
})),
new Separator(separator),
new Separator(' '.repeat(separator.length)),
new Separator(dim(separator)),
new Separator(dim(' '.repeat(separator.length))),
],
})
});
}

public async list<T>(config: {
name: string,
message: string,
choices: Array<{ name: Record<string, unknown>, short?: string, value: T }>,
message: string;
choices: Array<{ name: string; short?: string; value: T } | Separator>;
}): Promise<T> {

const separatorCount = config
.choices
.filter((c) => c instanceof Separator)
.length

const res = await prompt([{
type: 'list',
name: config.name,
pageSize: process.stdout.rows - separatorCount - 1,
message: config.message,
choices: config.choices,
}])

return res[config.name] as T

const pageSize = Math.max(1, process.stdout.rows - 2);
try {
const res = await select({
pageSize,
message: config.message,
choices: config.choices,
});

return res;
} catch (err) {
if (err instanceof Error && err.message.startsWith('User force closed the prompt with')) {
process.exit(0);
}
throw err;
}
}

}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"console.table": "0.10.0",
"fs-extra": "11.3.3",
"glob": "13.x",
"inquirer": "8.2.7",
"jsonpath": "1.2.1",
"proxy-agent": "^6.4.0",
"reflect-metadata": "^0.2.2",
Expand All @@ -102,6 +101,7 @@
"devDependencies": {
"@commitlint/cli": "20.4.1",
"@commitlint/config-conventional": "20.4.1",
"@inquirer/select": "^1.3.3",
"@nestjs/schematics": "11.0.9",
"@nestjs/testing": "^11.0.16",
"@nx/eslint": "22.5.0",
Expand All @@ -114,7 +114,6 @@
"@nx/workspace": "22.5.0",
"@semantic-release/changelog": "6.0.3",
"@types/fs-extra": "11.0.4",
"@types/inquirer": "8.2.12",
"@types/jest": "30.0.0",
"@types/jsonpath": "0.2.4",
"@types/node": "20.19.33",
Expand Down
Loading