/
ConventionalCommitUtilities.js
75 lines (61 loc) · 2.39 KB
/
ConventionalCommitUtilities.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
import ChildProcessUtilities from "./ChildProcessUtilities";
import FileSystemUtilities from "./FileSystemUtilities";
import dedent from "dedent";
import logger from "./logger";
import semver from "semver";
import path from "path";
const CHANGELOG_NAME = "CHANGELOG.md";
const CHANGELOG_HEADER = dedent(`# Change Log
All notable changes to this project will be documented in this file.
See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.`);
const RECOMMEND_CLI = require.resolve("conventional-recommended-bump/cli.js");
const CHANGELOG_CLI = require.resolve("conventional-changelog-cli/cli.js");
export default class ConventionalCommitUtilities {
@logger.logifySync()
static recommendVersion(pkg, opts) {
const name = pkg.name;
const version = pkg.version;
const pkgLocation = pkg.location;
const recommendedBump = ChildProcessUtilities.execSync(
`${RECOMMEND_CLI} -l ${name} --commit-path=${pkgLocation} -p angular`,
opts
);
return semver.inc(version, recommendedBump);
}
@logger.logifySync()
static updateChangelog(pkg, opts) {
const name = pkg.name;
const pkgLocation = pkg.location;
const pkgJsonLocation = path.join(pkgLocation, "package.json");
const changelogLocation = ConventionalCommitUtilities.changelogLocation(pkg);
let changelogContents = "";
if (FileSystemUtilities.existsSync(changelogLocation)) {
changelogContents = FileSystemUtilities.readFileSync(changelogLocation);
}
// run conventional-changelog-cli to generate the markdown
// for the upcoming release.
const newEntry = ChildProcessUtilities.execSync(
`${CHANGELOG_CLI} -l ${name} --commit-path=${pkgLocation} --pkg=${pkgJsonLocation} -p angular`,
opts
);
// CHANGELOG entries start with <a name=, we remove
// the header if it exists by starting at the first entry.
if (changelogContents.indexOf("<a name=") !== -1) {
changelogContents = changelogContents.substring(
changelogContents.indexOf("<a name=")
);
}
FileSystemUtilities.writeFileSync(
changelogLocation,
// only allow 1 \n at end of content.
dedent(
`${CHANGELOG_HEADER}
${newEntry}
${changelogContents}`.replace(/\n+$/, "\n"))
);
}
@logger.logifySync()
static changelogLocation(pkg) {
return path.join(pkg.location, CHANGELOG_NAME);
}
}