Skip to content

Commit

Permalink
Remove node-pty
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Apr 2, 2024
1 parent be5b01f commit 1286024
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
13 changes: 11 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/ethernaut-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"devDependencies": {
"fs-extra": "^11.2.0",
"mocha": "^10.4.0",
"node-pty": "^1.0.0",
"nyc": "^15.1.0",
"tree-kill": "^1.2.2",
"nyc-report-lcov-absolute": "^1.0.0"
},
"gitHead": "5ffea95c7b8186e6365da19d189a79f09a78d475"
Expand Down
70 changes: 35 additions & 35 deletions packages/ethernaut-common/src/test/terminal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const pty = require('node-pty')
const os = require('os')
const debug = require('ethernaut-common/src/ui/debug')
const assert = require('assert')
const chalk = require('chalk')
const wait = require('ethernaut-common/src/util/wait')
const { spawn } = require('child_process')
const kill = require('tree-kill')

// eslint-disable-next-line no-control-regex
const ansiEscapeCodesPattern = /\x1B\[[0-?]*[ -/]*[@-~]/g
Expand All @@ -21,44 +21,44 @@ class Terminal {
constructor() {
this.history = ''
this.output = ''
this.shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'
}

async run(command, delay = 10000) {
if (this.running) {
throw new Error('Terminal is already running a command')
}

this.process = pty.spawn(this.shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.cwd(),
env: process.env,
})
async run(command, delay = 10000, killAfter = false) {
const args = command.split(' ')
args.concat(['&&', 'sleep', '1', '&&', 'exit'])
const f = 'npx'
debug.log(`Running command: ${f} ${args.join(' ')}`, 'terminal')
this.process = spawn(f, args, { shell: true, stdio: 'pipe' })

if (this.listener) {
this.listener.dispose()
}

this.listener = this.process.onData((data) => {
const onData = (data) => {
const txt = this.stripAnsi(data.toString())
this.history += txt
this.output += txt
debug.log(`Terminal output: ${txt}`, 'terminal')
})

this.running = true
debug.log(`Running command: ${command}`, 'terminal')

const c = command.replace('hardhat', 'nyc hardhat')
this._write(`${c} && sleep 1 && exit\r`)

const completion = this._waitForCompletion()
const waitPromise = wait(delay)
await Promise.race([completion, waitPromise])
}
this.process.stdout.on('data', onData)

this.running = false
const onError = (data) => {
throw new Error(`Terminal error, ${data}`)
}
this.process.stderr.on('error', onError)

const completion = this._waitForCompletion().then(() => ({
type: 'completion',
}))
const waitPromise = wait(delay).then(() => ({ type: 'wait' }))
const result = await Promise.race([completion, waitPromise])
debug.log(`Terminal process ended with type: ${result.type}`, 'terminal')
if (result.type === 'wait') {
if (killAfter) {
this.process.stdout.removeListener('data', onData)
this.process.stderr.removeListener('data', onError)
kill(this.process.pid, 'SIGKILL', (err) => {
if (err) debug.log(`Unable to kill process: ${err}`, 'terminal')
else debug.log('Killed process', 'terminal')
})
}
}
}

async input(command, delay = 200) {
Expand All @@ -68,16 +68,16 @@ class Terminal {

_waitForCompletion() {
return new Promise((resolve) => {
this.process.onExit(() => {
debug.log('Command completed', 'terminal')
this.process.once('close', (code) => {
debug.log(`Command completed with code ${code}`, 'terminal')
resolve()
})
})
}

_write(content) {
this.output = ''
this.process.write(content)
this.process.stdin.write(content)
}

stripAnsi(inputString) {
Expand Down
7 changes: 6 additions & 1 deletion packages/ethernaut-network/test/tasks/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ describe('node', function () {
describe('when parameters are provided', function () {
describe('with none', function () {
before('run', async function () {
await terminal.run('hardhat network node --fork none --port 8547', 2000)
await terminal.run(
'hardhat network node --fork none --port 8547',
2000,
true,
)
})

it('starts a local chain', async function () {
Expand Down Expand Up @@ -47,6 +51,7 @@ describe('node', function () {
await terminal.run(
'hardhat network node --fork test__mainnet --port 8547',
5000,
true,
)
})

Expand Down

0 comments on commit 1286024

Please sign in to comment.