-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdefault.ts
82 lines (68 loc) · 2.62 KB
/
default.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
import { Args, Flags } from "@oclif/core";
import chalk from "chalk";
import { SwankySystemConfig } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../lib/errors.js";
import { getSwankyConfig, isLocalConfigCheck } from "../../lib/index.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
static description = "Set default account to use";
static flags = {
global: Flags.boolean({
char: "g",
description: "Set default account globally in Swanky system config.",
}),
};
static args = {
accountAlias: Args.string({
name: "accountAlias",
required: false,
description: "Alias of account to be used as default",
}),
};
constructor(argv: string[], baseConfig: any) {
super(argv, baseConfig);
(this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false;
}
async run(): Promise<void> {
const { args, flags } = await this.parse(DefaultAccount);
const configType = flags.global ? "global" : isLocalConfigCheck() ? "local" : "global";
const config = configType === "global" ? getSwankyConfig("global") : getSwankyConfig("local");
const accountAlias = args.accountAlias ?? (await this.promptForAccountAlias(config));
this.ensureAccountExists(config, accountAlias);
const newConfig = new ConfigBuilder(config).setDefaultAccount(accountAlias).build();
try {
await this.storeConfig(newConfig, configType);
} catch (cause) {
throw new FileError(`Error storing default account in ${configType} config`, {
cause,
});
}
this.log(
`${chalk.greenBright("✔")} Account with alias ${chalk.yellowBright(
accountAlias
)} set as default in ${configType} config`
);
}
private async promptForAccountAlias(config: SwankySystemConfig): Promise<string> {
const choices = config.accounts.map((account) => ({
name: `${account.alias} (${account.address})`,
value: account.alias,
}));
const answer = await inquirer.prompt([
{
type: "list",
name: "defaultAccount",
message: "Select default account",
choices: choices,
},
]);
return answer.defaultAccount;
}
private ensureAccountExists(config: SwankySystemConfig, alias: string) {
const isSomeAccount = config.accounts.some((account) => account.alias === alias);
if (!isSomeAccount)
throw new ConfigError(`Provided account alias ${chalk.yellowBright(alias)} not found`);
}
}