@ucdjs/release-scripts 0.1.0-beta.16 → 0.1.0-beta.17

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.
Files changed (2) hide show
  1. package/dist/index.mjs +83 -44
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -387,22 +387,23 @@ async function getGroupedFilesByCommitSha(workspaceRoot, from, to) {
387
387
  //#endregion
388
388
  //#region src/core/changelog.ts
389
389
  const DEFAULT_CHANGELOG_TEMPLATE = dedent`
390
- <% if (it.previousVersion) { %>
390
+ <% if (it.previousVersion) { -%>
391
391
  ## [<%= it.version %>](<%= it.compareUrl %>) (<%= it.date %>)
392
- <% } else { %>
392
+ <% } else { -%>
393
393
  ## <%= it.version %> (<%= it.date %>)
394
394
  <% } %>
395
395
 
396
396
  <% it.groups.forEach((group) => { %>
397
397
  <% if (group.commits.length > 0) { %>
398
- ### <%= group.title %>
399
398
 
399
+ ### <%= group.title %>
400
400
  <% group.commits.forEach((commit) => { %>
401
+
401
402
  * <%= commit.line %>
402
- <% }) %>
403
+ <% }); %>
403
404
 
404
405
  <% } %>
405
- <% }) %>
406
+ <% }); %>
406
407
  `;
407
408
  async function generateChangelogEntry(options) {
408
409
  const { packageName, version, previousVersion, date, commits, owner, repo, groups, template, githubClient } = options;
@@ -411,16 +412,7 @@ async function generateChangelogEntry(options) {
411
412
  includeNonConventional: false,
412
413
  mergeKeys: Object.fromEntries(groups.map((g) => [g.name, g.types]))
413
414
  });
414
- for (const commit of commits) {
415
- if (commit.authors.length === 0) continue;
416
- commit.resolvedAuthors = await Promise.all(commit.authors.map(async (author) => {
417
- const username = await githubClient.resolveAuthorInfo(author.email);
418
- return {
419
- ...author,
420
- username
421
- };
422
- }));
423
- }
415
+ const commitAuthors = await resolveCommitAuthors(commits, githubClient);
424
416
  const templateData = {
425
417
  packageName,
426
418
  version,
@@ -432,27 +424,12 @@ async function generateChangelogEntry(options) {
432
424
  groups: groups.map((group) => {
433
425
  const commitsInGroup = grouped.get(group.name) ?? [];
434
426
  if (commitsInGroup.length > 0) logger.verbose(`Found ${commitsInGroup.length} commits for group "${group.name}".`);
435
- const formattedCommits = commitsInGroup.map((commit) => {
436
- const commitUrl = `https://github.com/${owner}/${repo}/commit/${commit.hash}`;
437
- let line = `${commit.description}`;
438
- if (commit.references.length > 0) logger.verbose("Located references in commit", commit.references.length);
439
- for (const ref of commit.references) {
440
- if (!ref.value) continue;
441
- const number = Number.parseInt(ref.value.replace(/^#/, ""), 10);
442
- if (Number.isNaN(number)) continue;
443
- if (ref.type === "issue") {
444
- line += ` ([Issue ${ref.value}](https://github.com/${owner}/${repo}/issues/${number}))`;
445
- continue;
446
- }
447
- line += ` ([PR ${ref.value}](https://github.com/${owner}/${repo}/pull/${number}))`;
448
- }
449
- line += ` ([${commit.shortHash}](${commitUrl}))`;
450
- if (commit.authors.length > 0) {
451
- console.log(commit.resolvedAuthors);
452
- line += ` (by ${commit.authors.map((a) => a.name).join(", ")})`;
453
- }
454
- return { line };
455
- });
427
+ const formattedCommits = commitsInGroup.map((commit) => ({ line: formatCommitLine({
428
+ commit,
429
+ owner,
430
+ repo,
431
+ authors: commitAuthors.get(commit.hash) ?? []
432
+ }) }));
456
433
  return {
457
434
  name: group.name,
458
435
  title: group.title,
@@ -465,7 +442,7 @@ async function generateChangelogEntry(options) {
465
442
  return eta.renderString(templateToUse, templateData).trim();
466
443
  }
467
444
  async function updateChangelog(options) {
468
- const { version, previousVersion, commits, date, normalizedOptions, workspacePackage } = options;
445
+ const { version, previousVersion, commits, date, normalizedOptions, workspacePackage, githubClient } = options;
469
446
  const changelogPath = join(workspacePackage.path, "CHANGELOG.md");
470
447
  const changelogRelativePath = relative(normalizedOptions.workspaceRoot, join(workspacePackage.path, "CHANGELOG.md"));
471
448
  const existingContent = await readFileFromGit(normalizedOptions.workspaceRoot, normalizedOptions.branch.default, changelogRelativePath);
@@ -480,7 +457,7 @@ async function updateChangelog(options) {
480
457
  repo: normalizedOptions.repo,
481
458
  groups: normalizedOptions.groups,
482
459
  template: normalizedOptions.changelog?.template,
483
- githubClient: options.githubClient
460
+ githubClient
484
461
  });
485
462
  let updatedContent;
486
463
  if (!existingContent) {
@@ -514,6 +491,52 @@ async function updateChangelog(options) {
514
491
  }
515
492
  await writeFile(changelogPath, updatedContent, "utf-8");
516
493
  }
494
+ async function resolveCommitAuthors(commits, githubClient) {
495
+ const authorsByEmail = /* @__PURE__ */ new Map();
496
+ const commitAuthors = /* @__PURE__ */ new Map();
497
+ for (const commit of commits) {
498
+ const authorsForCommit = [];
499
+ commit.authors.forEach((author, idx) => {
500
+ if (!author.email || !author.name) return;
501
+ if (!authorsByEmail.has(author.email)) authorsByEmail.set(author.email, {
502
+ commits: [],
503
+ name: author.name,
504
+ email: author.email
505
+ });
506
+ const info = authorsByEmail.get(author.email);
507
+ if (idx === 0) info.commits.push(commit.shortHash);
508
+ authorsForCommit.push(info);
509
+ });
510
+ commitAuthors.set(commit.hash, authorsForCommit);
511
+ }
512
+ await Promise.all(Array.from(authorsByEmail.values()).map((info) => githubClient.resolveAuthorInfo(info)));
513
+ return commitAuthors;
514
+ }
515
+ function formatCommitLine({ commit, owner, repo, authors }) {
516
+ const commitUrl = `https://github.com/${owner}/${repo}/commit/${commit.hash}`;
517
+ let line = `${commit.description}`;
518
+ const references = commit.references ?? [];
519
+ if (references.length > 0) logger.verbose("Located references in commit", references.length);
520
+ for (const ref of references) {
521
+ if (!ref.value) continue;
522
+ const number = Number.parseInt(ref.value.replace(/^#/, ""), 10);
523
+ if (Number.isNaN(number)) continue;
524
+ if (ref.type === "issue") {
525
+ line += ` ([Issue ${ref.value}](https://github.com/${owner}/${repo}/issues/${number}))`;
526
+ continue;
527
+ }
528
+ line += ` ([PR ${ref.value}](https://github.com/${owner}/${repo}/pull/${number}))`;
529
+ }
530
+ line += ` ([${commit.shortHash}](${commitUrl}))`;
531
+ if (authors.length > 0) {
532
+ const authorList = authors.map((author) => {
533
+ if (author.login) return `[@${author.login}](https://github.com/${author.login})`;
534
+ return author.name;
535
+ }).join(", ");
536
+ line += ` (by ${authorList})`;
537
+ }
538
+ return line;
539
+ }
517
540
  function parseChangelog(content) {
518
541
  const lines = content.split("\n");
519
542
  let packageName = null;
@@ -647,11 +670,24 @@ var GitHubClient = class {
647
670
  });
648
671
  logger.info(`Commit status set to ${farver.cyan(state)} for ${farver.gray(sha.substring(0, 7))}`);
649
672
  }
650
- async resolveAuthorInfo(email) {
651
- const q = encodeURIComponent(`${email} type:user in:email`);
652
- const data = await this.request(`/search/users?q=${q}`);
653
- if (!data.items || data.items.length === 0) return null;
654
- return data.items[0].login;
673
+ async resolveAuthorInfo(info) {
674
+ if (info.login) return info;
675
+ try {
676
+ const q = encodeURIComponent(`${info.email} type:user in:email`);
677
+ const data = await this.request(`/search/users?q=${q}`);
678
+ if (!data.items || data.items.length === 0) return info;
679
+ info.login = data.items[0].login;
680
+ } catch (err) {
681
+ logger.warn(`Failed to resolve author info for email ${info.email}: ${err.message}`);
682
+ }
683
+ if (info.login) return info;
684
+ if (info.commits.length > 0) try {
685
+ const data = await this.request(`/repos/${this.owner}/${this.repo}/commits/${info.commits[0]}`);
686
+ if (data.author && data.author.login) info.login = data.author.login;
687
+ } catch (err) {
688
+ logger.warn(`Failed to resolve author info from commits for email ${info.email}: ${err.message}`);
689
+ }
690
+ return info;
655
691
  }
656
692
  };
657
693
  function createGitHubClient(options) {
@@ -1396,7 +1432,10 @@ async function normalizeReleaseOptions(options) {
1396
1432
  title: options.pullRequest?.title ?? "chore: release new version",
1397
1433
  body: options.pullRequest?.body ?? DEFAULT_PR_BODY_TEMPLATE
1398
1434
  },
1399
- changelog: { enabled: options.changelog?.enabled ?? true }
1435
+ changelog: {
1436
+ enabled: options.changelog?.enabled ?? true,
1437
+ template: options.changelog?.template ?? DEFAULT_CHANGELOG_TEMPLATE
1438
+ }
1400
1439
  };
1401
1440
  }
1402
1441
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ucdjs/release-scripts",
3
- "version": "0.1.0-beta.16",
3
+ "version": "0.1.0-beta.17",
4
4
  "description": "@ucdjs release scripts",
5
5
  "type": "module",
6
6
  "license": "MIT",