diff --git a/.changeset/moody-oranges-report.md b/.changeset/moody-oranges-report.md new file mode 100644 index 000000000..1c26f969c --- /dev/null +++ b/.changeset/moody-oranges-report.md @@ -0,0 +1,5 @@ +--- +'@chugsplash/plugins': patch +--- + +Improve the hardhat node task diff --git a/packages/plugins/src/env.ts b/packages/plugins/src/env.ts index 7d43ffaae..9c501c59f 100644 --- a/packages/plugins/src/env.ts +++ b/packages/plugins/src/env.ts @@ -1,9 +1,27 @@ /** - * Removes all command line args in the `process.argv` array that begin with '--'. This is necessary - * to prevent an error that occurs when running the executor from within a Hardhat plugin task. This - * error occurs because the BaseServiceV2 (inherited by the executor) parses these command line - * arguments and throws an error when it sees arguments that it does not recognize. + * Removes command line args in the `process.argv` array beginning with the first argument that + * starts with '--'. This is necessary to prevent an error that occurs when running the executor + * from within a Hardhat plugin task. This error occurs because the BaseServiceV2 (inherited by the + * executor) parses these command line arguments and throws an error when it sees an unrecognized + * argument that begins with '--'. + * + * @returns An array containing the removed command line args. */ -export const removeFlagsFromCommandLineArgs = (): void => { - process.argv = process.argv.filter((arg) => !arg.startsWith('--')) +export const removeFlagsFromCommandLineArgs = (): string[] => { + const indexToRemove = process.argv.findIndex((arg) => arg.startsWith('--')) + if (indexToRemove === -1) { + return [] + } + const removed = process.argv.slice(indexToRemove) + process.argv = process.argv.slice(0, indexToRemove) + return removed +} + +/** + * Adds the given array of arguments to `process.argv`. + * + * @param args The command line arguments to add. + */ +export const addCommandLineArgs = (args: string[]) => { + process.argv = process.argv.concat(args) } diff --git a/packages/plugins/src/executor.ts b/packages/plugins/src/executor.ts new file mode 100644 index 000000000..7338cf2ec --- /dev/null +++ b/packages/plugins/src/executor.ts @@ -0,0 +1,18 @@ +import { ChugSplashExecutor } from '@chugsplash/executor' + +import { addCommandLineArgs, removeFlagsFromCommandLineArgs } from './env' + +export const instantiateExecutor = (): ChugSplashExecutor => { + // We must remove the command line arguments that begin with '--' from the process.argv array, + // or else the BaseServiceV2 (inherited by the executor) will throw an error when we instantiate + // it. + const removed = removeFlagsFromCommandLineArgs() + + // Instantiate the executor. + const executor = new ChugSplashExecutor() + + // Add the command line args back to the array. + addCommandLineArgs(removed) + + return executor +} diff --git a/packages/plugins/src/hardhat/deployments.ts b/packages/plugins/src/hardhat/deployments.ts index ac1a7a29f..969fa90ff 100644 --- a/packages/plugins/src/hardhat/deployments.ts +++ b/packages/plugins/src/hardhat/deployments.ts @@ -34,7 +34,7 @@ import { monitorTask, TASK_CHUGSPLASH_VERIFY_BUNDLE, } from './tasks' -import { removeFlagsFromCommandLineArgs } from '../env' +import { instantiateExecutor } from '../executor' /** * TODO @@ -46,17 +46,15 @@ export const deployAllChugSplashConfigs = async ( hre: any, silent: boolean, ipfsUrl: string, - noCompile: boolean + noCompile: boolean, + spinner: ora.Ora = ora({ isSilent: true }) ) => { const remoteExecution = (await getChainId(hre.ethers.provider)) !== 31337 const fileNames = fs.readdirSync(hre.config.paths.chugsplash) let executor: ChugSplashExecutor if (!remoteExecution) { - // We must remove the command line arguments that begin with '--' from the process.argv array, - // or else the BaseServiceV2 (inherited by the executor) will throw an error. - removeFlagsFromCommandLineArgs() - executor = new ChugSplashExecutor() + executor = instantiateExecutor() } for (const fileName of fileNames) { @@ -73,7 +71,8 @@ export const deployAllChugSplashConfigs = async ( remoteExecution, ipfsUrl, noCompile, - executor + executor, + spinner ) } } @@ -106,7 +105,7 @@ export const deployChugSplashConfig = async ( parsedConfig.options.projectName ) - spinner.succeed('Parsed ChugSplash config file.') + spinner.succeed(`Parsed ${parsedConfig.options.projectName}.`) if (projectPreviouslyRegistered === false) { spinner.start(`Registering ${parsedConfig.options.projectName}...`) @@ -228,7 +227,7 @@ export const deployChugSplashConfig = async ( { privateKey: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', - logLevel: silent ? 'info' : 'error', + logLevel: 'error', }, provider, canonicalConfig diff --git a/packages/plugins/src/hardhat/tasks.ts b/packages/plugins/src/hardhat/tasks.ts index 3179c76fa..d3b2605e8 100644 --- a/packages/plugins/src/hardhat/tasks.ts +++ b/packages/plugins/src/hardhat/tasks.ts @@ -66,7 +66,7 @@ import { successfulProposalMessage, } from '../messages' import { monitorRemoteExecution, postExecutionActions } from './execution' -import { removeFlagsFromCommandLineArgs } from '../env' +import { instantiateExecutor } from '../executor' // Load environment variables from .env dotenv.config() @@ -183,10 +183,7 @@ export const chugsplashDeployTask = async ( let executor: ChugSplashExecutor if (!remoteExecution) { - // We must remove the command line arguments that begin with '--' from the process.argv array, - // or else the BaseServiceV2 (inherited by the executor) will throw an error. - removeFlagsFromCommandLineArgs() - executor = new ChugSplashExecutor() + executor = instantiateExecutor() } spinner.succeed('ChugSplash is ready to go.') @@ -280,7 +277,6 @@ export const chugsplashProposeTask = async ( const signer = provider.getSigner() const spinner = ora({ isSilent: silent }) - spinner.start('Booting up ChugSplash...') await deployChugSplashPredeploys(provider, provider.getSigner()) @@ -960,12 +956,8 @@ task(TASK_CHUGSPLASH_FUND) .addPositionalParam('configPath', 'Path to the ChugSplash config file') .setAction(chugsplashFundTask) -// TODO: change 'any' type task(TASK_NODE) - .addFlag( - 'setupInternals', - 'Setup the internal ChugSplash contracts. Skip executing all contracts defined in ChugSplash config files.' - ) + .addFlag('deployAll', 'Deploy all ChugSplash config files on startup') .addFlag( 'disableChugsplash', "Completely disable all of ChugSplash's activity." @@ -975,7 +967,7 @@ task(TASK_NODE) .setAction( async ( args: { - setupInternals: boolean + deployAll: boolean disableChugsplash: boolean hide: boolean noCompile: boolean @@ -983,14 +975,24 @@ task(TASK_NODE) hre: HardhatRuntimeEnvironment, runSuper ) => { - const { setupInternals, disableChugsplash, hide, noCompile } = args + const { deployAll, disableChugsplash, hide, noCompile } = args + if (!disableChugsplash) { + const spinner = ora({ isSilent: hide }) + spinner.start('Booting up ChugSplash...') + await deployChugSplashPredeploys( hre.ethers.provider, hre.ethers.provider.getSigner() ) - if (!setupInternals) { - await deployAllChugSplashConfigs(hre, hide, '', noCompile) + + spinner.succeed('ChugSplash has been initialized.') + + if (deployAll) { + if (!noCompile) { + await cleanThenCompile(hre) + } + await deployAllChugSplashConfigs(hre, hide, '', true, spinner) } await writeHardhatSnapshotId(hre) } @@ -1024,7 +1026,10 @@ task(TASK_TEST) hre.ethers.provider, hre.ethers.provider.getSigner() ) - await deployAllChugSplashConfigs(hre, !show, '', noCompile) + if (!noCompile) { + await cleanThenCompile(hre) + } + await deployAllChugSplashConfigs(hre, !show, '', true) } finally { await writeHardhatSnapshotId(hre) } @@ -1035,25 +1040,28 @@ task(TASK_TEST) task(TASK_RUN) .addFlag( - 'enableChugsplash', + 'deployAll', 'Deploy all ChugSplash configs before executing your script.' ) .setAction( async ( args: { - enableChugsplash: boolean + deployAll: boolean noCompile: boolean }, hre: any, runSuper ) => { - const { enableChugsplash, noCompile } = args - if (enableChugsplash) { + const { deployAll, noCompile } = args + if (deployAll) { await deployChugSplashPredeploys( hre.ethers.provider, hre.ethers.provider.getSigner() ) - await deployAllChugSplashConfigs(hre, true, '', noCompile) + if (!noCompile) { + await cleanThenCompile(hre) + } + await deployAllChugSplashConfigs(hre, true, '', true) } await runSuper(args) }