Created
September 18, 2023 11:49
-
-
Save darlanalves/3552e74ea738706b21f4618ce3bcb88d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
// generate a shell command from a text input | |
import { request } from 'node:https'; | |
import { exec } from 'node:child_process'; | |
import { createInterface } from 'node:readline/promises'; | |
import { stdin as input, stdout as output } from 'node:process'; | |
async function call() { | |
const task = process.argv.slice(2).join(' '); | |
const req = request("https://aifn.run/run/1e79c5e8-62f9-44f6-8391-e55b4eeae5c0", { method: "POST" }); | |
req.on('response', s => { | |
const a = []; | |
s.on('data', c => a.push(c)); | |
s.on('end', async () => { | |
const sh = Buffer.concat(a).toString('utf8'); | |
const rl = createInterface({ input, output }); | |
const answer = await rl.question(`> ${sh}\nRun it? [Y,n] `); | |
rl.close(); | |
if (answer.toLowerCase() === 'n') { | |
return; | |
} | |
const sub = exec(sh); | |
sub.stdout.pipe(output); | |
sub.stderr.pipe(output); | |
}); | |
}); | |
req.end(JSON.stringify({ inputs: { task } })); | |
} | |
call(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment