Skip to content

Commit

Permalink
refactor command-utils.ts and tasks.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
prxgr4mm3r committed Feb 29, 2024
1 parent 02e352f commit 9b673d1
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 183 deletions.
3 changes: 2 additions & 1 deletion src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";
import { getSwankyConfig, isLocalConfigCheck } from "../../lib/config.js";

export class CreateAccount extends SwankyAccountCommand<typeof CreateAccount> {
static description = "Create a new dev account in config";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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";
import { getSwankyConfig, isLocalConfigCheck } from "../../lib/config.js";

export class DefaultAccount extends SwankyCommand<typeof DefaultAccount> {
static description = "Set default account to use";
Expand Down
3 changes: 2 additions & 1 deletion src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ 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 { configName, getSwankyConfig } from "../../lib/config.js";

export class CompileContract extends SwankyCommand<typeof CompileContract> {
static description = "Compile the smart contract(s) in your contracts directory";
Expand Down
3 changes: 2 additions & 1 deletion src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Args, Flags } from "@oclif/core";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";
import { AbiType, ChainAccount, ChainApi, decrypt, resolveNetworkUrl, ensureAccountIsSet, configName, getSwankyConfig } from "../../lib/index.js";
import { AbiType, ChainAccount, ChainApi, decrypt, resolveNetworkUrl } from "../../lib/index.js";
import { BuildMode, Encrypted, SwankyConfig } 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, InputError, ProcessError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { configName, ensureAccountIsSet, getSwankyConfig } from "../../lib/config.js";

export class DeployContract extends SwankyCommand<typeof DeployContract> {
static description = "Deploy contract to a running node";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/contract/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SwankyCommand } from "../../lib/swankyCommand.js";
import { Args } from "@oclif/core";
import { Contract } from "../../lib/contract.js";
import { ConfigError, FileError } from "../../lib/errors.js";
import { configName } from "../../lib/index.js";
import { configName } from "../../lib/config.js";

export class ExplainContract extends SwankyCommand<typeof ExplainContract> {
static description = "Explain contract messages based on the contracts' metadata";
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 @@ -7,7 +7,6 @@ import {
processTemplates,
getTemplates,
prepareTestFiles,
getSwankyConfig,
} from "../../lib/index.js";
import { email, name, pickTemplate } from "../../lib/prompts.js";
import { paramCase, pascalCase, snakeCase } from "change-case";
Expand All @@ -16,6 +15,7 @@ import inquirer from "inquirer";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { InputError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { getSwankyConfig } from "../../lib/config.js";

export class NewContract extends SwankyCommand<typeof NewContract> {
static description = "Generate a new smart contract template inside a project";
Expand Down
3 changes: 2 additions & 1 deletion src/commands/contract/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError, InputError, ProcessError, TestError } from "../../lib/errors.js";
import { spawn } from "node:child_process";
import { configName, Spinner } from "../../lib/index.js";
import { Spinner } from "../../lib/index.js";
import { configName } from "../../lib/config.js";

declare global {
var contractTypesPath: string; // eslint-disable-line no-var
Expand Down
3 changes: 2 additions & 1 deletion src/commands/contract/verify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Args, Flags } from "@oclif/core";
import path from "node:path";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, getSwankyConfig, Spinner } from "../../lib/index.js";
import { ensureCargoContractVersionCompatibility, extractCargoContractVersion, Spinner } from "../../lib/index.js";
import { pathExists } from "fs-extra/esm";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, InputError, ProcessError } from "../../lib/errors.js";
import { spawn } from "node:child_process";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { BuildData, SwankyConfig } from "../../index.js";
import { getSwankyConfig } from "../../lib/config.js";

export class VerifyContract extends SwankyCommand<typeof VerifyContract> {
static description = "Verify the smart contract(s) in your contracts directory";
Expand Down
3 changes: 2 additions & 1 deletion src/commands/generate/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Args } from "@oclif/core";
import { configName, generateTypes } from "../../lib/index.js";
import { generateTypes } from "../../lib/index.js";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../lib/errors.js";
import { configName } from "../../lib/config.js";

export class GenerateTypes extends SwankyCommand<typeof GenerateTypes> {
static description = "Generate types from compiled contract metadata";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import inquirer from "inquirer";
import TOML from "@iarna/toml";
import { choice, email, name, pickNodeVersion, pickTemplate } from "../../lib/prompts.js";
import {
buildSwankyConfig,
checkCliDependencies,
copyCommonTemplateFiles,
copyContractTemplateFiles,
Expand All @@ -27,6 +26,7 @@ import inquirerFuzzyPath from "inquirer-fuzzy-path";
import chalk from "chalk";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { DEFAULT_NODE_INFO } from "../../lib/consts.js";
import { buildSwankyConfig } from "../../lib/config.js";

type TaskFunction = (...args: any[]) => any;

Expand Down
4 changes: 2 additions & 2 deletions src/commands/node/chopsticks/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import path from "node:path";
import { SwankyCommand } from "../../../lib/swankyCommand.js";
import {
copyChopsticksTemplateFile,
getSwankyConfig,
getTemplates,
} from "../../../lib/index.js";
import { ConfigBuilder } from "../../../lib/config-builder.js";
import { SwankyConfig } from "../../../types/index.js";
import { getSwankyConfig } from "../../../lib/config.js";

export const chopsticksConfig = "dev.yml";

export class InitChopsticks extends SwankyCommand<typeof InitChopsticks> {
static description = "Initialize Zombienet";
static description = "Initialize chopsticks config";

async run(): Promise<void> {
const localConfig = getSwankyConfig("local") as SwankyConfig;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/node/chopsticks/start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Flags } from "@oclif/core";
import { execaCommand } from "execa";
import { SwankyCommand } from "../../../lib/swankyCommand.js";
import { swankyNodeCheck } from "../../../lib/index.js";
import { pathExistsSync } from "fs-extra/esm";
import { ConfigError, FileError } from "../../../lib/errors.js";
import { ensureSwankyNodeInstalled } from "../../../lib/config.js";
export class StartChopsticks extends SwankyCommand<typeof StartChopsticks> {
static description = "Start chopsticks";

Expand All @@ -16,7 +16,7 @@ export class StartChopsticks extends SwankyCommand<typeof StartChopsticks> {
async run(): Promise<void> {
const { flags } = await this.parse(StartChopsticks);

swankyNodeCheck(this.swankyConfig);
ensureSwankyNodeInstalled(this.swankyConfig);

if(!this.swankyConfig.node.chopsticks && !flags.chopsticksConfigPath) {
throw new ConfigError("Chopsticks config not set in swanky config. Please set it in swanky config or provide the path to the chopsticks config file using --config flag.");
Expand Down
3 changes: 2 additions & 1 deletion src/commands/node/install.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { Flags } from "@oclif/core";
import { downloadNode, getSwankyConfig, swankyNodeVersions } from "../../lib/index.js";
import { downloadNode, swankyNodeVersions } from "../../lib/index.js";
import path from "node:path";
import inquirer from "inquirer";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { DEFAULT_NODE_INFO } from "../../lib/consts.js";
import { choice, pickNodeVersion } from "../../lib/prompts.js";
import { InputError } from "../../lib/errors.js";
import { getSwankyConfig } from "../../lib/config.js";

export class InstallNode extends SwankyCommand<typeof InstallNode> {
static description = "Install swanky node binary";
Expand Down
4 changes: 2 additions & 2 deletions src/commands/node/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Flags } from "@oclif/core";
import { execaCommand } from "execa";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import semver from "semver";
import { swankyNodeCheck } from "../../lib/index.js";
import { ensureSwankyNodeInstalled } from "../../lib/config.js";
export class StartNode extends SwankyCommand<typeof StartNode> {
static description = "Start a local node";

Expand Down Expand Up @@ -30,7 +30,7 @@ export class StartNode extends SwankyCommand<typeof StartNode> {
async run(): Promise<void> {
const { flags } = await this.parse(StartNode);

swankyNodeCheck(this.swankyConfig);
ensureSwankyNodeInstalled(this.swankyConfig);

// Run persistent mode by default. non-persistent mode in case flag is provided.
// Non-Persistent mode (`--dev`) allows all CORS origin, without `--dev`, users need to specify origins by `--rpc-cors`.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/zombienet/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
buildZombienetConfigFromBinaries,
copyZombienetTemplateFile,
downloadZombienetBinaries,
getSwankyConfig,
getTemplates,
osCheck,
Spinner,
Expand All @@ -14,6 +13,7 @@ import { pathExistsSync } from "fs-extra/esm";
import { zombienet, zombienetBinariesList } from "../../lib/zombienetInfo.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { SwankyConfig, ZombienetData } from "../../index.js";
import { getSwankyConfig } from "../../lib/config.js";

export const zombienetConfig = "zombienet.config.toml";

Expand Down
101 changes: 2 additions & 99 deletions src/lib/command-utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { execaCommand, execaCommandSync } from "execa";
import { copy, emptyDir, ensureDir, readJSONSync } from "fs-extra/esm";
import { copy, emptyDir, ensureDir } from "fs-extra/esm";
import path from "node:path";
import {
DEFAULT_NETWORK_URL,
ARTIFACTS_PATH,
TYPED_CONTRACTS_PATH,
DEFAULT_SHIBUYA_NETWORK_URL,
DEFAULT_SHIDEN_NETWORK_URL,
DEFAULT_ASTAR_NETWORK_URL,
DEFAULT_ACCOUNT,
DEFAULT_CONFIG_NAME,
DEFAULT_CONFIG_FOLDER_NAME,
DEFAULT_NODE_INFO,
} from "./consts.js";
import { SwankyConfig, SwankySystemConfig } from "../types/index.js";
import { SwankyConfig } from "../types/index.js";
import { ConfigError, FileError } from "./errors.js";
import { userInfo } from "os";
import { existsSync } from "fs";

export function commandStdoutOrNull(command: string): string | null {
try {
Expand All @@ -27,26 +18,6 @@ export function commandStdoutOrNull(command: string): string | null {
}
}

export function getSwankyConfig(configType: "local" | "global"): SwankyConfig | SwankySystemConfig {
let configPath: string;

if (configType === "global") {
configPath = getSystemConfigDirectoryPath() + `/${DEFAULT_CONFIG_NAME}`;
} else {
configPath = isEnvConfigCheck() ? process.env.SWANKY_CONFIG! : DEFAULT_CONFIG_NAME;
}

const config = readJSONSync(configPath);
return config;
}


export function getSystemConfigDirectoryPath(): string {
const homeDir = userInfo().homedir;
const configPath = homeDir + `/${DEFAULT_CONFIG_FOLDER_NAME}`;
return configPath;
}

export function resolveNetworkUrl(config: SwankyConfig, networkName: string): string {
if (networkName === "") {
return DEFAULT_NETWORK_URL;
Expand Down Expand Up @@ -144,72 +115,4 @@ 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: DEFAULT_NODE_INFO.polkadotPalletVersions,
supportedInk: DEFAULT_NODE_INFO.supportedInk,
version: DEFAULT_NODE_INFO.version,
},
defaultAccount: DEFAULT_ACCOUNT,
accounts: [
{
"alias": "alice",
"mnemonic": "//Alice",
"isDev": true,
"address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
},
{
"alias": "bob",
"mnemonic": "//Bob",
"isDev": true,
"address": "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
},
],
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 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 defaultLocalConfigPath = process.cwd() + `/${DEFAULT_CONFIG_NAME}`;
return process.env.SWANKY_CONFIG === undefined
? existsSync(defaultLocalConfigPath)
: existsSync(process.env.SWANKY_CONFIG);
}

export function configName(): string {
if (!isLocalConfigCheck()) {
return DEFAULT_CONFIG_NAME + " [system config]";
}

return process.env.SWANKY_CONFIG?.split("/").pop() ?? DEFAULT_CONFIG_NAME;
}

export function swankyNodeCheck(config: SwankyConfig){
if(config.node.localPath === "") {
throw new FileError('Swanky node is not installed. Please run `swanky node:install` first.');
}
}
Loading

0 comments on commit 9b673d1

Please sign in to comment.