import { forEachFiles } from '@utils/forEachFiles'; import { getPathOnCWD } from '@utils/path'; import { validateFilename } from '@utils/validateFileName'; import { COMMIT_TYPES_MAP, CommitType } from './getCommitInfo.data'; export const getCommitScopes = (options?: { include?: string[]; ignored?: string[]; }) => { const { include, ignored } = options || {}; const scopes: string[] = []; forEachFiles(getPathOnCWD('./'), (file) => scopes.push(file.name), { filter: ({ name }) => validateFilename({ filename: name, include, ignored, }), }); return scopes; }; export const isCommitType = (string: string): string is CommitType => { return string in COMMIT_TYPES_MAP; }; export const commitTypeToChoiceItem = (type: string) => { if (!isCommitType(type)) throw Error(`Invalid Commit Type : ${type}`); const { message, emoji, value } = COMMIT_TYPES_MAP[type]; const prefix = emoji + ' '; return { message: prefix + (value + ':').padEnd(12, ' ') + message, value, }; }; export const createCommitMessage = (arg: { type: string; message: string; scope?: string; detail?: string; issueNumber?: number; isCloseIssue?: boolean; }) => { const { type, message, scope, detail, issueNumber, isCloseIssue } = arg; if (!isCommitType(type)) throw Error(`Invalid Commit Type : ${type}`); const { emoji } = COMMIT_TYPES_MAP[type]; let subject = type + (scope ? `(${scope}):` : ':') + ' ' + `${emoji}${message}`; let details = ''; if (detail) details += detail; if (!!issueNumber) { if (isCloseIssue) details += '\nClose '; else details += '\nIssue '; details += '#' + issueNumber; } return { subject, details, }; };