-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmd.js
executable file
·179 lines (160 loc) · 4.31 KB
/
cmd.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
const Promise = require('bluebird')
const argv = require('minimist')(process.argv.slice(2))
const chalk = require('chalk')
const github = require('./lib/github')
const open = require('opn')
const path = require('path')
const noop = require('no-op')
const baseName = require('require-package-name').base
const githubUrl = require('github-url-to-object')
const publish = Promise.promisify(github.publish)
const auth = Promise.promisify(github.auth)
const config = Promise.promisify(require('./lib/config'))
const gitCommit = Promise.promisify(require('./lib/commit'))
const confirm = Promise.promisify(require('./lib/confirm'))
const loadPackage = Promise.promisify(require('./lib/package'))
//get organization
var org = argv.o || argv.org
if (org && typeof org !== 'string') {
console.error("No --org specified")
process.exit(1)
}
if (argv.v || argv.version) {
var version = require('./package.json').version
console.log(version)
return
}
if (argv.help) {
require('fs').createReadStream(path.join(__dirname, 'lib', 'help.txt'))
.pipe(process.stdout)
return
}
var dryRun = argv['dry-run']
if (dryRun) {
console.error(chalk.grey("(dry run)"))
start()
.then(function(result) {
console.log(JSON.stringify(result))
})
.catch(err())
} else {
start()
.then(publish)
.then(commit)
.then(argv.open !== false ? open : noop, err())
.catch(err())
}
function start() {
return getOpts().then(request)
}
function request(opt) {
var pkg = opt.package
var name = argv.n || argv.name || baseName(pkg.name)
var description = argv.d || argv.description || pkg.description || ''
var homepage = argv.h || argv.homepage || pkg.homepage || ''
if (!name) {
console.error("No name in package.json")
process.exit(1)
}
if (homepage && homepage.indexOf('https://github.com/') === 0)
homepage = ''
var user = opt.org
// try to glean default username from package.json repository URL
if (!user && pkg.repository && pkg.repository.url) {
var urlObj = githubUrl(pkg.repository.url)
if (urlObj) {
user = urlObj.user
}
}
user = user || opt.auth.user
var org = user === opt.auth.user ? undefined : user
var repo = [user, name].join('/')
var url = 'https://github.com/' + repo + '.git'
repo = chalk.magenta(repo)
var info = 'Publish new repo as ' + repo + '?'
var data = {
org: org,
name: name,
description: description,
homepage: homepage,
private: argv.p || argv.private,
team_id: argv.team
}
if (dryRun) {
return data
}
return confirm(info)
.then(function() {
return data
}, function() {
// user exited early
process.exit(0)
})
}
//commits current working dir, resolves to html_url
function commit(result) {
var url = result.html_url
//user opted not to commit anything
if (argv.b || argv.bare) {
return Promise.resolve(url)
}
return getMessage().then(function(message) {
return gitCommit({
message: message,
url: url + '.git'
}).catch(function() {
console.warn(chalk.dim("git commands ignored"))
return Promise.resolve(url)
}).then(function() {
return url
})
})
}
function getMessage() {
var msg = argv.m || argv.message
if (msg)
return Promise.resolve(msg)
var def = 'first commit'
//try getting it from config
return config().then(function(conf) {
return conf.get('init.ghrepo.message') || def
}, function(err) {
//default
return Promise.resolve(def)
})
}
function getOpts() {
return auth({
configName: 'ghrepo',
note: 'ghrepo - repo creation tool',
scopes: ['user', 'repo']
})
.then(function(auth) {
return [ auth, getPackage() ]
}, err('Could not authenticate'))
.spread(function(auth, pkg) {
return {
package: pkg,
org: org,
auth: auth
}
}, err('Error reading package.json'))
}
function getPackage() {
return loadPackage()
.then(null, function(err) {
console.warn(chalk.bgYellow("WARN"), chalk.magenta("could not open package.json"))
console.warn(chalk.dim(err.message))
return Promise.resolve({
name: path.basename(process.cwd())
})
})
}
function err(msg) {
msg = msg||''
return function(err) {
console.error([msg, err.message].join(' ').trim())
process.exit(1)
}
}