Skip to content

Commit

Permalink
fix: Correct work when env SWANKY_CONFIG variable is set
Browse files Browse the repository at this point in the history
  • Loading branch information
prxgr4mm3r committed Jan 30, 2024
1 parent 9a25dd8 commit f9a604c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
36 changes: 20 additions & 16 deletions src/commands/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError } from "../../lib/errors.js";
import { configName, getSwankySystemConfig, isLocalConfigCheck } from "../../lib/index.js";
import { configName, getSwankySystemConfig, isEnvConfigCheck, isLocalConfigCheck } from "../../lib/index.js";

export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
static description = "Set default account to use";
Expand Down Expand Up @@ -32,8 +32,6 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
async run(): Promise<void> {
const { args, flags } = await this.parse(DefaultAccount);

console.log(this.swankyConfig.accounts);

const systemConfig = await getSwankySystemConfig();

if (args.accountAlias) {
Expand All @@ -45,12 +43,16 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
);
if (isLocalConfigCheck()) {
if (!accountData) {
if (!systemAccountData) {
throw new ConfigError(`Provided account alias ${chalk.yellowBright(args.accountAlias)} not found in "${configName()}" and system config`);
if (!isEnvConfigCheck() || flags.global) {
if (!systemAccountData) {
throw new ConfigError(`Provided account alias ${chalk.yellowBright(args.accountAlias)} not found in "${configName()}" and system config`);
}
systemConfig.defaultAccount = systemAccountData.alias;
await this.storeSystemConfig(systemConfig);
console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(systemConfig.defaultAccount)} in system config`));
} else {
throw new ConfigError(`Provided account alias ${chalk.yellowBright(args.accountAlias)} not found in "${configName()}"`);
}
systemConfig.defaultAccount = systemAccountData.alias;
await this.storeSystemConfig(systemConfig);
console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(systemConfig.defaultAccount)} in system config`));
} else {
this.swankyConfig.defaultAccount = accountData.alias;
await this.storeConfig(process.cwd());
Expand Down Expand Up @@ -78,14 +80,16 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
value: { alias: account.alias, systemConfig: false },
};
});
systemConfig.accounts.forEach((account: AccountData) => {
if (!choices.find((choice: any) => choice.value.alias === account.alias)) {
choices.push({
name: `${account.alias} (${account.address}) [system config]`,
value: { alias: account.alias, systemConfig: true },
});
}
});
if (!isEnvConfigCheck() || flags.global) {
systemConfig.accounts.forEach((account: AccountData) => {
if (!choices.find((choice: any) => choice.value.alias === account.alias)) {
choices.push({
name: `${account.alias} (${account.address}) [system config]`,
value: { alias: account.alias, systemConfig: true },
});
}
});
}
await inquirer.prompt([
{
type: "list",
Expand Down
12 changes: 10 additions & 2 deletions src/lib/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function commandStdoutOrNull(command: string): Promise<string | nul
}

export async function getSwankyConfig(): Promise<SwankyConfig> {
const configPath = process.env.SWANKY_CONFIG ?? "swanky.config.json";
const configPath : string = isEnvConfigCheck() ? process.env.SWANKY_CONFIG! : "swanky.config.json";
try {
const config = await readJSON(configPath);
return config;
Expand Down Expand Up @@ -184,11 +184,19 @@ export function buildSwankyConfig() {
};
}

export function isEnvConfigCheck(): boolean {
if (process.env.SWANKY_CONFIG === undefined) {
return false;
} else if (existsSync(process.env.SWANKY_CONFIG)) {
return true;
} else {
throw new ConfigError(`Provided config path ${process.env.SWANKY_CONFIG} does not exist`);
}
}
export function isLocalConfigCheck(): boolean {
const defaultLocalPath = process.cwd() + "/swanky.config.json";
return process.env.SWANKY_CONFIG === undefined ? existsSync(defaultLocalPath) : existsSync(process.env.SWANKY_CONFIG);
}

export function configName() {
if(isLocalConfigCheck()) {
const configPathArray = (process.env.SWANKY_CONFIG === undefined ?
Expand Down
6 changes: 0 additions & 6 deletions src/lib/swankyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ export abstract class SwankyCommand<T extends typeof Command> extends Command {
try {
const localConfig = await getSwankyConfig();

Object.entries(this.swankyConfig).forEach((entry) => {
if (Object.entries(localConfig[entry[0] as keyof SwankyConfig] as object).length === 0) {
localConfig[entry[0] as keyof SwankyConfig] = entry[1];
}
});

Object.entries(localConfig).forEach((entry) => {
this.swankyConfig[entry[0] as keyof SwankyConfig] = entry[1];
});
Expand Down

0 comments on commit f9a604c

Please sign in to comment.