ai-worktool 1.0.7

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.
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.clone = clone;
7
+ exports.add = add;
8
+ exports.commit = commit;
9
+ exports.pull = pull;
10
+ exports.push = push;
11
+ exports.createBranch = createBranch;
12
+ exports.switchBranch = switchBranch;
13
+ exports.getStatus = getStatus;
14
+ exports.addRemote = addRemote;
15
+ exports.setUserInfo = setUserInfo;
16
+ const simple_git_1 = __importDefault(require("simple-git"));
17
+ const path_1 = require("path");
18
+ /**
19
+ * 创建 Git 实例
20
+ * @param repoPath 仓库路径,默认为当前工作目录
21
+ */
22
+ function createGitInstance(repoPath) {
23
+ const options = {
24
+ baseDir: repoPath,
25
+ binary: 'git',
26
+ maxConcurrentProcesses: 6
27
+ };
28
+ return (0, simple_git_1.default)(options);
29
+ }
30
+ /**
31
+ * 克隆远程仓库到本地
32
+ * @param remoteUrl 远程仓库URL
33
+ * @param localPath 本地存储路径
34
+ * @param branch 要克隆的分支
35
+ * @param repoPath 仓库根路径
36
+ */
37
+ async function clone(repoPath, remoteUrl, localPath, branch) {
38
+ const git = createGitInstance(repoPath);
39
+ try {
40
+ const clonePath = localPath ? (0, path_1.join)(repoPath, localPath) : repoPath;
41
+ const options = branch ? ['-b', branch] : [];
42
+ await git.clone(remoteUrl, clonePath, options);
43
+ console.log(`成功克隆仓库: ${remoteUrl} 到 ${clonePath}`);
44
+ }
45
+ catch (error) {
46
+ console.error(`克隆仓库失败: ${error}`);
47
+ throw error;
48
+ }
49
+ }
50
+ /**
51
+ * 添加文件到暂存区
52
+ * @param files 文件路径,可以是单个文件或文件数组
53
+ * @param repoPath 仓库路径
54
+ */
55
+ async function add(files = '.', repoPath) {
56
+ const git = createGitInstance(repoPath);
57
+ try {
58
+ await git.add(files);
59
+ console.log(`成功添加文件到暂存区: ${Array.isArray(files) ? files.join(', ') : files}`);
60
+ }
61
+ catch (error) {
62
+ console.error(`添加文件失败: ${error}`);
63
+ throw error;
64
+ }
65
+ }
66
+ /**
67
+ * 提交代码
68
+ * @param message 提交信息
69
+ * @param files 可选的文件路径,指定要提交的文件
70
+ * @param repoPath 仓库路径
71
+ */
72
+ async function commit(repoPath, message, files) {
73
+ const git = createGitInstance(repoPath);
74
+ try {
75
+ const result = files ? await git.commit(message, files) : await git.commit(message);
76
+ console.log(`提交成功: ${result.summary}`);
77
+ }
78
+ catch (error) {
79
+ console.error(`提交失败: ${error}`);
80
+ throw error;
81
+ }
82
+ }
83
+ /**
84
+ * 拉取远程分支
85
+ * @param remote 远程仓库名称,默认为 'origin'
86
+ * @param branch 分支名称,默认为当前分支
87
+ * @param repoPath 仓库路径
88
+ */
89
+ async function pull(repoPath, remote = 'origin', branch) {
90
+ const git = createGitInstance(repoPath);
91
+ try {
92
+ const currentBranch = branch || (await git.branchLocal()).current;
93
+ const result = await git.pull(remote, currentBranch);
94
+ console.log(`拉取成功: ${result.summary}`);
95
+ }
96
+ catch (error) {
97
+ console.error(`拉取失败: ${error}`);
98
+ throw error;
99
+ }
100
+ }
101
+ /**
102
+ * 推送代码到远程仓库
103
+ * @param remote 远程仓库名称,默认为 'origin'
104
+ * @param branch 分支名称,默认为当前分支
105
+ * @param repoPath 仓库路径
106
+ */
107
+ async function push(repoPath, remote = 'origin', branch) {
108
+ const git = createGitInstance(repoPath);
109
+ try {
110
+ const currentBranch = branch || (await git.branchLocal()).current;
111
+ await git.push(remote, currentBranch);
112
+ console.log(`推送成功: ${remote}/${currentBranch}`);
113
+ }
114
+ catch (error) {
115
+ console.error(`推送失败: ${error}`);
116
+ // 处理常见的推送失败情况(如远程分支有新提交)
117
+ if (error.message.includes('non-fast-forward')) {
118
+ console.log('提示: 远程分支有新提交,建议先拉取再推送');
119
+ }
120
+ throw error;
121
+ }
122
+ }
123
+ /**
124
+ * 创建并切换到新分支
125
+ * @param branchName 新分支名称
126
+ * @param repoPath 仓库路径
127
+ */
128
+ async function createBranch(branchName, repoPath) {
129
+ const git = createGitInstance(repoPath);
130
+ try {
131
+ await git.checkoutLocalBranch(branchName);
132
+ console.log(`成功创建并切换到分支: ${branchName}`);
133
+ }
134
+ catch (error) {
135
+ console.error(`创建分支失败: ${error}`);
136
+ throw error;
137
+ }
138
+ }
139
+ /**
140
+ * 切换分支
141
+ * @param branchName 目标分支名称
142
+ * @param repoPath 仓库路径
143
+ */
144
+ async function switchBranch(branchName, repoPath) {
145
+ const git = createGitInstance(repoPath);
146
+ try {
147
+ await git.checkout(branchName);
148
+ console.log(`成功切换到分支: ${branchName}`);
149
+ }
150
+ catch (error) {
151
+ console.error(`切换分支失败: ${error}`);
152
+ throw error;
153
+ }
154
+ }
155
+ /**
156
+ * 获取当前仓库状态
157
+ * @param repoPath 仓库路径
158
+ */
159
+ async function getStatus(repoPath) {
160
+ const git = createGitInstance(repoPath);
161
+ try {
162
+ return await git.status();
163
+ }
164
+ catch (error) {
165
+ console.error(`获取仓库状态失败: ${error}`);
166
+ throw error;
167
+ }
168
+ }
169
+ /**
170
+ * 添加远程仓库
171
+ * @param remoteName 远程仓库名称
172
+ * @param remoteUrl 远程仓库URL
173
+ * @param repoPath 仓库路径
174
+ */
175
+ async function addRemote(remoteName, remoteUrl, repoPath) {
176
+ const git = createGitInstance(repoPath);
177
+ try {
178
+ await git.addRemote(remoteName, remoteUrl);
179
+ console.log(`成功添加远程仓库: ${remoteName} -> ${remoteUrl}`);
180
+ }
181
+ catch (error) {
182
+ console.error(`添加远程仓库失败: ${error}`);
183
+ throw error;
184
+ }
185
+ }
186
+ /**
187
+ * 设置提交用户信息
188
+ * @param username 用户名
189
+ * @param email 邮箱
190
+ * @param repoPath 仓库路径
191
+ */
192
+ async function setUserInfo(username, email, repoPath) {
193
+ const git = createGitInstance(repoPath);
194
+ try {
195
+ await git.addConfig('user.name', username);
196
+ await git.addConfig('user.email', email);
197
+ console.log(`已设置用户信息: ${username} <${email}>`);
198
+ }
199
+ catch (error) {
200
+ console.error(`设置用户信息失败: ${error}`);
201
+ throw error;
202
+ }
203
+ }
204
+ //# sourceMappingURL=git.js.map
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.modifyTools = void 0;
18
+ exports.wait = wait;
19
+ exports.runTests = runTests;
20
+ const jest_1 = require("./jest");
21
+ const vitest_1 = require("./vitest");
22
+ const mocha_1 = require("./mocha");
23
+ __exportStar(require("./interface"), exports);
24
+ __exportStar(require("./file"), exports);
25
+ // export * from './javascript';
26
+ // export * from './typescript';
27
+ __exportStar(require("./jest"), exports);
28
+ __exportStar(require("./vitest"), exports);
29
+ __exportStar(require("./mocha"), exports);
30
+ __exportStar(require("./git"), exports);
31
+ __exportStar(require("./package"), exports);
32
+ __exportStar(require("./project"), exports);
33
+ exports.modifyTools = [
34
+ 'writeFile',
35
+ 'batchModifyLines',
36
+ 'insertLine',
37
+ 'deleteLine',
38
+ 'modifyLine',
39
+ 'renameFile',
40
+ 'initProject',
41
+ 'installDependencies',
42
+ 'createTsConfigFile',
43
+ 'createGitIgnore',
44
+ 'createPackageJson',
45
+ 'createTsConfigJson',
46
+ 'createJestConfig',
47
+ 'createVitestConfig',
48
+ 'createMochaConfig'
49
+ ];
50
+ async function wait(timerout = 1000) {
51
+ return new Promise((resolve) => {
52
+ setTimeout(() => {
53
+ resolve(true);
54
+ }, timerout);
55
+ });
56
+ }
57
+ function runTests(projectRoot, testFramework, coverageDir, configPath) {
58
+ if (testFramework === 'jest') {
59
+ return (0, jest_1.runJestTests)(projectRoot, coverageDir, configPath);
60
+ }
61
+ else if (testFramework === 'vitest') {
62
+ return (0, vitest_1.runVitestTests)(projectRoot, coverageDir, configPath);
63
+ }
64
+ else if (testFramework === 'mocha') {
65
+ return (0, mocha_1.runMochaTests)(projectRoot, coverageDir, configPath);
66
+ }
67
+ else {
68
+ throw new Error(`尚未支持 ${testFramework} 测试框架`);
69
+ }
70
+ }
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.parseJavaScriptFile = parseJavaScriptFile;
37
+ exports.extractExportedFunctions = extractExportedFunctions;
38
+ exports.extractExportedClasses = extractExportedClasses;
39
+ exports.extractImportedModules = extractImportedModules;
40
+ exports.loadCodeLine = loadCodeLine;
41
+ exports.loadCodeLines = loadCodeLines;
42
+ const fs = __importStar(require("fs"));
43
+ const acorn = __importStar(require("acorn"));
44
+ const walk = __importStar(require("acorn-walk"));
45
+ /**
46
+ * 解析 JavaScript 文件为 AST
47
+ * @param filePath 文件路径
48
+ * @returns AST 对象
49
+ */
50
+ function parseJavaScriptFile(filePath) {
51
+ if (!fs.existsSync(filePath)) {
52
+ throw new Error(`文件不存在: ${filePath}`);
53
+ }
54
+ const sourceCode = fs.readFileSync(filePath, 'utf-8');
55
+ try {
56
+ return acorn.parse(sourceCode, {
57
+ ecmaVersion: 2020,
58
+ sourceType: 'module'
59
+ });
60
+ }
61
+ catch (error) {
62
+ throw new Error(`解析文件失败: ${error.message}`);
63
+ }
64
+ }
65
+ /**
66
+ * 从 AST 中提取导出的函数
67
+ * @param ast AST 对象
68
+ * @returns 导出函数名称数组
69
+ */
70
+ function extractExportedFunctions(ast) {
71
+ const exportedFunctions = [];
72
+ walk.simple(ast, {
73
+ // 处理命名函数导出
74
+ ExportNamedDeclaration(node) {
75
+ if (node.declaration && node.declaration.type === 'FunctionDeclaration') {
76
+ exportedFunctions.push(node.declaration.id.name);
77
+ }
78
+ // 处理变量导出 (可能是函数)
79
+ if (node.specifiers) {
80
+ node.specifiers.forEach((specifier) => {
81
+ if (specifier.exported.type === 'Identifier') {
82
+ exportedFunctions.push(specifier.exported.name);
83
+ }
84
+ });
85
+ }
86
+ },
87
+ // 处理默认导出的函数
88
+ ExportDefaultDeclaration(node) {
89
+ if (node.declaration.type === 'FunctionDeclaration') {
90
+ exportedFunctions.push(node.declaration.id?.name || 'default');
91
+ }
92
+ else if (node.declaration.type === 'Identifier') {
93
+ exportedFunctions.push(`default (${node.declaration.name})`);
94
+ }
95
+ }
96
+ });
97
+ return exportedFunctions;
98
+ }
99
+ /**
100
+ * 从 AST 中提取导出的类
101
+ * @param ast AST 对象
102
+ * @returns 导出类名称数组
103
+ */
104
+ function extractExportedClasses(ast) {
105
+ const exportedClasses = [];
106
+ walk.simple(ast, {
107
+ // 处理命名类导出
108
+ ExportNamedDeclaration(node) {
109
+ if (node.declaration && node.declaration.type === 'ClassDeclaration') {
110
+ exportedClasses.push(node.declaration.id.name);
111
+ }
112
+ // 处理变量导出 (可能是类)
113
+ if (node.specifiers) {
114
+ node.specifiers.forEach((specifier) => {
115
+ if (specifier.exported.type === 'Identifier') {
116
+ exportedClasses.push(specifier.exported.name);
117
+ }
118
+ });
119
+ }
120
+ },
121
+ // 处理默认导出的类
122
+ ExportDefaultDeclaration(node) {
123
+ if (node.declaration.type === 'ClassDeclaration') {
124
+ exportedClasses.push(node.declaration.id?.name || 'default');
125
+ }
126
+ else if (node.declaration.type === 'Identifier') {
127
+ exportedClasses.push(`default (${node.declaration.name})`);
128
+ }
129
+ }
130
+ });
131
+ return exportedClasses;
132
+ }
133
+ /**
134
+ * 从 AST 中提取导入的模块
135
+ * @param ast AST 对象
136
+ * @returns 导入模块路径数组
137
+ */
138
+ function extractImportedModules(ast) {
139
+ const importedModules = [];
140
+ walk.simple(ast, {
141
+ ImportDeclaration(node) {
142
+ importedModules.push(node.source.value);
143
+ }
144
+ });
145
+ return importedModules;
146
+ }
147
+ /**
148
+ * 从文件中加载指定行的代码
149
+ * @param filePath 文件路径
150
+ * @param lineNumber 行号 (从1开始)
151
+ * @returns 指定行的代码
152
+ */
153
+ function loadCodeLine(filePath, lineNumber) {
154
+ if (!fs.existsSync(filePath)) {
155
+ throw new Error(`文件不存在: ${filePath}`);
156
+ }
157
+ const lines = fs.readFileSync(filePath, 'utf-8').split('\n');
158
+ if (lineNumber < 1 || lineNumber > lines.length) {
159
+ throw new Error(`行号超出范围 (1-${lines.length})`);
160
+ }
161
+ return lines[lineNumber - 1];
162
+ }
163
+ /**
164
+ * 从文件中加载指定范围的代码行
165
+ * @param filePath 文件路径
166
+ * @param startLine 起始行号 (从1开始)
167
+ * @param endLine 结束行号 (从1开始)
168
+ * @returns 指定范围的代码
169
+ */
170
+ function loadCodeLines(filePath, startLine, endLine) {
171
+ if (!fs.existsSync(filePath)) {
172
+ throw new Error(`文件不存在: ${filePath}`);
173
+ }
174
+ const lines = fs.readFileSync(filePath, 'utf-8').split('\n');
175
+ if (startLine < 1 || endLine > lines.length || startLine > endLine) {
176
+ throw new Error(`行号范围无效 (1-${lines.length})`);
177
+ }
178
+ return lines.slice(startLine - 1, endLine).join('\n');
179
+ }
180
+ //# sourceMappingURL=javascript.js.map