zen-gitsync 1.2.8 → 1.3.1

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,14 @@ $ 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
+ ```
65
+
66
+ #### 格式化打印git log
67
+ ```shell
68
+ $ g log
69
+ $ g log --n=5
70
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "1.2.8",
4
- "description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
3
+ "version": "1.3.1",
4
+ "description": "一个 git 自动查看差异并提交的工具",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -9,7 +9,10 @@
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",
14
+ "g:log": "node ./src/gitCommit.js log",
15
+ "g:log5": "node ./src/gitCommit.js log --n=5"
13
16
  },
14
17
  "files": [
15
18
  "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
 
@@ -64,6 +65,12 @@ class GitCommit {
64
65
  try {
65
66
  judgePlatform()
66
67
 
68
+ const logArg = process.argv.find(arg => arg === 'log');
69
+ if (logArg) {
70
+ await this.printGitLog(); // 如果有 log 参数,打印 Git 提交记录
71
+ return;
72
+ }
73
+
67
74
  this.statusOutput = this.execSyncGitCommand('git status')
68
75
  if (this.statusOutput.includes('nothing to commit, working tree clean')) {
69
76
  if (this.statusOutput.includes('use "git push')) {
@@ -73,15 +80,20 @@ class GitCommit {
73
80
  }
74
81
  return
75
82
  }
76
- this.execSyncGitCommand('git diff')
83
+
84
+ const no_diff = process.argv.find(arg => arg.startsWith('--no-diff'))
85
+ if(!no_diff){
86
+ this.execSyncGitCommand('git diff --color=always', {
87
+ head: `git diff`
88
+ })
89
+ }
77
90
 
78
91
  // 检查命令行参数,判断是否有 -y 参数
79
92
  const autoCommit = process.argv.includes('-y');
80
- let commitMessage = '提交'; // 默认提交信息
81
93
 
82
94
  if (!autoCommit) {
83
95
  // 如果没有 -y 参数,则等待用户输入提交信息
84
- commitMessage = await question('请输入提交信息:');
96
+ this.commitMessage = await question('请输入提交信息:');
85
97
  }
86
98
 
87
99
  // 执行 git add .
@@ -89,7 +101,7 @@ class GitCommit {
89
101
 
90
102
  // 执行 git commit
91
103
  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 || '提交'}"`)
104
+ this.execSyncGitCommand(`git commit -m "${this.commitMessage || '提交'}"`)
93
105
  }
94
106
 
95
107
  // 检查是否需要拉取更新
@@ -102,6 +114,29 @@ class GitCommit {
102
114
  console.log(`e ==> `, e)
103
115
  }
104
116
  }
117
+ async printGitLog() {
118
+ let n = 20;
119
+ let logArg = process.argv.find(arg => arg.startsWith('--n='));
120
+ if (logArg) {
121
+ n = parseInt(logArg.split('=')[1], 10);
122
+ }
123
+ const logCommand = `git log -n ${n} --pretty=format:"%C(green)%h%C(reset) | %C(cyan)%an%C(reset) | %C(yellow)%ad%C(reset) | %C(blue)%D%C(reset) | %C(magenta)%s%C(reset)" --date=format:"%Y-%m-%d %H:%M" --graph --decorate --color`
124
+ try {
125
+ const logOutput = this.execSyncGitCommand(logCommand, {
126
+ head: `git log`
127
+ });
128
+ // // 格式化输出 Git 提交记录
129
+ // const box = boxen(chalk.green.bold(logOutput), {
130
+ // borderStyle: 'round',
131
+ // borderColor: 'cyan',
132
+ // backgroundColor: 'black',
133
+ // });
134
+ // console.log(box); // 打印优雅的 Git 提交记录
135
+ } catch (error) {
136
+ console.error('无法获取 Git 提交记录:', error.message);
137
+ }
138
+ this.exec_exit(); // 打印完成后退出
139
+ }
105
140
 
106
141
  exec_push() {
107
142
  // 执行 git push
@@ -112,7 +147,10 @@ class GitCommit {
112
147
  }, (error, stdout, stderr) => {
113
148
 
114
149
  // 使用 boxen 绘制带边框的消息
115
- const message = chalk.green.bold(' SUCCESS: 提交完成 ');
150
+ let msg = ` SUCCESS: 提交完成
151
+ message: ${this.commitMessage || '提交'}
152
+ time: ${new Date().toLocaleString()} `
153
+ const message = chalk.green.bold(msg);
116
154
  const box = boxen(message, {
117
155
  // borderStyle: 'round', // 方框的样式
118
156
  // borderColor: 'whiteBright', // 边框颜色
@@ -120,21 +158,21 @@ class GitCommit {
120
158
  });
121
159
 
122
160
  console.log(box); // 打印带有边框的消息
123
- this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
161
+ // this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
124
162
  this.exec_exit();
125
163
  })
126
164
  }
127
165
 
128
166
  execSyncGitCommand(command, options = {}) {
129
167
  try {
130
- let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024} = options
168
+ let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, head = command} = options
131
169
  let cwd = getCwd()
132
170
  const output = execSync(command, {encoding, maxBuffer, cwd})
133
171
  if(options.spinner){
134
172
  options.spinner.stop();
135
173
  }
136
174
  let result = output.trim()
137
- coloredLog(command, result)
175
+ coloredLog(head, result)
138
176
  return result
139
177
  } catch (e) {
140
178
  console.log(`执行命令出错 ==> `, command, e)
@@ -150,7 +188,7 @@ class GitCommit {
150
188
  options.spinner.stop();
151
189
  }
152
190
  if (error) {
153
- coloredLog(command, error)
191
+ coloredLog(command, error, 'error')
154
192
  return
155
193
  }
156
194
  if (stdout) {
@@ -18,32 +18,57 @@ 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 - 4; // 4 是左右边框和分隔符的宽度
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: [chalk.reset()], // 边框颜色
35
+ compact: true, // 启用紧凑模式,去掉不必要的空白
36
+ }
37
+ }
38
+ // 创建表格实例
31
39
  const table = new Table({
32
- head: ['Name'], // 只有一个表头
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, // 启用自动换行
44
+ // chars: {
45
+ // 'top': '═', // 顶部边框使用长横线
46
+ // 'top-mid': '╤', // 顶部连接符
47
+ // 'top-left': '╔', // 左上角
48
+ // 'top-right': '╗', // 右上角
49
+ // 'bottom': '═', // 底部边框
50
+ // 'bottom-mid': '╧', // 底部连接符
51
+ // 'bottom-left': '╚',// 左下角
52
+ // 'bottom-right': '╝',// 右下角
53
+ // 'left': '║', // 左边框
54
+ // 'left-mid': '╟', // 左连接符
55
+ // 'mid': '═', // 中间分隔符
56
+ // 'mid-mid': '╪', // 中间连接符
57
+ // 'right': '║', // 右边框
58
+ // 'right-mid': '╢', // 右连接符
59
+ // 'middle': '│' // 中间内容的边界
60
+ // }
40
61
  });
41
62
 
42
63
  // 向表格中添加不同颜色的行
43
- table.push(
44
- [chalk.red('张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三')],
45
- [chalk.green('李四')],
46
- );
64
+ // eg:
65
+ // table.push(
66
+ // [chalk.red('张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三张三')],
67
+ // [chalk.green('李四')],
68
+ // );
69
+ content.forEach(item => {
70
+ table.push([item]);
71
+ })
47
72
 
48
73
  console.log(table.toString()); // 输出表格
49
74
  };
