/
index.js
41 lines (36 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env node
const meow = require('meow')
const parse = require('parse-git-config')
const hostedGitInfo = require("hosted-git-info")
const simpleGit = require('simple-git')
const cli = meow(`
Usage
$ git-remote-to-ssh <input>
Options
-r, --remote Specify a different remote
Examples
$ git-remote-to-ssh
Remote origin: git@github.com:RichardLitt/git-remote-to-ssh.git
$ git-remote-to-ssh -r test
Remote test: git@github.com:RichardLitt/whatever.git
`, {
alias: {
r: 'remote'
}
})
const opts = {
remote: cli.flags.remote || 'origin'
}
// Get the .gitconfig file as a JSON object
var config = parse.sync();
// Show me the remote
var oldURL = config[`remote "${opts.remote}"`].url
// TODO What if the remote is bogus?
var info = hostedGitInfo.fromUrl(oldURL)
// Convert to ssh
var newURL = info.ssh()
// Set the URL using git
simpleGit().remote(['set-url', opts.remote, newURL], (err, res) => {
if (err) console.log('All hell broke loose ' + err)
console.log(`Remote ${opts.remote}: ${newURL}`)
})