Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/commands/contracts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import os from "os";
import path from "path";
import {BaseAction} from "../../lib/actions/BaseAction";
import {pathToFileURL} from "url";
Expand Down Expand Up @@ -34,9 +35,14 @@ export class DeployAction extends BaseAction {
}

private async executeTsScript(filePath: string, rpcUrl?: string): Promise<void> {
const outFile = filePath.replace(/\.ts$/, ".compiled.js");
let tempDir: string | undefined;
this.startSpinner(`Transpiling TypeScript file: ${filePath}`);
try {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "genlayer-deploy-"));
const outFile = path.join(
tempDir,
path.basename(filePath).replace(/\.ts$/, ".compiled.js"),
);
buildSync({
entryPoints: [filePath],
outfile: outFile,
Expand All @@ -50,7 +56,9 @@ export class DeployAction extends BaseAction {
} catch (error) {
this.failSpinner(`Error executing: ${filePath}`, error);
} finally {
fs.unlinkSync(outFile);
if (tempDir) {
fs.rmSync(tempDir, {recursive: true, force: true});
}
}
}

Expand Down
23 changes: 19 additions & 4 deletions tests/actions/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {describe, test, vi, beforeEach, afterEach, expect} from "vitest";
import fs from "fs";
import os from "os";
import path from "path";
import {createClient, createAccount, isSuccessful, formatStakingAmount, DEPLOY_CALL_KEY} from "genlayer-js";
import {DeployAction, DeployOptions} from "../../src/commands/contracts/deploy";
import {buildSync} from "esbuild";
Expand Down Expand Up @@ -28,6 +29,8 @@ describe("DeployAction", () => {
vi.clearAllMocks();
// Setup mocks before creating the action (needed for constructor)
vi.mocked(os.homedir).mockReturnValue("/mocked/home");
vi.mocked(os.tmpdir).mockReturnValue("/mocked/tmp");
vi.mocked(fs.mkdtempSync).mockReturnValue("/mocked/tmp/genlayer-deploy-abc");
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({activeAccount: "default"}));

Expand Down Expand Up @@ -427,7 +430,7 @@ describe("DeployAction", () => {

test("executeTsScript transpiles and executes TypeScript", async () => {
const filePath = "/mocked/script.ts";
const outFile = "/mocked/script.compiled.js";
const outFile = path.join("/mocked/tmp/genlayer-deploy-abc", "script.compiled.js");

vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
vi.mocked(buildSync).mockImplementation((() => {}) as any);
Expand All @@ -446,7 +449,12 @@ describe("DeployAction", () => {
});

expect(deployer["executeJsScript"]).toHaveBeenCalledWith(filePath, outFile, undefined);
expect(fs.unlinkSync).toHaveBeenCalledWith(outFile);
expect(fs.mkdtempSync).toHaveBeenCalledWith(path.join("/mocked/tmp", "genlayer-deploy-"));
expect(fs.rmSync).toHaveBeenCalledWith("/mocked/tmp/genlayer-deploy-abc", {
recursive: true,
force: true,
});
expect(fs.unlinkSync).not.toHaveBeenCalled();
});

test("deployScripts fails when deploy folder is missing", async () => {
Expand Down Expand Up @@ -595,6 +603,10 @@ describe("DeployAction", () => {
await deployer["executeTsScript"](filePath);

expect(deployer["failSpinner"]).toHaveBeenCalledWith(`Error executing: ${filePath}`, error);
expect(fs.rmSync).toHaveBeenCalledWith("/mocked/tmp/genlayer-deploy-abc", {
recursive: true,
force: true,
});
});

test("deploys contract with rpc option", async () => {
Expand Down Expand Up @@ -649,7 +661,7 @@ describe("DeployAction", () => {

test("executeTsScript passes rpc url to executeJsScript", async () => {
const filePath = "/mocked/script.ts";
const outFile = "/mocked/script.compiled.js";
const outFile = path.join("/mocked/tmp/genlayer-deploy-abc", "script.compiled.js");
const rpcUrl = "https://custom-rpc-url.com";

vi.spyOn(deployer as any, "executeJsScript").mockResolvedValue(undefined);
Expand All @@ -669,7 +681,10 @@ describe("DeployAction", () => {
});

expect(deployer["executeJsScript"]).toHaveBeenCalledWith(filePath, outFile, rpcUrl);
expect(fs.unlinkSync).toHaveBeenCalledWith(outFile);
expect(fs.rmSync).toHaveBeenCalledWith("/mocked/tmp/genlayer-deploy-abc", {
recursive: true,
force: true,
});
});

test("deployScripts passes rpc url to script execution methods", async () => {
Expand Down
Loading