Created
April 11, 2024 07:21
-
-
Save RealYukiSan/e236f2798d5df04ed2dfbef35681a2e2 to your computer and use it in GitHub Desktop.
use sh inside javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('node:child_process') | |
const readline = require('node:readline'); | |
const { stdin: input, stdout: output } = require('node:process'); | |
const rl = readline.createInterface({ input, output }); | |
const shell = spawn('sh') | |
shell.stdout.on('data', (data) => { | |
process.stdout.write(data.toString()) | |
init() | |
}); | |
function init() { | |
rl.question('Enter command > ', (cmd) => { | |
if (cmd == 'quit') { | |
rl.close(); | |
shell.kill(); | |
} | |
else { | |
shell.stdin.write(`${cmd}\n`); | |
} | |
}); | |
} | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
read the docs: