@@ -13,7 +13,7 @@ import { AwesomeAddressBook } from './AwesomeAddressBook'
1313import { DefaultChainList , DefaultHardhatPluginsList } from './config'
1414import MockContractsList from './mockContracts'
1515import './type-extensions'
16- import { IChain , IExcludedFiles , IFileSetting , IHardhatPluginAvailableList , IMockContractsList , ITestAndScript } from './types'
16+ import { IChain , IExcludedFiles , IFileList , IFileSetting , IHardhatPluginAvailableList , IInquirerListField , IMockContractsList } from './types'
1717
1818const fileHardhatAwesomeCLI = 'hardhat-awesome-cli.json'
1919const fileEnvHardhatAwesomeCLI = '.env.hardhat-awesome-cli'
@@ -22,28 +22,35 @@ const fileContractsAddressDeployedHistory = 'contractsAddressDeployedHistory.jso
2222let contractsAddressDeployed = [ ]
2323let 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}
2929if ( 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}
3636if ( 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}
4350if ( 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
198205const 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
224231const 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+
245273const 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
265293const 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+
285333const 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+
9211010const 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
10401129const 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
12651354YP 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