@@ -68,71 +93,71 @@ function getRandomColor() {
68
93
  function resetColor() {
69
94
  return '\x1b[0m';
70
95
  }
96
+ const calcColor = (commandLine, str) => {
97
+ let color = 'reset'
98
+ switch (commandLine) {
99
+ case 'git status':
100
+ if (str.startsWith('\t')) {
101
+ color = 'red'
102
+ if (str.startsWith('new file:')) {
103
+ color = 'red'
104
+ }
105
+ if (str.startsWith('modified:')) {
106
+ color = 'green'
107
+ }
108
+ if (str.startsWith('deleted:')) {
109
+ color = 'red'
110
+ }
111
+ }
112
+ break;
113
+ case 'git diff':
114
+ // if (str.startsWith('---')) {
115
+ // color = 'red'
116
+ // }
117
+ // if (str.startsWith('+++')) {
118
+ // color = 'green'
119
+ // }
120
+ // if (str.startsWith('@@ ')) {
121
+ // color = 'cyan'
122
+ // }
123
+ break;
124
+ }
125
+ return color
126
+ }
127
+ const tableLog = (commandLine, content, type) => {
128
+ let handle_commandLine = `> ${commandLine}`
129
+ let head = chalk.bold.blue(handle_commandLine)
130
+ let style = {
131
+ // head: ['cyan'], // 表头文字颜色为cyan
132
+ border: [chalk.reset()], // 边框颜色
133
+ compact: true, // 启用紧凑模式,去掉不必要的空白
134
+ }
135
+ switch (type) {
136
+ case 'error':
137
+ style.head = ['red'];
138
+ content = content.toString().split('\n')
139
+ head = chalk.bold.red(handle_commandLine)
140
+ break;
141
+ case 'common':
142
+ style.head = ['blue'];
143
+ content = content.split('\n')
144
+ break;
145
+ default:
146
+ break;
147
+ }
148
+ content = content.map(item => {
149
+ let fontColor = calcColor(commandLine, item)
150
+ let row = item.replaceAll('\t', ' ')
151
+ return chalk[fontColor](row)
152
+ })
71
153
 
154
+ printTableWithHeaderUnderline(head, content, style)
155
+ }
72
156
  const coloredLog = (...args) => {
73
- const color = getRandomColor();
74
- // 获取控制台的宽度
75
- const terminalWidth = process.stdout.columns;
76
-
77
- const start_line = '┌' + '─'.repeat(terminalWidth - 2) + '┐';
78
- const end_line = '└' + '─'.repeat(terminalWidth - 2) + '┘';
79
- let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
80
- console.log(start_line);
81
- _args.map(async (arg, i) => {
82
- let _color = color;
83
- let trim_arg = arg.trim();
84
- if (_args[0] === 'git diff' && arg.startsWith('-')) {
85
- _color = '\x1b[31m';
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
113
- }
114
- if (i === 0) {
115
- fix2 = 2
116
- }
117
- let repeatLen = terminalWidth - length - 3 - fix2
118
- if (repeatLen < 0) {
119
- // repeatLen += terminalWidth
120
- repeatLen = repeatLen % terminalWidth + terminalWidth
121
- }
122
- fix_end = ' '.repeat(repeatLen)
123
- fix_end += "│"
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}`);
133
- }
134
- }
135
- });
136
- console.log(end_line);
157
+ // 获取参数内容
158
+ const commandLine = args[0];
159
+ const content = args[1];
160
+ const type = args[2] || 'common';
161
+ tableLog(commandLine, content, type);
137
162
  }
138
163
  export {coloredLog};