forked from justinawrey/shipit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshipit.ts
More file actions
120 lines (106 loc) · 3.62 KB
/
shipit.ts
File metadata and controls
120 lines (106 loc) · 3.62 KB
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
import {
DOCS_COMMIT_REGEX,
MAJOR_COMMIT_REGEX,
MINOR_COMMIT_REGEX,
OTHER_COMMIT_REGEX,
PATCH_COMMIT_REGEX,
} from "./commit-regex.ts";
import * as colors from "@std/fmt/colors";
import * as semver from "@std/semver";
import git, { ROOT } from "./git.ts";
import github, { type Commits, generateReleaseNotes } from "./github.ts";
import { logHeader } from "./log.ts";
if (!import.meta.main) {
Deno.exit(0);
}
// Check if there are any uncommitted changes.
if (!(await git.isClean())) {
console.error(
"ERROR: There are uncommitted changes. Please commit or stash them before running @hugojosefson/shipit.",
);
Deno.exit(2);
}
// Get latest version.
logHeader("Getting latest version...");
let ver: string | typeof ROOT = await git.latestRemoteTag();
// If there is no latest version, start at 0.1.0.
if (!ver) {
console.log("No previous version found: defaulting to 0.1.0\n");
ver = ROOT;
} else {
if (!semver.tryParse(ver)) {
throw new Error(`latest version is invalid: ${colors.bold(ver)}`);
}
console.log("Found latest version:", `${ver}\n`);
}
// Get commit history since latest version.
logHeader("Getting commit history since latest version...");
const [major, minor, patch, docs, other] = await Promise.all([
git.log({ grep: MAJOR_COMMIT_REGEX.source, since: ver }),
git.log({ grep: MINOR_COMMIT_REGEX.source, since: ver }),
git.log({ grep: PATCH_COMMIT_REGEX.source, since: ver }),
git.log({ grep: DOCS_COMMIT_REGEX.source, since: ver }),
git.log({ grep: OTHER_COMMIT_REGEX.source, since: ver }),
]);
if (major.length) console.log('Found "major" commits');
if (minor.length) console.log('Found "minor" commits');
if (patch.length) console.log('Found "patch" commits');
if (docs.length) console.log('Found "docs" commits');
if (other.length) console.log('Found "other" commits');
if (
!major.length && !minor.length && !patch.length && !docs.length &&
!other.length
) {
console.log("No identified changes since last release!");
Deno.exit(0);
} else {
// Log a newline.
console.log();
}
// Bump version.
let nextVer = "0.1.0";
if (ver !== ROOT) {
logHeader("Determining next version...");
if (major.length) {
nextVer = semver.format(semver.increment(semver.parse(ver), "major"));
} else if (minor.length || docs.length) {
nextVer = semver.format(semver.increment(semver.parse(ver), "minor"));
} else if (patch.length || other.length) {
nextVer = semver.format(semver.increment(semver.parse(ver), "patch"));
}
console.log("Next version:", `${nextVer}\n`);
}
// Generate release notes.
const commits: Commits = { major, minor, patch, docs, other };
const releaseNotes = generateReleaseNotes(nextVer, commits);
// If we're in dry run mode, just print the release notes and exit.
if (
Deno.env.get("DRY_RUN") || Deno.args.includes("--dry-run") ||
Deno.args.includes("-n")
) {
logHeader(
"DRY_RUN or --dry-run is set, so not pushing any tags, nor creating a release.",
);
console.log(`Release notes for ${nextVer} would be:`);
console.log(releaseNotes);
Deno.exit(0);
}
// Tag the new version.
logHeader("Creating new remote tag...");
await git.tag(nextVer);
await git.pushTag(nextVer);
console.log("New remote tag created:", `${nextVer}\n`);
// Create GitHub release.
logHeader("Creating new GitHub release...");
let url: string;
try {
url = await github.release(nextVer, releaseNotes);
} catch (error) {
// Something went wrong, so delete the local and remote tag and bail.
console.error(error);
await git.deleteLocalTag(nextVer);
await git.deleteRemoteTag(nextVer);
Deno.exit(1);
}
console.log("Successfully created new GitHub release! 🎉");
console.log("View it here:", url);