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