zen-gitsync 1.2.8 → 1.3.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/README.md +6 -6
- package/package.json +4 -3
- package/src/gitCommit.js +14 -7
- package/src/utils/index.js +87 -74
package/README.md
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
# zen-gitsync
|
|
3
3
|
|
|
4
|
-
`zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g
|
|
5
|
-
|
|
6
|
-
## 特性
|
|
7
|
-
|
|
8
|
-
- 一键执行 `git add`、`git commit`、`git push`
|
|
9
|
-
- 使用交互式命令行输入提交信息
|
|
4
|
+
`zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g`,并输入提交内容,即可自动执行 `git add`、`git commit` 和 `git push` 操作,极大提升 Git 工作流程的效率。
|
|
10
5
|
|
|
11
6
|
## 安装
|
|
12
7
|
|
|
@@ -62,3 +57,8 @@ $ g --cwd=./
|
|
|
62
57
|
```shell
|
|
63
58
|
start /min cmd /k "g -y --path=你要同步的文件夹 --interval"
|
|
64
59
|
```
|
|
60
|
+
|
|
61
|
+
#### 不显示git diff内容
|
|
62
|
+
```shell
|
|
63
|
+
$ g --no-diff
|
|
64
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zen-gitsync",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "一个 git 自动查看差异并提交的工具",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"g:y": "node ./src/gitCommit.js -y",
|
|
10
10
|
"g:y:interval": "node ./src/gitCommit.js -y --interval",
|
|
11
11
|
"g:y:interval10": "node ./src/gitCommit.js -y --interval=10",
|
|
12
|
-
"g:cwd": "node ./src/gitCommit.js --path=./"
|
|
12
|
+
"g:cwd": "node ./src/gitCommit.js --path=./",
|
|
13
|
+
"g:no-diff": "node ./src/gitCommit.js --no-diff"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"src/gitCommit.js",
|
package/src/gitCommit.js
CHANGED
|
@@ -51,6 +51,7 @@ class GitCommit {
|
|
|
51
51
|
constructor(options) {
|
|
52
52
|
this.statusOutput = null
|
|
53
53
|
this.exit = options.exit
|
|
54
|
+
this.commitMessage = `提交`
|
|
54
55
|
this.init()
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -73,15 +74,18 @@ class GitCommit {
|
|
|
73
74
|
}
|
|
74
75
|
return
|
|
75
76
|
}
|
|
76
|
-
|
|
77
|
+
|
|
78
|
+
const no_diff = process.argv.find(arg => arg.startsWith('--no-diff'))
|
|
79
|
+
if(!no_diff){
|
|
80
|
+
this.execSyncGitCommand('git diff')
|
|
81
|
+
}
|
|
77
82
|
|
|
78
83
|
// 检查命令行参数,判断是否有 -y 参数
|
|
79
84
|
const autoCommit = process.argv.includes('-y');
|
|
80
|
-
let commitMessage = '提交'; // 默认提交信息
|
|
81
85
|
|
|
82
86
|
if (!autoCommit) {
|
|
83
87
|
// 如果没有 -y 参数,则等待用户输入提交信息
|
|
84
|
-
commitMessage = await question('请输入提交信息:');
|
|
88
|
+
this.commitMessage = await question('请输入提交信息:');
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
// 执行 git add .
|
|
@@ -89,7 +93,7 @@ class GitCommit {
|
|
|
89
93
|
|
|
90
94
|
// 执行 git commit
|
|
91
95
|
if (this.statusOutput.includes('Untracked files:') || this.statusOutput.includes('Changes not staged for commit') || this.statusOutput.includes('Changes to be committed')) {
|
|
92
|
-
this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
|
|
96
|
+
this.execSyncGitCommand(`git commit -m "${this.commitMessage || '提交'}"`)
|
|
93
97
|
}
|
|
94
98
|
|
|
95
99
|
// 检查是否需要拉取更新
|
|
@@ -112,7 +116,10 @@ class GitCommit {
|
|
|
112
116
|
}, (error, stdout, stderr) => {
|
|
113
117
|
|
|
114
118
|
// 使用 boxen 绘制带边框的消息
|
|
115
|
-
|
|
119
|
+
let msg = ` SUCCESS: 提交完成
|
|
120
|
+
message: ${this.commitMessage || '提交'}
|
|
121
|
+
time: ${new Date().toLocaleString()} `
|
|
122
|
+
const message = chalk.green.bold(msg);
|
|
116
123
|
const box = boxen(message, {
|
|
117
124
|
// borderStyle: 'round', // 方框的样式
|
|
118
125
|
// borderColor: 'whiteBright', // 边框颜色
|
|
@@ -120,7 +127,7 @@ class GitCommit {
|
|
|
120
127
|
});
|
|
121
128
|
|
|
122
129
|
console.log(box); // 打印带有边框的消息
|
|
123
|
-
this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
|
|
130
|
+
// this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
|
|
124
131
|
this.exec_exit();
|
|
125
132
|
})
|
|
126
133
|
}
|
|
@@ -150,7 +157,7 @@ class GitCommit {
|
|
|
150
157
|
options.spinner.stop();
|
|
151
158
|
}
|
|
152
159
|
if (error) {
|
|
153
|
-
coloredLog(command, error)
|
|
160
|
+
coloredLog(command, error, 'error')
|
|
154
161
|
return
|
|
155
162
|
}
|
|
156
163
|
if (stdout) {
|
package/src/utils/index.js
CHANGED
|
@@ -18,32 +18,40 @@ import stringWidth from 'string-width';
|
|
|
18
18
|
import Table from 'cli-table3';
|
|
19
19
|
import chalk from 'chalk';
|
|
20
20
|
|
|
21
|
-
const printTableWithHeaderUnderline = () => {
|
|
21
|
+
const printTableWithHeaderUnderline = (head, content, style) => {
|
|
22
22
|
// 获取终端的列数(宽度)
|
|
23
23
|
const terminalWidth = process.stdout.columns;
|
|
24
24
|
|
|
25
25
|
// 计算表格的宽度,保证至少有 2 个字符留给边框
|
|
26
|
-
const tableWidth = terminalWidth -
|
|
26
|
+
const tableWidth = terminalWidth - 2; // 左右边框和分隔符的宽度
|
|
27
27
|
|
|
28
28
|
// 计算每列的宽度
|
|
29
29
|
const colWidths = [tableWidth]; // 只有一列,因此宽度设置为终端宽度
|
|
30
30
|
|
|
31
|
+
if (!style) {
|
|
32
|
+
style = {
|
|
33
|
+
// head: ['cyan'], // 表头文字颜色为cyan
|
|
34
|
+
// border: ['white'], // 边框颜色
|
|
35
|
+
compact: true, // 启用紧凑模式,去掉不必要的空白
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 创建表格实例
|
|
31
39
|
const table = new Table({
|
|
32
|
-
head: [
|
|
40
|
+
head: [head], // 只有一个表头
|
|
33
41
|
colWidths, // 使用动态计算的列宽
|
|
34
|
-
style:
|
|
35
|
-
head: ['cyan'], // 表头文字颜色为cyan
|
|
36
|
-
border: ['yellow'], // 边框颜色为黄色
|
|
37
|
-
compact: true, // 启用紧凑模式,去掉不必要的空白
|
|
38
|
-
},
|
|
42
|
+
style: style,
|
|
39
43
|
wordWrap: true, // 启用自动换行
|
|
40
44
|
});
|
|
41
45
|
|
|
42
46
|
// 向表格中添加不同颜色的行
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
)
|
|
47
|
+
// eg:
|
|
48
|
+
// table.push(
|
|
49
|
+
// [chalk.red('张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三')],
|
|
50
|
+
// [chalk.green('李四')],
|
|
51
|
+
// );
|
|
52
|
+
content.forEach(item => {
|
|
53
|
+
table.push([item]);
|
|
54
|
+
})
|
|
47
55
|
|
|
48
56
|
console.log(table.toString()); // 输出表格
|
|
49
57
|
};
|
|
@@ -68,71 +76,76 @@ function getRandomColor() {
|
|
|
68
76
|
function resetColor() {
|
|
69
77
|
return '\x1b[0m';
|
|
70
78
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
|
|
88
|
-
_color = '\x1b[31m';
|
|
89
|
-
}
|
|
90
|
-
if (_args[0] === 'git diff' && arg.startsWith('+')) {
|
|
91
|
-
_color = '\x1b[32m';
|
|
92
|
-
}
|
|
93
|
-
if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
|
|
94
|
-
_color = '\x1b[32m';
|
|
95
|
-
}
|
|
96
|
-
if (_args[0] === 'git status' && trim_arg.startsWith('deleted:')) {
|
|
97
|
-
_color = '\x1b[31m';
|
|
98
|
-
}
|
|
99
|
-
if (_args[0] === 'git diff' && arg.startsWith('@@ ')) {
|
|
100
|
-
_color = '\x1b[36m';
|
|
101
|
-
}
|
|
102
|
-
// 测试边框
|
|
103
|
-
let fix_end = ''
|
|
104
|
-
let length = stringWidth(arg);
|
|
105
|
-
// if (length < terminalWidth) {
|
|
106
|
-
let fix2 = 0
|
|
107
|
-
if (
|
|
108
|
-
_args[0] === 'git status' && trim_arg.startsWith('modified:')
|
|
109
|
-
|| _args[0] === 'git status' && trim_arg.startsWith('deleted:')
|
|
110
|
-
|| _args[0] === 'git status' && trim_arg.startsWith('new file:')
|
|
111
|
-
) {
|
|
112
|
-
fix2 = 6
|
|
79
|
+
const calcColor = (commandLine, str) => {
|
|
80
|
+
let color = 'reset'
|
|
81
|
+
switch (commandLine) {
|
|
82
|
+
case 'git status':
|
|
83
|
+
if (str.startsWith('\t')) {
|
|
84
|
+
color = 'red'
|
|
85
|
+
if (str.startsWith('new file:')) {
|
|
86
|
+
color = 'red'
|
|
87
|
+
}
|
|
88
|
+
if (str.startsWith('modified:')) {
|
|
89
|
+
color = 'green'
|
|
90
|
+
}
|
|
91
|
+
if (str.startsWith('deleted:')) {
|
|
92
|
+
color = 'red'
|
|
93
|
+
}
|
|
113
94
|
}
|
|
114
|
-
|
|
115
|
-
|
|
95
|
+
break;
|
|
96
|
+
case 'git diff':
|
|
97
|
+
if (str.startsWith('-')) {
|
|
98
|
+
color = 'red'
|
|
116
99
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// repeatLen += terminalWidth
|
|
120
|
-
repeatLen = repeatLen % terminalWidth + terminalWidth
|
|
100
|
+
if (str.startsWith('+')) {
|
|
101
|
+
color = 'green'
|
|
121
102
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
// }
|
|
125
|
-
// console.log(`fix_end ==> `, fix_end)
|
|
126
|
-
if (i === 0) {
|
|
127
|
-
console.log(`│ \x1b[1m\x1b[34m> ${arg}\x1b[22m\x1b[39m${fix_end}`);
|
|
128
|
-
let mid = '├' + '─'.repeat(terminalWidth - 2) + '┤';
|
|
129
|
-
console.log(mid);
|
|
130
|
-
} else {
|
|
131
|
-
if (arg.trim().length > 0) {
|
|
132
|
-
console.log(`│${_color} ${arg}${resetColor()}${fix_end}`);
|
|
103
|
+
if (str.startsWith('@@ ')) {
|
|
104
|
+
color = 'cyan'
|
|
133
105
|
}
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
return color
|
|
109
|
+
}
|
|
110
|
+
const tableLog = (commandLine, content, type) => {
|
|
111
|
+
let handle_commandLine = `> ${commandLine}`
|
|
112
|
+
let head = chalk.bold.blue(handle_commandLine)
|
|
113
|
+
let style = {
|
|
114
|
+
// head: ['cyan'], // 表头文字颜色为cyan
|
|
115
|
+
// border: ['whiteBright'], // 边框颜色
|
|
116
|
+
compact: true, // 启用紧凑模式,去掉不必要的空白
|
|
117
|
+
}
|
|
118
|
+
switch (type) {
|
|
119
|
+
case 'error':
|
|
120
|
+
style.head = ['red'];
|
|
121
|
+
content = content.toString().split('\n')
|
|
122
|
+
head = chalk.bold.red(handle_commandLine)
|
|
123
|
+
break;
|
|
124
|
+
case 'log':
|
|
125
|
+
style.head = ['blue'];
|
|
126
|
+
content = content.split('\n')
|
|
127
|
+
break;
|
|
128
|
+
default:
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
content = content.map(item => {
|
|
132
|
+
let fontColor = calcColor(commandLine, item)
|
|
133
|
+
let row = item.replaceAll('\t', ' ')
|
|
134
|
+
return chalk[fontColor](row)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
printTableWithHeaderUnderline(head, content, style)
|
|
138
|
+
}
|
|
139
|
+
const coloredLog = (...args) => {
|
|
140
|
+
// 获取参数内容
|
|
141
|
+
const commandLine = args[0];
|
|
142
|
+
const content = args[1];
|
|
143
|
+
const type = args[2] || 'log';
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
tableLog(commandLine, content, type);
|
|
147
|
+
|
|
148
|
+
// console.log(`args ==> `, args)
|
|
149
|
+
|
|
137
150
|
}
|
|
138
151
|
export {coloredLog};
|