广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-20 3471290659516cf21db3211a9053daff5f283e03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { prompt } from 'enquirer';
import { execCommand } from '../shared';
import { locales } from '../locales';
import type { Lang } from '../locales';
 
interface PromptObject {
  types: string;
  scopes: string;
  description: string;
}
 
/**
 * Git commit with Conventional Commits standard
 *
 * @param lang
 */
export async function gitCommit(lang: Lang = 'en-us') {
  const { gitCommitMessages, gitCommitTypes, gitCommitScopes } = locales[lang];
 
  const typesChoices = gitCommitTypes.map(([value, msg]) => {
    const nameWithSuffix = `${value}:`;
 
    const message = `${nameWithSuffix.padEnd(12)}${msg}`;
 
    return {
      name: value,
      message
    };
  });
 
  const scopesChoices = gitCommitScopes.map(([value, msg]) => ({
    name: value,
    message: `${value.padEnd(30)} (${msg})`
  }));
 
  const result = await prompt<PromptObject>([
    {
      name: 'types',
      type: 'select',
      message: gitCommitMessages.types,
      choices: typesChoices
    },
    {
      name: 'scopes',
      type: 'select',
      message: gitCommitMessages.scopes,
      choices: scopesChoices
    },
    {
      name: 'description',
      type: 'text',
      message: gitCommitMessages.description
    }
  ]);
 
  const breaking = result.description.startsWith('!') ? '!' : '';
 
  const description = result.description.replace(/^!/, '').trim();
 
  const commitMsg = `${result.types}(${result.scopes})${breaking}: ${description}`;
 
  await execCommand('git', ['commit', '-m', commitMsg], { stdio: 'inherit' });
}
 
/** Git commit message verify */
export async function gitCommitVerify(lang: Lang = 'en-us', ignores: RegExp[] = []) {
  const gitPath = await execCommand('git', ['rev-parse', '--show-toplevel']);
 
  const gitMsgPath = path.join(gitPath, '.git', 'COMMIT_EDITMSG');
 
  const commitMsg = readFileSync(gitMsgPath, 'utf8').trim();
 
  if (ignores.some(regExp => regExp.test(commitMsg))) return;
 
  const REG_EXP = /(?<type>[a-z]+)(?:\((?<scope>.+)\))?(?<breaking>!)?: (?<description>.+)/i;
 
  if (!REG_EXP.test(commitMsg)) {
    const errorMsg = locales[lang].gitCommitVerify;
 
    throw new Error(errorMsg);
  }
}