1 | const url = require('url')
|
2 |
|
3 | const gitHead = require('git-head')
|
4 | const GitHubApi = require('github')
|
5 | const parseSlug = require('parse-github-repo-url')
|
6 | const through = require('through2');
|
7 |
|
8 | module.exports = function (config, cb) {
|
9 | const pkg = config.pkg
|
10 | const options = config.options
|
11 | const ghConfig = options.githubUrl ? url.parse(options.githubUrl) : {}
|
12 |
|
13 | const github = new GitHubApi({
|
14 | version: '3.0.0',
|
15 | port: ghConfig.port,
|
16 | protocol: (ghConfig.protocol || '').split(':')[0] || null,
|
17 | host: ghConfig.hostname,
|
18 | pathPrefix: options.githubApiPathPrefix || null
|
19 | })
|
20 |
|
21 | let body = ""
|
22 | require('conventional-changelog')({
|
23 | preset: 'angular',
|
24 | pkg: pkg,
|
25 | })
|
26 | .on('error', function (err) {
|
27 | cb(err)
|
28 | })
|
29 | .pipe(through(function (chunk, enc, cb) {
|
30 | body += chunk.toString()
|
31 | cb()
|
32 | }, function () {
|
33 | gitHead(function (err, hash) {
|
34 | if (err) return cb(err)
|
35 |
|
36 | const ghRepo = parseSlug(pkg.repository.url)
|
37 | const release = {
|
38 | user: ghRepo[0],
|
39 | repo: ghRepo[1],
|
40 | name: 'v' + pkg.version,
|
41 | tag_name: 'v' + pkg.version,
|
42 | target_commitish: hash,
|
43 | draft: !!options.debug,
|
44 | body: log,
|
45 | prerelease: (pkg.publishConfig || {}).tag === "next"
|
46 | }
|
47 |
|
48 | if (options.debug && !options.githubToken) {
|
49 | return cb(null, false, release)
|
50 | }
|
51 |
|
52 | github.authenticate({
|
53 | type: 'oauth',
|
54 | token: options.githubToken
|
55 | })
|
56 |
|
57 | github.repos.createRelease(release, function (err) {
|
58 | if (err) return cb(err)
|
59 |
|
60 | cb(null, true, release)
|
61 | })
|
62 | })
|
63 | }))
|
64 | }
|