Repository: NomicFoundation/hardhat-ignition
Pull request #582 (2023-10-23 13:21:32)

Add a message when a deployment is being resumed


Patch:
diff --git a/packages/core/src/internal/deployer.ts b/packages/core/src/internal/deployer.ts
index b1b5be94..65aa4327 100644
--- a/packages/core/src/internal/deployer.ts
+++ b/packages/core/src/internal/deployer.ts
@@ -76,9 +76,16 @@ export class Deployer {
     accounts: string[],
     defaultSender: string
   ): Promise<DeploymentResult> {
-    let deploymentState = await this._getOrInitializeDeploymentState();
+    const deployment = await this._getOrInitializeDeploymentState();
 
-    this._emitDeploymentStartEvent(ignitionModule.id, this._deploymentDir);
+    const isResumed = deployment.isResumed;
+    let deploymentState = deployment.deploymentState;
+
+    this._emitDeploymentStartEvent(
+      ignitionModule.id,
+      this._deploymentDir,
+      isResumed
+    );
 
     const contracts =
       getFuturesFromModule(ignitionModule).filter(isContractFuture);
@@ -223,12 +230,27 @@ export class Deployer {
     };
   }
 
-  private async _getOrInitializeDeploymentState(): Promise<DeploymentState> {
+  /**
+   * Fetches the existing deployment state or initializes a new one.
+   *
+   * @returns An object with the deployment state and a boolean indicating
+   * if the deployment is being resumed (i.e. the deployment state is not
+   * new).
+   */
+  private async _getOrInitializeDeploymentState(): Promise<{
+    deploymentState: DeploymentState;
+    isResumed: boolean;
+  }> {
     const chainId = await this._jsonRpcClient.getChainId();
     const deploymentState = await loadDeploymentState(this._deploymentLoader);
 
     if (deploymentState === undefined) {
-      return initializeDeploymentState(chainId, this._deploymentLoader);
+      const newState = await initializeDeploymentState(
+        chainId,
+        this._deploymentLoader
+      );
+
+      return { deploymentState: newState, isResumed: false };
     }
 
     // TODO: this should be moved out, it is not obvious that a significant
@@ -240,12 +262,13 @@ export class Deployer {
       });
     }
 
-    return deploymentState;
+    return { deploymentState, isResumed: true };
   }
 
   private _emitDeploymentStartEvent(
     moduleId: string,
-    deploymentDir: string | undefined
+    deploymentDir: string | undefined,
+    isResumed: boolean
   ): void {
     if (this._executionEventListener === undefined) {
       return;
@@ -255,6 +278,7 @@ export class Deployer {
       type: ExecutionEventType.DEPLOYMENT_START,
       moduleName: moduleId,
       deploymentDir: deploymentDir ?? undefined,
+      isResumed,
     });
   }
 
