This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
206 lines (197 loc) · 5.66 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import mixinDeep from 'mixin-deep';
import { mentions, increment } from './plugins';
import { parse, stringify, validate, check } from './main';
import {
parseHeader,
stringifyHeader,
validateHeader,
checkHeader,
} from './header';
import {
parseCommit,
stringifyCommit,
validateCommit,
checkCommit,
} from './commit';
/**
* Apply a set of `plugins` over all of the given `commits`.
* A plugin is a simple function passed with `Commit` object,
* which may be returned to modify and set additional properties
* to the `Commit` object.
*
* _The `commits` should be coming from `parse`, `validate` (with `ret` option)
* or the `check` methods. It does not do checking and validation._
*
* @example
* import dedent from 'dedent';
* import { applyPlugins, plugins, parse, check } from './src';
*
* const commits = [
* 'fix: bar qux',
* dedent`feat(foo): yea yea
*
* Awesome body here with @some mentions
* resolves #123
*
* BREAKING CHANGE: ouch!`,
* 'chore(ci): updates for ci config',
* {
* header: { type: 'fix', subject: 'Barry White' },
* body: 'okey dude',
* foo: 'possible',
* },
* ];
*
* // Parses, normalizes, validates
* // and applies plugins
* const results = applyPlugins(plugins, check(parse(commits)));
*
* console.log(results);
* // => [ { body: null,
* // footer: null,
* // header: { scope: null, type: 'fix', subject: 'bar qux' },
* // mentions: [],
* // increment: 'patch',
* // isBreaking: false },
* // { body: 'Awesome body here with @some mentions\nresolves #123',
* // footer: 'BREAKING CHANGE: ouch!',
* // header: { scope: 'foo', type: 'feat', subject: 'yea yea' },
* // mentions: [ [Object] ],
* // increment: 'major',
* // isBreaking: true },
* // { body: null,
* // footer: null,
* // header:
* // { scope: 'ci', type: 'chore', subject: 'updates for ci config' },
* // mentions: [],
* // increment: false,
* // isBreaking: false },
* // { body: 'okey dude',
* // footer: null,
* // header: { scope: null, type: 'fix', subject: 'Barry White' },
* // foo: 'possible',
* // mentions: [],
* // increment: 'patch',
* // isBreaking: false } ]
*
* @name .applyPlugins
* @param {Array<Function>} plugins a simple function like `(commit) => {}`
* @param {string|object|array} commits a value which should already be gone through `parse`
* @returns {Array<Commit>} plus the modified or added properties from each function in `plugins`
* @public
*/
export function applyPlugins(plugins, commits) {
const plgs = [].concat(plugins).filter(Boolean);
return [].concat(commits).reduce((result, commit) => {
const cmt = plgs.reduce((acc, fn) => {
const res = fn(acc);
return mixinDeep(acc, res);
}, commit);
return result.concat(cmt);
}, []);
}
/**
* An array which includes `mentions` and `increment` built-in plugins.
* The `mentions` is an array of objects. Basically what's returned from
* the [collect-mentions][] package.
*
* @example
* import { plugins, applyPlugins, parse } from 'parse-commit-message';
*
* console.log(plugins); // => [mentions, increment]
* console.log(plugins[0]); // => [Function mentions]
* console.log(plugins[0]); // => [Function increment]
*
* const cmts = parse([
* 'fix: foo @bar @qux haha',
* 'feat(cli): awesome @tunnckoCore feature\n\nSuper duper baz!'
* 'fix: ooh\n\nBREAKING CHANGE: some awful api change'
* ]);
*
* const commits = applyPlugins(plugins, cmts);
* console.log(commits);
* // => [
* // {
* // header: { type: 'fix', scope: '', subject: 'foo bar baz' },
* // body: '',
* // footer: '',
* // increment: 'patch',
* // isBreaking: false,
* // mentions: [
* // { handle: '@bar', mention: 'bar', index: 8 },
* // { handle: '@qux', mention: 'qux', index: 13 },
* // ]
* // },
* // {
* // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' },
* // body: 'Super duper baz!',
* // footer: '',
* // increment: 'minor',
* // isBreaking: false,
* // mentions: [
* // { handle: '@tunnckoCore', mention: 'tunnckoCore', index: 18 },
* // ]
* // },
* // {
* // header: { type: 'fix', scope: '', subject: 'ooh' },
* // body: 'BREAKING CHANGE: some awful api change',
* // footer: '',
* // increment: 'major',
* // isBreaking: true,
* // mentions: [],
* // },
* // ]
*
* @name .plugins
* @public
*/
export const plugins = [mentions, increment];
/**
* An object (named set) which includes `mentions` and `increment` built-in plugins.
*
* @example
* import { mappers, applyPlugins, parse } from 'parse-commit-message';
*
* console.log(mappers); // => { mentions, increment }
* console.log(mappers.mentions); // => [Function mentions]
* console.log(mappers.increment); // => [Function increment]
*
* const flat = true;
* const parsed = parse('fix: bar', flat);
* console.log(parsed);
* // => {
* // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' },
* // body: 'Super duper baz!',
* // footer: '',
* // }
*
* const commit = applyPlugins([mappers.increment], parsed);
* console.log(commit)
* // => [{
* // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' },
* // body: 'Super duper baz!',
* // footer: '',
* // increment: 'patch',
* // }]
*
* @name .mappers
* @public
*/
export const mappers = { mentions, increment };
export {
// main
parse,
stringify,
validate,
check,
// Only for header (the first line of commit message)
parseHeader,
stringifyHeader,
validateHeader,
checkHeader,
// Whole commit message
parseCommit,
stringifyCommit,
validateCommit,
checkCommit,
};