|
1 | | -import * as fs from 'node:fs'; |
| 1 | +import { Message } from './message.ts'; |
| 2 | +import { FilePath } from './file-path.ts'; |
2 | 3 |
|
3 | | -// eslint-disable-next-line @stylistic/max-len |
4 | | -const commitRE = /^(revert:? "?|Revert "?)?(void|fix|feat|docs|style|perf|test|types|build|chore|refactor|workflow|ci|wip|release|breaking change)(\(.+\))?: .{1,50}/; |
5 | | -const mergeRE = /Merge (remote-tracking )?branch /; |
| 4 | +export { Message, FilePath }; |
6 | 5 |
|
7 | | -export const run = (commitMessage: string) => { |
8 | | - let content = ''; |
9 | | - if (!commitRE.test(commitMessage) && !mergeRE.test(commitMessage)) { |
10 | | - content += `\nInvalid commit message: "${commitMessage}".\n`; |
11 | | - content += `\nExamples: \n`; |
12 | | - content += ` - fix(Button): incorrect style\n`; |
13 | | - content += ` - feat(Button): incorrect style\n`; |
14 | | - content += ` - docs(Button): fix typo\n`; |
15 | | - content += `\nAllowed Types:\n`; |
16 | | - content += ` - fix:修补bug\n`; |
17 | | - content += ` - feat:新功能(feature)\n`; |
18 | | - content += ` - docs:文档(documentation)\n`; |
19 | | - content += ` - style:不影响代码含义的更改,可能与代码格式有关,例如空格、缺少分号等\n`; |
20 | | - content += ` - test:包括新的或更正以前的测试\n`; |
21 | | - content += ` - chore:构建过程或辅助工具的变动\n`; |
22 | | - content += ` - refactor:重构(即不是新增功能,也不是修改bug的代码变动)\n`; |
23 | | - content += ` - perf:性能改进(performance improvements)\n`; |
24 | | - content += ` - types:类型\n`; |
25 | | - content += ` - build:影响构建系统或外部依赖项的更改\n`; |
26 | | - content += ` - ci: 持续集成相关\n`; |
27 | | - content += ` - breaking change:破坏性修改\n`; |
28 | | - content += ` - void:无类型,通常用于初始化\n`; |
29 | | - content += ` - Merge branch 'foo' into 'bar'\n`; |
30 | | - content += ` - Revert ""\n`; |
31 | | - } |
32 | | - return content; |
33 | | -}; |
| 6 | +/** |
| 7 | + * 一次只执行一项任务 |
| 8 | + * @returns error |
| 9 | + */ |
| 10 | +const check = () => { |
| 11 | + for (let i = 0; i < process.argv.length; i++) { |
| 12 | + /* istanbul ignore next -- @preserve */ |
| 13 | + if (['--edit', '--message'].includes(process.argv[i])) { // @deprecated(--edit) |
| 14 | + /* istanbul ignore next -- @preserve */ |
| 15 | + return Message.run(i + 1); |
| 16 | + } |
34 | 17 |
|
35 | | -const index = process.argv.findIndex(arg => arg === '--edit'); |
36 | | -/* istanbul ignore next -- @preserve */ |
37 | | -const filepath = index !== -1 && process.argv[index + 1]; |
38 | | -/* istanbul ignore next -- @preserve */ |
39 | | -const message = filepath && fs.existsSync(filepath) |
40 | | - ? fs.readFileSync(filepath, 'utf-8').trim() |
41 | | - : (filepath || ''); |
| 18 | + /* istanbul ignore next -- @preserve */ |
| 19 | + if (['--file-path'].includes(process.argv[i])) { |
| 20 | + /* istanbul ignore next -- @preserve */ |
| 21 | + return FilePath.run(i + 1); |
| 22 | + } |
| 23 | + }; |
| 24 | +}; |
42 | 25 |
|
| 26 | +const error = check(); |
43 | 27 | /* istanbul ignore next -- @preserve */ |
44 | | -if (message) { |
45 | | - const error = run(message); |
46 | | - if (error) { |
47 | | - console.error(error); |
48 | | - process.exit(1); |
49 | | - } |
50 | | -} |
| 28 | +if (error) { |
| 29 | + console.error(error); |
| 30 | + process.exit(1); |
| 31 | +}; |
0 commit comments