diff --git a/packages/core/src/types/execution-events.ts b/packages/core/src/types/execution-events.ts
index 7fb61c06..64111807 100644
--- a/packages/core/src/types/execution-events.ts
+++ b/packages/core/src/types/execution-events.ts
@@ -77,6 +77,7 @@ export interface DeploymentStartEvent {
   type: ExecutionEventType.DEPLOYMENT_START;
   moduleName: string;
   deploymentDir: string | undefined;
+  isResumed: boolean;
 }
 
 /**
diff --git a/packages/hardhat-plugin/package.json b/packages/hardhat-plugin/package.json
index 38f480d5..20c62f1e 100644
--- a/packages/hardhat-plugin/package.json
+++ b/packages/hardhat-plugin/package.json
@@ -18,6 +18,13 @@
   ],
   "main": "dist/src/index.js",
   "types": "dist/src/index.d.ts",
+  "files": [
+    "dist/src/",
+    "src/",
+    "LICENSE",
+    "README.md",
+    "CHANGELOG.md"
+  ],
   "exports": {
     ".": "./dist/src/index.js",
     "./modules": "./dist/src/modules.js"
diff --git a/packages/hardhat-plugin/src/ui/helpers/calculate-deploying-module-panel.ts b/packages/hardhat-plugin/src/ui/helpers/calculate-deploying-module-panel.ts
index 84f03d8f..ed183624 100644
--- a/packages/hardhat-plugin/src/ui/helpers/calculate-deploying-module-panel.ts
+++ b/packages/hardhat-plugin/src/ui/helpers/calculate-deploying-module-panel.ts
@@ -2,10 +2,24 @@ import chalk from "chalk";
 
 import { UiState } from "../types";
 
+import { pathFromCwd } from "./cwd-relative-path";
+
 export function calculateDeployingModulePanel(state: UiState): string {
   let deployingMessage = `Hardhat Ignition 🚀
 
-${chalk.bold(`Deploying [ ${state.moduleName ?? "unknown"} ]`)}
+`;
+
+  if (state.isResumed === true) {
+    deployingMessage += `${chalk.bold(
+      `Resuming existing deployment from ${pathFromCwd(state.deploymentDir!)}`
+    )}
+
+`;
+  }
+
+  deployingMessage += `${chalk.bold(
+    `Deploying [ ${state.moduleName ?? "unknown"} ]`
+  )}
 `;
 
   if (state.warnings.length > 0) {
diff --git a/packages/hardhat-plugin/src/ui/helpers/cwd-relative-path.ts b/packages/hardhat-plugin/src/ui/helpers/cwd-relative-path.ts
new file mode 100644
index 00000000..ed6eeee0
--- /dev/null
+++ b/packages/hardhat-plugin/src/ui/helpers/cwd-relative-path.ts
@@ -0,0 +1,12 @@
+import path from "path";
+import process from "process";
+
+export function pathFromCwd(thePath: string): string {
+  const cwd = process.cwd();
+
+  if (thePath.startsWith(cwd)) {
+    return `.${path.sep}${path.relative(process.cwd(), thePath)}`;
+  }
+
+  return thePath;
+}
diff --git a/packages/hardhat-plugin/src/ui/pretty-event-handler.ts b/packages/hardhat-plugin/src/ui/pretty-event-handler.ts
index 8a1a1c49..36421f5a 100644
--- a/packages/hardhat-plugin/src/ui/pretty-event-handler.ts
+++ b/packages/hardhat-plugin/src/ui/pretty-event-handler.ts
@@ -60,6 +60,7 @@ export class PrettyEventHandler implements ExecutionEventListener {
     currentBatch: 0,
     result: null,
     warnings: [],
+    isResumed: null,
   };
 
   constructor(private _deploymentParams: DeploymentParameters = {}) {}
@@ -78,6 +79,7 @@ export class PrettyEventHandler implements ExecutionEventListener {
       status: UiStateDeploymentStatus.DEPLOYING,
       moduleName: event.moduleName,
       deploymentDir: event.deploymentDir,
+      isResumed: event.isResumed,
     };
 
     process.stdout.write(calculateStartingMessage(this.state));
diff --git a/packages/hardhat-plugin/src/ui/types.ts b/packages/hardhat-plugin/src/ui/types.ts
index 0c2e5066..ea7ba79e 100644
--- a/packages/hardhat-plugin/src/ui/types.ts
+++ b/packages/hardhat-plugin/src/ui/types.ts
@@ -61,6 +61,7 @@ export interface UiState {
   currentBatch: number;
   result: DeploymentResult | null;
   warnings: string[];
+  isResumed: boolean | null;
 }
 
 export interface AddressMap {


Test patch:
diff --git a/packages/hardhat-plugin/test/setup.ts b/packages/hardhat-plugin/test/setup.ts
index 69cdaf24..83ca4a59 100644
--- a/packages/hardhat-plugin/test/setup.ts
+++ b/packages/hardhat-plugin/test/setup.ts
@@ -4,6 +4,7 @@ import chaiAsPromised from "chai-as-promised";
 import sinon from "sinon";
 
 chai.use(chaiAsPromised);
+chai.config.truncateThreshold = 1000;
 
 afterEach(() => {
   sinon.restore();
diff --git a/packages/hardhat-plugin/test/ui/helpers/calculate-batch-display.ts b/packages/hardhat-plugin/test/ui/helpers/calculate-batch-display.ts
index 7cd582cb..25f3bf80 100644
--- a/packages/hardhat-plugin/test/ui/helpers/calculate-batch-display.ts
+++ b/packages/hardhat-plugin/test/ui/helpers/calculate-batch-display.ts
@@ -19,6 +19,7 @@ const exampleState: UiState = {
   currentBatch: 1,
   result: null,
   warnings: [],
+  isResumed: false,
 };
 
 describe("ui - calculate batch display", () => {
diff --git a/packages/hardhat-plugin/test/ui/helpers/calculate-deploying-module-panel-display.ts b/packages/hardhat-plugin/test/ui/helpers/calculate-deploying-module-panel-display.ts
index a28b4c80..c0afbe2d 100644
--- a/packages/hardhat-plugin/test/ui/helpers/calculate-deploying-module-panel-display.ts
+++ b/packages/hardhat-plugin/test/ui/helpers/calculate-deploying-module-panel-display.ts
@@ -1,5 +1,6 @@
 import { assert } from "chai";
 import chalk from "chalk";
+import path from "path";
 
 import { calculateDeployingModulePanel } from "../../../src/ui/helpers/calculate-deploying-module-panel";
 import { UiState, UiStateDeploymentStatus } from "../../../src/ui/types";
@@ -16,6 +17,7 @@ describe("ui - calculate starting message display", () => {
     currentBatch: 0,
     result: null,
     warnings: [],
+    isResumed: null,
   };
 
   it("should display the deploying module message", () => {
@@ -55,4 +57,41 @@ describe("ui - calculate starting message display", () => {
 
     assert.equal(actualText, expectedText);
   });
+
+  it("should display a message when the deployment is being resumed and the path is not in the CWD", () => {
+    const expectedText = testFormat(`
+    Hardhat Ignition 🚀
+
+    ${chalk.bold(`Resuming existing deployment from /users/example`)}
+
+    ${chalk.bold(`Deploying [ ExampleModule ]`)}
+    `);
+
+    const actualText = calculateDeployingModulePanel({
+      ...exampleState,
+      isResumed: true,
+    });
+
+    assert.equal(actualText, expectedText);
+  });
+
+  it("should display a message when the deployment is being resumed and the path is not in the CWD", () => {
+    const expectedText = testFormat(`
+    Hardhat Ignition 🚀
+
+    ${chalk.bold(
+      `Resuming existing deployment from .${path.sep}ignition${path.sep}deployments${path.sep}foo`
+    )}
+
+    ${chalk.bold(`Deploying [ ExampleModule ]`)}
+    `);
+
+    const actualText = calculateDeployingModulePanel({
+      ...exampleState,
+      isResumed: true,
+      deploymentDir: `${process.cwd()}/ignition/deployments/foo`,
+    });
+
+    assert.equal(actualText, expectedText);
+  });
 });