forked from semantic-release/semantic-release
/
index.js
101 lines (87 loc) Β· 2.86 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
var conventionalChangelog = require('conventional-changelog');
var fs = require('fs');
var path = require('path');
var dateFormat = require('dateformat');
var forEachPackage = require('lerna-semantic-release-utils').forEachPackage;
var tagging = require('lerna-semantic-release-utils').tagging;
var analyzer = require('lerna-semantic-release-analyze-commits');
var CHANGELOG_FILE_NAME = 'CHANGELOG.md';
function getPkgPath (packagePath) {
return path.join(packagePath, 'package.json')
}
function getPkg (packagePath) {
return JSON.parse(fs.readFileSync(getPkgPath(packagePath)));
}
/**
* Reformat a commit to contain a version (based on git tags) and the proper date.
* Mostly from the default (conventional-changelog-core/lib/merge-config.js)
* @param commit
* @returns commit the modified commit
*/
function reformatCommit (commit) {
if (commit.committerDate) {
commit.committerDate = dateFormat(commit.committerDate, 'yyyy-mm-dd', true);
}
var isReleaseCommit = commit.type === 'chore' && commit.scope === 'release';
if (!isReleaseCommit) {
return commit;
}
var affectedPackages = analyzer.getAffectedPackages(getAffectsLine(commit));
var hasVersion = affectedPackages[0] &&
tagging.getTagParts(affectedPackages[0]) &&
tagging.getTagParts(affectedPackages[0]).version;
if (!hasVersion) {
return commit;
}
var newVersion = tagging.getTagParts(affectedPackages[0]).version;
commit.version = newVersion;
return commit;
}
function getAffectsLine (commit) {
return analyzer.findAffectsLine({message: commit.body});
}
function createChangelog (done) {
var packagePath = this.packagePath;
var rootPackageRepository = this.rootPackageRepository;
var io = this.io;
var pkgJsonPath = getPkgPath(packagePath);
var writeStream = io.fs.createWriteStream(path.join(packagePath, CHANGELOG_FILE_NAME));
var stream = conventionalChangelog({
preset: 'angular',
transform: function (commit, cb) {
var pkgJsonFile = getPkg(packagePath);
var isRelevant = analyzer.isRelevant(getAffectsLine(commit), pkgJsonFile.name);
if (isRelevant) {
commit = reformatCommit(commit);
cb(null, commit);
return;
}
cb(null, null);
},
pkg: {
path: pkgJsonPath
},
releaseCount: 0
}, {
repoUrl: typeof rootPackageRepository !== 'object' ? rootPackageRepository : rootPackageRepository.url
}).pipe(writeStream);
stream.on('close', function () {
done(null);
});
}
module.exports = function (config) {
var rootPackageRepository = JSON.parse(fs.readFileSync('./package.json')).repository;
forEachPackage([
createChangelog,
], {
allPackages: config.io.lerna.getAllPackages(),
extraContext: {
rootPackageRepository: rootPackageRepository,
io: config.io
}
}, (err) => {
if (typeof config.callback === 'function') {
config.callback(err);
}
});
};