@r4lrgx/gitmoji
Version:
🧱 My customized Gitmoji Config - a fork of https://github.com/arvinxx/gitmoji-commit-workflow - just with several bug fixes and a cleaner look.
244 lines (238 loc) • 7.07 kB
JavaScript
import _ from 'lodash';
import pangu from 'pangu';
import fetch from 'sync-fetch';
// @r4lrgx/gitmoji v1.0.3
// MIT License
// src/changelog-config/handlers/displayScopeMap.ts
var displayScopeMap = (scope, customScopeMap) => {
if (!scope || !customScopeMap) return scope;
const custom = customScopeMap[scope];
if (custom) return custom;
const all = customScopeMap["*"];
if (all) {
return all.replace(/\*/g, scope);
}
return scope;
};
var { merge } = _;
var commitTypeMap = {
feat: {
emoji: "\u2728",
title: "Features",
subtitle: "New features and enhancements"
},
fix: {
emoji: "\u{1F41B}",
title: "Bug Fixes",
subtitle: "Resolved bugs and issues"
},
perf: {
emoji: "\u26A1",
title: "Performance Improvements",
subtitle: "Faster, leaner, better"
},
refactor: {
emoji: "\u267B",
title: "Code Refactoring",
subtitle: "Code structure improvements"
},
chore: {
emoji: "\u{1F527}",
title: "Chores",
subtitle: "Other tasks and maintenance"
},
docs: {
emoji: "\u{1F4DD}",
title: "Documentation",
subtitle: "Docs updates and improvements"
},
build: {
emoji: "\u{1F4E6}\uFE0F",
title: "Build System",
subtitle: "Changes to build tools and processes"
},
ci: {
emoji: "\u{1F477}",
title: "Continuous Integration",
subtitle: "CI config updates and automation"
},
test: {
emoji: "\u2705",
title: "Tests",
subtitle: "Added or updated tests"
},
style: {
emoji: "\u{1F3A8}",
title: "Styles",
subtitle: "Visual tweaks and formatting"
},
wip: {
emoji: "\u{1F691}\uFE0F",
title: "Cleaning",
subtitle: "Work in progress or cleanup"
},
revert: {
emoji: "\u23EA",
title: "Reverts",
subtitle: "Undone changes and rollbacks"
}
};
var defineCommitTypeMap = (customCommitTypeMap) => {
if (!customCommitTypeMap) return commitTypeMap;
return merge(customCommitTypeMap, commitTypeMap);
};
var displayCommitType = (commitType, config) => {
const { withEmoji = true, customCommitTypeMap = void 0 } = config;
const diplayCommitTypeMap = defineCommitTypeMap(customCommitTypeMap);
if (commitType in diplayCommitTypeMap) {
const item = diplayCommitTypeMap[commitType];
const { emoji, title } = item;
return `${withEmoji ? `${emoji} ` : ""}${title}`;
}
return commitType;
};
function gitHubLookup(email) {
const match = email.match(/^(\d*)\+?([a-zA-Z0-9\-]*)@users\.noreply\.github\.com$/);
let info = null;
if (match) {
const [_3, id, username] = match;
info = { id, username };
} else {
try {
const res = fetch(`https://api.github.com/search/commits?q=author-email:${encodeURIComponent(email)}`, {
headers: {
Accept: "application/vnd.github.cloak-preview+json"
}
});
if (!res.ok) return null;
const data = res.json();
if (data.items && data.items.length > 0) {
const username = data.items[0]?.author?.login;
const id = data.items[0]?.author?.id;
if (id && username) info = { id, username };
}
} catch (_err) {
return null;
}
}
if (!info?.id || !info?.username) return null;
try {
const res = fetch(`https://api.github.com/users/${info.username}`, {
headers: {
Accept: "application/vnd.github.cloak-preview+json"
}
});
if (!res.ok) return null;
const user = res.json();
return {
authorName: info.username,
authorAvatar: user.avatar_url,
authorUrl: user.html_url,
authorEmail: `${info.id}+${info.username}@users.noreply.github.com`
};
} catch (_err) {
return null;
}
}
// src/commit-types/index.ts
var commitTypes = [
// prettier
"feat",
"fix",
"perf",
"refactor",
"chore",
"docs",
"build",
"ci",
"test",
"style",
"wip",
"revert"
];
var commit_types_default = commitTypes;
// src/changelog-config/handlers/transformer.ts
var { cloneDeep } = _;
var users = /* @__PURE__ */ new Map();
var capitalize = (str) => {
if (!str?.length) return str;
return str.replace(/([a-zA-Z])/u, (firs) => firs.toUpperCase());
};
var transformer = (config) => (
/**
* Processes a single commit
* @param {Commit} commit - The commit object to transform
* @param {Context} context - The context object containing repository info
* @returns {Commit | void} The transformed commit or undefined if excluded
*/
(commit, context) => {
commit = cloneDeep(commit);
const issues = [];
let exclude = true;
commit.notes.forEach((note) => {
note.title = `${config.withEmoji === false ? "" : "\u{1F4A5}"} BREAKING CHANGES`;
exclude = false;
});
const { displayCommitTypes = commit_types_default } = config;
if (!displayCommitTypes.includes(commit.type) && exclude || !commit.type) return;
commit.type = displayCommitType(commit.type, config);
if (commit.scope) {
if (commit.scope === "*") {
commit.scope = "";
}
if (config.displayScopes) {
if (!config.displayScopes.includes(commit.scope)) return;
}
if (config.customScopeMap) {
commit.scope = displayScopeMap(commit.scope, config.customScopeMap);
}
commit.scope = capitalize(commit.scope);
}
if (commit.hash) {
commit.hash = commit.hash.substring(0, 7);
}
if (commit.subject && typeof commit.subject === "string") {
let repository = context.repository ? `${context.host}/${context.owner}/${context.repository}` : context.repoUrl;
if (repository) {
repository = `${repository}/issues/`;
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_3, issue) => {
issues.push({ issue });
return `[#${issue}](${repository}${issue})`;
});
}
if (commit.host && typeof commit.host === "string") {
commit.subject = commit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_3, username) => {
if (username.includes("/")) {
return `@${username}`;
}
return `[@${username}](${context.host}/${username})`;
});
}
}
commit.references = commit.references.filter((reference) => {
const values = new Set(issues.map((item) => item.issue));
return !values.has(reference.issue);
});
if (commit.subject) {
commit.rawSubject = commit.subject;
commit.subject = pangu.spacing(capitalize(commit.subject));
}
const authorEmail = commit.author.email;
if (config.showAuthor || config.showAuthorAvatar) {
if (!users.has(authorEmail)) {
try {
const author2 = gitHubLookup(authorEmail);
users.set(authorEmail, author2);
} catch (_e) {
users.set(authorEmail, null);
}
}
const author = users.get(authorEmail);
Object.assign(commit, author);
}
return commit;
}
);
export { commitTypeMap, defineCommitTypeMap, displayCommitType, displayScopeMap, transformer };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map