zen-gitsync 1.2.7 → 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 CHANGED
@@ -1,12 +1,7 @@
1
1
 
2
2
  # zen-gitsync
3
3
 
4
- `zen-gitsync` 是一个简单的命令行工具,用于自动化 Git 提交和推送操作。只需在控制台输入 `g`,即可自动执行 `git add`、`git commit` 和 `git push` 操作,极大提升 Git 工作流程的效率。
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.2.7",
4
- "description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
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",
@@ -31,7 +32,9 @@
31
32
  },
32
33
  "homepage": "https://github.com/xz333221/zen-gitsync#readme",
33
34
  "dependencies": {
35
+ "boxen": "^8.0.1",
34
36
  "chalk": "^5.4.1",
37
+ "cli-table3": "^0.6.5",
35
38
  "ora": "^8.1.1",
36
39
  "string-width": "^7.2.0"
37
40
  }
package/src/gitCommit.js CHANGED
@@ -6,6 +6,7 @@ import {coloredLog} from './utils/index.js';
6
6
  import readline from 'readline'
7
7
  import ora from 'ora';
8
8
  import chalk from 'chalk';
9
+ import boxen from 'boxen';
9
10
 
10
11
  let timer = null
11
12
 
@@ -50,6 +51,7 @@ class GitCommit {
50
51
  constructor(options) {
51
52
  this.statusOutput = null
52
53
  this.exit = options.exit
54
+ this.commitMessage = `提交`
53
55
  this.init()
54
56
  }
55
57
 
@@ -72,15 +74,18 @@ class GitCommit {
72
74
  }
73
75
  return
74
76
  }
75
- this.execSyncGitCommand('git diff')
77
+
78
+ const no_diff = process.argv.find(arg => arg.startsWith('--no-diff'))
79
+ if(!no_diff){
80
+ this.execSyncGitCommand('git diff')
81
+ }
76
82
 
77
83
  // 检查命令行参数,判断是否有 -y 参数
78
84
  const autoCommit = process.argv.includes('-y');
79
- let commitMessage = '提交'; // 默认提交信息
80
85
 
81
86
  if (!autoCommit) {
82
87
  // 如果没有 -y 参数,则等待用户输入提交信息
83
- commitMessage = await question('请输入提交信息:');
88
+ this.commitMessage = await question('请输入提交信息:');
84
89
  }
85
90
 
86
91
  // 执行 git add .
