UNPKG

12.5 kBMarkdownView Raw
1# [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status][coveralls-image]][coveralls-url]
2
3> Write logs based on conventional commits and templates
4
5## Install
6
7```sh
8$ npm install --save conventional-changelog-writer
9```
10
11## Usage
12
13```js
14var conventionalChangelogWriter = require('conventional-changelog-writer');
15
16conventionalChangelogWriter(context, options);
17```
18
19It returns a transform stream.
20
21It expects an object mode upstream that looks something like this:
22
23```js
24{ hash: '9b1aff905b638aa274a5fc8f88662df446d374bd',
25 header: 'feat(scope): broadcast $destroy event on scope destruction',
26 type: 'feat',
27 scope: 'scope',
28 subject: 'broadcast $destroy event on scope destruction',
29 body: null,
30 footer: 'Closes #1',
31 notes: [],
32 references: [ { action: 'Closes', owner: null, repository: null, issue: '1', raw: '#1' } ] }
33{ hash: '13f31602f396bc269076ab4d389cfd8ca94b20ba',
34 header: 'feat(ng-list): Allow custom separator',
35 type: 'feat',
36 scope: 'ng-list',
37 subject: 'Allow custom separator',
38 body: 'bla bla bla',
39 footer: 'BREAKING CHANGE: some breaking change',
40 notes: [ { title: 'BREAKING CHANGE', text: 'some breaking change' } ],
41 references: [] }
42```
43
44Each chunk should be a commit. Json object is also **valid**. Parts of the objects will be formatted and combined into a log based on the handlebars context, templates and options.
45
46The downstream might look something like this:
47
48```js
49## 0.0.1 "this is a title" (2015-05-29)
50
51
52### Features
53
54* **ng-list:** Allow custom separator ([13f3160](https://github.com/a/b/commits/13f3160))
55* **scope:** broadcast $destroy event on scope destruction ([9b1aff9](https://github.com/a/b/commits/9b1aff9)), closes [#1](https://github.com/a/b/issues/1)
56
57
58### BREAKING CHANGES
59
60* some breaking change
61```
62
63
64## API
65
66### conventionalChangelogWriter([context, [options]])
67
68Returns a transform stream.
69
70#### context
71
72Variables that will be interpolated to the template. This object contains, but not limits to the following fields.
73
74##### version
75
76Type: `string`
77
78Version number of the up-coming release. If `version` is found in the last commit before generating logs, it will be overwritten.
79
80##### title
81
82Type: `string`
83
84##### isPatch
85
86Type: `boolean` Default: `semver.patch(context.version) !== 0`
87
88By default, this value is true if `version`'s patch is `0`.
89
90##### host
91
92Type: `string`
93
94The hosting website. Eg: `'https://github.com'` or `'https://bitbucket.org'`
95
96##### owner
97
98Type: `string`
99
100The owner of the repository. Eg: `'stevemao'`.
101
102##### repository
103
104Type: `string`
105
106The repository name on `host`. Eg: `'conventional-changelog-writer'`.
107
108##### repoUrl
109
110Type: `string`
111
112The whole repository url. Eg: `'https://github.com/conventional-changelog/conventional-changelog-writer'`.
113The should be used as a fallback when `context.repository` doesn't exist.
114
115##### linkReferences
116
117Type: `boolean` Default: `true` if (`context.repository` or `context.repoUrl`), `context.commit` and `context.issue` are truthy
118
119Should all references be linked?
120
121##### commit
122
123Type: `string` Default: `'commits'`
124
125Commit keyword in the url if `context.linkReferences === true`.
126
127##### issue
128
129Type: `string` Default: `'issues'`
130
131Issue or pull request keyword in the url if `context.linkReferences === true`.
132
133##### date
134
135Type: `string` Default: `dateFormat(new Date(), 'yyyy-mm-dd', true)`
136
137Default to formatted (`'yyyy-mm-dd'`) today's date. [dateformat](https://github.com/felixge/node-dateformat) is used for formatting the date. If `version` is found in the last commit, `committerDate` will overwrite this.
138
139#### options
140
141Type: `object`
142
143##### transform
144
145Type: `object` or `function` Default: get the first 7 digits of hash, and `committerDate` will be formatted as `'yyyy-mm-dd'`.
146
147Replace with new values in each commit.
148
149If this is an object, the keys are paths to a nested object property. the values can be a string (static) and a function (dynamic) with the old value and path passed as arguments. This value is merged with your own transform object.
150
151If this is a function, the commit chunk will be passed as the argument and the returned value would be the new commit object. This is a handy function if you can't provide a transform stream as an upstream of this one. If returns a falsy value this commit is ignored.
152
153a `raw` object that is originally poured form upstream is attached to `commit`.
154
155##### groupBy
156
157Type: `string` Default: `'type'`
158
159How to group the commits. EG: based on the same type. If this value is falsy, commits are not grouped.
160
161##### commitGroupsSort
162
163Type: `function`, `string` or `array`
164
165A compare function used to sort commit groups. If it's a string or array, it sorts on the property(ies) by `localeCompare`. Will not sort if this is a falsy value.
166
167The string can be a dot path to a nested object property.
168
169##### commitsSort
170
171Type: `function`, `string` or `array` Default: `'header'`
172
173A compare function used to sort commits. If it's a string or array, it sorts on the property(ies) by `localeCompare`. Will not sort if this is a falsy value.
174
175The string can be a dot path to a nested object property.
176
177##### noteGroupsSort
178
179Type: `function`, `string` or `array` Default: `'title'`
180
181A compare function used to sort note groups. If it's a string or array, it sorts on the property(ies) by `localeCompare`. Will not sort if this is a falsy value.
182
183The string can be a dot path to a nested object property.
184
185##### notesSort
186
187Type: `function`, `string` or `array` Default: `'text'`
188
189A compare function used to sort note groups. If it's a string or array, it sorts on the property(ies) by `localeCompare`. Will not sort if this is a falsy value.
190
191The string can be a dot path to a nested object property.
192
193##### generateOn
194
195Type: `function`, `string` or `any` Default: if `commit.version` is a valid semver.
196
197When the upstream finishes pouring the commits it will generate a block of logs if `doFlush` is `true`. However, you can generate more than one block based on this criteria (usually a version) even if there are still commits from the upstream.
198
199###### generateOn(commit, commits, context, options)
200
201####### commit
202
203Current commit.
204
205####### commits
206
207Current collected commits.
208
209####### context
210
211The generated context based on original input `context` and `options`.
212
213####### options
214
215Normalized options.
216
217**NOTE**: It checks on the transformed commit chunk instead of the original one (you can check on the original by access the `raw` object on the `commit`). However, if the transformed commit is ignored it falls back to the original commit.
218
219If this value is a `string`, it checks the existence of the field. Set to other type to disable it.
220
221##### finalizeContext
222
223Type: `function` Default: pass through
224
225Last chance to modify your context before generating a changelog.
226
227###### finalizeContext(context, options, commits, keyCommit)
228
229####### context
230
231The generated context based on original input `context` and `options`.
232
233####### options
234
235Normalized options.
236
237####### commits
238
239Filtered commits from your git metadata.
240
241####### keyCommit
242
243The commit that triggers to generate the log.
244
245##### debug
246
247Type: `function` Default: `function() {}`
248
249A function to get debug information.
250
251##### reverse
252
253Type: `boolean` Default: `false`
254
255The normal order means reverse chronological order. `reverse` order means chronological order. Are the commits from upstream in the reverse order? You should only worry about this when generating more than one blocks of logs based on `generateOn`. If you find the last commit is in the wrong block inverse this value.
256
257##### includeDetails
258
259Type: `boolean` Default: `false`
260
261If this value is `true`, instead of emitting strings of changelog, it emits objects containing the details the block.
262
263*NOTE:* The downstream must be in object mode if this is `true`.
264
265##### ignoreReverted
266
267Type: `boolean` Default: `true`
268
269If `true`, reverted commits will be ignored.
270
271##### doFlush
272
273Type: `boolean` Default: `true`
274
275If `true`, the stream will flush out the last bit of commits (could be empty) to changelog.
276
277##### mainTemplate
278
279Type: `string` Default: [template.hbs](templates/template.hbs)
280
281The main handlebars template.
282
283##### headerPartial
284
285Type: `string` Default: [header.hbs](templates/header.hbs)
286
287##### commitPartial
288
289Type: `string` Default: [commit.hbs](templates/commit.hbs)
290
291##### footerPartial
292
293Type: `string` Default: [footer.hbs](templates/footer.hbs)
294
295##### partials
296
297Type: `object`
298
299Partials that used in the main template, if any. The key should be the partial name and the value should be handlebars template strings. If you are using handlebars template files, read files by yourself.
300
301
302## Customization Guide
303
304It is possible to customize this the changelog to suit your needs. Templates are written in [handlebars](http://handlebarsjs.com). You can customize all partials or the whole template. Template variables are from either `upstream` or `context`. The following are a suggested way of defining variables.
305
306### upstream
307
308Variables in upstream are commit specific and should be used per commit. Eg: *commit date* and *commit username*. You can think of them as "local" or "isolate" variables. A "raw" commit message (original commit poured from upstream) is attached to `commit`. `transform` can be used to modify a commit.
309
310### context
311
312context should be module specific and can be used across the whole log. Thus these variables should not be related to any single commit and should be generic information of the module or all commits. Eg: *repository url* and *author names*, etc. You can think of them as "global" or "root" variables.
313
314Basically you can make your own templates and define all your template context. Extra context are based on commits from upstream and `options`. For more details, please checkout [handlebars](http://handlebarsjs.com) and the source code of this module. `finalizeContext` can be used at last to modify context before generating a changelog.
315
316
317## CLI
318
319```sh
320$ npm install --global conventional-changelog-writer
321$ conventional-changelog-writer --help # for more details
322```
323
324It works with [Line Delimited JSON](http://en.wikipedia.org/wiki/Line_Delimited_JSON).
325
326If you have commits.ldjson
327
328```js
329{"hash":"9b1aff905b638aa274a5fc8f88662df446d374bd","header":"feat(ngMessages): provide support for dynamic message resolution","type":"feat","scope":"ngMessages","subject":"provide support for dynamic message resolution","body":"Prior to this fix it was impossible to apply a binding to a the ngMessage directive to represent the name of the error.","footer":"BREAKING CHANGE: The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.\nCloses #10036\nCloses #9338","notes":[{"title":"BREAKING CHANGE","text":"The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive."}],"references":[{"action":"Closes","owner",null,"repository":null,"issue":"10036","raw":"#10036"},{"action":"Closes","owner":null,"repository":null,"issue":"9338","raw":"#9338"}]}
330```
331
332And you run
333
334```sh
335$ conventional-changelog-writer commits.ldjson -o options.js
336```
337
338The output might look something like this
339
340```md
341# 1.0.0 (2015-04-09)
342
343
344### Features
345
346* **ngMessages:** provide support for dynamic message resolution 9b1aff9, closes #10036 #9338
347
348
349### BREAKING CHANGES
350
351* The `ngMessagesInclude` attribute is now its own directive and that must be placed as a **child** element within the element with the ngMessages directive.
352```
353
354It is printed to stdout.
355
356
357## License
358
359MIT © [Steve Mao](https://github.com/stevemao)
360
361
362[npm-image]: https://badge.fury.io/js/conventional-changelog-writer.svg
363[npm-url]: https://npmjs.org/package/conventional-changelog-writer
364[travis-image]: https://travis-ci.org/conventional-changelog/conventional-changelog-writer.svg?branch=master
365[travis-url]: https://travis-ci.org/conventional-changelog/conventional-changelog-writer
366[daviddm-image]: https://david-dm.org/conventional-changelog/conventional-changelog-writer.svg?theme=shields.io
367[daviddm-url]: https://david-dm.org/conventional-changelog/conventional-changelog-writer
368[coveralls-image]: https://coveralls.io/repos/conventional-changelog/conventional-changelog-writer/badge.svg
369[coveralls-url]: https://coveralls.io/r/conventional-changelog/conventional-changelog-writer