crabatool 1.0.861 → 1.0.862

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/lib/utils.js CHANGED
@@ -882,38 +882,57 @@ class Utils {
882
882
  }
883
883
  }
884
884
 
885
- // 获取 Git 用户信息(优先读取项目目录下的本地配置)
886
- getGitUser(cwd) {
887
- let name = '',
888
- email = '';
889
- try {
890
- name = String(execSync('git config user.name', { cwd: cwd || process.cwd() })).trim();
891
- } catch (e) { }
885
+ runGitCommand(args, cwd) {
892
886
  try {
893
- email = String(execSync('git config user.email', { cwd: cwd || process.cwd() })).trim();
894
- } catch (e) { }
887
+ return execFileSync('git', args, {
888
+ cwd: cwd || process.cwd(),
889
+ encoding: 'utf8',
890
+ stdio: ['ignore', 'pipe', 'pipe'],
891
+ }).trim();
892
+ } catch (e) {
893
+ return '';
894
+ }
895
+ }
896
+
897
+ isGitWorkTree(cwd) {
898
+ return this.runGitCommand(['rev-parse', '--is-inside-work-tree'], cwd) === 'true';
899
+ }
900
+
901
+ readGitConfig(key, cwd, globalOnly) {
902
+ const args = ['config', '--get'];
903
+ if (globalOnly) {
904
+ args.push('--global');
905
+ }
906
+
907
+ args.push(key);
908
+ return this.runGitCommand(args, cwd);
909
+ }
910
+
911
+ // 获取 Git 用户信息(仓库内优先读取项目配置;非仓库目录读取全局配置)
912
+ getGitUser(cwd) {
913
+ const gitCwd = cwd || process.cwd();
914
+ const globalOnly = !this.isGitWorkTree(gitCwd);
915
+ const name = this.readGitConfig('user.name', gitCwd, globalOnly);
916
+ const email = this.readGitConfig('user.email', gitCwd, globalOnly);
895
917
  return { name, email };
896
918
  }
897
919
 
898
920
  // 获取当前 Git 分支名(优先使用 git 命令,失败则返回空)
899
921
  getCurrentBranch(cwd) {
900
- try {
901
-
902
- const out = execSync('git rev-parse --abbrev-ref HEAD', { cwd: cwd || process.cwd() });
903
- return String(out).trim();
904
- } catch (e) {
922
+ if (!this.isGitWorkTree(cwd)) {
905
923
  return '';
906
924
  }
925
+
926
+ return this.runGitCommand(['rev-parse', '--abbrev-ref', 'HEAD'], cwd);
907
927
  }
908
928
 
909
929
  // 获取远程仓库 URL(origin)
910
930
  getRemoteUrl(cwd) {
911
- try {
912
- const out = execSync('git config --get remote.origin.url', { cwd: cwd || process.cwd() });
913
- return String(out).trim();
914
- } catch (e) {
931
+ if (!this.isGitWorkTree(cwd)) {
915
932
  return '';
916
933
  }
934
+
935
+ return this.readGitConfig('remote.origin.url', cwd, false);
917
936
  }
918
937
 
919
938
  // 根据远程仓库 URL 和分支名,推导分支的网页地址(支持 GitHub/GitLab/Gitee 等常见平台)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crabatool",
3
- "version": "1.0.861",
3
+ "version": "1.0.862",
4
4
  "description": "crabatool",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -101,6 +101,9 @@ function buildFolder(options) {
101
101
  var value;
102
102
  if (key == '$versionDate$') {
103
103
  value = new Date().toString('yyyyMMddhh');
104
+ if (arr[1]) {
105
+ value = arr[1] + value;
106
+ }
104
107
  } else if (arr.length > 1 && arr[1] != null) {
105
108
  value = arr[1].trim();
106
109
  } else {