UNPKG

4.43 kBJavaScriptView Raw
1'use strict'
2
3var dateFormat = require('dateformat')
4var join = require('path').join
5var readFileSync = require('fs').readFileSync
6var semverValid = require('semver').valid
7var through = require('through2')
8var util = require('./lib/util')
9var _ = require('lodash')
10
11function conventionalChangelogWriter (context, options) {
12 var savedKeyCommit
13 var commits = []
14 var firstRelease = true
15 var neverGenerated = true
16
17 context = _.extend({
18 commit: 'commits',
19 issue: 'issues',
20 date: dateFormat(new Date(), 'yyyy-mm-dd', true)
21 }, context)
22
23 if (!_.isBoolean(context.linkReferences) && (context.repository || context.repoUrl) && context.commit && context.issue) {
24 context.linkReferences = true
25 }
26
27 options = _.assign({
28 groupBy: 'type',
29 commitsSort: 'header',
30 noteGroupsSort: 'title',
31 notesSort: 'text',
32 generateOn: function (commit) {
33 return semverValid(commit.version)
34 },
35 finalizeContext: function (context) {
36 return context
37 },
38 debug: function () {},
39 reverse: false,
40 includeDetails: false,
41 ignoreReverted: true,
42 doFlush: true,
43 mainTemplate: readFileSync(join(__dirname, 'templates/template.hbs'), 'utf-8'),
44 headerPartial: readFileSync(join(__dirname, 'templates/header.hbs'), 'utf-8'),
45 commitPartial: readFileSync(join(__dirname, 'templates/commit.hbs'), 'utf-8'),
46 footerPartial: readFileSync(join(__dirname, 'templates/footer.hbs'), 'utf-8')
47 }, options)
48
49 if ((!_.isFunction(options.transform) && _.isObject(options.transform)) || _.isUndefined(options.transform)) {
50 options.transform = _.assign({
51 hash: function (hash) {
52 if (_.isString(hash)) {
53 return hash.substring(0, 7)
54 }
55 },
56 header: function (header) {
57 return header.substring(0, 100)
58 },
59 committerDate: function (date) {
60 if (!date) {
61 return
62 }
63
64 return dateFormat(date, 'yyyy-mm-dd', true)
65 }
66 }, options.transform)
67 }
68
69 var generateOn = options.generateOn
70 if (_.isString(generateOn)) {
71 generateOn = function (commit) {
72 return !_.isUndefined(commit[options.generateOn])
73 }
74 } else if (!_.isFunction(generateOn)) {
75 generateOn = function () {
76 return false
77 }
78 }
79
80 options.commitGroupsSort = util.functionify(options.commitGroupsSort)
81 options.commitsSort = util.functionify(options.commitsSort)
82 options.noteGroupsSort = util.functionify(options.noteGroupsSort)
83 options.notesSort = util.functionify(options.notesSort)
84
85 return through.obj(function (chunk, enc, cb) {
86 try {
87 var result
88 var commit = util.processCommit(chunk, options.transform, context)
89 var keyCommit = commit || chunk
90
91 // previous blocks of logs
92 if (options.reverse) {
93 if (commit) {
94 commits.push(commit)
95 }
96
97 if (generateOn(keyCommit, commits, context, options)) {
98 neverGenerated = false
99 result = util.generate(options, commits, context, keyCommit)
100 if (options.includeDetails) {
101 this.push({
102 log: result,
103 keyCommit: keyCommit
104 })
105 } else {
106 this.push(result)
107 }
108
109 commits = []
110 }
111 } else {
112 if (generateOn(keyCommit, commits, context, options)) {
113 neverGenerated = false
114 result = util.generate(options, commits, context, savedKeyCommit)
115
116 if (!firstRelease || options.doFlush) {
117 if (options.includeDetails) {
118 this.push({
119 log: result,
120 keyCommit: savedKeyCommit
121 })
122 } else {
123 this.push(result)
124 }
125 }
126
127 firstRelease = false
128 commits = []
129 savedKeyCommit = keyCommit
130 }
131
132 if (commit) {
133 commits.push(commit)
134 }
135 }
136
137 cb()
138 } catch (err) {
139 cb(err)
140 }
141 }, function (cb) {
142 if (!options.doFlush && (options.reverse || neverGenerated)) {
143 cb(null)
144 return
145 }
146
147 try {
148 var result = util.generate(options, commits, context, savedKeyCommit)
149
150 if (options.includeDetails) {
151 this.push({
152 log: result,
153 keyCommit: savedKeyCommit
154 })
155 } else {
156 this.push(result)
157 }
158
159 cb()
160 } catch (err) {
161 cb(err)
162 }
163 })
164}
165
166module.exports = conventionalChangelogWriter