zois-core 2.1.0 → 2.2.0

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.
@@ -2,10 +2,18 @@ import fs from "fs/promises";
2
2
  import path from "path";
3
3
  import chalk from 'chalk';
4
4
  import { Octokit } from "@octokit/rest";
5
+ import { existsSync } from "fs";
6
+ import { simpleGit } from "simple-git";
7
+ import { Terminal } from "./lib/terminal.mjs";
5
8
 
6
9
  const octokit = new Octokit();
10
+ const terminal = new Terminal("zois-core");
11
+ const git = simpleGit();
7
12
  const args = process.argv.slice(2);
8
13
  const command = args[0];
14
+ const cwd = process.cwd();
15
+ const CHANGELOG_CONFIG_FILE_NAME = "changelog.config.json";
16
+ const CHANGELOG_BUNDLE_FILE_NAME = "changelog.json";
9
17
 
10
18
  function getArg(name) {
11
19
  const arg = args.find((a) => a.startsWith("--" + name));
@@ -16,15 +24,15 @@ function getArg(name) {
16
24
  switch (command) {
17
25
  case "generate-changelog": {
18
26
  generateChangelog().catch((err) => {
19
- console.log(chalk.red("Script failed:"), err.message);
27
+ terminal.error(chalk.red("Script failed:"), err.message);
20
28
  process.exit(1);
21
29
  });
22
30
  break;
23
31
  }
24
32
  default: {
25
- console.log(chalk.red("Unknown command"), `"${chalk.cyan(command)}"`);
26
- console.log("Available commands:")
27
- console.log(`• ${chalk.cyan("generate-changelog")} — Generate changelog bundle`)
33
+ terminal.log(chalk.red("Unknown command"), `"${chalk.cyan(command)}"`);
34
+ terminal.log("Available commands:")
35
+ terminal.log(`• ${chalk.cyan("generate-changelog")} — Generate changelog bundle`)
28
36
  }
29
37
  }
30
38
 
@@ -74,47 +82,97 @@ async function getCommitsSince(owner, repo, sinceSha, perPage = 50) {
74
82
  }));
75
83
 
76
84
  } catch (error) {
77
- console.log(chalk.red("Error getting commits:"), error.message);
85
+ terminal.log(chalk.red("Error getting commits:"), error.message);
78
86
  throw error;
79
87
  }
80
88
  }
81
89
 
90
+ async function getGitRemote() {
91
+ try {
92
+ const remotes = await git.getRemotes(true);
93
+ const origin = remotes.find(r => r.name === "origin");
94
+
95
+ if (!origin || !origin.refs || !origin.refs.fetch) {
96
+ return null;
97
+ }
98
+
99
+ const url = origin.refs.fetch;
100
+ const match = url.match(/(?:[:/])([^/]+)\/([^/]+?)(?:\.git)?$/);
101
+
102
+ if (!match) {
103
+ throw new Error("Git remote could not be identified:", url);
104
+ }
105
+
106
+ return {
107
+ owner: match[1],
108
+ name: match[2],
109
+ url
110
+ };
111
+ } catch (err) {
112
+ terminal.log(chalk.red(err));
113
+ return null;
114
+ }
115
+ }
116
+
82
117
  async function generateChangelog() {
83
- const owner = getArg("owner");
84
- if (!owner) {
85
- console.log(chalk.red("--owner argument not specified"));
86
- process.exit(1);
118
+ let config;
119
+ if (existsSync(path.join(cwd, CHANGELOG_CONFIG_FILE_NAME))) {
120
+ const configText = await fs.readFile(path.join(cwd, CHANGELOG_CONFIG_FILE_NAME), "utf-8");
121
+
122
+ try {
123
+ config = JSON.parse(configText);
124
+ } catch {
125
+ terminal.log(chalk.red(`Failed to parse ${CHANGELOG_CONFIG_FILE_NAME}`));
126
+ }
127
+ } else {
128
+ terminal.log(`${CHANGELOG_CONFIG_FILE_NAME} was not detected`);
87
129
  }
88
- const repo = getArg("repo");
89
- if (!repo) {
90
- console.log(chalk.red("--repo argument not specified"));
130
+
131
+ const repo = await getGitRemote();
132
+ if (repo === null) {
133
+ terminal.log(chalk.red("Git remote is not detected"));
91
134
  process.exit(1);
92
135
  }
136
+
137
+ const { owner: repoOwner, name: repoName } = repo;
138
+
93
139
  const fromCommit = getArg("from");
94
140
  if (!fromCommit) {
95
- console.log(chalk.red("--from argument not specified"));
141
+ terminal.log(chalk.red("--from argument not specified"));
96
142
  process.exit(1);
97
143
  }
98
- console.log(`Generating changelog from commit ${chalk.cyan(fromCommit)}...`);
99
144
 
100
- const commits = await getCommitsSince(owner, repo, fromCommit);
145
+ terminal.log(`Generating changelog from commit ${chalk.cyan(fromCommit)}...`);
146
+
147
+ const commits = await getCommitsSince(repoOwner, repoName, fromCommit);
148
+
149
+ const contributors = [];
150
+ for (const commit of commits) {
151
+ const name = config?.overrides?.authors?.[commit.author.name]?.name ? config.overrides.authors[commit.author.name].name : commit.author.name;
152
+ if (contributors.find((p) => p.name === name)) continue;
153
+ const avatar_url = config?.overrides?.authors?.[commit.author.name]?.avatar_url ? config.overrides.authors[commit.author.name].avatar_url : (commit.author.avatar_url ?? "https://avatars.githubusercontent.com/" + commit.author.name);
154
+ const is_owner = commit.author.name === repoOwner;
155
+ contributors.push({
156
+ name,
157
+ avatar_url,
158
+ is_owner
159
+ });
160
+ }
101
161
 
102
162
  const resultToWrite = {
103
163
  generated_at: (new Date()).toISOString(),
104
- changes: commits.map((c) => ({
105
- message: c.message,
106
- sha: c.sha,
107
- author: {
108
- name: c.author.name,
109
- avatar_url: c.author.avatar_url ?? "https://avatars.githubusercontent.com/" + c.author.name,
110
- },
111
- date: c.date,
164
+ contributors,
165
+ changes: commits.map((commit) => ({
166
+ message: commit.message,
167
+ sha: commit.sha,
168
+ author: config?.overrides?.authors?.[commit.author.name]?.name ? config.overrides.authors[commit.author.name].name : commit.author.name,
169
+ date: commit.date,
112
170
  tags: [],
113
- commit_url: `https://github.com/${owner}/${repo}/commit/${c.sha}`
171
+ commit_url: `https://github.com/${repoOwner}/${repoName}/commit/${commit.sha}`
114
172
  }))
115
173
  };
116
174
 
117
- const outputPath = path.join(process.cwd(), "changelog.json");
175
+ const outputPath = path.join(cwd, CHANGELOG_BUNDLE_FILE_NAME);
118
176
 
119
177
  await fs.writeFile(
120
178
  outputPath,
@@ -122,5 +180,5 @@ async function generateChangelog() {
122
180
  "utf-8"
123
181
  );
124
182
 
125
- console.log(`Generated changelog.json. (${commits.length} commits)`);
183
+ terminal.log(`Generated changelog.json. (${commits.length} commits)`);
126
184
  }