From a6aaf74064efb0968480648a4ab333520858da17 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Sat, 2 Mar 2024 16:11:33 -0300 Subject: [PATCH] Progress with network autocomplete port --- .github/workflows/ci.yml | 17 +++++++++++++ .../src/autocomplete/activate.js | 25 ------------------- .../src/autocomplete/params}/alias.js | 6 ++--- .../src/autocomplete/params}/fork.js | 2 +- .../autocomplete/{add.js => params/url.js} | 8 +----- .../src/autocomplete/tasks/activate.js | 7 ++++++ .../src/autocomplete/tasks/add.js | 5 ++++ .../src/autocomplete/tasks/edit.js | 6 +++++ .../src/autocomplete/tasks/info.js | 6 +++++ .../src/autocomplete/tasks/node.js | 5 ++++ .../src/autocomplete/tasks/remove.js | 7 ++++++ packages/ethernaut-network-ui/src/index.js | 12 ++++----- packages/ethernaut-network/src/tasks/edit.js | 6 +---- packages/ethernaut-network/src/tasks/info.js | 6 +---- packages/ethernaut-network/src/tasks/node.js | 6 ++--- .../ethernaut-network/src/tasks/remove.js | 6 +---- .../src/autocomplete/{ => tasks}/unit.js | 0 packages/ethernaut-util-ui/src/index.js | 2 +- 18 files changed, 69 insertions(+), 63 deletions(-) delete mode 100644 packages/ethernaut-network-ui/src/autocomplete/activate.js rename packages/{ethernaut-network/src/tasks/autocomplete => ethernaut-network-ui/src/autocomplete/params}/alias.js (71%) rename packages/{ethernaut-network/src/tasks/autocomplete => ethernaut-network-ui/src/autocomplete/params}/fork.js (89%) rename packages/ethernaut-network-ui/src/autocomplete/{add.js => params/url.js} (85%) create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/activate.js create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/add.js create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/edit.js create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/info.js create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/node.js create mode 100644 packages/ethernaut-network-ui/src/autocomplete/tasks/remove.js rename packages/ethernaut-util-ui/src/autocomplete/{ => tasks}/unit.js (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 08cd854a..44b65237 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,6 +157,23 @@ jobs: - run: npm run build --if-present - run: npm run compile --if-present - run: cd packages/ethernaut-network && npm t + ethernaut-network-ui: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [20.x] + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + - name: Install Foundry + uses: foundry-rs/foundry-toolchain@v1 + - run: npm ci + - run: npm run build --if-present + - run: npm run compile --if-present + - run: cd packages/ethernaut-network-ui && npm t ethernaut-wallet: runs-on: ubuntu-latest strategy: diff --git a/packages/ethernaut-network-ui/src/autocomplete/activate.js b/packages/ethernaut-network-ui/src/autocomplete/activate.js deleted file mode 100644 index 1560b282..00000000 --- a/packages/ethernaut-network-ui/src/autocomplete/activate.js +++ /dev/null @@ -1,25 +0,0 @@ -const prompt = require('common/src/prompt') -const storage = require('ethernaut-network/src/internal/storage') - -module.exports = function setup(hre) { - const activate = hre.scopes.network.tasks.activate - - activate.positionalParamDefinitions.find( - (p) => p.name === 'alias', - ).autocomplete = autocompleteAlias('Select a network to activate') -} - -function autocompleteAlias(message = 'Select a network') { - return async function autocompleteAlias({ alias }) { - if (alias) return undefined - const choices = Object.keys(storage.readNetworks()).filter( - (alias) => alias !== 'activeNetwork', - ) - return await prompt({ - type: 'autocomplete', - message, - limit: 15, - choices, - }) - } -} diff --git a/packages/ethernaut-network/src/tasks/autocomplete/alias.js b/packages/ethernaut-network-ui/src/autocomplete/params/alias.js similarity index 71% rename from packages/ethernaut-network/src/tasks/autocomplete/alias.js rename to packages/ethernaut-network-ui/src/autocomplete/params/alias.js index 7b66c230..fef009ab 100644 --- a/packages/ethernaut-network/src/tasks/autocomplete/alias.js +++ b/packages/ethernaut-network-ui/src/autocomplete/params/alias.js @@ -1,14 +1,12 @@ const prompt = require('common/src/prompt') -const storage = require('../../internal/storage') +const storage = require('ethernaut-network/src/internal/storage') -module.exports = (message = 'Select a network') => { +module.exports = function autocompleteAlias(message = 'Select a network') { return async function autocompleteAlias({ alias }) { if (alias) return undefined - const choices = Object.keys(storage.readNetworks()).filter( (alias) => alias !== 'activeNetwork', ) - return await prompt({ type: 'autocomplete', message, diff --git a/packages/ethernaut-network/src/tasks/autocomplete/fork.js b/packages/ethernaut-network-ui/src/autocomplete/params/fork.js similarity index 89% rename from packages/ethernaut-network/src/tasks/autocomplete/fork.js rename to packages/ethernaut-network-ui/src/autocomplete/params/fork.js index 6f6326ff..b6853538 100644 --- a/packages/ethernaut-network/src/tasks/autocomplete/fork.js +++ b/packages/ethernaut-network-ui/src/autocomplete/params/fork.js @@ -1,5 +1,5 @@ const prompt = require('common/src/prompt') -const storage = require('../../internal/storage') +const storage = require('ethernaut-network/src/internal/storage') module.exports = async function autocompleteFork({ fork, paramDefault }) { const valueProvided = fork !== undefined diff --git a/packages/ethernaut-network-ui/src/autocomplete/add.js b/packages/ethernaut-network-ui/src/autocomplete/params/url.js similarity index 85% rename from packages/ethernaut-network-ui/src/autocomplete/add.js rename to packages/ethernaut-network-ui/src/autocomplete/params/url.js index 5aa2ef0b..3f8ff65f 100644 --- a/packages/ethernaut-network-ui/src/autocomplete/add.js +++ b/packages/ethernaut-network-ui/src/autocomplete/params/url.js @@ -6,13 +6,7 @@ const strategies = { MANUAL: 'Enter url manually', } -module.exports = function setup(hre) { - const add = hre.scopes.network.tasks.add - - add.paramDefinitions.url.autocomplete = autocompleteUrl -} - -async function autocompleteUrl({ url }) { +module.exports = async function autocompleteUrl({ url }) { if (url) return undefined const choice = await selectStrategy() diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/activate.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/activate.js new file mode 100644 index 00000000..9f18c55a --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/activate.js @@ -0,0 +1,7 @@ +module.exports = function setup(hre) { + const activate = hre.scopes.network.tasks.activate + + activate.positionalParamDefinitions.find( + (p) => p.name === 'alias', + ).autocomplete = require('../params/alias')('Select a network to activate') +} diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/add.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/add.js new file mode 100644 index 00000000..c4c50168 --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/add.js @@ -0,0 +1,5 @@ +module.exports = function setup(hre) { + const add = hre.scopes.network.tasks.add + + add.paramDefinitions.url.autocomplete = require('../params/url') +} diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/edit.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/edit.js new file mode 100644 index 00000000..80d8c0fb --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/edit.js @@ -0,0 +1,6 @@ +module.exports = function setup(hre) { + const edit = hre.scopes.network.tasks.edit + + edit.positionalParamDefinitions.find((p) => p.name === 'alias').autocomplete = + require('../params/alias')('Select a network to edit') +} diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/info.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/info.js new file mode 100644 index 00000000..3bb80891 --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/info.js @@ -0,0 +1,6 @@ +module.exports = function setup(hre) { + const info = hre.scopes.network.tasks.info + + info.positionalParamDefinitions.find((p) => p.name === 'alias').autocomplete = + require('../params/alias')('Select a network') +} diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/node.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/node.js new file mode 100644 index 00000000..4baf377c --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/node.js @@ -0,0 +1,5 @@ +module.exports = function setup(hre) { + const node = hre.scopes.network.tasks.node + + node.paramDefinitions.fork.autocomplete = require('../params/fork') +} diff --git a/packages/ethernaut-network-ui/src/autocomplete/tasks/remove.js b/packages/ethernaut-network-ui/src/autocomplete/tasks/remove.js new file mode 100644 index 00000000..61a317e0 --- /dev/null +++ b/packages/ethernaut-network-ui/src/autocomplete/tasks/remove.js @@ -0,0 +1,7 @@ +module.exports = function setup(hre) { + const remove = hre.scopes.network.tasks.remove + + remove.positionalParamDefinitions.find( + (p) => p.name === 'alias', + ).autocomplete = require('../params/alias')('Select a network') +} diff --git a/packages/ethernaut-network-ui/src/index.js b/packages/ethernaut-network-ui/src/index.js index 1b325ce8..e39060ef 100644 --- a/packages/ethernaut-network-ui/src/index.js +++ b/packages/ethernaut-network-ui/src/index.js @@ -4,10 +4,10 @@ require('ethernaut-ui/src/index') require('ethernaut-network/src/index') extendEnvironment((hre) => { - require('./autocomplete/activate')(hre) - require('./autocomplete/add')(hre) - // require('./autocomplete/edit')(hre) - // require('./autocomplete/info')(hre) - // require('./autocomplete/node')(hre) - // require('./autocomplete/remove')(hre) + require('./autocomplete/tasks/activate')(hre) + require('./autocomplete/tasks/add')(hre) + require('./autocomplete/tasks/edit')(hre) + require('./autocomplete/tasks/info')(hre) + require('./autocomplete/tasks/node')(hre) + require('./autocomplete/tasks/remove')(hre) }) diff --git a/packages/ethernaut-network/src/tasks/edit.js b/packages/ethernaut-network/src/tasks/edit.js index b4378100..59442609 100644 --- a/packages/ethernaut-network/src/tasks/edit.js +++ b/packages/ethernaut-network/src/tasks/edit.js @@ -1,9 +1,8 @@ const { types } = require('hardhat/config') const output = require('common/src/output') -const autocompleteAlias = require('./autocomplete/alias') const storage = require('../internal/storage') -const edit = require('../scopes/network') +require('../scopes/network') .task('edit', 'Edits a network') .addPositionalParam( 'alias', @@ -29,6 +28,3 @@ const edit = require('../scopes/network') return output.errorBox(err) } }) - -edit.positionalParamDefinitions.find((p) => p.name === 'alias').autocomplete = - autocompleteAlias('Select a network to edit') diff --git a/packages/ethernaut-network/src/tasks/info.js b/packages/ethernaut-network/src/tasks/info.js index 25fef80b..8cdf6655 100644 --- a/packages/ethernaut-network/src/tasks/info.js +++ b/packages/ethernaut-network/src/tasks/info.js @@ -3,9 +3,8 @@ const output = require('common/src/output') const storage = require('../internal/storage') const { chains } = require('common/src/chains') const { isUrl } = require('common/src/url') -const autocompleteAlias = require('./autocomplete/alias') -const info = require('../scopes/network') +require('../scopes/network') .task('info', 'Provides information about a network') .addPositionalParam( 'alias', @@ -77,6 +76,3 @@ async function populateRemoteChainInfo(info, hre) { return info } - -info.positionalParamDefinitions.find((p) => p.name === 'alias').autocomplete = - autocompleteAlias('Select a network') diff --git a/packages/ethernaut-network/src/tasks/node.js b/packages/ethernaut-network/src/tasks/node.js index f312040e..71987d2e 100644 --- a/packages/ethernaut-network/src/tasks/node.js +++ b/packages/ethernaut-network/src/tasks/node.js @@ -3,10 +3,10 @@ const output = require('common/src/output') const { execSync } = require('child_process') const { isUrl } = require('common/src/url') const storage = require('../internal/storage') -const autocompleteFork = require('./autocomplete/fork') +// const autocompleteFork = require('./autocomplete/fork') const applyEnvVars = require('../internal/apply-env-vars') -const local = require('../scopes/network') +require('../scopes/network') .task('node', 'Starts a local development chain, potentially with a fork.') .addOptionalParam( 'fork', @@ -70,5 +70,3 @@ function startAnvil(forkUrl, port) { execSync(`anvil --fork-url ${forkUrl} --port ${port}`, { stdio: 'inherit' }) } } - -local.paramDefinitions.fork.autocomplete = autocompleteFork diff --git a/packages/ethernaut-network/src/tasks/remove.js b/packages/ethernaut-network/src/tasks/remove.js index 91b72deb..326f74ec 100644 --- a/packages/ethernaut-network/src/tasks/remove.js +++ b/packages/ethernaut-network/src/tasks/remove.js @@ -1,9 +1,8 @@ const { types } = require('hardhat/config') const output = require('common/src/output') -const autocompleteAlias = require('./autocomplete/alias') const storage = require('../internal/storage') -const remove = require('../scopes/network') +require('../scopes/network') .task('remove', 'Removes a network from the cli') .addPositionalParam( 'alias', @@ -32,6 +31,3 @@ const remove = require('../scopes/network') return output.errorBox(err) } }) - -remove.positionalParamDefinitions.find((p) => p.name === 'alias').autocomplete = - autocompleteAlias('Select a network to remove') diff --git a/packages/ethernaut-util-ui/src/autocomplete/unit.js b/packages/ethernaut-util-ui/src/autocomplete/tasks/unit.js similarity index 100% rename from packages/ethernaut-util-ui/src/autocomplete/unit.js rename to packages/ethernaut-util-ui/src/autocomplete/tasks/unit.js diff --git a/packages/ethernaut-util-ui/src/index.js b/packages/ethernaut-util-ui/src/index.js index 0ccf5963..effc8ef7 100644 --- a/packages/ethernaut-util-ui/src/index.js +++ b/packages/ethernaut-util-ui/src/index.js @@ -4,5 +4,5 @@ require('ethernaut-ui/src/index') require('ethernaut-util/src/index') extendEnvironment((hre) => { - require('./autocomplete/unit')(hre) + require('./autocomplete/tasks/unit')(hre) })