@quiteer/scripts 0.0.11 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @quiteer/scripts
2
2
  > 简化多包仓库的版本管理与变更日志生成,提供便捷的 CLI 命令。
3
- > [文档](https://quiteerjs.github.io/web/ci/scripts.html)
3
+ > [文档](https://quiteerjs.github.io/web/cli/scripts.html)
4
4
 
5
5
  ## 安装
6
6
 
package/dist/index.mjs CHANGED
@@ -12,7 +12,7 @@ import enquirer from "enquirer";
12
12
  import { versionBump } from "bumpp";
13
13
 
14
14
  //#region package.json
15
- var version = "0.0.11";
15
+ var version = "0.1.0";
16
16
 
17
17
  //#endregion
18
18
  //#region src/locales/changelog.ts
@@ -550,14 +550,14 @@ const defaultOptions = {
550
550
  }
551
551
  };
552
552
  async function loadCliOptions(overrides, cwd = process.cwd()) {
553
- const { config } = await loadConfig({
553
+ const { config: config$1 } = await loadConfig({
554
554
  name: "quiteer",
555
555
  defaults: defaultOptions,
556
556
  overrides,
557
557
  cwd,
558
558
  packageJson: true
559
559
  });
560
- return config;
560
+ return config$1;
561
561
  }
562
562
 
563
563
  //#endregion
@@ -906,6 +906,13 @@ async function release(tagPrefix) {
906
906
  groupOutput: cli.changelog.groupOutput,
907
907
  timelineOutput: cli.changelog.timelineOutput
908
908
  });
909
+ await execCommand("git", [
910
+ "-C",
911
+ repoRoot,
912
+ "add",
913
+ "-A"
914
+ ]);
915
+ await gitCommit(lang);
909
916
  }
910
917
 
911
918
  //#endregion
@@ -986,6 +993,151 @@ async function updatePkg(args = ["--deep", "-u"]) {
986
993
  execCommand("npx", ["npm-check-updates", ...args], { stdio: "inherit" });
987
994
  }
988
995
 
996
+ //#endregion
997
+ //#region src/customize/git-commit.ts
998
+ /**
999
+ * 解析分支名获取版本号
1000
+ *
1001
+ * 从分支名中提取版本号部分并添加 'v' 前缀。例如将 'dev-1.2.3.4-patch' 转换为 'v1.2.3.4-patch'。
1002
+ * 这样符合 Conventional Commits 的 scope 规范,同时也满足特定的版本管理需求。
1003
+ *
1004
+ * @param branch - Git 分支名称
1005
+ * @returns 格式化后的版本号字符串,如果不匹配 'dev-' 前缀则返回原分支名
1006
+ *
1007
+ * @example
1008
+ * ```ts
1009
+ * parseBranchToVersion('dev-1.2.3.4-patch') // 返回 'v1.2.3.4-patch'
1010
+ * parseBranchToVersion('feat-login') // 返回 'feat-login'
1011
+ * ```
1012
+ */
1013
+ function parseBranchToVersion(branch) {
1014
+ if (!branch) return "";
1015
+ const match = branch.match(/^dev-(\d+\.\d+\.\d+\.\d+(?:-[a-z0-9]+)?)$/);
1016
+ if (match && match[1]) return `v${match[1]}`;
1017
+ return branch;
1018
+ }
1019
+ /**
1020
+ * 获取当前 Git 分支名称
1021
+ *
1022
+ * 使用 git symbolic-ref 获取当前分支的短名称
1023
+ *
1024
+ * @returns {Promise<string>} 分支名称,获取失败返回空字符串
1025
+ */
1026
+ async function getBranchName() {
1027
+ try {
1028
+ return (await execCommand("git", [
1029
+ "symbolic-ref",
1030
+ "--short",
1031
+ "HEAD"
1032
+ ])).trim();
1033
+ } catch {
1034
+ return "";
1035
+ }
1036
+ }
1037
+ const config = {
1038
+ gitCommitMessages: {
1039
+ types: "请选择提交类型",
1040
+ scopes: "请输入当前分支信息或者按下 [Tab] 键自动填充",
1041
+ description: `请输入描述信息(${yellow("!")}开头表示破坏性改动`
1042
+ },
1043
+ gitCommitTypes: [
1044
+ ["feat", "新功能"],
1045
+ ["feat-wip", "开发中的功能,比如某功能的部分代码"],
1046
+ ["fix", "修复Bug"],
1047
+ ["docs", "只涉及文档更新"],
1048
+ ["typo", "代码或文档勘误,比如错误拼写"],
1049
+ ["style", "修改代码风格,不影响代码含义的变更"],
1050
+ ["refactor", "代码重构,既不修复 bug 也不添加功能的代码变更"],
1051
+ ["perf", "可提高性能的代码更改"],
1052
+ ["optimize", "优化代码质量的代码更改"],
1053
+ ["test", "添加缺失的测试或更正现有测试"],
1054
+ ["build", "影响构建系统或外部依赖项的更改"],
1055
+ ["ci", "对 CI 配置文件和脚本的更改"],
1056
+ ["chore", "没有修改src或测试文件的其他变更"],
1057
+ ["revert", "还原先前的提交"]
1058
+ ]
1059
+ };
1060
+ /**
1061
+ * 交互式生成符合 Conventional Commits 的提交信息并执行提交 (天泽智联定制版)
1062
+ * - 提交范围 (scope) 默认为当前 Git 分支名称
1063
+ * - 支持取消或非交互环境下安全退出
1064
+ * @param {Lang} lang 交互提示语言
1065
+ * @returns {Promise<void>} 异步任务
1066
+ */
1067
+ async function gitCommitTz() {
1068
+ try {
1069
+ const { prompt: ask } = enquirer;
1070
+ const { gitCommitMessages, gitCommitTypes } = config;
1071
+ const initialScope = parseBranchToVersion(await getBranchName());
1072
+ const typesChoices = gitCommitTypes.map(([value, msg]) => {
1073
+ return {
1074
+ name: value,
1075
+ message: `${`${value}:`.padEnd(12)}${msg}`
1076
+ };
1077
+ });
1078
+ const result = await ask([
1079
+ {
1080
+ name: "types",
1081
+ type: "select",
1082
+ message: gitCommitMessages.types,
1083
+ choices: typesChoices
1084
+ },
1085
+ {
1086
+ name: "scopes",
1087
+ type: "input",
1088
+ message: gitCommitMessages.scopes,
1089
+ initial: initialScope,
1090
+ validate(value) {
1091
+ if (!value.trim()) return "分支信息 (scope) 不能为空";
1092
+ return true;
1093
+ }
1094
+ },
1095
+ {
1096
+ name: "description",
1097
+ type: "input",
1098
+ message: gitCommitMessages.description,
1099
+ initial: "输入本次提交的描述信息",
1100
+ validate(value) {
1101
+ if (!value.trim() || value === "输入本次提交的描述信息") return "提交描述不能为空";
1102
+ if (value.length < 2) return "提交描述长度不能少于 2 个字符";
1103
+ return true;
1104
+ }
1105
+ }
1106
+ ]);
1107
+ if (!result) return;
1108
+ const breaking = result.description.startsWith("!") ? "!" : "";
1109
+ const description = result.description.replace(/^!/, "").trim();
1110
+ await execCommand("git", [
1111
+ "commit",
1112
+ "-m",
1113
+ `${result.types}(${result.scopes})${breaking}: ${description}`
1114
+ ], { stdio: "inherit" });
1115
+ } catch {}
1116
+ }
1117
+
1118
+ //#endregion
1119
+ //#region src/customize/git-commit-verify.ts
1120
+ /** Git commit message verify */
1121
+ async function gitCommitVerifyTz(lang = "zh-cn", ignores = []) {
1122
+ const gitPath = await execCommand("git", ["rev-parse", "--show-toplevel"]);
1123
+ const commitMsg = readFileSync(path.join(gitPath, ".git", "COMMIT_EDITMSG"), "utf8").trim();
1124
+ if (ignores.some((regExp) => regExp.test(commitMsg))) return;
1125
+ const REG_EXP = /^(?<type>[a-z-]+)(?:\((?<scope>.+)\))?(?<breaking>!)?: (?<description>.+)$/i;
1126
+ const VERSION_REG_EXP = /^v\d+\.\d+\.\d+\.\d+(-[a-z0-9]+)?$/;
1127
+ const match = commitMsg.match(REG_EXP);
1128
+ if (!match) {
1129
+ const errorMsg = locales[lang].gitCommitVerify;
1130
+ throw new Error(errorMsg);
1131
+ }
1132
+ const { type, scope } = match.groups || {};
1133
+ if (type === "fix") {
1134
+ if (!scope || !VERSION_REG_EXP.test(scope)) {
1135
+ const errorMsg = lang === "zh-cn" ? `[校验失败]: 当提交类型为 'fix' 时,scope 必须是符合规范的版本号 (例如: v1.2.3.4 或 v1.2.3.4-patch)` : `[Verify Failed]: When commit type is 'fix', scope must be a valid version (e.g., v1.2.3.4 or v1.2.3.4-patch)`;
1136
+ throw new Error(errorMsg);
1137
+ }
1138
+ }
1139
+ }
1140
+
989
1141
  //#endregion
990
1142
  //#region src/index.ts
991
1143
  async function setupCli() {
@@ -1046,6 +1198,13 @@ async function setupCli() {
1046
1198
  timelineOutput: args.timelineOutput || cfg.timelineOutput
1047
1199
  });
1048
1200
  });
1201
+ cli.command("git-commit-tianze", `${bgGreen(white("便捷命令"))} ${lightBlue("qui gz")} 天泽智联的 git 提交前后的操作和规范等`).alias("gz").option("--add", "添加所有变更文件到暂存区", { default: cliOptions.gitCommit.add }).action(async (args) => {
1202
+ if (args?.add) await gitCommitAdd();
1203
+ await gitCommitTz();
1204
+ });
1205
+ cli.command("git-commit-verify-tianze", `${bgGreen(white("便捷命令"))} ${lightBlue("qui gzv")} 天泽智联的 git commit 校验提交信息是否符合标准`).alias("gzv").action(async () => {
1206
+ await gitCommitVerifyTz();
1207
+ });
1049
1208
  cli.version(lightGreen(version)).help().parse();
1050
1209
  }
1051
1210
  setupCli();
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@quiteer/scripts",
3
3
  "type": "module",
4
- "version": "0.0.11",
5
- "homepage": "https://quiteerjs.github.io/web/ci/scripts.html",
4
+ "version": "0.1.0",
5
+ "homepage": "https://quiteerjs.github.io/web/cli/scripts.html",
6
6
  "publishConfig": {
7
7
  "access": "public",
8
8
  "registry": "https://registry.npmjs.org/"
@@ -49,6 +49,6 @@
49
49
  "scripts": {
50
50
  "dev": "tsdown -w",
51
51
  "build": "tsdown",
52
- "release": "qui r --tag-prefix scripts && qui gc && tsdown && pnpm publish"
52
+ "release": "qui r --tag-prefix scripts && tsdown && pnpm publish"
53
53
  }
54
54
  }