(
type: keyof typeof commitMessageTemplates,
config: CmsConfig,
{ slug, path, collection, authorLogin, authorName, authorEmail }: Options,
isOpenAuthoring?: boolean,
)
| 48 | }; |
| 49 | |
| 50 | export function commitMessageFormatter( |
| 51 | type: keyof typeof commitMessageTemplates, |
| 52 | config: CmsConfig, |
| 53 | { slug, path, collection, authorLogin, authorName, authorEmail }: Options, |
| 54 | isOpenAuthoring?: boolean, |
| 55 | ) { |
| 56 | const templates = { ...commitMessageTemplates, ...(config.backend.commit_messages || {}) }; |
| 57 | |
| 58 | let trailers = ''; |
| 59 | if (config.backend.signoff_commits) { |
| 60 | if (!authorName) { |
| 61 | console.warn('Option signoff_commits is enabled, but author name is unknown'); |
| 62 | } else if (!authorEmail) { |
| 63 | console.warn('Option signoff_commits is enabled, but author email is unknown'); |
| 64 | } else { |
| 65 | trailers = `\n\nSigned-off-by: ${authorName} <${authorEmail}>\n`; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const commitMessage = templates[type].replace(variableRegex, (_, variable) => { |
| 70 | switch (variable) { |
| 71 | case 'slug': |
| 72 | return slug || ''; |
| 73 | case 'path': |
| 74 | return path || ''; |
| 75 | case 'collection': |
| 76 | return collection ? collection.get('label_singular') || collection.get('label') : ''; |
| 77 | case 'author-login': |
| 78 | return authorLogin || ''; |
| 79 | case 'author-name': |
| 80 | return authorName || ''; |
| 81 | default: |
| 82 | console.warn(`Ignoring unknown variable “${variable}” in commit message template.`); |
| 83 | return ''; |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | if (!isOpenAuthoring) { |
| 88 | return commitMessage + trailers; |
| 89 | } |
| 90 | |
| 91 | const message = templates.openAuthoring.replace(variableRegex, (_, variable) => { |
| 92 | switch (variable) { |
| 93 | case 'message': |
| 94 | return commitMessage; |
| 95 | case 'author-login': |
| 96 | return authorLogin || ''; |
| 97 | case 'author-name': |
| 98 | return authorName || ''; |
| 99 | default: |
| 100 | console.warn(`Ignoring unknown variable “${variable}” in open authoring message template.`); |
| 101 | return ''; |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | return message + trailers; |
| 106 | } |
| 107 |
no test coverage detected