Skip to content

Commit 1485566

Browse files
authored
Merge pull request #5432 from NomicFoundation/run-task
Add `run` task
2 parents 09b1a20 + 60f4854 commit 1485566

File tree

25 files changed

+284
-45
lines changed

25 files changed

+284
-45
lines changed

v-next/core/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
3333
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
3434
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
35-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
36-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
37-
"test:coverage": "c8 --reporter html --reporter text --all --src src --exclude src/types node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
35+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
36+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
37+
"test:coverage": "c8 --reporter html --reporter text --all --src src --exclude src/types node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
3838
"pretest": "pnpm build",
3939
"build": "tsc --build .",
4040
"prepublishOnly": "pnpm build",

v-next/hardhat-build-system/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
2020
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
2121
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
22-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
23-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
22+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
23+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
2424
"pretest": "pnpm build",
2525
"build": "tsc --build .",
2626
"prepublishOnly": "pnpm build",

v-next/hardhat-errors/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
2222
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
2323
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
24-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
25-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
24+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
25+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
2626
"pretest": "pnpm build",
2727
"build": "tsc --build .",
2828
"prepublishOnly": "pnpm build",

v-next/hardhat-errors/src/descriptors.ts

+16
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,22 @@ The first supported version is {firstSupportedVersion}`,
640640
Please use a newer, supported version.`,
641641
shouldBeReported: true,
642642
},
643+
RUN_FILE_NOT_FOUND: {
644+
number: 603,
645+
messageTemplate: `Script {script} doesn't exist`,
646+
websiteTitle: "Script doesn't exist",
647+
websiteDescription: `Tried to use \`hardhat run\` to execute a nonexistent script.
648+
649+
Please double check your script's path.`,
650+
},
651+
RUN_SCRIPT_ERROR: {
652+
number: 604,
653+
messageTemplate: `Error running script {script}: {error}`,
654+
websiteTitle: "Error running script",
655+
websiteDescription: `Running a script resulted in an error.
656+
657+
Please check Hardhat's output for more details.`,
658+
},
643659
},
644660
ARTIFACTS: {
645661
NOT_FOUND: {

v-next/hardhat-utils/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
3535
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
3636
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
37-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
38-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
37+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
38+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
3939
"pretest": "pnpm build",
4040
"build": "tsc --build .",
4141
"prepublishOnly": "pnpm build",

v-next/hardhat-zod-utils/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
2222
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
2323
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
24-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
25-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
24+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
25+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
2626
"pretest": "pnpm build",
2727
"build": "tsc --build .",
2828
"prepublishOnly": "pnpm build",

v-next/hardhat/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
3636
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
3737
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
38-
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
39-
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/**/*.ts\"",
38+
"test": "node --import tsx/esm --test --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
39+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@nomicfoundation/hardhat-node-test-reporter \"test/!(fixture-projects|helpers)/**/*.ts\"",
4040
"pretest": "pnpm build",
4141
"build": "tsc --build .",
4242
"prepublishOnly": "pnpm build",

v-next/hardhat/src/config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
import { findClosestHardhatConfig } from "./internal/helpers/config-loading.js";
2+
13
export type * from "@nomicfoundation/hardhat-core/config";
24
export * from "@nomicfoundation/hardhat-core/config";
5+
6+
/**
7+
* Attempts to find the nearest Hardhat config file, starting from the current
8+
* working directory. If no config file is found, an error is thrown.
9+
*
10+
* @returns The path to the nearest Hardhat config file.
11+
*/
12+
export async function resolveHardhatConfigPath(): Promise<string> {
13+
const configPath = process.env.HARDHAT_CONFIG;
14+
15+
if (configPath !== undefined) {
16+
return configPath;
17+
}
18+
19+
return findClosestHardhatConfig();
20+
}

v-next/hardhat/src/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {
2-
importUserConfig,
3-
resolveConfigPath,
4-
} from "./internal/helpers/config-loading.js";
1+
import { resolveHardhatConfigPath } from "./config.js";
2+
import { importUserConfig } from "./internal/helpers/config-loading.js";
53
import { getHardhatRuntimeEnvironmentSingleton } from "./internal/hre-singleton.js";
64

7-
const configPath = await resolveConfigPath();
5+
const configPath = await resolveHardhatConfigPath();
86
const userConfig = await importUserConfig(configPath);
97

108
const hre = await getHardhatRuntimeEnvironmentSingleton(userConfig);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { HardhatPlugin } from "@nomicfoundation/hardhat-core/types/plugins";
22

33
import hardhatFoo from "./hardhat-foo/index.js";
4+
import run from "./run/index.js";
45

5-
export const builtinPlugins: HardhatPlugin[] = [hardhatFoo];
6+
export const builtinPlugins: HardhatPlugin[] = [hardhatFoo, run];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { HardhatPlugin } from "@nomicfoundation/hardhat-core/types/plugins";
2+
3+
import { ParameterType, task } from "@nomicfoundation/hardhat-core/config";
4+
5+
export default {
6+
id: "run",
7+
tasks: [
8+
task("run", "Runs a user-defined script after compiling the project")
9+
.addPositionalParameter({
10+
name: "script",
11+
description: "A js or ts file to be run within hardhat's environment",
12+
type: ParameterType.STRING,
13+
})
14+
.addFlag({
15+
name: "noCompile",
16+
description: "Don't compile before running this task",
17+
})
18+
.setAction(import.meta.resolve("./runScriptWithHardhat.js"))
19+
.build(),
20+
],
21+
} satisfies HardhatPlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { NewTaskActionFunction } from "@nomicfoundation/hardhat-core/types/tasks";
2+
3+
import { isAbsolute, resolve } from "node:path";
4+
import { pathToFileURL } from "node:url";
5+
6+
import {
7+
HardhatError,
8+
assertHardhatInvariant,
9+
} from "@nomicfoundation/hardhat-errors";
10+
import { exists } from "@nomicfoundation/hardhat-utils/fs";
11+
12+
const runScriptWithHardhat: NewTaskActionFunction = async (
13+
{ script, noCompile },
14+
_hre,
15+
) => {
16+
assertHardhatInvariant(
17+
typeof script === "string",
18+
"Expected script to be a string",
19+
);
20+
21+
assertHardhatInvariant(
22+
typeof noCompile === "boolean",
23+
"Expected noCompile to be a boolean",
24+
);
25+
26+
const normalizedPath = isAbsolute(script)
27+
? script
28+
: resolve(process.cwd(), script);
29+
30+
if (!(await exists(normalizedPath))) {
31+
throw new HardhatError(
32+
HardhatError.ERRORS.BUILTIN_TASKS.RUN_FILE_NOT_FOUND,
33+
{ script },
34+
);
35+
}
36+
37+
if (!noCompile) {
38+
// todo: run compile task
39+
}
40+
41+
try {
42+
await import(pathToFileURL(normalizedPath).href);
43+
} catch (error) {
44+
if (error instanceof Error) {
45+
throw new HardhatError(
46+
HardhatError.ERRORS.BUILTIN_TASKS.RUN_SCRIPT_ERROR,
47+
{
48+
script,
49+
error: error.message,
50+
},
51+
error,
52+
);
53+
}
54+
55+
throw error;
56+
}
57+
};
58+
59+
export default runScriptWithHardhat;

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ import {
2626
import { isCi } from "@nomicfoundation/hardhat-utils/ci";
2727
import { kebabToCamelCase } from "@nomicfoundation/hardhat-utils/string";
2828

29+
import { resolveHardhatConfigPath } from "../../config.js";
2930
import { builtinPlugins } from "../builtin-plugins/index.js";
30-
import {
31-
importUserConfig,
32-
resolveConfigPath,
33-
} from "../helpers/config-loading.js";
31+
import { importUserConfig } from "../helpers/config-loading.js";
3432
import { getHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
3533

3634
import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js";
@@ -55,7 +53,7 @@ export async function main(cliArguments: string[], print = console.log) {
5553
}
5654

5755
if (hardhatSpecialArgs.configPath === undefined) {
58-
hardhatSpecialArgs.configPath = await resolveConfigPath();
56+
hardhatSpecialArgs.configPath = await resolveHardhatConfigPath();
5957
}
6058

6159
try {

v-next/hardhat/src/internal/helpers/config-loading.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HardhatError } from "@nomicfoundation/hardhat-errors";
55
import { findUp } from "@nomicfoundation/hardhat-utils/fs";
66
import { isObject } from "@nomicfoundation/hardhat-utils/lang";
77

8-
async function findClosestHardhatConfig(): Promise<string> {
8+
export async function findClosestHardhatConfig(): Promise<string> {
99
let hardhatConfigPath = await findUp("hardhat.config.ts");
1010

1111
if (hardhatConfigPath !== undefined) {
@@ -21,16 +21,6 @@ async function findClosestHardhatConfig(): Promise<string> {
2121
throw new HardhatError(HardhatError.ERRORS.GENERAL.NO_CONFIG_FILE_FOUND);
2222
}
2323

24-
export async function resolveConfigPath(): Promise<string> {
25-
const configPath = process.env.HARDHAT_CONFIG;
26-
27-
if (configPath !== undefined) {
28-
return configPath;
29-
}
30-
31-
return findClosestHardhatConfig();
32-
}
33-
3424
export async function importUserConfig(configPath: string) {
3525
const normalizedPath = isAbsolute(configPath)
3626
? configPath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { task } from "@nomicfoundation/hardhat/config";
2+
3+
export default {
4+
tasks: [
5+
task("test", "Prints a test")
6+
.setAction(async () => {
7+
console.log("test!");
8+
})
9+
.build(),
10+
],
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import hre from "@nomicfoundation/hardhat";
2+
3+
if (!hre.tasks.rootTasks.has("test")) {
4+
throw new Error("test task not found");
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error("broken script");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { task } from "@nomicfoundation/hardhat/config";
2+
3+
export default {
4+
tasks: [
5+
task("test", "Prints a test")
6+
.setAction(async () => {
7+
console.log("test!");
8+
})
9+
.build(),
10+
],
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import hre from "@nomicfoundation/hardhat";
2+
3+
if (!hre.tasks.rootTasks.has("test")) {
4+
throw new Error("test task not found");
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error("broken script");

v-next/hardhat/test/helpers/project.ts

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ async function getFixtureProjectPath(
4343
}
4444

4545
if (!(await exists(projectPath))) {
46-
console.log("projectPath", projectPath);
4746
throw new Error(`Fixture project ${projectName} doesn't exist`);
4847
}
4948

v-next/hardhat/test/hre/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

4+
import { resolveHardhatConfigPath } from "../../src/config.js";
45
import { createHardhatRuntimeEnvironment } from "../../src/hre.js";
56
import { builtinPlugins } from "../../src/internal/builtin-plugins/index.js";
6-
import { resolveConfigPath } from "../../src/internal/helpers/config-loading.js";
77
import {
88
getHardhatRuntimeEnvironmentSingleton,
99
resetHardhatRuntimeEnvironmentSingleton,
@@ -47,13 +47,13 @@ describe("HRE", () => {
4747
it("should return the HARDHAT_CONFIG env variable if it is set", async () => {
4848
process.env.HARDHAT_CONFIG = "env.config.js";
4949

50-
assert.equal(await resolveConfigPath(), "env.config.js");
50+
assert.equal(await resolveHardhatConfigPath(), "env.config.js");
5151

5252
delete process.env.HARDHAT_CONFIG;
5353
});
5454

5555
it("should throw if the config file is not found", async () => {
56-
await assert.rejects(resolveConfigPath(), {
56+
await assert.rejects(resolveHardhatConfigPath(), {
5757
message: "HHE5: No Hardhat config file found",
5858
});
5959
});
@@ -63,7 +63,7 @@ describe("HRE", () => {
6363
useFixtureProject("config-js");
6464

6565
it("should load a config file in the current directory", async () => {
66-
const configPath = await resolveConfigPath();
66+
const configPath = await resolveHardhatConfigPath();
6767

6868
assert(
6969
configPath.endsWith("hardhat.config.js"),
@@ -76,7 +76,7 @@ describe("HRE", () => {
7676
useFixtureProject("config-js", "nested-folder");
7777

7878
it("should load a config file in the parent directory", async () => {
79-
const configPath = await resolveConfigPath();
79+
const configPath = await resolveHardhatConfigPath();
8080

8181
assert(
8282
configPath.endsWith("hardhat.config.js"),
@@ -91,7 +91,7 @@ describe("HRE", () => {
9191
useFixtureProject("config-ts");
9292

9393
it("should load a config file in the current directory", async () => {
94-
const configPath = await resolveConfigPath();
94+
const configPath = await resolveHardhatConfigPath();
9595

9696
assert(
9797
configPath.endsWith("hardhat.config.ts"),
@@ -104,7 +104,7 @@ describe("HRE", () => {
104104
useFixtureProject("config-ts", "nested-folder");
105105

106106
it("should load a config file in the parent directory", async () => {
107-
const configPath = await resolveConfigPath();
107+
const configPath = await resolveHardhatConfigPath();
108108

109109
assert(
110110
configPath.endsWith("hardhat.config.ts"),

0 commit comments

Comments
 (0)