Skip to content

Commit

Permalink
Config for hiding tasks and scopes from navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Feb 28, 2024
1 parent 878b3d3 commit 01ea901
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 16 deletions.
16 changes: 16 additions & 0 deletions packages/ethernaut-cli/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ module.exports = {
solidity: '0.8.19',
defaultNetwork: 'localhost',
ethernaut: {
ui: {
exclude: {
scopes: ['vars', 'hardhat'],
tasks: [
'compile',
'check',
'clean',
'flatten',
'node',
'test',
'navigate',
'help',
'run',
],
},
},
ai: {
interpreter: {
additionalInstructions: [''],
Expand Down
16 changes: 15 additions & 1 deletion packages/ethernaut-ui/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { extendEnvironment } = require('hardhat/config')
const { extendEnvironment, extendConfig } = require('hardhat/config')
const requireAll = require('common/src/require-all')
const makeTasksInteractive = require('./internal/make-interactive')
const spinner = require('common/src/spinner')
Expand All @@ -14,3 +14,17 @@ extendEnvironment((hre) => {
// TODO: Hack further, or seek support from Nomic
// bundleLooseTasks(); // This is too hacky, need mods to hardhat
})

extendConfig((config, userConfig) => {
config = {
...config,
ethernaut: {
ui: {
exclude: {
scopes: userConfig.ethernaut?.ui?.exclude?.scopes || [],
tasks: userConfig.ethernaut?.ui?.exclude?.tasks || [],
},
},
},
}
})
16 changes: 11 additions & 5 deletions packages/ethernaut-ui/src/internal/navigate-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ const prompt = require('common/src/prompt')
const getNodes = require('common/src/get-nodes')
const chalk = require('chalk')

module.exports = async function navigateFrom(node) {
const children = getNodes(node).sort((a, b) => b.isScope - a.isScope)
module.exports = async function navigateFrom(node, hre) {
let children = getNodes(node).sort((a, b) => b.isScope - a.isScope)

const exclude = [
...hre.config.ethernaut.ui.exclude.scopes,
...hre.config.ethernaut.ui.exclude.tasks,
]
children = children.filter((node) => !exclude.includes(node.name))

const choices = children.map((node) => {
// Cap desciption to 1 sentence and 150 characters
Expand Down Expand Up @@ -32,18 +38,18 @@ module.exports = async function navigateFrom(node) {
})

if (response === upTitle) {
await navigateFrom(hre)
await navigateFrom(hre, hre)
}

const nextLocation = children.find((node) => response.includes(node.name))

if (nextLocation.isScope) {
await navigateFrom(nextLocation)
await navigateFrom(nextLocation, hre)
} else {
await hre.run({ task: nextLocation.name, scope: nextLocation.scope })

// When running a task from navigation
// return to navigation after the task is done
await navigateFrom(node)
await navigateFrom(node, hre)
}
}
4 changes: 2 additions & 2 deletions packages/ethernaut-ui/src/tasks/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ task('help', 'Jumps into the help navigator').setAction(

if (process.argv.length >= 3) {
const scope = process.argv[2]
await navigateFrom(hre.scopes[scope] || hre)
await navigateFrom(hre.scopes[scope] || hre, hre)
} else {
await navigateFrom(hre)
await navigateFrom(hre, hre)
}
} catch (err) {
output.errorBox(err)
Expand Down
2 changes: 1 addition & 1 deletion packages/ethernaut-ui/src/tasks/navigate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ task('navigate', 'Navigates tasks with enquirer')
.addOptionalPositionalParam('scope', 'The group of tasks to navigate')
.setAction(async ({ scope }) => {
try {
await navigateFrom(hre.scopes[scope] || hre)
await navigateFrom(hre.scopes[scope] || hre, hre)
} catch (err) {
output.errorBox(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ module.exports = {
url: 'http://localhost:8545',
},
},
ethernaut: {
ui: {
exclude: {
scopes: ['vars', 'hardhat'],
tasks: ['compile'],
},
},
},
}
4 changes: 4 additions & 0 deletions packages/ethernaut-ui/test/tasks/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe('help', function () {
it('displays the util scope', async function () {
terminal.has('[util]')
})

it('does not display the vars scope', async function () {
terminal.notHas('[vars]')
})
})

describe('when entering the cli with the --help option or task', function () {
Expand Down
12 changes: 5 additions & 7 deletions packages/ethernaut-ui/test/tasks/navigate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ describe('navigate', function () {
)
})

it('displays the compile task', async function () {
it('displays the console task', async function () {
assert.equal(
findLineWith('compile', terminal.output),
'Compiles the entire project, building all artifacts',
findLineWith('console', terminal.output),
'Opens a hardhat console',
)
})

describe('when using the arrow keys to navigate to the util package', function () {
before('interact', async function () {
await terminal.input(keys.DOWN, 100)
await terminal.input(keys.DOWN, 100)
await terminal.input(keys.ENTER, 1000)
await terminal.input('util\r', 1000)
})

it('shows that util was picked', async function () {
Expand Down Expand Up @@ -104,7 +102,7 @@ describe('navigate', function () {

describe('when selecting the unit util', function () {
before('press enter', async function () {
await terminal.input(keys.ENTER, 500)
await terminal.input(keys.ENTER, 1000)
})

it('displays a prompt for entering the value to convert', async function () {
Expand Down

0 comments on commit 01ea901

Please sign in to comment.