Skip to content

Commit 590b1b1

Browse files
authored
Merge pull request #5494 from NomicFoundation/improve-builtin-options
Include the builtin options as part of the global options & augment the types
2 parents 4c11d36 + e09c0f4 commit 590b1b1

File tree

12 files changed

+288
-83
lines changed

12 files changed

+288
-83
lines changed

v-next/hardhat/src/hre.ts

+14
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import type { HardhatRuntimeEnvironment } from "./types/hre.js";
44
import type { UnsafeHardhatRuntimeEnvironmentOptions } from "@ignored/hardhat-vnext-core/types/cli";
55

66
import {
7+
buildGlobalOptionDefinitions,
78
// eslint-disable-next-line no-restricted-imports -- This is the one place where we allow it
89
createBaseHardhatRuntimeEnvironment,
910
resolvePluginList,
1011
} from "@ignored/hardhat-vnext-core";
1112

13+
import { BUILTIN_GLOBAL_OPTIONS_DEFINITIONS } from "./internal/builtin-global-options.js";
1214
import { builtinPlugins } from "./internal/builtin-plugins/index.js";
1315

1416
/**
@@ -37,6 +39,18 @@ export async function createHardhatRuntimeEnvironment(
3739
unsafeOptions.resolvedPlugins = resolvedPlugins;
3840
}
3941

42+
if (unsafeOptions.globalOptionDefinitions === undefined) {
43+
const pluginGlobalOptionDefinitions = buildGlobalOptionDefinitions(
44+
unsafeOptions.resolvedPlugins,
45+
);
46+
const globalOptionDefinitions = new Map([
47+
...BUILTIN_GLOBAL_OPTIONS_DEFINITIONS,
48+
...pluginGlobalOptionDefinitions,
49+
]);
50+
51+
unsafeOptions.globalOptionDefinitions = globalOptionDefinitions;
52+
}
53+
4054
return createBaseHardhatRuntimeEnvironment(
4155
config,
4256
userProvidedGlobalOptions,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { GlobalOptionDefinitions } from "../types/global-options.js";
2+
3+
import { globalOption, ArgumentType } from "../config.js";
4+
5+
export const BUILTIN_GLOBAL_OPTIONS_DEFINITIONS: GlobalOptionDefinitions =
6+
new Map([
7+
[
8+
"config",
9+
{
10+
pluginId: "builtin",
11+
option: globalOption({
12+
name: "config",
13+
description: "A Hardhat config file.",
14+
type: ArgumentType.STRING,
15+
defaultValue: "",
16+
}),
17+
},
18+
],
19+
[
20+
"help",
21+
{
22+
pluginId: "builtin",
23+
option: globalOption({
24+
name: "help",
25+
description:
26+
"Shows this message, or a task's help if its name is provided.",
27+
type: ArgumentType.BOOLEAN,
28+
defaultValue: false,
29+
}),
30+
},
31+
],
32+
[
33+
"init",
34+
{
35+
pluginId: "builtin",
36+
option: globalOption({
37+
name: "init",
38+
description: "Initializes a Hardhat project.",
39+
type: ArgumentType.BOOLEAN,
40+
defaultValue: false,
41+
}),
42+
},
43+
],
44+
[
45+
"showStackTraces",
46+
{
47+
pluginId: "builtin",
48+
option: globalOption({
49+
name: "showStackTraces",
50+
description: "Show stack traces (always enabled on CI servers).",
51+
type: ArgumentType.BOOLEAN,
52+
defaultValue: false,
53+
}),
54+
},
55+
],
56+
[
57+
"version",
58+
{
59+
pluginId: "builtin",
60+
option: globalOption({
61+
name: "version",
62+
description: "Shows hardhat's version.",
63+
type: ArgumentType.BOOLEAN,
64+
defaultValue: false,
65+
}),
66+
},
67+
],
68+
]);

v-next/hardhat/src/internal/builtin-options.ts

-31
This file was deleted.

v-next/hardhat/src/internal/cli/helpers/utils.ts

+4-19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import type { ArgumentType } from "@ignored/hardhat-vnext-core/config";
22
import type { GlobalOptionDefinitions } from "@ignored/hardhat-vnext-core/types/global-options";
33
import type { Task } from "@ignored/hardhat-vnext-core/types/tasks";
44

5-
import { BUILTIN_OPTIONS } from "../../builtin-options.js";
6-
75
export const GLOBAL_NAME_PADDING = 6;
86

97
interface ArgumentDescriptor {
@@ -16,23 +14,10 @@ interface ArgumentDescriptor {
1614
export function parseGlobalOptions(
1715
globalOptionDefinitions: GlobalOptionDefinitions,
1816
): ArgumentDescriptor[] {
19-
const formattedBuiltinOptions = BUILTIN_OPTIONS.map(
20-
({ name, description }) => ({
21-
name: formatOptionName(name),
22-
description,
23-
}),
24-
);
25-
26-
const formattedUserOptions = Array.from(globalOptionDefinitions).map(
27-
([, entry]) => {
28-
return {
29-
name: formatOptionName(entry.option.name),
30-
description: entry.option.description,
31-
};
32-
},
33-
);
34-
35-
return [...formattedBuiltinOptions, ...formattedUserOptions];
17+
return [...globalOptionDefinitions].map(([, { option }]) => ({
18+
name: formatOptionName(option.name),
19+
description: option.description,
20+
}));
3621
}
3722

3823
export function parseTasks(taskMap: Map<string, Task>): {

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
2828

2929
import { resolveHardhatConfigPath } from "../../config.js";
3030
import { createHardhatRuntimeEnvironment } from "../../hre.js";
31+
import { BUILTIN_GLOBAL_OPTIONS_DEFINITIONS } from "../builtin-global-options.js";
3132
import { builtinPlugins } from "../builtin-plugins/index.js";
3233
import { setGlobalHardhatRuntimeEnvironment } from "../global-hre-instance.js";
3334
import { importUserConfig } from "../helpers/config-loading.js";
@@ -77,8 +78,12 @@ export async function main(
7778
builtinGlobalOptions.configPath,
7879
);
7980

80-
const globalOptionDefinitions =
81+
const pluginGlobalOptionDefinitions =
8182
buildGlobalOptionDefinitions(resolvedPlugins);
83+
const globalOptionDefinitions = new Map([
84+
...BUILTIN_GLOBAL_OPTIONS_DEFINITIONS,
85+
...pluginGlobalOptionDefinitions,
86+
]);
8287
const userProvidedGlobalOptions = await parseGlobalOptions(
8388
globalOptionDefinitions,
8489
cliArguments,
@@ -87,7 +92,7 @@ export async function main(
8792

8893
const hre = await createHardhatRuntimeEnvironment(
8994
userConfig,
90-
userProvidedGlobalOptions,
95+
{ ...builtinGlobalOptions, ...userProvidedGlobalOptions },
9196
{ resolvedPlugins, globalOptionDefinitions },
9297
);
9398

+12
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
import "@ignored/hardhat-vnext-core/types/global-options";
2+
3+
declare module "@ignored/hardhat-vnext-core/types/global-options" {
4+
export interface GlobalOptions {
5+
config: string;
6+
help: boolean;
7+
init: boolean;
8+
showStackTraces: boolean;
9+
version: boolean;
10+
}
11+
}
12+
113
export type * from "@ignored/hardhat-vnext-core/types/global-options";
214
export * from "@ignored/hardhat-vnext-core/types/global-options";

v-next/hardhat/test/fixture-projects/loaded-config/hardhat.config.js

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";
2+
import type { HardhatPlugin } from "@ignored/hardhat-vnext-core/types/plugins";
3+
4+
import { globalOption } from "@ignored/hardhat-vnext/config";
5+
6+
export const testPlugin: HardhatPlugin = {
7+
id: "test-plugin",
8+
globalOptions: [
9+
globalOption({
10+
name: "myGlobalOption",
11+
description: "A global option",
12+
defaultValue: "default",
13+
}),
14+
],
15+
};
16+
17+
const config: HardhatUserConfig = {
18+
plugins: [testPlugin],
19+
};
20+
21+
export default config;

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,27 @@ describe("HRE", () => {
132132
describe("programmatic API", () => {
133133
useFixtureProject("loaded-config");
134134

135-
it("should load the config file", async () => {
135+
it("should load the plugins from the config file", async () => {
136136
const hre = await import("../../src/index.js");
137+
const { testPlugin } = await import(
138+
"../fixture-projects/loaded-config/hardhat.config.js"
139+
);
140+
141+
assert.deepEqual(hre.config.plugins, [...builtinPlugins, testPlugin]);
142+
});
137143

138-
assert.deepEqual(hre.config.plugins, [
139-
...builtinPlugins,
140-
{ id: "test-plugin" },
141-
]);
144+
it("should load the global options", async () => {
145+
const hre = await import("../../src/index.js");
146+
147+
assert.deepEqual(hre.globalOptions, {
148+
config: "",
149+
help: false,
150+
init: false,
151+
showStackTraces: false,
152+
version: false,
153+
fooPluginFlag: false,
154+
myGlobalOption: "default",
155+
});
142156
});
143157
});
144158
});

0 commit comments

Comments
 (0)