Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prxgr4mm3r committed Jan 24, 2024
1 parent 9b264a7 commit b70851d
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 116 deletions.
15 changes: 13 additions & 2 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Flags } from "@oclif/core";
import chalk from "chalk";
import { ChainAccount, encrypt } from "../../lib/index.js";
import { ChainAccount, encrypt, isLocalConfigCheck } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
static description = "Create a new dev account in config";

static flags = {
global: Flags.boolean({
description: "Create account globally",
}),
generate: Flags.boolean({
char: "g",
}),
Expand Down Expand Up @@ -79,7 +82,15 @@ export class CreateAccount extends SwankyCommand<typeof CreateAccount> {
if(this.swankyConfig.defaultAccount === null) {
this.swankyConfig.defaultAccount = accountData.alias;
}
await this.storeSystemConfig();

if(flags.global) {
await this.storeSystemConfig();
}
else if(isLocalConfigCheck()) {
await this.storeConfig(process.cwd());
} else {
throw new Error("Cannot store account to config. Please run this command in a swanky project directory");
}

this.log(
`${chalk.greenBright("✔")} Account with alias ${chalk.yellowBright(
Expand Down
23 changes: 20 additions & 3 deletions src/commands/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { Args } from "@oclif/core";
import { Args, Flags } from "@oclif/core";
import chalk from "chalk";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError } from "../../lib/errors.js";
import { isLocalConfigCheck } from "../../lib/index.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",
}),
}

static args = {
accountAlias: Args.string({
name: "accountAlias",
Expand All @@ -16,7 +24,7 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
};

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

if(args.accountAlias) {
const accountData = this.swankyConfig.accounts.find(
Expand All @@ -43,7 +51,16 @@ export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
this.swankyConfig.defaultAccount = answers.defaultAccount;
});
}
await this.storeSystemConfig();

if(flags.global) {
await this.storeSystemConfig();
}
else if(isLocalConfigCheck()) {
await this.storeConfig(process.cwd());
} else {
throw new Error("Cannot store account to config. Please run this command in a swanky project directory");
}

console.log(chalk.greenBright(`Default account set to ${chalk.yellowBright(this.swankyConfig.defaultAccount)}`));
}
}
20 changes: 16 additions & 4 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Listr } from "listr2";
import { commandStdoutOrNull } from "../../lib/index.js";
import { commandStdoutOrNull, isLocalConfigCheck } from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync} from "fs-extra/esm";
import { readFileSync } from "fs";
import path from "node:path";
import TOML from "@iarna/toml";
import semver from "semver";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { FileError } from "../../lib/errors.js";

interface Ctx {
versions: {
Expand Down Expand Up @@ -60,11 +61,20 @@ export default class Check extends SwankyCommand<typeof Check> {
},
},
{
title: "Read ink dependencies",
title: "Check Swanky Config",
task: async (ctx) => {
if (this.swankyConfig == undefined){
throw new FileError("Swanky config not found")
}
ctx.swankyConfig = this.swankyConfig;

for (const contract in ctx.swankyConfig.contracts) {
}
},
{
title: "Read ink dependencies",
enabled: isLocalConfigCheck(),
skip: (ctx) => ctx.swankyConfig == undefined || Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
for (const contract in ctx.swankyConfig!.contracts) {
const tomlPath = path.resolve(`contracts/${contract}/Cargo.toml`);
const doesCargoTomlExist = pathExistsSync(tomlPath);
if (!doesCargoTomlExist) {
Expand All @@ -89,6 +99,8 @@ export default class Check extends SwankyCommand<typeof Check> {
},
{
title: "Verify ink version",
enabled: isLocalConfigCheck(),
skip: (ctx) => ctx.swankyConfig == undefined || Object.keys(ctx.swankyConfig.contracts).length == 0,
task: async (ctx) => {
const supportedInk = ctx.swankyConfig?.node.supportedInk;

Expand Down
14 changes: 8 additions & 6 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Args, Flags } from "@oclif/core";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";
import { resolveNetworkUrl, ChainApi, ChainAccount, decrypt, AbiType } from "../../lib/index.js";
import { resolveNetworkUrl, ChainApi, ChainAccount, decrypt, AbiType, ensureAccountIsSet } from "../../lib/index.js";
import { AccountData, Encrypted } from "../../types/index.js";
import inquirer from "inquirer";
import chalk from "chalk";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError, ConfigError, FileError } from "../../lib/errors.js";
import { DEFAULT_ACCOUNT } from "../../lib/consts.js";

export class DeployContract extends SwankyCommand<typeof DeployContract> {
static description = "Deploy contract to a running node";

static flags = {
account: Flags.string({
default: "",
default: DEFAULT_ACCOUNT,
description: "Alias of account to be used",
}),
gas: Flags.integer({
Expand All @@ -30,6 +31,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
}),
network: Flags.string({
char: "n",
default: "local",
description: "Network name to connect to",
}),
};
Expand All @@ -45,6 +47,8 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
async run(): Promise<void> {
const { args, flags } = await this.parse(DeployContract);

console.log("flags", flags);

const contractRecord = this.swankyConfig.contracts[args.contractName];
if (!contractRecord) {
throw new ConfigError(
Expand All @@ -68,9 +72,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
);
}

if(!flags.account && this.swankyConfig.defaultAccount === null) {
throw new ConfigError("No default account set. Please set one or provide an account alias with --account");
}
ensureAccountIsSet(flags.account, this.swankyConfig);

const accountAlias = flags.account ?? this.swankyConfig.defaultAccount;

Expand Down Expand Up @@ -154,7 +156,7 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
},
];

await this.storeLocalConfig(process.cwd());
await this.storeConfig(process.cwd());
}, "Writing config");

this.log(`Contract deployed!`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/contract/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class NewContract extends SwankyCommand<typeof NewContract> {
deployments: [],
};

await this.storeLocalConfig(process.cwd())}, "Writing config");
await this.storeConfig(process.cwd())}, "Writing config");

this.log("😎 New contract successfully generated! 😎");
}
Expand Down
59 changes: 6 additions & 53 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@ import {
copyContractTemplateFiles,
downloadNode,
installDeps,
ChainAccount,
processTemplates,
swankyNode,
getTemplates,
} from "../../lib/index.js";
import {
DEFAULT_ASTAR_NETWORK_URL,
DEFAULT_NETWORK_URL,
DEFAULT_SHIBUYA_NETWORK_URL,
DEFAULT_SHIDEN_NETWORK_URL,
} from "../../lib/consts.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { InputError, UnknownError } from "../../lib/errors.js";
import { GlobEntry, globby } from "globby";
import { merge } from "lodash-es";
import inquirerFuzzyPath from "inquirer-fuzzy-path";
import { SwankyConfig } from "../../types/index.js";
import chalk from "chalk";

type TaskFunction = (...args: any[]) => any;
Expand Down Expand Up @@ -93,22 +85,6 @@ export class Init extends SwankyCommand<typeof Init> {
}
projectPath = "";

configBuilder: Partial<SwankyConfig> = {
node: {
localPath: "",
polkadotPalletVersions: swankyNode.polkadotPalletVersions,
supportedInk: swankyNode.supportedInk,
},
accounts: [],
networks: {
local: { url: DEFAULT_NETWORK_URL },
astar: { url: DEFAULT_ASTAR_NETWORK_URL },
shiden: { url: DEFAULT_SHIDEN_NETWORK_URL },
shibuya: { url: DEFAULT_SHIBUYA_NETWORK_URL },
},
contracts: {},
};

taskQueue: Task[] = [];

async run(): Promise<void> {
Expand Down Expand Up @@ -166,42 +142,19 @@ export class Init extends SwankyCommand<typeof Init> {
args: [this.projectPath, swankyNode, this.spinner],
runningMessage: "Downloading Swanky node",
callback: (result) =>
this.configBuilder.node ? (this.configBuilder.node.localPath = result) : null,
this.swankyConfig.node ? (this.swankyConfig.node.localPath = result) : null,
});
}
}

this.configBuilder.accounts = [
{
alias: "alice",
mnemonic: "//Alice",
isDev: true,
address: new ChainAccount("//Alice").pair.address,
},
{
alias: "bob",
mnemonic: "//Bob",
isDev: true,
address: new ChainAccount("//Bob").pair.address,
},
];

this.configBuilder.defaultAccount = "alice";

Object.keys(this.configBuilder.contracts!).forEach(async (contractName) => {
Object.keys(this.swankyConfig.contracts).forEach(async (contractName) => {
await ensureDir(path.resolve(this.projectPath, "artifacts", contractName));
await ensureDir(path.resolve(this.projectPath, "tests", contractName));
});

this.taskQueue.push({
task: () => {
if (Object.keys(this.swankyConfig.networks).length === 0 || this.swankyConfig.accounts.length === 0) {
this.swankyConfig = this.configBuilder as SwankyConfig;
} else {
this.swankyConfig.node = this.configBuilder.node!;
this.swankyConfig.contracts = this.configBuilder.contracts!;
}
this.storeLocalConfig(this.projectPath);
this.storeConfig(this.projectPath);
this.storeSystemConfig();
},
args: [],
Expand Down Expand Up @@ -294,7 +247,7 @@ export class Init extends SwankyCommand<typeof Init> {
runningMessage: "Processing templates",
});

this.configBuilder.contracts = {
this.swankyConfig.contracts = {
[contractName as string]: {
name: contractName,
moduleName: snakeCase(contractName),
Expand Down Expand Up @@ -370,10 +323,10 @@ export class Init extends SwankyCommand<typeof Init> {
},
});

if (!this.configBuilder.contracts) this.configBuilder.contracts = {};
if (!this.swankyConfig.contracts) this.swankyConfig.contracts = {};

for (const contract of confirmedCopyList.contracts) {
this.configBuilder.contracts[contract.name] = {
this.swankyConfig.contracts[contract.name] = {
name: contract.name,
moduleName: contract.moduleName!,
deployments: [],
Expand Down
2 changes: 1 addition & 1 deletion src/commands/node/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class InstallNode extends SwankyCommand<typeof InstallNode> {

await this.spinner.runCommand(
() =>
this.storeLocalConfig(projectPath),
this.storeConfig(projectPath),
"Updating swanky config"
);

Expand Down
46 changes: 42 additions & 4 deletions src/lib/command-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { execaCommand } from "execa";
import { copy, emptyDir, ensureDir, readJSON } from "fs-extra/esm";
import path from "node:path";
import { DEFAULT_NETWORK_URL, ARTIFACTS_PATH, TYPED_CONTRACTS_PATH } from "./consts.js";
import { SwankyConfig, SwankyLocalConfig, SwankySystemConfig } from "../types/index.js";
import {
DEFAULT_NETWORK_URL,
ARTIFACTS_PATH,
TYPED_CONTRACTS_PATH,
DEFAULT_SHIBUYA_NETWORK_URL,
DEFAULT_SHIDEN_NETWORK_URL, DEFAULT_ASTAR_NETWORK_URL, DEFAULT_ACCOUNT,
} from "./consts.js";
import { SwankyConfig, SwankySystemConfig } from "../types/index.js";
import { ConfigError, FileError, InputError } from "./errors.js";
import { userInfo } from "os";
import { swankyNode } from "./nodeInfo.js";
import { existsSync } from "fs";

export async function commandStdoutOrNull(command: string): Promise<string | null> {
try {
Expand All @@ -15,9 +23,10 @@ export async function commandStdoutOrNull(command: string): Promise<string | nul
}
}

export async function getSwankyLocalConfig(): Promise<SwankyLocalConfig> {
export async function getSwankyConfig(): Promise<SwankyConfig> {
const configPath = process.env.SWANKY_CONFIG ?? "swanky.config.json";
try {
const config = await readJSON("swanky.config.json");
const config = await readJSON(configPath);
return config;
} catch (cause) {
throw new InputError("Error reading swanky.config.json in the current directory!", { cause });
Expand Down Expand Up @@ -137,3 +146,32 @@ export async function generateTypes(contractName: string) {
`npx typechain-polkadot --in ${relativeInputPath} --out ${relativeOutputPath}`
);
}
export function ensureAccountIsSet(account: string | undefined, config: SwankyConfig) {
if(!account && config.defaultAccount === null) {
throw new ConfigError("No default account set. Please set one or provide an account alias with --account");
}
}

export function buildSwankyConfig() {
return {
node: {
localPath: "",
polkadotPalletVersions: swankyNode.polkadotPalletVersions,
supportedInk: swankyNode.supportedInk,
},
defaultAccount: DEFAULT_ACCOUNT,
accounts: [],
networks: {
local: { url: DEFAULT_NETWORK_URL },
astar: { url: DEFAULT_ASTAR_NETWORK_URL },
shiden: { url: DEFAULT_SHIDEN_NETWORK_URL },
shibuya: { url: DEFAULT_SHIBUYA_NETWORK_URL },
},
contracts: {},
};
}

export function isLocalConfigCheck() {
console.log("process.env.SWANKY_CONFIG", process.env.SWANKY_CONFIG);
return process.env.SWANKY_CONFIG != "" || existsSync(process.cwd() + "/swanky.config.json");
}
Loading

0 comments on commit b70851d

Please sign in to comment.