From 9b673d10c218f4e5ff37b2c78a6168e4cfb26fbb Mon Sep 17 00:00:00 2001 From: prxgr4mm3r Date: Thu, 29 Feb 2024 20:56:18 +0200 Subject: [PATCH] refactor command-utils.ts and tasks.ts --- src/commands/account/create.ts | 3 +- src/commands/account/default.ts | 2 +- src/commands/contract/compile.ts | 3 +- src/commands/contract/deploy.ts | 3 +- src/commands/contract/explain.ts | 2 +- src/commands/contract/new.ts | 2 +- src/commands/contract/test.ts | 3 +- src/commands/contract/verify.ts | 3 +- src/commands/generate/types.ts | 3 +- src/commands/init/index.ts | 2 +- src/commands/node/chopsticks/init.ts | 4 +- src/commands/node/chopsticks/start.ts | 4 +- src/commands/node/install.ts | 3 +- src/commands/node/start.ts | 4 +- src/commands/zombienet/init.ts | 2 +- src/lib/command-utils.ts | 101 +------------------------- src/lib/config.ts | 101 ++++++++++++++++++++++++++ src/lib/contractCall.ts | 3 +- src/lib/swankyCommand.ts | 6 +- src/lib/tasks.ts | 61 ---------------- src/lib/templates.ts | 64 ++++++++++++++++ 21 files changed, 196 insertions(+), 183 deletions(-) create mode 100644 src/lib/config.ts diff --git a/src/commands/account/create.ts b/src/commands/account/create.ts index 8972f8af..f887aff5 100644 --- a/src/commands/account/create.ts +++ b/src/commands/account/create.ts @@ -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 { static description = "Create a new dev account in config"; diff --git a/src/commands/account/default.ts b/src/commands/account/default.ts index 7ffdb422..0aac5f84 100644 --- a/src/commands/account/default.ts +++ b/src/commands/account/default.ts @@ -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 { static description = "Set default account to use"; diff --git a/src/commands/contract/compile.ts b/src/commands/contract/compile.ts index 93a00538..87cf7675 100644 --- a/src/commands/contract/compile.ts +++ b/src/commands/contract/compile.ts @@ -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 { static description = "Compile the smart contract(s) in your contracts directory"; diff --git a/src/commands/contract/deploy.ts b/src/commands/contract/deploy.ts index 44d3cd8d..74a63b94 100644 --- a/src/commands/contract/deploy.ts +++ b/src/commands/contract/deploy.ts @@ -1,6 +1,6 @@ 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"; @@ -8,6 +8,7 @@ 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 { static description = "Deploy contract to a running node"; diff --git a/src/commands/contract/explain.ts b/src/commands/contract/explain.ts index 3efba84e..e3e74215 100644 --- a/src/commands/contract/explain.ts +++ b/src/commands/contract/explain.ts @@ -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 { static description = "Explain contract messages based on the contracts' metadata"; diff --git a/src/commands/contract/new.ts b/src/commands/contract/new.ts index 0547f2eb..f0a273d1 100644 --- a/src/commands/contract/new.ts +++ b/src/commands/contract/new.ts @@ -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"; @@ -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 { static description = "Generate a new smart contract template inside a project"; diff --git a/src/commands/contract/test.ts b/src/commands/contract/test.ts index 29bd6e6a..b14694bf 100644 --- a/src/commands/contract/test.ts +++ b/src/commands/contract/test.ts @@ -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 diff --git a/src/commands/contract/verify.ts b/src/commands/contract/verify.ts index a0d4b85f..9afa5d8b 100644 --- a/src/commands/contract/verify.ts +++ b/src/commands/contract/verify.ts @@ -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 { static description = "Verify the smart contract(s) in your contracts directory"; diff --git a/src/commands/generate/types.ts b/src/commands/generate/types.ts index a6c0ed24..bb9fcf75 100644 --- a/src/commands/generate/types.ts +++ b/src/commands/generate/types.ts @@ -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 { static description = "Generate types from compiled contract metadata"; diff --git a/src/commands/init/index.ts b/src/commands/init/index.ts index 1cbe45ce..cfc63f3e 100644 --- a/src/commands/init/index.ts +++ b/src/commands/init/index.ts @@ -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, @@ -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; diff --git a/src/commands/node/chopsticks/init.ts b/src/commands/node/chopsticks/init.ts index 4e5acdf5..fc811e3b 100644 --- a/src/commands/node/chopsticks/init.ts +++ b/src/commands/node/chopsticks/init.ts @@ -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 { - static description = "Initialize Zombienet"; + static description = "Initialize chopsticks config"; async run(): Promise { const localConfig = getSwankyConfig("local") as SwankyConfig; diff --git a/src/commands/node/chopsticks/start.ts b/src/commands/node/chopsticks/start.ts index babc1cd9..dc706d76 100644 --- a/src/commands/node/chopsticks/start.ts +++ b/src/commands/node/chopsticks/start.ts @@ -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 { static description = "Start chopsticks"; @@ -16,7 +16,7 @@ export class StartChopsticks extends SwankyCommand { async run(): Promise { 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."); diff --git a/src/commands/node/install.ts b/src/commands/node/install.ts index 9af510e1..4a727b99 100644 --- a/src/commands/node/install.ts +++ b/src/commands/node/install.ts @@ -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 { static description = "Install swanky node binary"; diff --git a/src/commands/node/start.ts b/src/commands/node/start.ts index 6a09fb29..08ec7da8 100644 --- a/src/commands/node/start.ts +++ b/src/commands/node/start.ts @@ -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 { static description = "Start a local node"; @@ -30,7 +30,7 @@ export class StartNode extends SwankyCommand { async run(): Promise { 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`. diff --git a/src/commands/zombienet/init.ts b/src/commands/zombienet/init.ts index cea138f8..5ccca218 100644 --- a/src/commands/zombienet/init.ts +++ b/src/commands/zombienet/init.ts @@ -5,7 +5,6 @@ import { buildZombienetConfigFromBinaries, copyZombienetTemplateFile, downloadZombienetBinaries, - getSwankyConfig, getTemplates, osCheck, Spinner, @@ -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"; diff --git a/src/lib/command-utils.ts b/src/lib/command-utils.ts index 53f1040f..1dd1ea0d 100644 --- a/src/lib/command-utils.ts +++ b/src/lib/command-utils.ts @@ -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 { @@ -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; @@ -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.'); - } -} \ No newline at end of file diff --git a/src/lib/config.ts b/src/lib/config.ts new file mode 100644 index 00000000..4af932bf --- /dev/null +++ b/src/lib/config.ts @@ -0,0 +1,101 @@ +import { SwankyConfig, SwankySystemConfig } from "../types/index.js"; +import { + DEFAULT_ACCOUNT, DEFAULT_ASTAR_NETWORK_URL, + DEFAULT_CONFIG_FOLDER_NAME, + DEFAULT_CONFIG_NAME, + DEFAULT_NETWORK_URL, + DEFAULT_NODE_INFO, DEFAULT_SHIBUYA_NETWORK_URL, DEFAULT_SHIDEN_NETWORK_URL, +} from "./consts.js"; +import { readJSONSync } from "fs-extra"; +import { userInfo } from "os"; +import { ConfigError, FileError } from "./errors.js"; +import { existsSync } from "fs"; + +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 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 ensureSwankyNodeInstalled(config: SwankyConfig){ + if(config.node.localPath === "") { + throw new FileError('Swanky node is not installed. Please run `swanky node:install` first.'); + } +} \ No newline at end of file diff --git a/src/lib/contractCall.ts b/src/lib/contractCall.ts index a65f4894..c32eaee4 100644 --- a/src/lib/contractCall.ts +++ b/src/lib/contractCall.ts @@ -1,4 +1,4 @@ -import { AbiType, ChainAccount, ChainApi, configName, ensureAccountIsSet, decrypt, resolveNetworkUrl } from "./index.js"; +import { AbiType, ChainAccount, ChainApi, decrypt, resolveNetworkUrl } from "./index.js"; import { ContractData, DeploymentData, Encrypted } from "../types/index.js"; import { Args, Command, Flags, Interfaces } from "@oclif/core"; import inquirer from "inquirer"; @@ -7,6 +7,7 @@ import { SwankyCommand } from "./swankyCommand.js"; import { cryptoWaitReady } from "@polkadot/util-crypto/crypto"; import { Contract } from "./contract.js"; import { ConfigError, FileError, NetworkError } from "./errors.js"; +import { configName, ensureAccountIsSet } from "./config.js"; export type JoinedFlagsType = Interfaces.InferredFlags< (typeof ContractCall)["baseFlags"] & T["flags"] diff --git a/src/lib/swankyCommand.ts b/src/lib/swankyCommand.ts index a184cb86..5491fefa 100644 --- a/src/lib/swankyCommand.ts +++ b/src/lib/swankyCommand.ts @@ -1,9 +1,6 @@ import { Command, Flags, Interfaces } from "@oclif/core"; import chalk from "chalk"; -import { buildSwankyConfig, - configName, - getSwankyConfig, - getSystemConfigDirectoryPath, Spinner } from "./index.js"; +import { Spinner } from "./index.js"; import { AccountData, SwankyConfig, SwankySystemConfig } from "../types/index.js"; import { writeJSON } from "fs-extra/esm"; import { existsSync, mkdirSync } from "fs"; @@ -12,6 +9,7 @@ import { swankyLogger } from "./logger.js"; import { Logger } from "winston"; import path from "node:path"; import { DEFAULT_CONFIG_FOLDER_NAME, DEFAULT_CONFIG_NAME } from "./consts.js"; +import { buildSwankyConfig, configName, getSwankyConfig, getSystemConfigDirectoryPath } from "./config.js"; export type Flags = Interfaces.InferredFlags< (typeof SwankyCommand)["baseFlags"] & T["flags"] diff --git a/src/lib/tasks.ts b/src/lib/tasks.ts index 7a5b745e..65e3c4f7 100644 --- a/src/lib/tasks.ts +++ b/src/lib/tasks.ts @@ -1,9 +1,6 @@ import { execaCommand } from "execa"; import { copy, ensureDir, remove } from "fs-extra/esm"; -import { readFile, rename, rm, writeFile } from "fs/promises"; import path from "node:path"; -import { globby } from "globby"; -import handlebars from "handlebars"; import { DownloadEndedStats, DownloaderHelper } from "node-downloader-helper"; import process from "node:process"; import semver from "semver"; @@ -18,7 +15,6 @@ import { readFileSync } from "fs"; import TOML from "@iarna/toml"; import { writeFileSync } from "node:fs"; import { commandStdoutOrNull } from "./command-utils.js"; -import { chopsticksConfig } from "../commands/node/chopsticks/init.js"; export async function checkCliDependencies(spinner: Spinner) { const dependencyList = [ @@ -59,33 +55,6 @@ export function osCheck() { return { platform, arch }; } -export async function copyCommonTemplateFiles(templatesPath: string, projectPath: string) { - await ensureDir(projectPath); - const commonFiles = await globby(`*`, { cwd: templatesPath }); - await Promise.all( - commonFiles.map(async (file) => { - await copy(path.resolve(templatesPath, file), path.resolve(projectPath, file)); - }), - ); - await rename(path.resolve(projectPath, "gitignore"), path.resolve(projectPath, ".gitignore")); - await rename( - path.resolve(projectPath, "mocharc.json"), - path.resolve(projectPath, ".mocharc.json"), - ); - await copy(path.resolve(templatesPath, "github"), path.resolve(projectPath, ".github")); -} - -export async function copyContractTemplateFiles( - contractTemplatePath: string, - contractName: string, - projectPath: string, -) { - await copy( - path.resolve(contractTemplatePath, "contract"), - path.resolve(projectPath, "contracts", contractName), - ); -} - export async function prepareTestFiles( testType: TestType, templatePath: string, @@ -121,20 +90,6 @@ export async function prepareTestFiles( } } -export async function processTemplates(projectPath: string, templateData: Record) { - const templateFiles = await globby(projectPath, { - expandDirectories: { extensions: ["hbs"] }, - }); - - for (const tplFilePath of templateFiles) { - const rawTemplate = await readFile(tplFilePath, "utf8"); - const template = handlebars.compile(rawTemplate); - const compiledFile = template(templateData); - await rm(tplFilePath); - await writeFile(tplFilePath.split(".hbs")[0], compiledFile); - } -} - export async function downloadNode(projectPath: string, nodeInfo: nodeInfo, spinner: Spinner) { const binPath = path.resolve(projectPath, "bin"); await ensureDir(binPath); @@ -186,22 +141,6 @@ export async function downloadNode(projectPath: string, nodeInfo: nodeInfo, spin return path.resolve(binPath, dlFileDetails.filePath); } -export async function copyZombienetTemplateFile(templatePath: string, configPath: string) { - await ensureDir(configPath); - await copy( - path.resolve(templatePath, zombienetConfig), - path.resolve(configPath, zombienetConfig), - ); -} - -export async function copyChopsticksTemplateFile(templatePath: string, configPath: string) { - await ensureDir(configPath); - await copy( - path.resolve(templatePath, chopsticksConfig), - path.resolve(configPath, chopsticksConfig), - ); -} - export async function downloadZombienetBinaries(binaries: string[], projectPath: string, swankyConfig: SwankyConfig, spinner: Spinner) { const binPath = path.resolve(projectPath, "zombienet", "bin"); await ensureDir(binPath); diff --git a/src/lib/templates.ts b/src/lib/templates.ts index 225278ca..42d3de28 100644 --- a/src/lib/templates.ts +++ b/src/lib/templates.ts @@ -1,6 +1,12 @@ import { readdirSync } from "fs"; import { fileURLToPath } from "url"; import path from "node:path"; +import { copy, ensureDir } from "fs-extra"; +import { globby } from "globby"; +import { readFile, rename, rm, writeFile } from "fs/promises"; +import handlebars from "handlebars"; +import { zombienetConfig } from "../commands/zombienet/init.js"; +import { chopsticksConfig } from "../commands/node/chopsticks/init.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -25,3 +31,61 @@ export function getTemplates() { chopsticksTemplatesPath, }; } + +export async function copyCommonTemplateFiles(templatesPath: string, projectPath: string) { + await ensureDir(projectPath); + const commonFiles = await globby(`*`, { cwd: templatesPath }); + await Promise.all( + commonFiles.map(async (file) => { + await copy(path.resolve(templatesPath, file), path.resolve(projectPath, file)); + }), + ); + await rename(path.resolve(projectPath, "gitignore"), path.resolve(projectPath, ".gitignore")); + await rename( + path.resolve(projectPath, "mocharc.json"), + path.resolve(projectPath, ".mocharc.json"), + ); + await copy(path.resolve(templatesPath, "github"), path.resolve(projectPath, ".github")); +} + +export async function copyContractTemplateFiles( + contractTemplatePath: string, + contractName: string, + projectPath: string, +) { + await copy( + path.resolve(contractTemplatePath, "contract"), + path.resolve(projectPath, "contracts", contractName), + ); +} + +export async function processTemplates(projectPath: string, templateData: Record) { + const templateFiles = await globby(projectPath, { + expandDirectories: { extensions: ["hbs"] }, + }); + + for (const tplFilePath of templateFiles) { + const rawTemplate = await readFile(tplFilePath, "utf8"); + const template = handlebars.compile(rawTemplate); + const compiledFile = template(templateData); + await rm(tplFilePath); + await writeFile(tplFilePath.split(".hbs")[0], compiledFile); + } +} + + +export async function copyZombienetTemplateFile(templatePath: string, configPath: string) { + await ensureDir(configPath); + await copy( + path.resolve(templatePath, zombienetConfig), + path.resolve(configPath, zombienetConfig), + ); +} + +export async function copyChopsticksTemplateFile(templatePath: string, configPath: string) { + await ensureDir(configPath); + await copy( + path.resolve(templatePath, chopsticksConfig), + path.resolve(configPath, chopsticksConfig), + ); +} \ No newline at end of file