-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement system/local config + support default/dev account (#62)
Co-authored-by: Igor Papandinas <igor.papandinas@posteo.net>
- Loading branch information
1 parent
e28c7c6
commit e315716
Showing
22 changed files
with
589 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.