Skip to content

Commit 2a364d1

Browse files
Implement logic for the --version cli global argument (#5384)
1 parent e16155d commit 2a364d1

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

v-next/hardhat/src/internal/cli/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
} from "../helpers/config-loading.js";
3434
import { getHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
3535

36+
import { printVersionMessage } from "./version.js";
37+
3638
export async function main(cliArguments: string[], print = console.log) {
3739
const hreInitStart = performance.now();
3840

@@ -46,7 +48,7 @@ export async function main(cliArguments: string[], print = console.log) {
4648
);
4749

4850
if (hardhatSpecialArgs.version) {
49-
print("3.0.0");
51+
await printVersionMessage(print);
5052
return;
5153
}
5254

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getHardhatVersion } from "../utils/package.js";
2+
3+
export async function printVersionMessage(print = console.log) {
4+
print(await getHardhatVersion());
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { PackageJson } from "@nomicfoundation/hardhat-utils/package";
2+
3+
import { readClosestPackageJson } from "@nomicfoundation/hardhat-utils/package";
4+
5+
export async function getHardhatVersion(): Promise<string> {
6+
const packageJson: PackageJson = await readClosestPackageJson(
7+
import.meta.url,
8+
);
9+
10+
return packageJson.version;
11+
}

v-next/hardhat/test/internal/cli/main.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
parseTaskAndArguments,
3030
} from "../../../src/internal/cli/main.js";
3131
import { resetHardhatRuntimeEnvironmentSingleton } from "../../../src/internal/hre-singleton.js";
32+
import { getHardhatVersion } from "../../../src/internal/utils/package.js";
3233
import { useFixtureProject } from "../../helpers/project.js";
3334

3435
async function getTasksAndHreEnvironment(
@@ -82,26 +83,25 @@ describe("main", function () {
8283
describe("version", function () {
8384
useFixtureProject("cli/parsing/base-project");
8485

85-
// TODO: as soon as the 'version task' is done, this test should be updated
86-
it.todo(
87-
"should print the version and instantly return",
88-
async function () {
89-
const lines: string[] = [];
86+
it("should print the version and instantly return", async function () {
87+
const lines: string[] = [];
9088

91-
const command = "npx hardhat --version";
92-
const cliArguments = command.split(" ").slice(2);
89+
const command = "npx hardhat --version";
90+
const cliArguments = command.split(" ").slice(2);
9391

94-
await main(cliArguments, (msg) => {
95-
lines.push(msg);
96-
});
92+
await main(cliArguments, (msg) => {
93+
lines.push(msg);
94+
});
9795

98-
assert.equal(lines.length, 1);
99-
assert.equal(lines[0], "3.0.0");
100-
// Check that the process exits right after printing the version, the remaining parsing logic should not be executed
101-
const tasksResults = await getTasksAndSubtaskResults();
102-
assert.equal(tasksResults.wasParam1Used, false);
103-
},
104-
);
96+
// Get the expected package version
97+
const expectedVersion = await getHardhatVersion();
98+
99+
assert.equal(lines.length, 1);
100+
assert.equal(lines[0], expectedVersion);
101+
// Check that the process exits right after printing the version, the remaining parsing logic should not be executed
102+
const tasksResults = await getTasksAndSubtaskResults();
103+
assert.equal(tasksResults.wasParam1Used, false);
104+
});
105105
});
106106

107107
describe("show-stack-traces", function () {

0 commit comments

Comments
 (0)