@@ -18,14 +18,29 @@ process.chdir(root);
1818
1919console . log ( `Creating a new Hardhat project in ${ root } .` ) ;
2020
21+ // Set yarn to v2
22+ console . log ( 'Setting yarn to enable v2...' ) ;
23+ spawnSync ( 'yarn' , [ 'set' , 'version' , 'berry' ] , { stdio : 'inherit' } ) ;
24+
2125// Initialize a new npm project
2226console . log ( 'Initializing a new npm project...' ) ;
23- spawnSync ( 'npm' , [ 'init' , '-y' ] , { stdio : 'inherit' } ) ;
27+ spawnSync ( 'yarn' , [ 'init' , '--yes' ] , { stdio : 'inherit' } ) ;
28+
29+ // Remove README.md created by yarn init
30+ console . log ( 'Removing README.md...' ) ;
31+ spawnSync ( 'rm' , [ 'README.md' ] , { stdio : 'inherit' } ) ;
32+
33+ // Make sure the nodeLinker is set to node-modules to avoid issues with npx
34+ console . log ( 'Add nodeLinker to .yarnrc.yml...' ) ;
35+ const yarnrcPath = path . join ( root , '.yarnrc.yml' ) ;
36+ const yarnrcContent = fs . readFileSync ( yarnrcPath , 'utf-8' ) ;
37+ fs . writeFileSync ( yarnrcPath , yarnrcContent + 'nodeLinker: node-modules\n' ) ;
2438
2539// Install Hardhat and initialize a new TypeScript project
2640console . log ( 'Installing Hardhat...' ) ;
27- spawnSync ( 'npm ' , [ 'install ' , 'hardhat' ] , { stdio : 'inherit' } ) ;
41+ spawnSync ( 'yarn ' , [ 'add ' , 'hardhat' ] , { stdio : 'inherit' } ) ;
2842
43+ // Initialize a new Hardhat TypeScript project
2944console . log ( 'Initializing a new Hardhat TypeScript project...' ) ;
3045const result = execSync (
3146 'HARDHAT_CREATE_TYPESCRIPT_PROJECT_WITH_DEFAULTS=true npx hardhat' ,
@@ -34,13 +49,13 @@ const result = execSync(
3449 }
3550) ;
3651
52+ // Install Hardhat Plugins
3753console . log ( 'Installing Hardhat Plugins...' ) ;
3854spawnSync (
39- 'npm ' ,
55+ 'yarn ' ,
4056 [
41- 'install' ,
42- '--save-dev' ,
43- '-f' ,
57+ 'add' ,
58+ '-D' ,
4459 'solhint' ,
4560 'prettier' ,
4661 'prettier-plugin-solidity' ,
@@ -49,19 +64,23 @@ spawnSync(
4964 '@types/mocha' ,
5065 '@types/node' ,
5166 'dotenv' ,
52- '@nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers' ,
67+ '@nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers@latest ' ,
5368 '@openzeppelin/contracts' ,
5469 'hardhat-contract-sizer' ,
5570 'hardhat-gas-reporter' ,
5671 ] ,
5772 { stdio : 'inherit' }
5873) ;
5974
60- // Add the "compile" script to package.json
75+ // Add the "compile" and "test" script to package.json
6176console . log ( "Adding the 'compile' script to package.json..." ) ;
6277const packageJsonPath = path . join ( root , 'package.json' ) ;
6378const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf-8' ) ) ;
64- packageJson . scripts . compile = 'hardhat compile' ;
79+ packageJson . scripts = {
80+ ...( packageJson . script ?? { } ) ,
81+ compile : 'hardhat compile' ,
82+ test : 'hardhat test' ,
83+ } ;
6584fs . writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) ) ;
6685
6786// Edit the hardhat.config.ts
@@ -131,6 +150,7 @@ let modifiedContent = configContent.replace(
131150 `
132151) ;
133152
153+ // Add plugins to the config
134154modifiedContent = modifiedContent . replace (
135155 `import "@nomicfoundation/hardhat-toolbox";` ,
136156 `import '@nomicfoundation/hardhat-toolbox';
@@ -139,7 +159,6 @@ import 'hardhat-gas-reporter';
139159import 'hardhat-contract-sizer';
140160import { nodeUrl, accounts } from './utils/network';
141161import * as dotenv from 'dotenv';
142- import './tasks/acl';
143162
144163dotenv.config();
145164 `
@@ -150,24 +169,21 @@ fs.writeFileSync(configPath, modifiedContent);
150169console . log ( 'Copying files to the project directory...' ) ;
151170fs . copySync ( path . resolve ( __dirname , '../files' ) , `${ root } /` ) ;
152171
172+ // Success message
153173console . log ( `
154174Success! Created ${ projectName } at ${ root } .
155175
156176Inside that directory, you can run several commands:
157177
158- npm run compile
178+ yarn run compile
159179 Compiles the contracts.
160180
161- npm run test
181+ yarn run test
162182 Runs the tests.
163183
164- npm run test:watch
165- Runs the tests in watch mode.
166-
167- We suggest that you begin by typing:
184+ We suggest that you install Hardhat shorthand:
168185
169- cd ${ projectName }
170- npm run test
186+ npm install --global hardhat-shorthand
171187
172188Happy hacking!
173189` ) ;
0 commit comments