0

I'm working on a Node.js file to automatically create a new Github repo.

When I run curl -u 'BretCameron' https://api.github.com/user/repos -d '{"name":"test_repo"}' in the terminal, it asks me for my GitHub password and then creates the new repo.

But I can't get this to work in Node, even though the git commands in my sample code are working fine:

const { exec, execSync } = require('child_process');

function run(func) {
 console.log(execSync(func).toString())
}

run('touch README.md');
run('git init');
run('git add README.md');
run('git commit -m "First commit"');
exec(`curl -u 'BretCameron' https://api.github.com/user/repos -d '{"name":"test_repo"}'`));

I have tried with the exec and execSync functions from the child_process module, as well as putting it into my helper function run. Can anyone offer some advice?

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
Bret Cameron
  • 451
  • 1
  • 4
  • 18
  • 1
    Why would you need to? You can make requests from Node directly. – jonrsharpe Jun 20 '19 at 19:59
  • Ultimately, I want to put this into a `.npm-init.js` file, so that a GitHub repository is created automatically when running `npm init` – Bret Cameron Jun 20 '19 at 20:01
  • Do you want it to actually prompt for the credentials interactively? That seems like a problematic thing to run automatically. – Daniel Pryden Jun 20 '19 at 20:02
  • If possible, yes! – Bret Cameron Jun 20 '19 at 20:03
  • How do you know that wherever you're running `npm init` from actually has an interactive terminal attached? What do you want to have happen if it doesn't have a terminal? What about if it has a stdin pipe, but not an attached tty/pty and so it can't set the terminal into noecho mode? – Daniel Pryden Jun 20 '19 at 20:08

0 Answers0