Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 6839010

Browse files
Merge pull request #20 from marc-aurele-besner/dev
Add flatten option💪
2 parents 301a5d4 + 071e710 commit 6839010

File tree

5 files changed

+143
-37
lines changed

5 files changed

+143
-37
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ npm link hardhat-awesome-cli
4848
- Run tests (Allow you you to run tests on all files or specific files in test/)
4949
- Run scripts (Allow you you to run scripts on specific files in scripts/)
5050
- Select scripts and tests to run (Allow you to select a script to execute and all or one test to perform afterward)
51+
- Flatten all your contract or a specific contract
5152
- Run coverage tests (Available only if solidity-coverage is installed and available as a task)
5253
- Setup chains, RPC and accounts
5354
- Add/Remove chains from the chain selection
@@ -73,9 +74,16 @@ npm link hardhat-awesome-cli
7374
- Ethereum - Mainnet (chainId: 1)
7475
- Ethereum - Ropstein (chainId 3)
7576
- Ethereum - Rinkeby (chainId 4)
77+
- Ethereum - Goerli (chainId 5)
7678
- Ethereum - Kovan (chainId 42)
7779
- Polygon - Mainnet (chainId 137)
7880
- Polygon - Mumbai (chainId 80001)
81+
- Binance Smart Chain - Mainnet (chainId 56)
82+
- Binance Smart Chain - Tesnet (chainId 97)
83+
- Optimism - Mainnet (chainId 10)
84+
- Optimism - Testnet Kovan (chainId 69)
85+
- Avalanche - Mainnet (chainId 43114)
86+
7987

8088
In 'More settings' you can also add a custom chain, create an issue or pull request to add other chains.
8189

@@ -129,6 +137,7 @@ await addressBook.retrieveContract('MockERC20', 'ethereum')
129137
- Offer to create test scripts
130138
- Tool to log all contracts deploy on each chain (1 unique contractName/chain + full log) and retrieve them (not tested yet)
131139
- hre.addressBook.{ saveContract, retrieveContract }
140+
- Flatten your contracts (All contracts, or specific contracts) save in contractsFlatten/
132141

133142

134143
## 🏗️ To do:
@@ -151,5 +160,4 @@ await addressBook.retrieveContract('MockERC20', 'ethereum')
151160
- Offer to rename the Mock contract and set all constructor input (or initialize input) via cli
152161
- Verify that the input name does not conflict with inheritance
153162
- Rename the Mock file, contract name, deployment script, test scripts (and the test values)
154-
- Add a flatten options (All contracts, or specific contracts) save in contractsFlatten/
155163
- Write some test on the package using mocha

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hardhat-awesome-cli",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Hardhat made awesome with a flexible CLI to help run test, deploy and more.",
55
"repository": "https://github.com/marc-aurele-besner/hardhat-awesome-cli.git",
66
"author": "Marc-Aurele Besner <82244926+marc-aurele-besner@users.noreply.github.com>",

src/config.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ export const DefaultChainList: IChain[] = [
3232
currency: 'ETH',
3333
defaultBlockExplorer: 'https://rinkeby.etherscan.io/'
3434
},
35-
{
36-
name: 'Ethereum - Kovan',
37-
chainName: 'kovan',
38-
chainId: 42,
39-
gas: 'auto',
40-
currency: 'ETH',
41-
defaultBlockExplorer: 'https://kovan.etherscan.io/'
42-
},
4335
{
4436
name: 'Ethereum - Goerli',
4537
chainName: 'goerli',
@@ -48,6 +40,14 @@ export const DefaultChainList: IChain[] = [
4840
currency: 'ETH',
4941
defaultBlockExplorer: 'https://goerli.etherscan.io/'
5042
},
43+
{
44+
name: 'Ethereum - Kovan',
45+
chainName: 'kovan',
46+
chainId: 42,
47+
gas: 'auto',
48+
currency: 'ETH',
49+
defaultBlockExplorer: 'https://kovan.etherscan.io/'
50+
},
5151
{
5252
name: 'Polygon - Mainnet',
5353
chainName: 'polygon',
@@ -96,10 +96,10 @@ export const DefaultChainList: IChain[] = [
9696
{
9797
name: 'Optimism - Testnet Kovan',
9898
chainName: 'optimismTestnetKovan',
99-
chainId: 56,
99+
chainId: 69,
100100
gas: 'auto',
101101
currency: 'ETH',
102-
defaultRpcUrl: 'https://bsc-dataseed1.binance.org'
102+
defaultRpcUrl: 'https://kovan.optimism.io'
103103
},
104104
{
105105
name: 'Avalanche - Mainnet',

src/index.ts

Lines changed: 117 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AwesomeAddressBook } from './AwesomeAddressBook'
1313
import { DefaultChainList, DefaultHardhatPluginsList } from './config'
1414
import MockContractsList from './mockContracts'
1515
import './type-extensions'
16-
import { IChain, IExcludedFiles, IFileSetting, IHardhatPluginAvailableList, IMockContractsList, ITestAndScript } from './types'
16+
import { IChain, IExcludedFiles, IFileList, IFileSetting, IHardhatPluginAvailableList, IInquirerListField, IMockContractsList } from './types'
1717

1818
const fileHardhatAwesomeCLI = 'hardhat-awesome-cli.json'
1919
const fileEnvHardhatAwesomeCLI = '.env.hardhat-awesome-cli'
@@ -22,28 +22,35 @@ const fileContractsAddressDeployedHistory = 'contractsAddressDeployedHistory.jso
2222
let contractsAddressDeployed = []
2323
let contractsAddressDeployedHistory = []
2424

25-
let inquirerRunTests: any = {
25+
let inquirerRunTests: IInquirerListField | string = {
2626
name: 'Run tests',
2727
disabled: "We can't run tests without a test/ directory"
2828
}
2929
if (fs.existsSync('test')) {
3030
inquirerRunTests = 'Run tests'
3131
}
32-
let inquirerRunScripts: any = {
32+
let inquirerRunScripts: IInquirerListField | string = {
3333
name: 'Run scripts',
3434
disabled: "We can't run scripts without a scripts/ directory"
3535
}
3636
if (fs.existsSync('scripts')) {
3737
inquirerRunScripts = 'Run scripts'
3838
}
39-
let inquirerRunMockContractCreator: any = {
39+
let inquirerFlattenContracts: IInquirerListField | string = {
40+
name: 'Flatten contracts',
41+
disabled: "We can't flatten contracts without a contracts/ directory"
42+
}
43+
if (fs.existsSync('contracts')) {
44+
inquirerFlattenContracts = 'Flatten contracts'
45+
}
46+
let inquirerRunMockContractCreator: IInquirerListField | string = {
4047
name: 'Create Mock contracts',
4148
disabled: "We can't create Mock contracts without a contracts/ directory"
4249
}
4350
if (fs.existsSync('contracts')) {
4451
inquirerRunMockContractCreator = 'Create Mock contracts'
4552
}
46-
let inquirerFileContractsAddressDeployed: any = {
53+
let inquirerFileContractsAddressDeployed: IInquirerListField | string = {
4754
name: 'Get the previously deployed contracts address',
4855
disabled: 'Please deploy the contracts first'
4956
}
@@ -52,7 +59,7 @@ if (fs.existsSync(fileContractsAddressDeployed)) {
5259
contractsAddressDeployed = JSON.parse(rawdata)
5360
inquirerFileContractsAddressDeployed = 'Get the previously deployed contracts address'
5461
}
55-
let inquirerFileContractsAddressDeployedHistory: any = {
62+
let inquirerFileContractsAddressDeployedHistory: IInquirerListField | string = {
5663
name: 'Get all the previously deployed contracts address',
5764
disabled: 'Please deploy the contracts first'
5865
}
@@ -196,7 +203,7 @@ const buildActivatedChainList = async () => {
196203
}
197204

198205
const buildAllTestsList = async () => {
199-
const testList: ITestAndScript[] = []
206+
const testList: IFileList[] = []
200207
if (fs.existsSync('test')) {
201208
testList.push({
202209
name: 'All tests',
@@ -222,7 +229,7 @@ const buildAllTestsList = async () => {
222229
}
223230

224231
const buildAllScriptsList = async () => {
225-
const scriptsList: ITestAndScript[] = []
232+
const scriptsList: IFileList[] = []
226233
if (fs.existsSync('scripts')) {
227234
const files = fs.readdirSync('scripts')
228235
files.map((file) => {
@@ -242,8 +249,29 @@ const buildAllScriptsList = async () => {
242249
return scriptsList
243250
}
244251

252+
const buildAllContractsList = async () => {
253+
const scontractsList: IFileList[] = []
254+
if (fs.existsSync('contracts')) {
255+
const files = fs.readdirSync('contracts')
256+
files.map((file) => {
257+
let fileName = file.replace(/\.[^/.]+$/, '')
258+
const words = fileName.split(' ')
259+
for (let i = 0; i < words.length; i++) {
260+
words[i] = words[i][0].toUpperCase() + words[i].substr(1)
261+
}
262+
fileName = words.join(' ')
263+
scontractsList.push({
264+
name: fileName,
265+
type: 'file',
266+
filePath: file
267+
})
268+
})
269+
}
270+
return scontractsList
271+
}
272+
245273
const buildTestsList = async () => {
246-
let allTestList: ITestAndScript[] = await buildAllTestsList()
274+
let allTestList: IFileList[] = await buildAllTestsList()
247275
let excludedFiles: IExcludedFiles[] = await buildExcludedFile()
248276
const buildFilePath: string[] = []
249277
if (excludedFiles && excludedFiles.length > 0) {
@@ -252,7 +280,7 @@ const buildTestsList = async () => {
252280
excludedFiles.map((file: IExcludedFiles) => {
253281
buildFilePath.push(file.filePath)
254282
})
255-
allTestList = allTestList.filter((script: ITestAndScript) => {
283+
allTestList = allTestList.filter((script: IFileList) => {
256284
return !buildFilePath.includes(script.filePath)
257285
})
258286
return allTestList
@@ -263,7 +291,7 @@ const buildTestsList = async () => {
263291
}
264292

265293
const buildScriptsList = async () => {
266-
let allScriptList: ITestAndScript[] = await buildAllScriptsList()
294+
let allScriptList: IFileList[] = await buildAllScriptsList()
267295
let excludedFiles: IExcludedFiles[] = await buildExcludedFile()
268296
const buildFilePath: string[] = []
269297
if (excludedFiles && excludedFiles.length > 0) {
@@ -272,7 +300,7 @@ const buildScriptsList = async () => {
272300
excludedFiles.map((file: any) => {
273301
buildFilePath.push(file.filePath)
274302
})
275-
allScriptList = allScriptList.filter((script: ITestAndScript) => {
303+
allScriptList = allScriptList.filter((script: IFileList) => {
276304
return !buildFilePath.includes(script.filePath)
277305
})
278306
return allScriptList
@@ -282,6 +310,26 @@ const buildScriptsList = async () => {
282310
}
283311
}
284312

313+
const buildContractsList = async () => {
314+
let allContractsList: IFileList[] = await buildAllContractsList()
315+
let excludedFiles: IExcludedFiles[] = await buildExcludedFile()
316+
const buildFilePath: string[] = []
317+
if (excludedFiles && excludedFiles.length > 0) {
318+
excludedFiles = excludedFiles.filter((test: any) => test.directory === 'contracts')
319+
if (excludedFiles && excludedFiles.length > 0) {
320+
excludedFiles.map((file: any) => {
321+
buildFilePath.push(file.filePath)
322+
})
323+
allContractsList = allContractsList.filter((script: IFileList) => {
324+
return !buildFilePath.includes(script.filePath)
325+
})
326+
return allContractsList
327+
}
328+
} else {
329+
return allContractsList
330+
}
331+
}
332+
285333
const buildMockContract = async (contractName: string) => {
286334
if (require && require.main) {
287335
const packageRootPath = path.join(path.dirname(require.main.filename), '../../../hardhat-awesome-cli/src/mockContracts')
@@ -505,8 +553,8 @@ const removeExcludedFiles = async (directory: string, filePath: string) => {
505553
})
506554
} else if (directory === 'script') {
507555
allFiles = (await buildAllScriptsList())
508-
.filter((script: ITestAndScript) => script.type === 'file')
509-
.map((file: ITestAndScript) => {
556+
.filter((script: IFileList) => script.type === 'file')
557+
.map((file: IFileList) => {
510558
return file.filePath
511559
})
512560
}
@@ -850,7 +898,7 @@ const serveTestSelector = async (env: any, command: string, firstCommand: string
850898
const testFilesObject = await buildTestsList()
851899
let testFilesList: string[] = []
852900
if (testFilesObject) {
853-
testFilesList = testFilesObject.map((file: ITestAndScript) => {
901+
testFilesList = testFilesObject.map((file: IFileList) => {
854902
return file.name
855903
})
856904
if (testFilesList.length > 0) {
@@ -864,7 +912,7 @@ const serveTestSelector = async (env: any, command: string, firstCommand: string
864912
}
865913
])
866914
.then(async (testSelected: { test: string }) => {
867-
testFilesObject.forEach((file: ITestAndScript) => {
915+
testFilesObject.forEach((file: IFileList) => {
868916
if (file.name === testSelected.test) {
869917
if (file.type === 'file') {
870918
command = command + ' test/' + file.filePath
@@ -885,7 +933,7 @@ const serveScriptSelector = async (env: any, ServeTestSelector: any) => {
885933
const scriptFilesObject = await buildScriptsList()
886934
const scriptFilesList: string[] = []
887935
if (scriptFilesObject) {
888-
scriptFilesObject.map((file: ITestAndScript) => {
936+
scriptFilesObject.map((file: IFileList) => {
889937
scriptFilesList.push(file.name)
890938
})
891939
if (scriptFilesObject.length > 0) {
@@ -900,7 +948,7 @@ const serveScriptSelector = async (env: any, ServeTestSelector: any) => {
900948
])
901949
.then(async (scriptSelected: { script: string }) => {
902950
let command = 'npx hardhat run'
903-
scriptFilesObject.forEach((file: ITestAndScript) => {
951+
scriptFilesObject.forEach((file: IFileList) => {
904952
if (file.name === scriptSelected.script) {
905953
if (file.type === 'file') {
906954
command = command + ' scripts/' + file.filePath
@@ -918,6 +966,47 @@ const serveScriptSelector = async (env: any, ServeTestSelector: any) => {
918966
}
919967
}
920968

969+
const serveFlattenContractsSelector = async (env: any, command: string) => {
970+
const contractsFilesObject = await buildContractsList()
971+
const contractsFilesList: string[] = ['Flatten all contracts']
972+
if (contractsFilesObject) {
973+
contractsFilesObject.map((file: IFileList) => {
974+
contractsFilesList.push(file.name)
975+
})
976+
if (contractsFilesList.length > 0) {
977+
await inquirer
978+
.prompt([
979+
{
980+
type: 'list',
981+
name: 'flatten',
982+
message: 'Select a contract to flatten',
983+
choices: contractsFilesList
984+
}
985+
])
986+
.then(async (contractsSelected: { flatten: string }) => {
987+
if (!fs.existsSync('contractsFlatten')) {
988+
fs.mkdirSync('contractsFlatten')
989+
}
990+
let contractFlattenName: string = ''
991+
if (contractsSelected.flatten !== 'Flatten all contracts') {
992+
contractsFilesObject.forEach((file: IFileList) => {
993+
if (file.name === contractsSelected.flatten) {
994+
if (file.type === 'file') {
995+
command = command + ' contracts/' + file.filePath
996+
contractFlattenName = file.filePath.replace('.sol', 'Flatten.sol')
997+
}
998+
}
999+
})
1000+
} else {
1001+
contractFlattenName = 'AllContractsFlatten.sol'
1002+
}
1003+
await runCommand(command, '', ' > contractsFlatten/' + contractFlattenName)
1004+
await sleep(3000)
1005+
})
1006+
}
1007+
}
1008+
}
1009+
9211010
const serveEnvBuilder = async (env: any, chainSelected: string) => {
9221011
const ActivatedChainList = await buildActivatedChainList()
9231012
if (ActivatedChainList.find((chain: IChain) => chain.name === chainSelected)) {
@@ -1038,7 +1127,7 @@ const serveSettingSelector = async (env: any) => {
10381127
}
10391128

10401129
const serveExcludeFileSelector = async (option: string) => {
1041-
let allFiles: ITestAndScript[] = []
1130+
let allFiles: IFileList[] = []
10421131
let excludedFiles: IExcludedFiles[] = await buildExcludedFile()
10431132
const allFilesSelection: string[] = []
10441133
let allExcludedSelection: string[] = []
@@ -1048,10 +1137,10 @@ const serveExcludeFileSelector = async (option: string) => {
10481137
allFiles = await buildAllScriptsList()
10491138
}
10501139
if (allFiles && allFiles.length > 0) {
1051-
if (allFiles.filter((test: ITestAndScript) => test.type === 'file').length > 0) {
1140+
if (allFiles.filter((test: IFileList) => test.type === 'file').length > 0) {
10521141
allFiles
1053-
.filter((test: ITestAndScript) => test.type === 'file')
1054-
.map((file: ITestAndScript) => {
1142+
.filter((test: IFileList) => test.type === 'file')
1143+
.map((file: IFileList) => {
10551144
allFilesSelection.push(file.filePath)
10561145
})
10571146
}
@@ -1075,7 +1164,7 @@ const serveExcludeFileSelector = async (option: string) => {
10751164
}
10761165
])
10771166
.then(async (activateFilesSelected: { allFiles: string[] }) => {
1078-
allFiles.map(async (file: ITestAndScript) => {
1167+
allFiles.map(async (file: IFileList) => {
10791168
if (activateFilesSelected.allFiles.includes(file.name)) {
10801169
await addExcludedFiles(option, file.name)
10811170
} else {
@@ -1265,7 +1354,7 @@ d8' '8b 88 I8I 88 88' 88' YP .8P Y8. 88'YbdP'88 88' d8P Y8 88
12651354
YP YP '8b8' '8d8' Y88888P '8888Y' 'Y88P' YP YP YP Y88888P 'Y88P' Y88888P Y888888P
12661355
`
12671356
)
1268-
const buildMainOptions = [inquirerRunTests, inquirerRunScripts]
1357+
const buildMainOptions: any = [inquirerRunTests, inquirerRunScripts, inquirerFlattenContracts]
12691358
if (inquirerRunTests === 'Run tests' && inquirerRunScripts === 'Run scripts') buildMainOptions.push('Select scripts and tests to run')
12701359
const solidityCoverageDetected = await detectPackage('solidity-coverage', false, false)
12711360
if (solidityCoverageDetected) buildMainOptions.push('Run coverage tests')
@@ -1302,6 +1391,10 @@ YP YP '8b8' '8d8' Y88888P '8888Y' 'Y88P' YP YP YP Y88888P 'Y88P' Y8
13021391
if (answers.action === 'Run scripts') {
13031392
await serveScriptSelector(env, '')
13041393
}
1394+
if (answers.action === 'Flatten contracts') {
1395+
command = 'npx hardhat flatten'
1396+
await serveFlattenContractsSelector(env, command)
1397+
}
13051398
if (answers.action === 'Select scripts and tests to run') {
13061399
await serveScriptSelector(env, serveTestSelector)
13071400
}

0 commit comments

Comments
 (0)