Skip to content

Commit

Permalink
chore: reorg tasks helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Mar 18, 2024
1 parent 01cd1f9 commit 32273e1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 86 deletions.
5 changes: 2 additions & 3 deletions src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Args, Flags } from "@oclif/core";
import { spawn } from "node:child_process";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import {
ensureCargoContractVersionCompatibility,
extractCargoContractVersion,
Spinner,
storeArtifacts,
Expand All @@ -12,7 +11,7 @@ import {
import { InputError, ProcessError } from "../../lib/errors.js";
import { BuildMode, SwankyConfig } from "../../index.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import { ensureContractNameOrAllFlagIsSet, ensureContractPathExists } from "../../lib/checks.js";
import { ensureContractNameOrAllFlagIsSet, ensureContractPathExists, ensureCargoContractVersionCompatibility, } from "../../lib/checks.js";

export class CompileContract extends SwankyCommand<typeof CompileContract> {
static description = "Compile the smart contract(s) in your contracts directory";
Expand Down Expand Up @@ -59,7 +58,7 @@ export class CompileContract extends SwankyCommand<typeof CompileContract> {

for (const contractName of contractNames) {
this.logger.info(`Started compiling contract [${contractName}]`);

const contractRecord = findContractRecord(this.swankyConfig, contractName);

await ensureContractPathExists(contractName);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/contract/verify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Args, Flags } from "@oclif/core";
import {
ensureCargoContractVersionCompatibility,
extractCargoContractVersion,
findContractRecord,
getSwankyConfig,
Expand All @@ -11,7 +10,7 @@ import { 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 { ensureContractNameOrAllFlagIsSet, ensureContractPathExists } from "../../lib/checks.js";
import { ensureContractNameOrAllFlagIsSet, ensureContractPathExists, ensureCargoContractVersionCompatibility } from "../../lib/checks.js";

export class VerifyContract extends SwankyCommand<typeof VerifyContract> {
static description = "Verify the smart contract(s) in your contracts directory";
Expand Down
21 changes: 20 additions & 1 deletion src/lib/checks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ConfigError, FileError, InputError } from "./errors.js";
import { ConfigError, FileError, InputError, ProcessError } from "./errors.js";
import { pathExists } from "fs-extra";
import chalk from "chalk";
import path from "node:path";
import semver from "semver";
import { Contract } from "./contract.js";
import { AccountData, ContractData } from "../types/index.js";

Expand Down Expand Up @@ -59,3 +60,21 @@ export function ensureDevAccountNotInProduction(accountData: AccountData, networ
);
}
}

export function ensureCargoContractVersionCompatibility(
cargoContractVersion: string,
minimalVersion: string,
invalidVersionsList?: string[]
) {
if (invalidVersionsList?.includes(cargoContractVersion)) {
throw new ProcessError(
`The cargo-contract version ${cargoContractVersion} is not supported. Please update or change the version.`
);
}

if (!semver.satisfies(cargoContractVersion.replace(/-.*$/, ""), `>=${minimalVersion}`)) {
throw new ProcessError(
`cargo-contract version >= ${minimalVersion} required, but found version ${cargoContractVersion}. Please update to a compatible version.`
);
}
}
80 changes: 0 additions & 80 deletions src/lib/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
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";
import { nodeInfo } from "./nodeInfo.js";
import decompress from "decompress";
import { Spinner } from "./spinner.js";
Expand All @@ -17,7 +13,6 @@ import { zombienetConfig } from "../commands/zombienet/init.js";
import { readFileSync } from "fs";
import TOML from "@iarna/toml";
import { writeFileSync } from "node:fs";
import { chopsticksConfig } from "../commands/node/chopsticks/init.js";

export async function checkCliDependencies(spinner: Spinner) {
const dependencyList = [
Expand Down Expand Up @@ -84,33 +79,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,
Expand Down Expand Up @@ -146,20 +114,6 @@ export async function prepareTestFiles(
}
}

export async function processTemplates(projectPath: string, templateData: Record<string, string>) {
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);
Expand Down Expand Up @@ -211,22 +165,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);
Expand Down Expand Up @@ -359,21 +297,3 @@ export async function installDeps(projectPath: string) {
await execaCommand(installCommand, { cwd: projectPath });
}
}

export function ensureCargoContractVersionCompatibility(
cargoContractVersion: string,
minimalVersion: string,
invalidVersionsList?: string[]
) {
if (invalidVersionsList?.includes(cargoContractVersion)) {
throw new ProcessError(
`The cargo-contract version ${cargoContractVersion} is not supported. Please update or change the version.`
);
}

if (!semver.satisfies(cargoContractVersion.replace(/-.*$/, ""), `>=${minimalVersion}`)) {
throw new ProcessError(
`cargo-contract version >= ${minimalVersion} required, but found version ${cargoContractVersion}. Please update to a compatible version.`
);
}
}
63 changes: 63 additions & 0 deletions src/lib/templates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { readdirSync } from "fs";
import { fileURLToPath } from "url";
import path from "node:path";
import { globby } from "globby";
import handlebars from "handlebars";
import { ensureDir, copy } from "fs-extra";
import { readFile, rename, rm, writeFile } from "fs/promises";
import { chopsticksConfig } from "../commands/node/chopsticks/init.js";
import { zombienetConfig } from "../commands/zombienet/init.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -25,3 +31,60 @@ export function getTemplates() {
chopsticksTemplatesPath,
};
}

export async function copyContractTemplateFiles(
contractTemplatePath: string,
contractName: string,
projectPath: string,
) {
await copy(
path.resolve(contractTemplatePath, "contract"),
path.resolve(projectPath, "contracts", contractName),
);
}

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 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 processTemplates(projectPath: string, templateData: Record<string, string>) {
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);
}
}

0 comments on commit 32273e1

Please sign in to comment.