-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.ts
211 lines (176 loc) · 5.57 KB
/
create.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import ora, { Ora } from 'ora';
import inquirer from 'inquirer';
import { Flags } from '@oclif/core';
import Command from '../../base.js';
import { AVAILABLE_PLATFORMS } from '../../constants.js';
import { createDebugLogger } from '../../utils/output.js';
export default class AppCreate extends Command {
static description = 'create an app';
static flags = {
...Command.flags,
app: Flags.string({
char: 'a',
description: 'app id',
}),
platform: Flags.string({
description: 'platform',
}),
plan: Flags.string({
description: 'plan',
}),
network: Flags.string({
char: 'n',
description: 'network',
}),
'read-only': Flags.string({
char: 'r',
description: 'read-only filesystem',
options: ['true', 'false'],
}),
};
static aliases = ['create'];
spinner!: Ora;
async run() {
this.spinner = ora();
const { flags } = await this.parse(AppCreate);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const name = flags.app || (await this.promptAppName());
const account = await this.getCurrentAccount();
await this.setGotConfig(flags);
((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');
const platform = flags.platform || (await this.promptPlatform());
if (!AVAILABLE_PLATFORMS.includes(platform)) {
this.error(`Unknown platform: ${platform}`);
}
const network = flags.network
? await this.getNetwork(flags.network)
: await this.promptNetwork();
const planID = flags.plan || (await this.promptPlan());
const readOnly =
flags['read-only'] === 'true'
? true
: flags['read-only'] === 'false'
? false
: undefined;
try {
await this.got.post('v1/projects/', {
json: {
name,
planID,
platform,
network: network?._id,
readOnlyRootFilesystem: readOnly,
},
});
this.log(`App ${name} created.`);
} catch (error) {
debug(error.message);
if (error.response && error.response.body) {
debug(JSON.stringify(error.response.body));
}
if (error.response && error.response.statusCode === 404) {
this.error(`Could not create the app.`);
}
if (error.response && error.response.statusCode === 409) {
this.error(
`The app already exists. Please use a unique name for your app.`
);
}
if (
error.response &&
error.response.statusCode === 403 &&
error.response.body
) {
const body = JSON.parse(error.response.body);
if (body.data.code === 'free_plan_platform') {
this.error(
`The free plan is not available for ${platform} platform.`
);
}
if (body.data.code === 'free_plan_count') {
this.error(`You are allowed to create only one app on the free plan`);
}
}
this.error(`Could not create the app. Please try again.`);
}
}
async promptPlan() {
this.spinner.start('Loading...');
try {
// TODO: Use proper type for plans
const { plans } = await this.got('v1/me').json<{ plans: any }>();
this.spinner.stop();
const { plan } = (await inquirer.prompt({
name: 'plan',
type: 'list',
message: 'Please select a plan:',
choices: [
...Object.keys(plans.projects)
.filter((plan) => {
if (
(plan === 'free' || plan.includes('g2')) &&
plans.projects[plan].available &&
plans.projects[plan].region === 'iran'
) {
return true;
}
})
.map((plan) => {
const availablePlan = plans.projects[plan];
const ram = availablePlan.RAM.amount;
const cpu = availablePlan.CPU.amount;
const disk = availablePlan.volume;
const price = availablePlan.price * 720;
const storageClass = availablePlan.storageClass;
return {
value: plan,
name: `RAM: ${ram}${' '.repeat(
5 - ram.toString().length
)} GB, CPU: ${cpu}${' '.repeat(
6 - cpu.toString().length
)}Core, Disk: ${disk}${
' '.repeat(5 - disk.toString().length) + 'GB'
}${storageClass || 'SSD'}, Price: ${price.toLocaleString()}${
price
? ' '.repeat(7 - Math.floor(price).toString().length) +
'Tomans/Month'
: ''
}`,
};
}),
],
})) as { plan: string };
return plan;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptPlatform() {
this.spinner.start('Loading...');
try {
this.spinner.stop();
const { platform } = (await inquirer.prompt({
name: 'platform',
type: 'list',
message: 'Please select a platform:',
choices: [...AVAILABLE_PLATFORMS.map((platform) => platform)],
})) as { platform: string };
return platform;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptAppName(): Promise<string> {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'input',
message: 'Enter app name:',
validate: (input) => input.length > 2,
})) as { name: string };
return name;
}
}