hardhat 2.9.9 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/internal/cli/cli.js +37 -7
  2. package/internal/cli/cli.js.map +1 -1
  3. package/internal/cli/hardhat-vscode-installation.d.ts +8 -0
  4. package/internal/cli/hardhat-vscode-installation.d.ts.map +1 -0
  5. package/internal/cli/hardhat-vscode-installation.js +41 -0
  6. package/internal/cli/hardhat-vscode-installation.js.map +1 -0
  7. package/internal/cli/project-creation.d.ts +0 -1
  8. package/internal/cli/project-creation.d.ts.map +1 -1
  9. package/internal/cli/project-creation.js +87 -179
  10. package/internal/cli/project-creation.js.map +1 -1
  11. package/internal/cli/prompt.d.ts +14 -0
  12. package/internal/cli/prompt.d.ts.map +1 -0
  13. package/internal/cli/prompt.js +120 -0
  14. package/internal/cli/prompt.js.map +1 -0
  15. package/internal/cli/types.d.ts +4 -0
  16. package/internal/cli/types.d.ts.map +1 -0
  17. package/internal/cli/types.js +3 -0
  18. package/internal/cli/types.js.map +1 -0
  19. package/internal/core/config/config-loading.js +1 -1
  20. package/internal/core/errors-list.js +4 -4
  21. package/internal/core/errors-list.js.map +1 -1
  22. package/internal/core/providers/construction.d.ts.map +1 -1
  23. package/internal/core/providers/construction.js +0 -4
  24. package/internal/core/providers/construction.js.map +1 -1
  25. package/internal/core/providers/gas-providers.d.ts +0 -15
  26. package/internal/core/providers/gas-providers.d.ts.map +1 -1
  27. package/internal/core/providers/gas-providers.js +1 -33
  28. package/internal/core/providers/gas-providers.js.map +1 -1
  29. package/internal/hardhat-network/provider/modules/logger.js +1 -1
  30. package/internal/hardhat-network/provider/modules/logger.js.map +1 -1
  31. package/internal/hardhat-network/provider/node.js +1 -1
  32. package/internal/hardhat-network/provider/node.js.map +1 -1
  33. package/internal/hardhat-network/stack-traces/solidity-errors.js +1 -1
  34. package/internal/hardhat-network/stack-traces/solidity-errors.js.map +1 -1
  35. package/internal/util/global-dir.d.ts +5 -0
  36. package/internal/util/global-dir.d.ts.map +1 -1
  37. package/internal/util/global-dir.js +17 -1
  38. package/internal/util/global-dir.js.map +1 -1
  39. package/internal/util/keys-derivation.d.ts.map +1 -1
  40. package/internal/util/keys-derivation.js +3 -1
  41. package/internal/util/keys-derivation.js.map +1 -1
  42. package/package.json +6 -6
  43. package/recommended-gitignore.txt +2 -0
  44. package/sample-projects/{basic → javascript}/LICENSE.md +1 -1
  45. package/sample-projects/javascript/README.md +13 -0
  46. package/sample-projects/javascript/contracts/Lock.sol +34 -0
  47. package/sample-projects/javascript/hardhat.config.js +6 -0
  48. package/sample-projects/javascript/scripts/deploy.js +29 -0
  49. package/sample-projects/javascript/test/Lock.js +126 -0
  50. package/sample-projects/typescript/LICENSE.md +11 -0
  51. package/sample-projects/typescript/README.md +13 -0
  52. package/sample-projects/typescript/contracts/Lock.sol +34 -0
  53. package/sample-projects/typescript/hardhat.config.ts +8 -0
  54. package/sample-projects/typescript/scripts/deploy.ts +23 -0
  55. package/sample-projects/typescript/test/Lock.ts +124 -0
  56. package/sample-projects/typescript/tsconfig.json +10 -0
  57. package/src/internal/cli/cli.ts +55 -7
  58. package/src/internal/cli/hardhat-vscode-installation.ts +43 -0
  59. package/src/internal/cli/project-creation.ts +124 -290
  60. package/src/internal/cli/prompt.ts +143 -0
  61. package/src/internal/cli/types.ts +3 -0
  62. package/src/internal/core/config/config-loading.ts +1 -1
  63. package/src/internal/core/errors-list.ts +4 -4
  64. package/src/internal/core/providers/construction.ts +0 -9
  65. package/src/internal/core/providers/gas-providers.ts +0 -39
  66. package/src/internal/hardhat-network/provider/modules/logger.ts +1 -1
  67. package/src/internal/hardhat-network/provider/node.ts +1 -1
  68. package/src/internal/hardhat-network/stack-traces/solidity-errors.ts +1 -1
  69. package/src/internal/util/global-dir.ts +19 -0
  70. package/src/internal/util/keys-derivation.ts +3 -1
  71. package/sample-projects/advanced/.env.example +0 -3
  72. package/sample-projects/advanced/.eslintignore +0 -4
  73. package/sample-projects/advanced/.eslintrc.js +0 -22
  74. package/sample-projects/advanced/.prettierignore +0 -5
  75. package/sample-projects/advanced/.prettierrc +0 -1
  76. package/sample-projects/advanced/.solhint.json +0 -7
  77. package/sample-projects/advanced/.solhintignore +0 -1
  78. package/sample-projects/advanced/README.md +0 -42
  79. package/sample-projects/advanced/hardhat.config.js +0 -40
  80. package/sample-projects/advanced/npmignore +0 -3
  81. package/sample-projects/advanced/scripts/deploy.js +0 -30
  82. package/sample-projects/advanced-ts/.eslintrc.js +0 -24
  83. package/sample-projects/advanced-ts/README.md +0 -46
  84. package/sample-projects/advanced-ts/hardhat.config.ts +0 -43
  85. package/sample-projects/advanced-ts/npmignore +0 -3
  86. package/sample-projects/advanced-ts/scripts/deploy.ts +0 -30
  87. package/sample-projects/advanced-ts/test/index.ts +0 -19
  88. package/sample-projects/advanced-ts/tsconfig.json +0 -12
  89. package/sample-projects/basic/README.md +0 -15
  90. package/sample-projects/basic/contracts/Greeter.sol +0 -22
  91. package/sample-projects/basic/hardhat.config.js +0 -21
  92. package/sample-projects/basic/scripts/sample-script.js +0 -32
  93. package/sample-projects/basic/test/sample-test.js +0 -19
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.9;
3
+
4
+ // Import this file to use console.log
5
+ import "hardhat/console.sol";
6
+
7
+ contract Lock {
8
+ uint public unlockTime;
9
+ address payable public owner;
10
+
11
+ event Withdrawal(uint amount, uint when);
12
+
13
+ constructor(uint _unlockTime) payable {
14
+ require(
15
+ block.timestamp < _unlockTime,
16
+ "Unlock time should be in the future"
17
+ );
18
+
19
+ unlockTime = _unlockTime;
20
+ owner = payable(msg.sender);
21
+ }
22
+
23
+ function withdraw() public {
24
+ // Uncomment this line to print a log in your terminal
25
+ // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
26
+
27
+ require(block.timestamp >= unlockTime, "You can't withdraw yet");
28
+ require(msg.sender == owner, "You aren't the owner");
29
+
30
+ emit Withdrawal(address(this).balance, block.timestamp);
31
+
32
+ owner.transfer(address(this).balance);
33
+ }
34
+ }
@@ -0,0 +1,6 @@
1
+ require("@nomicfoundation/hardhat-toolbox");
2
+
3
+ /** @type import('hardhat/config').HardhatUserConfig */
4
+ module.exports = {
5
+ solidity: "0.8.9",
6
+ };
@@ -0,0 +1,29 @@
1
+ // We require the Hardhat Runtime Environment explicitly here. This is optional
2
+ // but useful for running the script in a standalone fashion through `node <script>`.
3
+ //
4
+ // You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
5
+ // will compile your contracts, add the Hardhat Runtime Environment's members to the
6
+ // global scope, and execute the script.
7
+ const hre = require("hardhat");
8
+
9
+ async function main() {
10
+ const currentTimestampInSeconds = Math.round(Date.now() / 1000);
11
+ const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
12
+ const unlockTime = currentTimestampInSeconds + ONE_YEAR_IN_SECS;
13
+
14
+ const lockedAmount = hre.ethers.utils.parseEther("1");
15
+
16
+ const Lock = await hre.ethers.getContractFactory("Lock");
17
+ const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
18
+
19
+ await lock.deployed();
20
+
21
+ console.log("Lock with 1 ETH deployed to:", lock.address);
22
+ }
23
+
24
+ // We recommend this pattern to be able to use async/await everywhere
25
+ // and properly handle errors.
26
+ main().catch((error) => {
27
+ console.error(error);
28
+ process.exitCode = 1;
29
+ });
@@ -0,0 +1,126 @@
1
+ const {
2
+ time,
3
+ loadFixture,
4
+ } = require("@nomicfoundation/hardhat-network-helpers");
5
+ const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
6
+ const { expect } = require("chai");
7
+
8
+ describe("Lock", function () {
9
+ // We define a fixture to reuse the same setup in every test.
10
+ // We use loadFixture to run this setup once, snapshot that state,
11
+ // and reset Hardhat Network to that snapshopt in every test.
12
+ async function deployOneYearLockFixture() {
13
+ const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
14
+ const ONE_GWEI = 1_000_000_000;
15
+
16
+ const lockedAmount = ONE_GWEI;
17
+ const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
18
+
19
+ // Contracts are deployed using the first signer/account by default
20
+ const [owner, otherAccount] = await ethers.getSigners();
21
+
22
+ const Lock = await ethers.getContractFactory("Lock");
23
+ const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
24
+
25
+ return { lock, unlockTime, lockedAmount, owner, otherAccount };
26
+ }
27
+
28
+ describe("Deployment", function () {
29
+ it("Should set the right unlockTime", async function () {
30
+ const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
31
+
32
+ expect(await lock.unlockTime()).to.equal(unlockTime);
33
+ });
34
+
35
+ it("Should set the right owner", async function () {
36
+ const { lock, owner } = await loadFixture(deployOneYearLockFixture);
37
+
38
+ expect(await lock.owner()).to.equal(owner.address);
39
+ });
40
+
41
+ it("Should receive and store the funds to lock", async function () {
42
+ const { lock, lockedAmount } = await loadFixture(
43
+ deployOneYearLockFixture
44
+ );
45
+
46
+ expect(await ethers.provider.getBalance(lock.address)).to.equal(
47
+ lockedAmount
48
+ );
49
+ });
50
+
51
+ it("Should fail if the unlockTime is not in the future", async function () {
52
+ // We don't use the fixture here because we want a different deployment
53
+ const latestTime = await time.latest();
54
+ const Lock = await ethers.getContractFactory("Lock");
55
+ await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
56
+ "Unlock time should be in the future"
57
+ );
58
+ });
59
+ });
60
+
61
+ describe("Withdrawals", function () {
62
+ describe("Validations", function () {
63
+ it("Should revert with the right error if called too soon", async function () {
64
+ const { lock } = await loadFixture(deployOneYearLockFixture);
65
+
66
+ await expect(lock.withdraw()).to.be.revertedWith(
67
+ "You can't withdraw yet"
68
+ );
69
+ });
70
+
71
+ it("Should revert with the right error if called from another account", async function () {
72
+ const { lock, unlockTime, otherAccount } = await loadFixture(
73
+ deployOneYearLockFixture
74
+ );
75
+
76
+ // We can increase the time in Hardhat Network
77
+ await time.increaseTo(unlockTime);
78
+
79
+ // We use lock.connect() to send a transaction from another account
80
+ await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
81
+ "You aren't the owner"
82
+ );
83
+ });
84
+
85
+ it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
86
+ const { lock, unlockTime } = await loadFixture(
87
+ deployOneYearLockFixture
88
+ );
89
+
90
+ // Transactions are sent using the first signer by default
91
+ await time.increaseTo(unlockTime);
92
+
93
+ await expect(lock.withdraw()).not.to.be.reverted;
94
+ });
95
+ });
96
+
97
+ describe("Events", function () {
98
+ it("Should emit an event on withdrawals", async function () {
99
+ const { lock, unlockTime, lockedAmount } = await loadFixture(
100
+ deployOneYearLockFixture
101
+ );
102
+
103
+ await time.increaseTo(unlockTime);
104
+
105
+ await expect(lock.withdraw())
106
+ .to.emit(lock, "Withdrawal")
107
+ .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
108
+ });
109
+ });
110
+
111
+ describe("Transfers", function () {
112
+ it("Should transfer the funds to the owner", async function () {
113
+ const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
114
+ deployOneYearLockFixture
115
+ );
116
+
117
+ await time.increaseTo(unlockTime);
118
+
119
+ await expect(lock.withdraw()).to.changeEtherBalances(
120
+ [owner, lock],
121
+ [lockedAmount, -lockedAmount]
122
+ );
123
+ });
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,11 @@
1
+ # License
2
+
3
+ This is free and unencumbered software released into the public domain.
4
+
5
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
6
+
7
+ In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ For more information, please refer to [https://unlicense.org](https://unlicense.org)
@@ -0,0 +1,13 @@
1
+ # Sample Hardhat Project
2
+
3
+ This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.
4
+
5
+ Try running some of the following tasks:
6
+
7
+ ```shell
8
+ npx hardhat help
9
+ npx hardhat test
10
+ GAS_REPORT=true npx hardhat test
11
+ npx hardhat node
12
+ npx hardhat run scripts/deploy.ts
13
+ ```
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: UNLICENSED
2
+ pragma solidity ^0.8.9;
3
+
4
+ // Import this file to use console.log
5
+ import "hardhat/console.sol";
6
+
7
+ contract Lock {
8
+ uint public unlockTime;
9
+ address payable public owner;
10
+
11
+ event Withdrawal(uint amount, uint when);
12
+
13
+ constructor(uint _unlockTime) payable {
14
+ require(
15
+ block.timestamp < _unlockTime,
16
+ "Unlock time should be in the future"
17
+ );
18
+
19
+ unlockTime = _unlockTime;
20
+ owner = payable(msg.sender);
21
+ }
22
+
23
+ function withdraw() public {
24
+ // Uncomment this line to print a log in your terminal
25
+ // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
26
+
27
+ require(block.timestamp >= unlockTime, "You can't withdraw yet");
28
+ require(msg.sender == owner, "You aren't the owner");
29
+
30
+ emit Withdrawal(address(this).balance, block.timestamp);
31
+
32
+ owner.transfer(address(this).balance);
33
+ }
34
+ }
@@ -0,0 +1,8 @@
1
+ import { HardhatUserConfig } from "hardhat/config";
2
+ import "@nomicfoundation/hardhat-toolbox";
3
+
4
+ const config: HardhatUserConfig = {
5
+ solidity: "0.8.9",
6
+ };
7
+
8
+ export default config;
@@ -0,0 +1,23 @@
1
+ import { ethers } from "hardhat";
2
+
3
+ async function main() {
4
+ const currentTimestampInSeconds = Math.round(Date.now() / 1000);
5
+ const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
6
+ const unlockTime = currentTimestampInSeconds + ONE_YEAR_IN_SECS;
7
+
8
+ const lockedAmount = ethers.utils.parseEther("1");
9
+
10
+ const Lock = await ethers.getContractFactory("Lock");
11
+ const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
12
+
13
+ await lock.deployed();
14
+
15
+ console.log("Lock with 1 ETH deployed to:", lock.address);
16
+ }
17
+
18
+ // We recommend this pattern to be able to use async/await everywhere
19
+ // and properly handle errors.
20
+ main().catch((error) => {
21
+ console.error(error);
22
+ process.exitCode = 1;
23
+ });
@@ -0,0 +1,124 @@
1
+ import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers";
2
+ import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
3
+ import { expect } from "chai";
4
+ import { ethers } from "hardhat";
5
+
6
+ describe("Lock", function () {
7
+ // We define a fixture to reuse the same setup in every test.
8
+ // We use loadFixture to run this setup once, snapshot that state,
9
+ // and reset Hardhat Network to that snapshopt in every test.
10
+ async function deployOneYearLockFixture() {
11
+ const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
12
+ const ONE_GWEI = 1_000_000_000;
13
+
14
+ const lockedAmount = ONE_GWEI;
15
+ const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
16
+
17
+ // Contracts are deployed using the first signer/account by default
18
+ const [owner, otherAccount] = await ethers.getSigners();
19
+
20
+ const Lock = await ethers.getContractFactory("Lock");
21
+ const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
22
+
23
+ return { lock, unlockTime, lockedAmount, owner, otherAccount };
24
+ }
25
+
26
+ describe("Deployment", function () {
27
+ it("Should set the right unlockTime", async function () {
28
+ const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
29
+
30
+ expect(await lock.unlockTime()).to.equal(unlockTime);
31
+ });
32
+
33
+ it("Should set the right owner", async function () {
34
+ const { lock, owner } = await loadFixture(deployOneYearLockFixture);
35
+
36
+ expect(await lock.owner()).to.equal(owner.address);
37
+ });
38
+
39
+ it("Should receive and store the funds to lock", async function () {
40
+ const { lock, lockedAmount } = await loadFixture(
41
+ deployOneYearLockFixture
42
+ );
43
+
44
+ expect(await ethers.provider.getBalance(lock.address)).to.equal(
45
+ lockedAmount
46
+ );
47
+ });
48
+
49
+ it("Should fail if the unlockTime is not in the future", async function () {
50
+ // We don't use the fixture here because we want a different deployment
51
+ const latestTime = await time.latest();
52
+ const Lock = await ethers.getContractFactory("Lock");
53
+ await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
54
+ "Unlock time should be in the future"
55
+ );
56
+ });
57
+ });
58
+
59
+ describe("Withdrawals", function () {
60
+ describe("Validations", function () {
61
+ it("Should revert with the right error if called too soon", async function () {
62
+ const { lock } = await loadFixture(deployOneYearLockFixture);
63
+
64
+ await expect(lock.withdraw()).to.be.revertedWith(
65
+ "You can't withdraw yet"
66
+ );
67
+ });
68
+
69
+ it("Should revert with the right error if called from another account", async function () {
70
+ const { lock, unlockTime, otherAccount } = await loadFixture(
71
+ deployOneYearLockFixture
72
+ );
73
+
74
+ // We can increase the time in Hardhat Network
75
+ await time.increaseTo(unlockTime);
76
+
77
+ // We use lock.connect() to send a transaction from another account
78
+ await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
79
+ "You aren't the owner"
80
+ );
81
+ });
82
+
83
+ it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
84
+ const { lock, unlockTime } = await loadFixture(
85
+ deployOneYearLockFixture
86
+ );
87
+
88
+ // Transactions are sent using the first signer by default
89
+ await time.increaseTo(unlockTime);
90
+
91
+ await expect(lock.withdraw()).not.to.be.reverted;
92
+ });
93
+ });
94
+
95
+ describe("Events", function () {
96
+ it("Should emit an event on withdrawals", async function () {
97
+ const { lock, unlockTime, lockedAmount } = await loadFixture(
98
+ deployOneYearLockFixture
99
+ );
100
+
101
+ await time.increaseTo(unlockTime);
102
+
103
+ await expect(lock.withdraw())
104
+ .to.emit(lock, "Withdrawal")
105
+ .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
106
+ });
107
+ });
108
+
109
+ describe("Transfers", function () {
110
+ it("Should transfer the funds to the owner", async function () {
111
+ const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
112
+ deployOneYearLockFixture
113
+ );
114
+
115
+ await time.increaseTo(unlockTime);
116
+
117
+ await expect(lock.withdraw()).to.changeEtherBalances(
118
+ [owner, lock],
119
+ [lockedAmount, -lockedAmount]
120
+ );
121
+ });
122
+ });
123
+ });
124
+ });
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2020",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true
9
+ }
10
+ }
@@ -21,6 +21,8 @@ import { Reporter } from "../sentry/reporter";
21
21
  import { isRunningOnCiServer } from "../util/ci-detection";
