@infly/libs 2.1.6 → 2.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -59,7 +59,7 @@
59
59
  "author": "Kahal",
60
60
  "license": "ISC",
61
61
  "dependencies": {
62
- "@infly/ts-libs": "^0.1.15",
62
+ "@infly/ts-libs": "^1.0.1",
63
63
  "connect": "^3.7.0",
64
64
  "dotenv": "16.6.1",
65
65
  "enquirer": "^2.4.1",
@@ -1,113 +1,113 @@
1
- const path = require("node:path");
2
- const { spawnSync } = require("node:child_process");
3
-
4
- /**
5
- * 在指定目录执行 git 命令,自动处理 safe.directory
6
- */
7
- function runGit(cwd, args, { allowFailure = false } = {}) {
8
- const absoluteCwd = path.resolve(cwd);
9
- const safeDirectory = absoluteCwd.replaceAll("\\", "/");
10
- const result = spawnSync(
11
- "git",
12
- ["-c", `safe.directory=${safeDirectory}`, "-C", absoluteCwd, ...args],
13
- { encoding: "utf8", windowsHide: true },
14
- );
15
-
16
- if (result.status !== 0 && !allowFailure) {
17
- const detail = (result.stderr || result.stdout || "Git command failed").trim();
18
- throw new Error(detail);
19
- }
20
- if (result.status !== 0 && allowFailure) {
21
- const detail = String(result.stderr || result.stdout || "").trim();
22
- if (detail) {
1
+ const path = require("node:path");
2
+ const { spawnSync } = require("node:child_process");
3
+
4
+ /**
5
+ * 在指定目录执行 git 命令,自动处理 safe.directory
6
+ */
7
+ function runGit(cwd, args, { allowFailure = false } = {}) {
8
+ const absoluteCwd = path.resolve(cwd);
9
+ const safeDirectory = absoluteCwd.replaceAll("\\", "/");
10
+ const result = spawnSync(
11
+ "git",
12
+ ["-c", `safe.directory=${safeDirectory}`, "-C", absoluteCwd, ...args],
13
+ { encoding: "utf8", windowsHide: true },
14
+ );
15
+
16
+ if (result.status !== 0 && !allowFailure) {
17
+ const detail = (result.stderr || result.stdout || "Git command failed").trim();
18
+ throw new Error(detail);
19
+ }
20
+ if (result.status !== 0 && allowFailure) {
21
+ const detail = String(result.stderr || result.stdout || "").trim();
22
+ if (detail) {
23
23
  process.stderr.write(`[status:submodule] git ${args.join(" ")}: ${detail}\n`);
24
- }
25
- }
26
-
27
- return result.status === 0 ? result.stdout.trim() : "";
28
- }
29
-
30
- /**
31
- * 从 .gitmodules 中读取所有已配置的子模块(名称、路径、配置分支)
32
- */
33
- function getConfiguredSubmodules(rootDir) {
34
- const output = runGit(rootDir, [
35
- "config",
36
- "--file",
37
- ".gitmodules",
38
- "--get-regexp",
39
- "^submodule\\..*\\.path$",
40
- ]);
41
-
42
- return output.split(/\r?\n/).filter(Boolean).map((line) => {
43
- const separator = line.search(/\s/);
44
- const key = line.slice(0, separator);
45
- const submodulePath = line.slice(separator).trim();
46
- const name = key.slice("submodule.".length, -".path".length);
47
- const configuredBranch = runGit(
48
- rootDir,
49
- ["config", "--file", ".gitmodules", "--get", `submodule.${name}.branch`],
50
- { allowFailure: true },
51
- );
52
-
53
- return { name, path: submodulePath, configuredBranch };
54
- });
55
- }
56
-
57
- /**
58
- * 获取主仓 HEAD 中记录的某个子模块指针 commit
59
- */
60
- function getParentPointer(rootDir, submodulePath) {
61
- const treeEntry = runGit(rootDir, ["ls-tree", "HEAD", "--", submodulePath]);
62
- return treeEntry ? treeEntry.split(/\s+/)[2] : "";
63
- }
64
-
65
- /**
66
- * 检查单个子模块的状态:当前分支、HEAD、指针漂移、远程漂移、是否 dirty
67
- */
68
- function inspectSubmodule(rootDir, submodule) {
69
- const worktree = path.join(rootDir, submodule.path);
70
- const pointer = getParentPointer(rootDir, submodule.path);
71
- const head = runGit(worktree, ["rev-parse", "HEAD"], { allowFailure: true });
72
-
73
- if (!head) {
74
- return {
75
- path: submodule.path,
76
- branch: "-",
77
- head: "-",
78
- pointer: pointer ? pointer.slice(0, 8) : "-",
79
- originMaster: "-",
80
- state: "not-initialized",
81
- };
82
- }
83
-
84
- const branch = runGit(worktree, ["branch", "--show-current"], { allowFailure: true });
85
- const remoteBranch = submodule.configuredBranch || branch || "master";
86
- const originHead = runGit(
87
- worktree,
88
- ["rev-parse", "--verify", `origin/${remoteBranch}`],
89
- { allowFailure: true },
90
- );
91
- const dirty = Boolean(runGit(worktree, ["status", "--porcelain"], { allowFailure: true }));
92
- const states = [];
93
-
94
- if (dirty) states.push("dirty");
95
- if (pointer && head !== pointer) states.push("pointer-drift");
96
- if (originHead && head !== originHead) states.push("remote-drift");
97
-
98
- return {
99
- path: submodule.path,
100
- branch: branch || "DETACHED",
101
- head: head.slice(0, 8),
102
- pointer: pointer ? pointer.slice(0, 8) : "-",
103
- originMaster: originHead ? originHead.slice(0, 8) : "-",
104
- state: states.length > 0 ? states.join(",") : "ok",
105
- };
106
- }
107
-
108
- module.exports = {
109
- getConfiguredSubmodules,
110
- getParentPointer,
111
- inspectSubmodule,
112
- runGit,
113
- };
24
+ }
25
+ }
26
+
27
+ return result.status === 0 ? result.stdout.trim() : "";
28
+ }
29
+
30
+ /**
31
+ * 从 .gitmodules 中读取所有已配置的子模块(名称、路径、配置分支)
32
+ */
33
+ function getConfiguredSubmodules(rootDir) {
34
+ const output = runGit(rootDir, [
35
+ "config",
36
+ "--file",
37
+ ".gitmodules",
38
+ "--get-regexp",
39
+ "^submodule\\..*\\.path$",
40
+ ]);
41
+
42
+ return output.split(/\r?\n/).filter(Boolean).map((line) => {
43
+ const separator = line.search(/\s/);
44
+ const key = line.slice(0, separator);
45
+ const submodulePath = line.slice(separator).trim();
46
+ const name = key.slice("submodule.".length, -".path".length);
47
+ const configuredBranch = runGit(
48
+ rootDir,
49
+ ["config", "--file", ".gitmodules", "--get", `submodule.${name}.branch`],
50
+ { allowFailure: true },
51
+ );
52
+
53
+ return { name, path: submodulePath, configuredBranch };
54
+ });
55
+ }
56
+
57
+ /**
58
+ * 获取主仓 HEAD 中记录的某个子模块指针 commit
59
+ */
60
+ function getParentPointer(rootDir, submodulePath) {
61
+ const treeEntry = runGit(rootDir, ["ls-tree", "HEAD", "--", submodulePath]);
62
+ return treeEntry ? treeEntry.split(/\s+/)[2] : "";
63
+ }
64
+
65
+ /**
66
+ * 检查单个子模块的状态:当前分支、HEAD、指针漂移、远程漂移、是否 dirty
67
+ */
68
+ function inspectSubmodule(rootDir, submodule) {
69
+ const worktree = path.join(rootDir, submodule.path);
70
+ const pointer = getParentPointer(rootDir, submodule.path);
71
+ const head = runGit(worktree, ["rev-parse", "HEAD"], { allowFailure: true });
72
+
73
+ if (!head) {
74
+ return {
75
+ path: submodule.path,
76
+ branch: "-",
77
+ head: "-",
78
+ pointer: pointer ? pointer.slice(0, 8) : "-",
79
+ originMaster: "-",
80
+ state: "not-initialized",
81
+ };
82
+ }
83
+
84
+ const branch = runGit(worktree, ["branch", "--show-current"], { allowFailure: true });
85
+ const remoteBranch = submodule.configuredBranch || branch || "master";
86
+ const originHead = runGit(
87
+ worktree,
88
+ ["rev-parse", "--verify", `origin/${remoteBranch}`],
89
+ { allowFailure: true },
90
+ );
91
+ const dirty = Boolean(runGit(worktree, ["status", "--porcelain"], { allowFailure: true }));
92
+ const states = [];
93
+
94
+ if (dirty) states.push("dirty");
95
+ if (pointer && head !== pointer) states.push("pointer-drift");
96
+ if (originHead && head !== originHead) states.push("remote-drift");
97
+
98
+ return {
99
+ path: submodule.path,
100
+ branch: branch || "DETACHED",
101
+ head: head.slice(0, 8),
102
+ pointer: pointer ? pointer.slice(0, 8) : "-",
103
+ originMaster: originHead ? originHead.slice(0, 8) : "-",
104
+ state: states.length > 0 ? states.join(",") : "ok",
105
+ };
106
+ }
107
+
108
+ module.exports = {
109
+ getConfiguredSubmodules,
110
+ getParentPointer,
111
+ inspectSubmodule,
112
+ runGit,
113
+ };