@@ -88,7 +93,7 @@ class GitCommit {
88
93
 
89
94
  // 执行 git commit
90
95
  if (this.statusOutput.includes('Untracked files:') || this.statusOutput.includes('Changes not staged for commit') || this.statusOutput.includes('Changes to be committed')) {
91
- this.execSyncGitCommand(`git commit -m "${commitMessage || '提交'}"`)
96
+ this.execSyncGitCommand(`git commit -m "${this.commitMessage || '提交'}"`)
92
97
  }
93
98
 
94
99
  // 检查是否需要拉取更新
@@ -109,8 +114,20 @@ class GitCommit {
109
114
  this.execGitCommand('git push', {
110
115
  spinner
111
116
  }, (error, stdout, stderr) => {
112
- console.log(chalk.green.bold('✔ SUCCESS: 提交完成。')); // 使用绿色对勾图标
113
- this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
117
+
118
+ // 使用 boxen 绘制带边框的消息
119
+ let msg = ` SUCCESS: 提交完成
120
+ message: ${this.commitMessage || '提交'}
121
+ time: ${new Date().toLocaleString()} `
122
+ const message = chalk.green.bold(msg);
123
+ const box = boxen(message, {
124
+ // borderStyle: 'round', // 方框的样式
125
+ // borderColor: 'whiteBright', // 边框颜色
126
+ // backgroundColor: 'black', // 背景颜色
127
+ });
128
+
129
+ console.log(box); // 打印带有边框的消息
130
+ // this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
114
131
  this.exec_exit();
115
132
  })
116
133
  }
@@ -140,7 +157,7 @@ class GitCommit {
140
157
  options.spinner.stop();
141
158
  }
142
159
  if (error) {
143
- coloredLog(command, error)
160
+ coloredLog(command, error, 'error')
144
161
  return
145
162
  }
146
163
  if (stdout) {
@@ -15,6 +15,48 @@
15
15
  // const boxedMessage = boxen(message, options);
16
16
  // console.log(boxedMessage);
17
17
  import stringWidth from 'string-width';
18
+ import Table from 'cli-table3';
19
+ import chalk from 'chalk';
20
+
21
+ const printTableWithHeaderUnderline = (head, content, style) => {
22
+ // 获取终端的列数(宽度)
23
+ const terminalWidth = process.stdout.columns;
24
+
25
+ // 计算表格的宽度,保证至少有 2 个字符留给边框
26
+ const tableWidth = terminalWidth - 2; // 左右边框和分隔符的宽度
27
+
28
+ // 计算每列的宽度
29
+ const colWidths = [tableWidth]; // 只有一列,因此宽度设置为终端宽度
30
+
31
+ if (!style) {
32
+ style = {
33
+ // head: ['cyan'], // 表头文字颜色为cyan
34
+ // border: ['white'], // 边框颜色
35
+ compact: true, // 启用紧凑模式,去掉不必要的空白
36
+ }
37
+ }
38
+ // 创建表格实例
39
+ const table = new Table({
40
+ head: [head], // 只有一个表头
41
+ colWidths, // 使用动态计算的列宽
42
+ style: style,
43
+ wordWrap: true, // 启用自动换行
44
+ });
45
+
46
+ // 向表格中添加不同颜色的行
47
+ // eg:
48
+ // table.push(
49
+ // [chalk.red('张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三')],
50
+ // [chalk.green('李四')],
51
+ // );
52
+ content.forEach(item => {
53
+ table.push([item]);
54
+ })
55
+
56
+ console.log(table.toString()); // 输出表格
57
+ };
58
+
59
+ // printTableWithHeaderUnderline();
18
60
 
19
61
  const colors = [
20
62
  '\x1b[31m', // 红色
@@ -34,71 +76,76 @@ function getRandomColor() {
34
76
  function resetColor() {
35
77
  return '\x1b[0m';
36
78
  }
37
-
38
- const coloredLog = (...args) => {
39
- const color = getRandomColor();
40
- // 获取控制台的宽度
41
- const terminalWidth = process.stdout.columns;
42
-
43
- const start_line = '┌' + '─'.repeat(terminalWidth - 2) + '┐';
44
- const end_line = '' + '─'.repeat(terminalWidth - 2) + '┘';
45
- let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
46
- console.log(start_line);
47
- _args.map(async (arg, i) => {
48
- let _color = color;
49
- let trim_arg = arg.trim();
50
- if (_args[0] === 'git diff' && arg.startsWith('-')) {
51
- _color = '\x1b[31m';
52
- }
53
- if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
54
- _color = '\x1b[31m';
55
- }
56
- if (_args[0] === 'git diff' && arg.startsWith('+')) {
57
- _color = '\x1b[32m';
58
- }
59
- if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
60
- _color = '\x1b[32m';
61
- }
62
- if (_args[0] === 'git status' && trim_arg.startsWith('deleted:')) {
63
- _color = '\x1b[31m';
64
- }
65
- if (_args[0] === 'git diff' && arg.startsWith('@@ ')) {
66
- _color = '\x1b[36m';
67
- }
68
- // 测试边框
69
- let fix_end = ''
70
- let length = stringWidth(arg);
71
- // if (length < terminalWidth) {
72
- let fix2 = 0
73
- if (
74
- _args[0] === 'git status' && trim_arg.startsWith('modified:')
75
- || _args[0] === 'git status' && trim_arg.startsWith('deleted:')
76
- || _args[0] === 'git status' && trim_arg.startsWith('new file:')
77
- ) {
78
- 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
+ }
79
94
  }
80
- if (i === 0) {
81
- fix2 = 2
95
+ break;
96
+ case 'git diff':
97
+ if (str.startsWith('-')) {
98
+ color = 'red'
82
99
  }
83
- let repeatLen = terminalWidth - length - 3 - fix2
84
- if (repeatLen < 0) {
85
- // repeatLen += terminalWidth
86
- repeatLen = repeatLen % terminalWidth + terminalWidth
100
+ if (str.startsWith('+')) {
101
+ color = 'green'
87
102
  }
88
- fix_end = ' '.repeat(repeatLen)
89
- fix_end += "│"
90
- // }
91
- // console.log(`fix_end ==> `, fix_end)
92
- if (i === 0) {
93
- console.log(`│ \x1b[1m\x1b[34m> ${arg}\x1b[22m\x1b[39m${fix_end}`);
94
- let mid = '├' + '─'.repeat(terminalWidth - 2) + '┤';
95
- console.log(mid);
96
- } else {
97
- if (arg.trim().length > 0) {
98
- console.log(`│${_color} ${arg}${resetColor()}${fix_end}`);
103
+ if (str.startsWith('@@ ')) {
104
+ color = 'cyan'
99
105
  }
100
- }
101
- });
102
- console.log(end_line);
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
+
103
150
  }
104
151
  export {coloredLog};