22
22
  import {
23
23
  hasConsentedTelemetry,
24
+ hasPromptedForHHVSCode,
25
+ writePromptedForHHVSCode,
24
26
  writeTelemetryConsent,
25
27
  } from "../util/global-dir";
26
28
  import { getPackageJson, PackageJson } from "../util/packageInfo";
@@ -29,7 +31,13 @@ import { applyWorkaround } from "../util/antlr-prototype-pollution-workaround";
29
31
  import { Analytics } from "./analytics";
30
32
  import { ArgumentsParser } from "./ArgumentsParser";
31
33
  import { enableEmoji } from "./emoji";
32
- import { confirmTelemetryConsent, createProject } from "./project-creation";
34
+ import { createProject } from "./project-creation";
35
+ import { confirmHHVSCodeInstallation, confirmTelemetryConsent } from "./prompt";
36
+ import {
37
+ InstallationState,
38
+ installHardhatVSCode,
39
+ isHardhatVSCodeInstalled,
40
+ } from "./hardhat-vscode-installation";
33
41
 
34
42
  const log = debug("hardhat:core:cli");
35
43
 
@@ -56,6 +64,39 @@ To learn more about which versions of Node.js are supported go to https://hardha
56
64
  }
57
65
  }
58
66
 
67
+ async function suggestInstallingHardhatVscode() {
68
+ const alreadyPrompted = hasPromptedForHHVSCode();
69
+ if (alreadyPrompted) {
70
+ return;
71
+ }
72
+
73
+ const isInstalled = isHardhatVSCodeInstalled();
74
+ writePromptedForHHVSCode();
75
+
76
+ if (isInstalled !== InstallationState.EXTENSION_NOT_INSTALLED) {
77
+ return;
78
+ }
79
+
80
+ const installationConsent = await confirmHHVSCodeInstallation();
81
+
82
+ if (installationConsent === true) {
83
+ console.log("Installing Hardhat for Visual Studio Code...");
84
+ const installed = installHardhatVSCode();
85
+
86
+ if (installed) {
87
+ console.log("Hardhat for Visual Studio Code was successfully installed");
88
+ } else {
89
+ console.log(
90
+ "Hardhat for Visual Studio Code couldn't be installed. To learn more about it, go to https://hardhat.org/hardhat-vscode"
91
+ );
92
+ }
93
+ } else {
94
+ console.log(
95
+ "To learn more about Hardhat for Visual Studio Code, go to https://hardhat.org/hardhat-vscode"
96
+ );
97
+ }
98
+ }
99
+
59
100
  async function main() {
60
101
  // We first accept this argument anywhere, so we know if the user wants
61
102
  // stack traces before really parsing the arguments.
@@ -103,12 +144,9 @@ async function main() {
103
144
  if (hardhatArguments.config === undefined && !isCwdInsideProject()) {
104
145
  if (
105
146
  process.stdout.isTTY === true ||
106
- process.env.HARDHAT_CREATE_BASIC_SAMPLE_PROJECT_WITH_DEFAULTS !==
147
+ process.env.HARDHAT_CREATE_JAVASCRIPT_PROJECT_WITH_DEFAULTS !==
107
148
  undefined ||
108
- process.env.HARDHAT_CREATE_ADVANCED_SAMPLE_PROJECT_WITH_DEFAULTS !==
109
- undefined ||
110
- process.env
111
- .HARDHAT_CREATE_ADVANCED_TYPESCRIPT_SAMPLE_PROJECT_WITH_DEFAULTS !==
149
+ process.env.HARDHAT_CREATE_TYPESCRIPT_PROJECT_WITH_DEFAULTS !==
112
150
  undefined
113
151
  ) {
114
152
  await createProject();
@@ -226,6 +264,16 @@ async function main() {
226
264
  } else {
227
265
  abortAnalytics();
228
266
  }
267
+
268
+ // VSCode extension prompt for installation
269
+ if (
270
+ taskName === "test" &&
271
+ !isRunningOnCiServer() &&
272
+ process.stdout.isTTY === true
273
+ ) {
274
+ await suggestInstallingHardhatVscode();
275
+ }
276
+
229
277
  log(`Killing Hardhat after successfully running task ${taskName}`);
230
278
  } catch (error) {
231
279
  let isHardhatError = false;
@@ -259,7 +307,7 @@ async function main() {
259
307
  } else {
260
308
  if (!isHardhatError) {
261
309
  console.error(
262
- `If you think this is a bug in Hardhat, please report it here: https://hardhat.org/reportbug`
310
+ `If you think this is a bug in Hardhat, please report it here: https://hardhat.org/report-bug`
263
311
  );
264
312
  }
265
313
 
@@ -0,0 +1,43 @@
1
+ import { spawnSync } from "child_process";
2
+
3
+ export enum InstallationState {
4
+ VSCODE_FAILED_OR_NOT_INSTALLED,
5
+ EXTENSION_INSTALLED,
6
+ EXTENSION_NOT_INSTALLED,
7
+ }
8
+
9
+ const HARDHAT_VSCODE_ID = "NomicFoundation.hardhat-solidity";
10
+
11
+ export function isHardhatVSCodeInstalled(): InstallationState {
12
+ try {
13
+ const { stdout, status } = spawnSync("code", ["--list-extensions"], {
14
+ encoding: "utf8",
15
+ });
16
+
17
+ if (status !== 0) {
18
+ return InstallationState.VSCODE_FAILED_OR_NOT_INSTALLED;
19
+ }
20
+
21
+ return stdout.includes(HARDHAT_VSCODE_ID)
22
+ ? InstallationState.EXTENSION_INSTALLED
23
+ : InstallationState.EXTENSION_NOT_INSTALLED;
24
+ } catch (e) {
25
+ return InstallationState.VSCODE_FAILED_OR_NOT_INSTALLED;
26
+ }
27
+ }
28
+
29
+ export function installHardhatVSCode(): boolean {
30
+ try {
31
+ const { status } = spawnSync(
32
+ "code",
33
+ ["--install-extension", HARDHAT_VSCODE_ID],
34
+ {
35
+ encoding: "utf8",
36
+ }
37
+ );
38
+
39
+ return status === 0;
40
+ } catch (e) {
41
+ return false;
42
+ }
43
+ }