|
1 |
| -const through2 = require('through2') |
2 |
| - , stripAnsi = require('strip-ansi') |
| 1 | +import through2 from 'through2' |
| 2 | +import stripAnsi from 'strip-ansi' |
3 | 3 |
|
4 |
| - |
5 |
| -module.exports = commitStream |
6 |
| - |
7 |
| - |
8 |
| -function commitStream (ghUser, ghProject) { |
9 |
| - var commit |
| 4 | +export default function commitStream (ghUser, ghProject) { |
| 5 | + let commit |
10 | 6 |
|
11 | 7 | return through2.obj(onLine, onEnd)
|
12 | 8 |
|
13 | 9 | function addLine (line) {
|
14 | 10 | line = stripAnsi(line)
|
15 | 11 |
|
16 |
| - if (!line) |
17 |
| - return |
| 12 | + if (line.length === 0) return |
18 | 13 |
|
19 |
| - var old, m |
| 14 | + let old, m |
20 | 15 |
|
21 | 16 | if (/^commit \w+$/.test(line)) {
|
22 | 17 | old = commit
|
23 |
| - commit = { |
24 |
| - sha: line.split(' ')[1] |
| 18 | + commit = { sha: line.split(' ')[1] } |
| 19 | + } else if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) { |
| 20 | + if (commit === undefined || commit === null) { |
| 21 | + throw new Error('Something unexpected occurred') |
25 | 22 | }
|
26 |
| - } else if (m = line.match(/^CommitDate:\s+(.*)$/)) { |
27 |
| - if (!commit) |
28 |
| - throw new Error('wut?') |
29 | 23 | commit.commitDate = m[1].trim()
|
30 |
| - } else if (m = line.match(/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i)) { |
31 |
| - if (!commit) |
32 |
| - throw new Error('wut?') |
33 |
| - if (!commit.authors) |
| 24 | + } else if ( |
| 25 | + (m = line.match( |
| 26 | + /^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i |
| 27 | + )) !== null |
| 28 | + ) { |
| 29 | + if (commit === undefined || commit === null) { |
| 30 | + throw new Error('Something unexpected occurred') |
| 31 | + } |
| 32 | + if (commit.authors === undefined || commit.authors === null) { |
34 | 33 | commit.authors = []
|
| 34 | + } |
35 | 35 | commit.authors.push({ name: m[1], email: m[2] })
|
36 |
| - } else if (m = line.match(/^AuthorDate:\s+(.*)$/)) { |
37 |
| - if (!commit) |
38 |
| - throw new Error('wut?') |
39 |
| - commit.authorDate = m[1].trim() |
40 |
| - } else if (m = line.match(/^Date:\s+(.*)$/)) { |
41 |
| - if (!commit) |
42 |
| - throw new Error('wut?') |
| 36 | + } else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) { |
| 37 | + if (commit === undefined || commit === null) { |
| 38 | + throw new Error('Something unexpected occurred') |
| 39 | + } |
43 | 40 | commit.authorDate = m[1].trim()
|
44 |
| - } else if (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) { |
45 |
| - if (!commit.reviewers) |
| 41 | + } else if ( |
| 42 | + (m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !== |
| 43 | + null |
| 44 | + ) { |
| 45 | + if (commit.reviewers === undefined || commit.reviewers === null) { |
46 | 46 | commit.reviewers = []
|
| 47 | + } |
47 | 48 | commit.reviewers.push({ name: m[1], email: m[2] })
|
48 |
| - } else if (m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) { |
| 49 | + } else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/)) !== null) { |
49 | 50 | commit.prUrl = m[1]
|
50 |
| - if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/))) { |
51 |
| - commit.prUrl = 'https://github.com/' + ghUser + '/' + ghProject + '/pull/' + m[1] |
| 51 | + if ( |
| 52 | + typeof ghUser === 'string' && |
| 53 | + ghUser.length !== 0 && |
| 54 | + typeof ghProject === 'string' && |
| 55 | + ghProject.length !== 0 && |
| 56 | + (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null |
| 57 | + ) { |
| 58 | + commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}` |
52 | 59 | }
|
53 |
| - if (m = commit.prUrl.match(/^(https?:\/\/.+\/([^\/]+)\/([^\/]+))\/\w+\/(\d+)(\/)?$/i)) { |
54 |
| - commit.ghIssue = +m[4] |
55 |
| - commit.ghUser = m[2] |
| 60 | + if ( |
| 61 | + (m = commit.prUrl.match( |
| 62 | + /^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i |
| 63 | + )) !== null |
| 64 | + ) { |
| 65 | + commit.ghIssue = parseInt(m[4]) |
| 66 | + commit.ghUser = m[2] |
56 | 67 | commit.ghProject = m[3]
|
57 | 68 | }
|
58 |
| - } else if (/^ /.test(line) && (line = line.trim()).length) { |
59 |
| - if (!commit.summary) { |
| 69 | + } else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) { |
| 70 | + if (commit.summary === undefined || commit.summary === null) { |
60 | 71 | commit.summary = line
|
61 | 72 | } else {
|
62 |
| - if (!commit.description) |
| 73 | + if (commit.description === undefined || commit.description === null) { |
63 | 74 | commit.description = []
|
| 75 | + } |
64 | 76 | commit.description.push(line)
|
65 | 77 | }
|
66 |
| - } else if (/^ [^ ]/.test(line) && (line = line.trim()).length) { |
67 |
| - if (m = line.match(/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/)) { |
68 |
| - commit.changes = { |
69 |
| - files : Number(m[1]) |
70 |
| - , insertions : Number(m[2] || 0) |
71 |
| - , deletions : Number(m[3] || 0) |
72 |
| - } |
| 78 | + } else if ( |
| 79 | + /^ [^ ]/.test(line) && |
| 80 | + (line = line.trim()).length !== 0 && |
| 81 | + (m = line.match( |
| 82 | + /^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/ |
| 83 | + )) !== null |
| 84 | + ) { |
| 85 | + commit.changes = { |
| 86 | + files: parseInt(m[1]), |
| 87 | + insertions: parseInt(m[2] ?? 0), |
| 88 | + deletions: parseInt(m[3] ?? 0) |
73 | 89 | }
|
74 | 90 | }
|
75 | 91 |
|
76 | 92 | return old
|
77 | 93 | }
|
78 | 94 |
|
79 | 95 | function onLine (line, _, callback) {
|
80 |
| - var commit = addLine(line) |
81 |
| - if (commit && commit.authors && commit.authors.length > 0) |
| 96 | + const commit = addLine(line) |
| 97 | + |
| 98 | + if (commit?.authors?.length > 0) { |
82 | 99 | commit.author = commit.authors[0]
|
83 |
| - if (commit) |
| 100 | + |
84 | 101 | this.push(commit)
|
| 102 | + } |
| 103 | + |
85 | 104 | callback()
|
86 | 105 | }
|
87 | 106 |
|
88 | 107 | function onEnd (callback) {
|
89 |
| - if (commit && commit.authors && commit.authors.length > 0) |
| 108 | + if (commit?.authors?.length > 0) { |
90 | 109 | commit.author = commit.authors[0]
|
91 |
| - if (commit) |
| 110 | + |
92 | 111 | this.push(commit)
|
| 112 | + } |
| 113 | + |
93 | 114 | callback()
|
94 | 115 | }
|
95 | 116 | }
|
0 commit comments