forked from freeall/create-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·72 lines (59 loc) · 1.61 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
var ghauth = require('ghauth');
var GH = require('github');
var path = require('path');
var minimist = require('minimist');
var exec = require('child_process').exec;
var github = new GH({
version:'3.0.0',
headers: {
'User-Agent': 'create-repository'
}
});
var pkg = {};
try { pkg = require(path.resolve('package.json'));}
catch(err) {}
var argv = minimist(process.argv, {
string: ['name', 'description'],
alias: {
name: 'n',
description: 'd'
},
default: {
name: pkg.name,
description: pkg.description
}
});
exec('git status', function(err, stdout, stderr) {
if (stderr.indexOf('Not a git repository') > -1) return console.error('Not a git folder. Maybe you forgot to \'git init\'');
ghauth({
configName: 'create-repository',
scopes: ['repo']
}, function(err, auth) {
if (err) throw err;
github.authenticate({
type: 'oauth',
token: auth.token
});
github.repos.create({
name: argv.name,
description: argv.description
}, function(err, res) {
if (err) {
if (err.message[0] !== '{') throw err; // not json
err = JSON.parse(err.message);
console.error(err.errors[0].message);
process.exit(1);
}
exec('git remote', function(err, stdout, stderr) {
if (err) throw err;
console.log('Repository created https://github.com/'+auth.user+'/'+argv.name);
if (stdout.indexOf('origin') > -1) return;
exec('git remote add origin git@github.com:'+auth.user+'/'+argv.name+'.git', function(err, stdout, stderr) {
if (err) throw err;
console.log('Added origin. You might want to: git push -u origin master');
});
});
});
});
});