zen-gitsync 1.3.5 → 1.3.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.
- package/README.md +8 -0
- package/package.json +5 -3
- package/src/config.js +29 -2
- package/src/gitCommit.js +60 -26
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zen-gitsync",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.7",
|
|
4
4
|
"description": "一个 git 自动查看差异并提交的工具",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
"g:no-diff": "node ./src/gitCommit.js --no-diff",
|
|
17
17
|
"g:log": "node ./src/gitCommit.js log",
|
|
18
18
|
"g:log5": "node ./src/gitCommit.js log --n=5",
|
|
19
|
-
"g:h": "node ./src/gitCommit.js -h"
|
|
19
|
+
"g:h": "node ./src/gitCommit.js -h",
|
|
20
|
+
"g:get-config": "node ./src/gitCommit.js get-config",
|
|
21
|
+
"g:set-default-message": "node ./src/gitCommit.js --set-default-message=\"提交\""
|
|
20
22
|
},
|
|
21
23
|
"files": [
|
|
22
24
|
"src/*",
|
|
@@ -30,7 +32,7 @@
|
|
|
30
32
|
"type": "git",
|
|
31
33
|
"url": "git+https://github.com/xz333221/zen-gitsync.git"
|
|
32
34
|
},
|
|
33
|
-
"keywords": [],
|
|
35
|
+
"keywords": ["gitsync", "git"],
|
|
34
36
|
"author": "",
|
|
35
37
|
"license": "MIT",
|
|
36
38
|
"bugs": {
|
package/src/config.js
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
|
|
5
|
+
const configPath = path.join(os.homedir(), '.git-commit-tool.json');
|
|
6
|
+
|
|
7
|
+
// 默认配置
|
|
8
|
+
const defaultConfig = {
|
|
9
|
+
defaultCommitMessage: "submit"
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// 异步读取配置文件
|
|
13
|
+
async function loadConfig() {
|
|
14
|
+
try {
|
|
15
|
+
const data = await fs.readFile(configPath, 'utf-8');
|
|
16
|
+
return { ...defaultConfig, ...JSON.parse(data) };
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return defaultConfig;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 异步保存配置
|
|
23
|
+
async function saveConfig(config) {
|
|
24
|
+
await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
|
3
25
|
}
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
loadConfig,
|
|
29
|
+
saveConfig
|
|
30
|
+
};
|
package/src/gitCommit.js
CHANGED
|
@@ -7,8 +7,8 @@ import readline from 'readline'
|
|
|
7
7
|
import ora from 'ora';
|
|
8
8
|
import chalk from 'chalk';
|
|
9
9
|
import boxen from 'boxen';
|
|
10
|
-
import config from './config.js'
|
|
11
|
-
|
|
10
|
+
import config from './config.js';
|
|
11
|
+
const { loadConfig, saveConfig } = config;
|
|
12
12
|
const {defaultCommitMessage} = config
|
|
13
13
|
|
|
14
14
|
let timer = null
|
|
@@ -17,16 +17,18 @@ const showHelp = () => {
|
|
|
17
17
|
Usage: g [options]
|
|
18
18
|
|
|
19
19
|
Options:
|
|
20
|
-
-h, --help
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
--
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
-h, --help Show this help message
|
|
21
|
+
--set-default-message=<msg> Set default commit message
|
|
22
|
+
get-config Show current configuration
|
|
23
|
+
-y Auto commit with default message
|
|
24
|
+
-m <message> Commit message (use quotes if message contains spaces)
|
|
25
|
+
-m=<message> Commit message (use this form without spaces around '=')
|
|
26
|
+
--path=<path> Set custom working directory
|
|
27
|
+
--cwd=<path> Set custom working directory
|
|
28
|
+
--interval=<seconds> Set interval time for automatic commits (in seconds)
|
|
29
|
+
log Show git commit logs
|
|
30
|
+
--n=<number> Number of commits to show with --log
|
|
31
|
+
--no-diff Skip displaying git diff
|
|
30
32
|
|
|
31
33
|
Example:
|
|
32
34
|
g -m "Initial commit" Commit with a custom message
|
|
@@ -49,7 +51,25 @@ Start a background process for automatic commits:
|
|
|
49
51
|
console.log(helpMessage);
|
|
50
52
|
process.exit();
|
|
51
53
|
};
|
|
54
|
+
// 添加配置管理函数
|
|
55
|
+
async function handleConfigCommands() {
|
|
56
|
+
if (process.argv.includes('get-config')) {
|
|
57
|
+
const currentConfig = await loadConfig();
|
|
58
|
+
console.log('Current configuration:');
|
|
59
|
+
console.log(currentConfig);
|
|
60
|
+
process.exit();
|
|
61
|
+
}
|
|
52
62
|
|
|
63
|
+
const setMsgArg = process.argv.find(arg => arg.startsWith('--set-default-message='));
|
|
64
|
+
if (setMsgArg) {
|
|
65
|
+
const newMessage = setMsgArg.split('=')[1];
|
|
66
|
+
const currentConfig = await loadConfig();
|
|
67
|
+
currentConfig.defaultCommitMessage = newMessage;
|
|
68
|
+
await saveConfig(currentConfig);
|
|
69
|
+
console.log(chalk.green(`✓ 默认提交信息已设置为: "${newMessage}"`));
|
|
70
|
+
process.exit();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
53
73
|
const judgePlatform = () => {
|
|
54
74
|
// 判断是否是 Windows 系统
|
|
55
75
|
if (os.platform() === 'win32') {
|
|
@@ -91,8 +111,11 @@ class GitCommit {
|
|
|
91
111
|
constructor(options) {
|
|
92
112
|
this.statusOutput = null
|
|
93
113
|
this.exit = options.exit
|
|
94
|
-
|
|
95
|
-
|
|
114
|
+
// 从配置加载默认提交信息
|
|
115
|
+
loadConfig().then(config => {
|
|
116
|
+
this.commitMessage = config.defaultCommitMessage;
|
|
117
|
+
this.init();
|
|
118
|
+
});
|
|
96
119
|
}
|
|
97
120
|
|
|
98
121
|
exec_exit() {
|
|
@@ -100,6 +123,7 @@ class GitCommit {
|
|
|
100
123
|
process.exit()
|
|
101
124
|
}
|
|
102
125
|
}
|
|
126
|
+
|
|
103
127
|
judgeLog() {
|
|
104
128
|
const logArg = process.argv.find(arg => arg === 'log');
|
|
105
129
|
if (logArg) {
|
|
@@ -107,12 +131,14 @@ class GitCommit {
|
|
|
107
131
|
return true;
|
|
108
132
|
}
|
|
109
133
|
}
|
|
134
|
+
|
|
110
135
|
judgeHelp() {
|
|
111
136
|
if (process.argv.includes('-h') || process.argv.includes('--help')) {
|
|
112
137
|
showHelp();
|
|
113
138
|
return true;
|
|
114
139
|
}
|
|
115
140
|
}
|
|
141
|
+
|
|
116
142
|
execDiff() {
|
|
117
143
|
const no_diff = process.argv.find(arg => arg.startsWith('--no-diff'))
|
|
118
144
|
if (!no_diff) {
|
|
@@ -121,6 +147,7 @@ class GitCommit {
|
|
|
121
147
|
})
|
|
122
148
|
}
|
|
123
149
|
}
|
|
150
|
+
|
|
124
151
|
async execAddAndCommit() {
|
|
125
152
|
// 检查 -m 参数(提交信息)
|
|
126
153
|
const commitMessageArg = process.argv.find(arg => arg.startsWith('-m'));
|
|
@@ -152,8 +179,9 @@ class GitCommit {
|
|
|
152
179
|
this.execSyncGitCommand(`git commit -m "${this.commitMessage || defaultCommitMessage}"`)
|
|
153
180
|
}
|
|
154
181
|
}
|
|
182
|
+
|
|
155
183
|
async judgeRemote() {
|
|
156
|
-
try{
|
|
184
|
+
try {
|
|
157
185
|
const spinner = ora('正在检查远程更新...').start();
|
|
158
186
|
// 检查是否有远程更新
|
|
159
187
|
// 先获取远程最新状态
|
|
@@ -190,20 +218,21 @@ class GitCommit {
|
|
|
190
218
|
spinner.stop();
|
|
191
219
|
console.log(chalk.green('✓ 本地已是最新'));
|
|
192
220
|
}
|
|
193
|
-
}catch (e) {
|
|
221
|
+
} catch (e) {
|
|
194
222
|
// console.log(`e ==> `, e)
|
|
195
223
|
spinner.stop();
|
|
196
224
|
throw new Error(e)
|
|
197
225
|
}
|
|
198
226
|
}
|
|
199
|
-
|
|
200
|
-
|
|
227
|
+
|
|
228
|
+
async execPull() {
|
|
229
|
+
try {
|
|
201
230
|
// 检查是否需要拉取更新
|
|
202
231
|
const spinner = ora('正在拉取代码...').start();
|
|
203
232
|
await this.execGitCommand('git pull', {
|
|
204
233
|
spinner
|
|
205
234
|
})
|
|
206
|
-
}catch (e) {
|
|
235
|
+
} catch (e) {
|
|
207
236
|
console.log(chalk.yellow('⚠️ 拉取远程更新合并失败,可能存在冲突,请手动处理'));
|
|
208
237
|
throw Error(e)
|
|
209
238
|
}
|
|
@@ -214,14 +243,14 @@ class GitCommit {
|
|
|
214
243
|
judgePlatform()
|
|
215
244
|
|
|
216
245
|
// 检查是否有 log 参数
|
|
217
|
-
if(this.judgeLog()) return
|
|
246
|
+
if (this.judgeLog()) return
|
|
218
247
|
|
|
219
248
|
// 检查帮助参数
|
|
220
|
-
if(this.judgeHelp()) return
|
|
249
|
+
if (this.judgeHelp()) return
|
|
221
250
|
|
|
222
251
|
this.statusOutput = this.execSyncGitCommand('git status')
|
|
223
252
|
const hasUnmerged = this.statusOutput.includes('You have unmerged paths');
|
|
224
|
-
if(hasUnmerged){
|
|
253
|
+
if (hasUnmerged) {
|
|
225
254
|
errorLog('错误', '存在未合并的文件,请先解决冲突')
|
|
226
255
|
process.exit(1);
|
|
227
256
|
}
|
|
@@ -239,7 +268,7 @@ class GitCommit {
|
|
|
239
268
|
await this.judgeRemote() // 等待 judgeRemote 完成
|
|
240
269
|
|
|
241
270
|
this.exec_push()
|
|
242
|
-
}else{
|
|
271
|
+
} else {
|
|
243
272
|
if (this.statusOutput.includes('use "git push')) {
|
|
244
273
|
this.exec_push()
|
|
245
274
|
} else if (this.statusOutput.includes('use "git pull')) {
|
|
@@ -250,6 +279,7 @@ class GitCommit {
|
|
|
250
279
|
}
|
|
251
280
|
}
|
|
252
281
|
} catch (e) {
|
|
282
|
+
// console.log(`e ==> `, e)
|
|
253
283
|
// console.log(`e.message ==> `, e.message)
|
|
254
284
|
// 应该提供更具体的错误信息
|
|
255
285
|
// console.error('Git operation failed:', e.message);
|
|
@@ -313,8 +343,8 @@ class GitCommit {
|
|
|
313
343
|
}
|
|
314
344
|
|
|
315
345
|
execSyncGitCommand(command, options = {}) {
|
|
346
|
+
let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, head = command, log = true} = options
|
|
316
347
|
try {
|
|
317
|
-
let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, head = command, log = true} = options
|
|
318
348
|
let cwd = getCwd()
|
|
319
349
|
const output = execSync(command, {encoding, maxBuffer, cwd})
|
|
320
350
|
if (options.spinner) {
|
|
@@ -358,7 +388,11 @@ class GitCommit {
|
|
|
358
388
|
})
|
|
359
389
|
}
|
|
360
390
|
}
|
|
361
|
-
|
|
391
|
+
// 在 judgeInterval 函数前添加配置命令处理
|
|
392
|
+
async function main() {
|
|
393
|
+
await handleConfigCommands();
|
|
394
|
+
judgeInterval();
|
|
395
|
+
}
|
|
362
396
|
const judgeInterval = () => {
|
|
363
397
|
// 判断是否有 --interval 参数
|
|
364
398
|
const intervalArg = process.argv.find(arg => arg.startsWith('--interval'));
|
|
@@ -393,4 +427,4 @@ const judgeInterval = () => {
|
|
|
393
427
|
}
|
|
394
428
|
};
|
|
395
429
|
|
|
396
|
-
|
|
430
|
+
main()
|