@ckpack/ai-commit 1.0.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +103 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ckvv
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
创建 TS lib 库的启动模版
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { EOL } from "node:os";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
function runCommand(command, args, options = {}) {
|
|
7
|
+
const result = spawnSync(command, args, {
|
|
8
|
+
encoding: "utf-8",
|
|
9
|
+
...options
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
stdout: result.stdout || "",
|
|
13
|
+
stderr: result.stderr || "",
|
|
14
|
+
status: result.status,
|
|
15
|
+
error: result.error
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function ensureNonEmptyDiff(options) {
|
|
19
|
+
const ignores = (options?.ignores ?? [
|
|
20
|
+
"**/*.log",
|
|
21
|
+
"**/pnpm-lock.yaml",
|
|
22
|
+
"**/package-lock.json",
|
|
23
|
+
"**/yarn.lock"
|
|
24
|
+
]).map((v) => `:(top,glob,exclude)${v}`);
|
|
25
|
+
const baseArgs = [
|
|
26
|
+
"diff",
|
|
27
|
+
"--no-color",
|
|
28
|
+
"-U3",
|
|
29
|
+
"--"
|
|
30
|
+
];
|
|
31
|
+
const stagedDiff = runCommand("git", [
|
|
32
|
+
...baseArgs,
|
|
33
|
+
"--staged",
|
|
34
|
+
...ignores
|
|
35
|
+
]).stdout.trim();
|
|
36
|
+
if (stagedDiff) return stagedDiff;
|
|
37
|
+
const workingDiff = runCommand("git", [...baseArgs, ...ignores]).stdout.trim();
|
|
38
|
+
if (workingDiff) return workingDiff;
|
|
39
|
+
throw new Error("No changes detected. Stage changes or modify files before generating a commit message.");
|
|
40
|
+
}
|
|
41
|
+
function buildPrompt(diff) {
|
|
42
|
+
return [
|
|
43
|
+
"You are an assistant that writes git commit messages.",
|
|
44
|
+
"Return a single-line conventional commit: <type>[optional scope]: <description>.",
|
|
45
|
+
"Use English and keep the description under 50 characters.",
|
|
46
|
+
"Only include the commit message text, no explanations.",
|
|
47
|
+
"Diff:",
|
|
48
|
+
diff
|
|
49
|
+
].join(EOL);
|
|
50
|
+
}
|
|
51
|
+
function sanitizeMessage(raw) {
|
|
52
|
+
return (raw.split(/\r?\n/).map((line) => line.trim()).find(Boolean) ?? "").replace(/\s+/g, " ").trim();
|
|
53
|
+
}
|
|
54
|
+
function isConventional(msg) {
|
|
55
|
+
return (/* @__PURE__ */ new RegExp(`^(revert: )?(${[
|
|
56
|
+
"feat",
|
|
57
|
+
"fix",
|
|
58
|
+
"docs",
|
|
59
|
+
"dx",
|
|
60
|
+
"style",
|
|
61
|
+
"refactor",
|
|
62
|
+
"perf",
|
|
63
|
+
"test",
|
|
64
|
+
"workflow",
|
|
65
|
+
"build",
|
|
66
|
+
"ci",
|
|
67
|
+
"chore",
|
|
68
|
+
"types",
|
|
69
|
+
"wip",
|
|
70
|
+
"release"
|
|
71
|
+
].join("|")})(\\(.+\\))?: .{1,50}$`)).test(msg);
|
|
72
|
+
}
|
|
73
|
+
function tryCodex(prompt) {
|
|
74
|
+
const bin = process.env.CODEX_BIN || "codex";
|
|
75
|
+
const args = process.env.CODEX_ARGS?.split(/\s+/).filter(Boolean) ?? ["exec"];
|
|
76
|
+
const result = args.includes("-") ? runCommand(bin, args, { input: prompt }) : runCommand(bin, [...args, prompt]);
|
|
77
|
+
if (result.status !== 0) {
|
|
78
|
+
if (result.error) console.warn(`[commit-msg] codex execution failed: ${result.error.message}`);
|
|
79
|
+
else if (result.stderr.trim()) console.warn(`[commit-msg] codex stderr: ${result.stderr.trim()}`);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return sanitizeMessage(result.stdout) || null;
|
|
83
|
+
}
|
|
84
|
+
function fallbackMessage() {
|
|
85
|
+
const files = runCommand("git", ["status", "--short"]).stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
86
|
+
if (files.length === 0) return "chore: update changes";
|
|
87
|
+
return `chore: update ${files.slice(0, 3).map((line) => line.replace(/^\S+\s+/, "")).join(", ")}${files.length > 3 ? ` and ${files.length - 3} more` : ""}`;
|
|
88
|
+
}
|
|
89
|
+
function main() {
|
|
90
|
+
try {
|
|
91
|
+
const candidate = sanitizeMessage(tryCodex(buildPrompt(ensureNonEmptyDiff())) ?? "");
|
|
92
|
+
const message = isConventional(candidate) ? candidate : fallbackMessage();
|
|
93
|
+
console.log(message);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const fallback = fallbackMessage();
|
|
96
|
+
console.warn(`[commit-msg] ${error instanceof Error ? error.message : String(error)}. Using fallback message.`);
|
|
97
|
+
console.log(fallback);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
main();
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ckpack/ai-commit",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"private": false,
|
|
6
|
+
"packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac",
|
|
7
|
+
"description": "Create the startup template for the TS lib library.",
|
|
8
|
+
"author": "ckvv",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": "https://github.com/ckvv/template",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./dist/index.js",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"ai-commit": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"commit:generate": "node --experimental-strip-types src/index.ts",
|
|
26
|
+
"dev": "node --import=tsx src/index.ts",
|
|
27
|
+
"build": "tsdown",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"lint": "eslint --fix",
|
|
30
|
+
"publish:npm": "npm run build && npm publish --registry https://registry.npmjs.org/ --access public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^24",
|
|
34
|
+
"@antfu/eslint-config": "^6.7.3",
|
|
35
|
+
"eslint": "^9.39.2",
|
|
36
|
+
"lint-staged": "^16.2.7",
|
|
37
|
+
"simple-git-hooks": "^2.13.1",
|
|
38
|
+
"tsdown": "^0.18.3",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
},
|
|
41
|
+
"simple-git-hooks": {
|
|
42
|
+
"pre-commit": "npx lint-staged"
|
|
43
|
+
},
|
|
44
|
+
"lint-staged": {
|
|
45
|
+
"*.{js,ts,mjs,cjs,tsx,jsx,vue,md}": [
|
|
46
|
+
"eslint --fix"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
}
|