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

Commit

Permalink
improve config flexibility and run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Nov 1, 2022
1 parent ced2766 commit 15fb974
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 74 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

# 👷 hardhat-awesome-cli

```
# # ## ##### ##### # # ## ##### ## # # ###### #### #### # # ###### #### # #
# # # # # # # # # # # # # # # # # # # # # ## ## # # # # #
###### # # # # # # ###### # # # ##### # # # # ##### #### # # # ## # ##### ##### # # #
# # ###### ##### # # # # ###### # ###### # ## # # # # # # # # # # #
# # # # # # # # # # # # # # # ## ## # # # # # # # # # # # #
# # # # # # ##### # # # # # # # # # ###### #### #### # # ###### #### ###### #
```

Hardhat made awesome with a flexible CLI to help run tests, deploy and more.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 61 additions & 26 deletions src/AwesomeAddressBook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'

import { fileContractsAddressDeployed, fileContractsAddressDeployedHistory } from './config'
import { getAddressBookConfig } from './config'
import { TAddressBookFields } from './types'

interface IAddressDetails {
Expand All @@ -17,6 +17,12 @@ interface IAddressDetails {
}

export class AwesomeAddressBook {
private readonly _env: any

constructor(hre: any) {
this._env = hre
}

public formatSaveContract(
contractName: string,
contractAddress: string,
Expand Down Expand Up @@ -67,10 +73,14 @@ export class AwesomeAddressBook {
)
let contractsAddressDeployed = []
let contractsAddressDeployedHistory = []

const addressBookConfig = getAddressBookConfig(this._env.userConfig)
// Add folder if not exist
if (!fs.existsSync(addressBookConfig.savePath)) fs.mkdirSync(addressBookConfig.savePath)
// Add or edit contract address if deploy on same network
if (fs.existsSync(fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployed)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed
)
contractsAddressDeployed = JSON.parse(rawdata)
if (contractsAddressDeployed !== undefined && contractsAddressDeployed.length > 0) {
let recordModify = false
Expand All @@ -92,28 +102,33 @@ export class AwesomeAddressBook {
contractsAddressDeployed.push(contractToAdd)
}
}
fs.unlinkSync(fileContractsAddressDeployed)
fs.unlinkSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)
} else {
contractsAddressDeployed.push(contractToAdd)
}
try {
fs.writeFileSync(fileContractsAddressDeployed, JSON.stringify(contractsAddressDeployed, null, 2))
fs.writeFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed,
JSON.stringify(contractsAddressDeployed, null, 2)
)
} catch (err) {
console.log('Error writing address to file: ', err)
}

