githublogen 0.3.8 → 0.3.9-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.js ADDED
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ consola
4
+ } from "./chunk-4LD7KT4M.js";
5
+
6
+ // src/cli.ts
7
+ import process from "process";
8
+ import cac from "cac";
9
+ import { blue, bold, cyan as cyan2, dim, red as red2, yellow as yellow2 } from "kolorist";
10
+ import { getChangelogMarkdown } from "@soybeanjs/changelog";
11
+
12
+ // package.json
13
+ var version = "0.3.9-beta.1";
14
+
15
+ // src/github.ts
16
+ import { ofetch } from "ofetch";
17
+ import { cyan, green, red, yellow } from "kolorist";
18
+ function getHeaders(githubToken) {
19
+ return {
20
+ accept: "application/vnd.github.v3+json",
21
+ authorization: `token ${githubToken}`
22
+ };
23
+ }
24
+ async function hasTagOnGitHub(tag, repo, githubToken) {
25
+ try {
26
+ await ofetch(`https://api.github.com/repos/${repo}/git/ref/tags/${tag}`, {
27
+ headers: getHeaders(githubToken)
28
+ });
29
+ return true;
30
+ } catch (e) {
31
+ return false;
32
+ }
33
+ }
34
+ async function sendRelease(options, content) {
35
+ const headers = getHeaders(options.github.token);
36
+ const github = options.github.repo;
37
+ let url = `https://api.github.com/repos/${github}/releases`;
38
+ let method = "POST";
39
+ try {
40
+ const exists = await ofetch(`https://api.github.com/repos/${github}/releases/tags/${options.to}`, {
41
+ headers
42
+ });
43
+ if (exists.url) {
44
+ url = exists.url;
45
+ method = "PATCH";
46
+ }
47
+ } catch (e) {
48
+ }
49
+ const body = {
50
+ body: content,
51
+ draft: false,
52
+ name: options.to,
53
+ prerelease: options.prerelease,
54
+ tag_name: options.to
55
+ };
56
+ const webUrl = `https://github.com/${github}/releases/new?title=${encodeURIComponent(
57
+ String(body.name)
58
+ )}&body=${encodeURIComponent(String(body.body))}&tag=${encodeURIComponent(String(options.to))}&prerelease=${options.prerelease}`;
59
+ try {
60
+ consola.log(cyan(method === "POST" ? "Creating release notes..." : "Updating release notes..."));
61
+ const res = await ofetch(url, {
62
+ method,
63
+ body: JSON.stringify(body),
64
+ headers
65
+ });
66
+ consola.log(green(`Released on ${res.html_url}`));
67
+ } catch (e) {
68
+ consola.error(red("Failed to create the release. Using the following link to create it manually:"));
69
+ consola.error(yellow(webUrl));
70
+ throw e;
71
+ }
72
+ }
73
+ async function execCommand(cmd, args) {
74
+ const { execa } = await import("execa");
75
+ const res = await execa(cmd, args);
76
+ return res.stdout.trim();
77
+ }
78
+ async function isRepoShallow() {
79
+ return (await execCommand("git", ["rev-parse", "--is-shallow-repository"])).trim() === "true";
80
+ }
81
+
82
+ // src/cli.ts
83
+ function setupCli() {
84
+ const cli = cac("githublogen");
85
+ cli.version(version).option("-t, --token <path>", "GitHub Token").help();
86
+ cli.command("").action(async (args) => {
87
+ var _a;
88
+ try {
89
+ const cwd = process.cwd();
90
+ const { options, commits, markdown } = await getChangelogMarkdown(
91
+ {
92
+ cwd,
93
+ ...args
94
+ },
95
+ false
96
+ );
97
+ consola.log(cyan2(options.from) + dim(" -> ") + blue(options.to) + dim(` (${commits.length} commits)`));
98
+ if (!await hasTagOnGitHub(options.to, options.github.repo, options.github.token)) {
99
+ consola.error(yellow2(`Current ref "${bold(options.to)}" is not available as tags on GitHub. Release skipped.`));
100
+ if (process.exitCode) {
101
+ process.exitCode = 1;
102
+ }
103
+ }
104
+ if (!commits.length && await isRepoShallow()) {
105
+ consola.error(
106
+ yellow2(
107
+ "The repo seems to be clone shallowly, which make changelog failed to generate. You might want to specify `fetch-depth: 0` in your CI config."
108
+ )
109
+ );
110
+ if (process.exitCode) {
111
+ process.exitCode = 1;
112
+ }
113
+ return;
114
+ }
115
+ await sendRelease(options, markdown);
116
+ } catch (e) {
117
+ consola.error(red2(String(e)));
118
+ if (e == null ? void 0 : e.stack) {
119
+ consola.error(dim((_a = e.stack) == null ? void 0 : _a.split("\n").slice(1).join("\n")));
120
+ }
121
+ process.exit(1);
122
+ }
123
+ });
124
+ cli.parse();
125
+ }
126
+ setupCli();