forked from OpenAPITools/openapi-generator-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-manager.controller.ts
More file actions
124 lines (106 loc) · 3.6 KB
/
version-manager.controller.ts
File metadata and controls
124 lines (106 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Controller, Inject } from '@nestjs/common';
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
import { Command } from 'commander';
import chalk from 'chalk';
import { UIService, Version, VersionManagerService } from '../services';
@Controller()
export class VersionManagerController {
private readonly mainCommand = this.program
.command('version-manager')
.description('Manage used / installed generator version');
private readonly listCommand = this.mainCommand
.command('list [versionTags...]')
.description('lists all published versions')
.option('-j, --json', 'print as json', false)
.action((tags) => this.list(tags));
private readonly setCommand = this.mainCommand
.command('set <versionTags...>')
.description('set version to use')
.action((tags) => this.set(tags));
constructor(
@Inject(LOGGER) private readonly logger: LOGGER,
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
private readonly ui: UIService,
private readonly service: VersionManagerService,
) {}
private list = async (versionTags: string[]) => {
const versions = await this.service.search(versionTags).toPromise();
if (this.listCommand.opts().json) {
this.logger.log(JSON.stringify(versions, null, 2));
return;
}
if (versions.length < 1) {
this.logger.log(chalk.red('No results for: ' + versionTags.join(' ')));
return;
}
const { version, installed } = await this.table(versions);
const isSelected = this.service.isSelectedVersion(version);
const choice = (
name: string,
cb: () => Promise<unknown> = () => Promise.resolve(),
color = (v: string) => v,
) => ({
name: color(name),
value: cb,
});
const choices = [choice('exit')];
if (!installed) {
choices.unshift(
choice('download', () => this.service.download(version), chalk.yellow),
);
} else if (!isSelected) {
choices.unshift(
choice('remove', () => this.service.remove(version), chalk.red),
);
}
if (!isSelected) {
choices.unshift(
choice(
'use',
() => this.service.setSelectedVersion(version),
chalk.green,
),
);
}
await (
await this.ui.list({ message: 'Whats next?', choices })
)();
};
private set = async (versionTags: string[]) => {
const versions = await this.service.search(versionTags).toPromise();
if (versions.length > 0) {
await this.service.setSelectedVersion(versions[0].version);
return;
}
this.logger.log(
chalk.red(
`Unable to find version matching criteria "${versionTags.join(' ')}"`,
),
);
};
private table = (versions: Version[]) =>
this.ui.table({
printColNum: false,
message: 'The following releases are available:',
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,
);
return {
value: version,
short: version.version,
row: {
'☐': selected ? '☒' : '☐',
releasedAt: version.releaseDate.toISOString().split('T')[0],
version: stable
? chalk.yellow(version.version)
: chalk.gray(version.version),
installed: version.installed ? chalk.green('yes') : chalk.red('no'),
versionTags: versionTags.join(' '),
},
};
}),
});
}