Skip to content

Commit

Permalink
Revert "feat: Implement system/local config + support default/dev acc…
Browse files Browse the repository at this point in the history
…ount (#62)"

This reverts commit 4ef3ca2.
  • Loading branch information
ipapandinas authored Mar 25, 2024
1 parent 5588c4e commit 72eb8f0
Show file tree
Hide file tree
Showing 22 changed files with 210 additions and 589 deletions.
40 changes: 6 additions & 34 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { ChainAccount, encrypt, getSwankyConfig, isLocalConfigCheck } from "../../lib/index.js";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { FileError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { SwankyAccountCommand } from "./swankyAccountCommands.js";

export class CreateAccount extends SwankyAccountCommand<typeof CreateAccount> {
static description = "Create a new dev account in config";

static flags = {
global: Flags.boolean({
generate: Flags.boolean({
char: "g",
description: "Create account globally stored in Swanky system config.",

}),
new: Flags.boolean({
char: "n",
description: "Generate a brand new account.",
}),
dev: Flags.boolean({
char: "d",
description: "Make this account a dev account for local network usage.",
}),
};

constructor(argv: string[], baseConfig: any) {
super(argv, baseConfig);
(this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false;
}

async run(): Promise<void> {
const { flags } = await this.parse(CreateAccount);

Expand All @@ -51,8 +36,8 @@ export class CreateAccount extends SwankyAccountCommand<typeof CreateAccount> {
);
}

let tmpMnemonic = "";
if (flags.new) {
let tmpMnemonic: string;
if (flags.generate) {
tmpMnemonic = ChainAccount.generate();
console.log(
`${
Expand Down Expand Up @@ -91,22 +76,9 @@ export class CreateAccount extends SwankyAccountCommand<typeof CreateAccount> {
accountData.mnemonic = tmpMnemonic;
}

const configType = flags.global ? "global" : isLocalConfigCheck() ? "local" : "global";
const config = configType === "global" ? getSwankyConfig("global") : getSwankyConfig("local");

const configBuilder = new ConfigBuilder(config).addAccount(accountData);
this.swankyConfig.accounts.push(accountData);

if (config.defaultAccount === null) {
configBuilder.setDefaultAccount(accountData.alias);
}

try {
await this.storeConfig(configBuilder.build(), configType);
} catch (cause) {
throw new FileError(`Error storing created account in ${configType} config`, {
cause,
});
}
await this.storeConfig();

this.log(
`${chalk.greenBright("✔")} Account with alias ${chalk.yellowBright(
Expand Down
82 changes: 0 additions & 82 deletions src/commands/account/default.ts

This file was deleted.

31 changes: 3 additions & 28 deletions src/commands/account/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,11 @@ export class ListAccounts extends SwankyCommand<typeof ListAccounts> {
static description = "List dev accounts stored in config";
static aliases = [`account:ls`];

constructor(argv: string[], baseConfig: any) {
super(argv, baseConfig);
(this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false;
}

async run(): Promise<void> {
const countOfDevAccounts = this.swankyConfig.accounts.filter((account) => account.isDev).length;

if(countOfDevAccounts !== 0) {
this.log(`${chalk.greenBright("✔")} Stored dev accounts:`);

for (const account of this.swankyConfig.accounts) {
if(account.isDev){
this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias} \
${chalk.yellowBright("Address: ")} ${account.address} ${this.swankyConfig.defaultAccount === account.alias ? chalk.greenBright("<- Default") : ""}`);
}
}
}

const countOfProdAccounts = this.swankyConfig.accounts.length - countOfDevAccounts;

if(countOfProdAccounts !== 0) {
this.log(`${chalk.greenBright("✔")} Stored prod accounts:`);
this.log(`${chalk.greenBright("✔")} Stored dev accounts:`);

for (const account of this.swankyConfig.accounts) {
if(!account.isDev){
this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias} \
${chalk.yellowBright("Address: ")} ${account.address} ${this.swankyConfig.defaultAccount === account.alias ? chalk.greenBright("<- Default") : ""}`);
}
}
for (const account of this.swankyConfig.accounts) {
this.log(`\t${chalk.yellowBright("Alias: ")} ${account.alias}`);
}
}
}
20 changes: 8 additions & 12 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Listr } from "listr2";
import { commandStdoutOrNull, extractCargoContractVersion } from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync, writeJson } from "fs-extra/esm";
import { pathExistsSync, readJSON, writeJson } from "fs-extra/esm";
import { readFileSync } from "fs";
import path from "node:path";
import TOML from "@iarna/toml";
Expand Down Expand Up @@ -31,7 +31,7 @@ interface Ctx {
contracts: Record<string, Record<string, string>>;
swankyNode: string | null;
};
swankyConfig: SwankyConfig;
swankyConfig?: SwankyConfig;
mismatchedVersions: Record<string, string>;
looseDefinitionDetected: boolean;
}
Expand All @@ -46,16 +46,11 @@ export default class Check extends SwankyCommand<typeof Check> {
}),
};

constructor(argv: string[], baseConfig: any) {
super(argv, baseConfig);
(this.constructor as typeof SwankyCommand).ENSURE_SWANKY_CONFIG = false;
}

public async run(): Promise<void> {
const { flags } = await this.parse(Check);
const swankyNodeVersion = this.swankyConfig.node.version;
const isSwankyNodeInstalled = !!swankyNodeVersion;
const anyContracts = Object.keys(this.swankyConfig.contracts ?? {}).length > 0;
const anyContracts = Object.keys(this.swankyConfig?.contracts).length > 0;
const tasks = new Listr<Ctx>([
{
title: "Check OS",
Expand Down Expand Up @@ -141,9 +136,11 @@ export default class Check extends SwankyCommand<typeof Check> {
{
title: "Read ink dependencies",
enabled: anyContracts,
skip: (ctx) => Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
for (const contract in ctx.swankyConfig.contracts) {
const swankyConfig = await readJSON("swanky.config.json");
ctx.swankyConfig = swankyConfig;

for (const contract in swankyConfig.contracts) {
const tomlPath = path.resolve(`contracts/${contract}/Cargo.toml`);
const doesCargoTomlExist = pathExistsSync(tomlPath);
if (!doesCargoTomlExist) {
Expand Down Expand Up @@ -171,7 +168,7 @@ export default class Check extends SwankyCommand<typeof Check> {
skip: (ctx) => Object.keys(ctx.versions.contracts).length === 0,
enabled: anyContracts && isSwankyNodeInstalled,
task: async (ctx) => {
const supportedInk = ctx.swankyConfig.node.supportedInk;
const supportedInk = ctx.swankyConfig!.node.supportedInk;
const mismatched: Record<string, string> = {};
Object.entries(ctx.versions.contracts).forEach(([contract, inkDependencies]) => {
Object.entries(inkDependencies).forEach(([depName, version]) => {
Expand Down Expand Up @@ -259,7 +256,6 @@ export default class Check extends SwankyCommand<typeof Check> {
contracts: {},
swankyNode: swankyNodeVersion || null,
},
swankyConfig: this.swankyConfig,
looseDefinitionDetected: false,
mismatchedVersions: {}
});
Expand Down
29 changes: 11 additions & 18 deletions src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import path from "node:path";
import { spawn } from "node:child_process";
import { pathExists } from "fs-extra/esm";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, Spinner, storeArtifacts, configName, getSwankyConfig } from "../../lib/index.js";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, Spinner, storeArtifacts } from "../../lib/index.js";
import { ConfigError, InputError, ProcessError } from "../../lib/errors.js";
import { BuildMode, SwankyConfig } from "../../index.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { BuildMode } from "../../index.js";

export class CompileContract extends SwankyCommand<typeof CompileContract> {
static description = "Compile the smart contract(s) in your contracts directory";
Expand Down Expand Up @@ -42,8 +41,6 @@ export class CompileContract extends SwankyCommand<typeof CompileContract> {
async run(): Promise<void> {
const { args, flags } = await this.parse(CompileContract);

const localConfig = getSwankyConfig("local") as SwankyConfig;

if (args.contractName === undefined && !flags.all) {
throw new InputError("No contracts were selected to compile", { winston: { stack: true } });
}
Expand All @@ -58,7 +55,7 @@ export class CompileContract extends SwankyCommand<typeof CompileContract> {
const contractInfo = this.swankyConfig.contracts[contractName];
if (!contractInfo) {
throw new ConfigError(
`Cannot find contract info for ${contractName} contract in "${configName()}"`
`Cannot find contract info for ${contractName} contract in swanky.config.json`,
);
}
const contractPath = path.resolve("contracts", contractInfo.name);
Expand Down Expand Up @@ -133,18 +130,14 @@ export class CompileContract extends SwankyCommand<typeof CompileContract> {
return storeArtifacts(artifactsPath, contractInfo.name, contractInfo.moduleName);
}, "Moving artifacts");

await this.spinner.runCommand(async () => {
const buildData = {
timestamp: Date.now(),
artifactsPath,
buildMode,
isVerified: false,
};
const newLocalConfig = new ConfigBuilder(localConfig)
.addContractBuild(args.contractName, buildData)
.build();
await this.storeConfig(newLocalConfig, "local");
}, "Writing config");
this.swankyConfig.contracts[contractName].build = {
timestamp: Date.now(),
artifactsPath,
buildMode,
isVerified: false,
};

await this.storeConfig();
}
}
}
Loading

0 comments on commit 72eb8f0

Please sign in to comment.