diff --git a/packages/cli/index.js b/packages/cli/index.js new file mode 100644 index 0000000..1e87ff1 --- /dev/null +++ b/packages/cli/index.js @@ -0,0 +1,50 @@ +import { stdin, stdout, exit } from 'process' +import os from 'os' +import pty from 'node-pty' + +stdin.setRawMode(true) +stdin.resume() +stdin.setEncoding('utf8') + +// Create a new pseudoterminal +const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash' +const ptyProcess = pty.spawn(shell, [], { + name: 'xterm-color', + cols: 80, + rows: 30, + cwd: process.env.HOME, + env: process.env, +}) + +// Listen for output data +ptyProcess.on('data', function (data) { + // Intercept and process the output here + stdout.write(data) +}) + +// Write a command to the pseudoterminal +ptyProcess.write('ls\r') + +let commandBuffer = '' + +stdin.on('data', (key) => { + if (key === '\u0004') { + exit() + } else if (key === '\r') { + // User pressed enter, log and execute the command + console.log('User command: ', commandBuffer) + commandBuffer = '' + } else if (key === '\u007F') { + // User pressed backspace, remove the last character from the buffer + commandBuffer = commandBuffer.slice(0, -1) + } else { + // Add the typed character to the command buffer + commandBuffer += key + } + ptyProcess.write(key) +}) + +// Listen for exit event +ptyProcess.on('exit', function (exitCode) { + console.log('Process exited with code: ', exitCode) +}) diff --git a/packages/cli/package.json b/packages/cli/package.json new file mode 100644 index 0000000..85f8223 --- /dev/null +++ b/packages/cli/package.json @@ -0,0 +1,10 @@ +{ + "name": "cli", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "type": "module", + "dependencies": { + "node-pty": "^1.0.0" + } +} diff --git a/yarn.lock b/yarn.lock index dc22e73..88291a4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1538,10 +1538,6 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== -commander@^11.0.0: - version "11.0.0" - resolved "https://registry.yarnpkg.com/commander/-/commander-11.0.0.tgz#43e19c25dbedc8256203538e8d7e9346877a6f67" - integrity sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ== combined-stream@^1.0.8: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" @@ -1549,6 +1545,11 @@ combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" +commander@^11.0.0: + version "11.0.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-11.0.0.tgz#43e19c25dbedc8256203538e8d7e9346877a6f67" + integrity sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ== + commander@^4.0.0: version "4.1.1" resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068" @@ -3611,6 +3612,13 @@ node-int64@^0.4.0: resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" integrity sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw== +node-pty@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-pty/-/node-pty-1.0.0.tgz#7daafc0aca1c4ca3de15c61330373af4af5861fd" + integrity sha512-wtBMWWS7dFZm/VgqElrTvtfMq4GzJ6+edFI0Y0zyzygUSZMgZdraDUMUhCIvkjhJjme15qWmbyJbtAx4ot4uZA== + dependencies: + nan "^2.17.0" + node-releases@^2.0.12: version "2.0.13" resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"