// Log all contracts deployed
if (fs.existsSync(fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployedHistory)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory
)
contractsAddressDeployedHistory = JSON.parse(rawdata)
contractsAddressDeployedHistory.push(contractToAdd)
fs.unlinkSync(fileContractsAddressDeployedHistory)
fs.unlinkSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory)
} else {
contractsAddressDeployedHistory.push(contractToAdd)
}
try {
fs.writeFileSync(
fileContractsAddressDeployedHistory,
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory,
JSON.stringify(contractsAddressDeployedHistory, null, 2)
)
} catch (err) {
Expand All @@ -123,8 +138,11 @@ export class AwesomeAddressBook {

public retrieveContract(contractName: string, deployedNetwork: string) {
let returnContractAddress = ''
if (fs.existsSync(fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployed)
const addressBookConfig = getAddressBookConfig(this._env.userConfig)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed
)
const contractsAddressDeployed: IAddressDetails[] = JSON.parse(rawdata)
if (contractsAddressDeployed !== undefined && contractsAddressDeployed.length > 0) {
if (
Expand All @@ -141,8 +159,11 @@ export class AwesomeAddressBook {
}

public retrieveContractObject(contractName: string, deployedNetwork: string) {
if (fs.existsSync(fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployed)
const addressBookConfig = getAddressBookConfig(this._env.userConfig)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed
)
const contractsAddressDeployed: IAddressDetails[] = JSON.parse(rawdata)
if (contractsAddressDeployed !== undefined && contractsAddressDeployed.length > 0) {
if (
Expand All @@ -160,6 +181,7 @@ export class AwesomeAddressBook {

public retrieveOZAdminProxyContract(chainId: number) {
let returnContractAddress = ''
const addressBookConfig = getAddressBookConfig(this._env.userConfig)
let ozFileName = ''
switch (chainId) {
case 1:
Expand All @@ -175,17 +197,20 @@ export class AwesomeAddressBook {
default:
ozFileName = `unknown-${chainId}`
}
if (fs.existsSync(`.openzeppelin/${ozFileName}.json`)) {
const ozFileRawdata: any = fs.readFileSync(`.openzeppelin/${ozFileName}.json`)
if (fs.existsSync(`${addressBookConfig.openzeppelinPath}/${ozFileName}.json`)) {
const ozFileRawdata: any = fs.readFileSync(`${addressBookConfig.openzeppelinPath}/${ozFileName}.json`)
returnContractAddress = JSON.parse(ozFileRawdata).admin.address
}
return returnContractAddress
}

public retrieveContractHistory(deployedNetwork: string) {
const addressBookConfig = getAddressBookConfig(this._env.userConfig)
const returnContractAddress: IAddressDetails[] = []
if (fs.existsSync(fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployedHistory)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory
)
const contractsAddressDeployedHistory: IAddressDetails[] = JSON.parse(rawdata)
if (contractsAddressDeployedHistory !== undefined && contractsAddressDeployedHistory.length > 0) {
contractsAddressDeployedHistory
Expand All @@ -204,45 +229,55 @@ export class AwesomeAddressBook {
applyToPrimary: boolean = true,
applyToHistory: boolean = true
) {
const addressBookConfig = getAddressBookConfig(this._env.userConfig)
if (applyToPrimary)
if (fs.existsSync(fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployed)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed
)
const contractsAddressDeployed: IAddressDetails[] = JSON.parse(rawdata)
if (contractsAddressDeployed !== undefined && contractsAddressDeployed.length > 0) {
if (contractsAddressDeployed.find((c: IAddressDetails) => c[field] === value)) {
const contractsAddressDeployedFiltered = contractsAddressDeployed.filter(
(c: IAddressDetails) => c[field] !== value
)
fs.unlinkSync(fileContractsAddressDeployed)
fs.unlinkSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)
const contractsAddressDeployedFilteredString =
contractsAddressDeployedFiltered.length > 0
? JSON.stringify(contractsAddressDeployedFiltered, null, 2)
: ''
try {
fs.writeFileSync(fileContractsAddressDeployed, contractsAddressDeployedFilteredString)
fs.writeFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed,
contractsAddressDeployedFilteredString
)
} catch (err) {
console.log('Error writing address to file: ', err)
}
}
}
}
if (applyToHistory)
if (fs.existsSync(fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(fileContractsAddressDeployedHistory)
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory)) {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory
)
const contractsAddressHistoryDeployed: IAddressDetails[] = JSON.parse(rawdata)
if (contractsAddressHistoryDeployed !== undefined && contractsAddressHistoryDeployed.length > 0) {
if (contractsAddressHistoryDeployed.find((c: IAddressDetails) => c[field] === value)) {
const contractsAddressDeployedHistoryFiltered = contractsAddressHistoryDeployed.filter(
(c: IAddressDetails) => c[field] !== value
)
fs.unlinkSync(fileContractsAddressDeployedHistory)
fs.unlinkSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory
)
const contractsAddressDeployedHistoryFilteredString =
contractsAddressDeployedHistoryFiltered.length > 0
? JSON.stringify(contractsAddressDeployedHistoryFiltered, null, 2)
: ''
try {
fs.writeFileSync(
fileContractsAddressDeployedHistory,
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory,
contractsAddressDeployedHistoryFilteredString
)
} catch (err) {
Expand Down
3 changes: 2 additions & 1 deletion src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Directories
- [githubWorkflows/](./githubWorkflows/) - [mockContracts/](./mockContracts/)

- [AwesomeAddressBook.ts](./AwesomeAddressBook.ts) - [buildEnv.ts](./buildEnv.ts) - [buildExcludedFile.ts](./buildExcludedFile.ts) - [buildFilesList.ts](./buildFilesList.ts) - [buildFoundrySetting.ts](./buildFoundrySetting.ts) - [buildMockContracts.ts](./buildMockContracts.ts) - [buildNetworks.ts](./buildNetworks.ts) - [buildWorkflows.ts](./buildWorkflows.ts) - [config.ts](./config.ts) - [index.ts](./index.ts) - [packageInstaller.ts](./packageInstaller.ts) - [serveInquirer.ts](./serveInquirer.ts) - [type-extensions.ts](./type-extensions.ts) - [types.ts](./types.ts) - [utils.ts](./utils.ts)
- [AwesomeAddressBook.ts](./AwesomeAddressBook.ts) - [buildEnv.ts](./buildEnv.ts) - [buildExcludedFile.ts](./buildExcludedFile.ts) - [buildFilesList.ts](./buildFilesList.ts) - [buildFoundrySetting.ts](./buildFoundrySetting.ts) - [buildMockContracts.ts](./buildMockContracts.ts) - [buildNetworks.ts](./buildNetworks.ts) - [buildWorkflows.ts](./buildWorkflows.ts) - [config.ts](./config.ts) - [index.ts](./index.ts) - [packageInstaller.ts](./packageInstaller.ts) - [README.md](./README.md) - [serveInquirer.ts](./serveInquirer.ts) - [type-extensions.ts](./type-extensions.ts) - [types.ts](./types.ts) - [utils.ts](./utils.ts)
## Directory Tree
[<- Previous](https://github.com/marc-aurele-besner/hardhat-awesome-cli.git)
src/
Expand All @@ -29,6 +29,7 @@ src/
│ config.ts/
│ index.ts/
│ packageInstaller.ts/
│ README.md/
│ serveInquirer.ts/
│ type-extensions.ts/
│ types.ts/
Expand Down
20 changes: 11 additions & 9 deletions src/buildEnv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'

import { fileEnvHardhatAwesomeCLI } from './config'
import { getAddressBookConfig } from './config'

const addEnvFileInGitiignore = async (ignoreFile: string, envFile: string, createIfNotExist: boolean) => {
if (fs.existsSync(ignoreFile)) {
Expand All @@ -21,8 +21,9 @@ const addEnvFileInGitiignore = async (ignoreFile: string, envFile: string, creat
}

export const getEnvValue = (envName: string) => {
if (fs.existsSync(fileEnvHardhatAwesomeCLI)) {
const allEnv = require('dotenv').config({ path: fileEnvHardhatAwesomeCLI })
const addressBookConfig = getAddressBookConfig()
if (fs.existsSync(addressBookConfig.fileEnvHardhatAwesomeCLI)) {
const allEnv = require('dotenv').config({ path: addressBookConfig.fileEnvHardhatAwesomeCLI })
const oldEnv = Object.entries(allEnv.parsed)
for (const [key, value] of oldEnv) {
if (key && value && key === envName) return getEnvValue
Expand All @@ -32,6 +33,7 @@ export const getEnvValue = (envName: string) => {
}

const writeToEnv = async (env: any, chainName: string, envToBuild: { rpcUrl: string; privateKeyOrMnemonic: any }) => {
const addressBookConfig = getAddressBookConfig(env.userConfig)
let isRpcUrl = false
let isPrivateKey = false
let isMnemonic = false
Expand Down Expand Up @@ -60,8 +62,8 @@ const writeToEnv = async (env: any, chainName: string, envToBuild: { rpcUrl: str
if (isPrivateKey) envToWrite = privateKeyEnv + '\n'
if (isMnemonic) envToWrite = mnemonicEnv + '\n'

if (fs.existsSync(fileEnvHardhatAwesomeCLI)) {
const allEnv = require('dotenv').config({ path: fileEnvHardhatAwesomeCLI })
if (fs.existsSync(addressBookConfig.fileEnvHardhatAwesomeCLI)) {
const allEnv = require('dotenv').config({ path: addressBookConfig.fileEnvHardhatAwesomeCLI })
const oldEnv = Object.entries(allEnv.parsed)
let newEnv = ''
const wipEnv: any = []
Expand Down Expand Up @@ -98,11 +100,11 @@ const writeToEnv = async (env: any, chainName: string, envToBuild: { rpcUrl: str
if (isRpcUrl && !isRpcUrlEnvExist) newEnv += rpcUrlEnv + '\n'
if (isPrivateKey && !isPrivateKeyEnvExist) newEnv += privateKeyEnv + '\n'
if (isMnemonic && !isMnemoniclEnvExist) newEnv += mnemonicEnv + '\n'
fs.writeFileSync(fileEnvHardhatAwesomeCLI, newEnv)
} else fs.writeFileSync(fileEnvHardhatAwesomeCLI, envToWrite)
fs.writeFileSync(addressBookConfig.fileEnvHardhatAwesomeCLI, newEnv)
} else fs.writeFileSync(addressBookConfig.fileEnvHardhatAwesomeCLI, envToWrite)
console.log('\x1b[32m%s\x1b[0m', 'Env file updated')
await addEnvFileInGitiignore('.gitignore', fileEnvHardhatAwesomeCLI, true)
await addEnvFileInGitiignore('.npmignore', fileEnvHardhatAwesomeCLI, false)
await addEnvFileInGitiignore('.gitignore', addressBookConfig.fileEnvHardhatAwesomeCLI, true)
await addEnvFileInGitiignore('.npmignore', addressBookConfig.fileEnvHardhatAwesomeCLI, false)
}

export default writeToEnv
Loading

0 comments on commit 15fb974

Please sign in to comment.