-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathdefault.ts
180 lines (162 loc) Β· 4.89 KB
/
default.ts
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
import { existsSync, promises as fsp } from "node:fs";
import type { Argv } from "mri";
import { resolve } from "pathe";
import consola from "consola";
import { execa } from "execa";
import {
loadChangelogConfig,
getGitDiff,
parseCommits,
bumpVersion,
generateMarkDown,
filterCommits,
BumpVersionOptions,
} from "..";
import { npmPublish, renamePackage } from "../package";
import { githubRelease } from "./github";
export default async function defaultMain(args: Argv) {
const cwd = resolve(args._[0] /* bw compat */ || args.dir || "");
process.chdir(cwd);
consola.wrapConsole();
const config = await loadChangelogConfig(cwd, {
from: args.from,
to: args.to,
output: args.output,
newVersion: typeof args.r === "string" ? args.r : undefined,
});
const logger = consola.create({ stdout: process.stderr });
logger.info(`Generating changelog for ${config.from || ""}...${config.to}`);
const rawCommits = await getGitDiff(config.from, config.to);
// Parse commits as conventional commits
const commits = parseCommits(rawCommits, config).filter(
(c) =>
config.types[c.type] &&
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
);
const filteredCommits = filterCommits(commits, config);
// Shortcut for canary releases
if (args.canary) {
if (args.bump === undefined) {
args.bump = true;
}
if (args.versionSuffix === undefined) {
args.versionSuffix = true;
}
if (args.nameSuffix === undefined && typeof args.canary === "string") {
args.nameSuffix = args.canary;
}
}
// Rename package name optionally
if (typeof args.nameSuffix === "string") {
await renamePackage(config, `-${args.nameSuffix}`);
}
// Bump version optionally
if (args.bump || args.release) {
const bumpOptions = _getBumpVersionOptions(args);
const newVersion = await bumpVersion(filteredCommits, config, bumpOptions);
if (!newVersion) {
consola.error("Unable to bump version based on changes.");
process.exit(1);
}
config.newVersion = newVersion;
}
// Generate markdown
const markdown = await generateMarkDown(filteredCommits, config);
// Show changelog in CLI unless bumping or releasing
const displayOnly = !args.bump && !args.release;
if (displayOnly) {
consola.log("\n\n" + markdown + "\n\n");
}
// Update changelog file (only when bumping or releasing or when --output is specified as a file)
if (typeof config.output === "string" && (args.output || !displayOnly)) {
let changelogMD: string;
if (existsSync(config.output)) {
consola.info(`Updating ${config.output}`);
changelogMD = await fsp.readFile(config.output, "utf8");
} else {
consola.info(`Creating ${config.output}`);
changelogMD = "# Changelog\n\n";
}
const lastEntry = changelogMD.match(/^###?\s+.*$/m);
if (lastEntry) {
changelogMD =
changelogMD.slice(0, lastEntry.index) +
markdown +
"\n\n" +
changelogMD.slice(lastEntry.index);
} else {
changelogMD += "\n" + markdown + "\n\n";
}
await fsp.writeFile(config.output, changelogMD);
}
// Commit and tag changes for release mode
if (args.release) {
if (args.commit !== false) {
const filesToAdd = [config.output, "package.json"].filter(
(f) => f && typeof f === "string"
) as string[];
await execa("git", ["add", ...filesToAdd], { cwd });
const msg = config.templates.commitMessage.replaceAll(
"{{newVersion}}",
config.newVersion
);
await execa("git", ["commit", "-m", msg], { cwd });
}
if (args.tag !== false) {
const msg = config.templates.tagMessage.replaceAll(
"{{newVersion}}",
config.newVersion
);
const body = config.templates.tagBody.replaceAll(
"{{newVersion}}",
config.newVersion
);
await execa("git", ["tag", "-am", msg, body], { cwd });
}
if (args.push === true) {
await execa("git", ["push", "--follow-tags"], { cwd });
}
if (args.github !== false && config.repo?.provider === "github") {
await githubRelease(config, {
version: config.newVersion,
body: markdown.split("\n").slice(2).join("\n"),
});
}
}
// Publish package optionally
if (args.publish) {
if (args.publishTag) {
config.publish.tag = args.publishTag;
}
await npmPublish(config);
}
}
function _getBumpVersionOptions(args: Argv): BumpVersionOptions {
if (args.versionSuffix) {
return {
suffix: args.versionSuffix,
};
}
for (const type of [
"major",
"premajor",
"minor",
"preminor",
"patch",
"prepatch",
"prerelease",
] as const) {
const value = args[type];
if (value) {
if (type.startsWith("pre")) {
return {
type,
preid: typeof value === "string" ? value : "",
};
}
return {
type,
};
}
}
}