/
unset.ts
77 lines (64 loc) · 2.06 KB
/
unset.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
import inquirer from 'inquirer';
import Command from '../../base.js';
import { Flags, Args } from '@oclif/core';
import { createDebugLogger } from '../../utils/output.js';
import { IEnv, IGetProjectResponse } from './set.js';
export default class EnvUnset extends Command {
static description = 'remove environment variables from an app';
static strict = false;
static args = {
env: Args.string({ description: 'key' }),
};
static flags = {
...Command.flags,
app: Flags.string({ char: 'a', description: 'app id' }),
force: Flags.boolean({ char: 'f', description: 'force update' }),
};
async run() {
const { flags, argv } = await this.parse(EnvUnset);
await this.setGotConfig(flags);
const debug = createDebugLogger(flags.debug);
if (!argv.length) {
EnvUnset.run(['-h']);
this.exit(0);
}
if (argv.join(' ').includes('=')) {
return this.error(
`You can't use '=' in the key. Please check your input.`
);
}
const app = flags.app || (await this.promptProject());
const appliedEnvs = await this.fetchEnvs(app);
const variables = appliedEnvs.filter((v) => !argv.includes(v.key));
try {
if (flags.force || (await this.confirm())) {
await this.got.post(`v1/projects/update-envs`, {
json: { project: app, variables },
});
this.log(`Configuration variable removed and restarting ${app}`);
}
} catch (error) {
debug(error.message);
}
}
async fetchEnvs(app: string): Promise<IEnv[]> {
const { project } = await this.got(
`v1/projects/${app}`
).json<IGetProjectResponse>();
const envs = project.envs.map((env) => {
const key = env.key;
const value = env.value;
return { key, value };
});
return envs;
}
async confirm() {
const { confirm } = (await inquirer.prompt({
name: 'confirm',
type: 'confirm',
message: `Your app will be restarted due to these configuration changes. Confirm: `,
default: false,
})) as { confirm: boolean };
return confirm;
}
}