zen-gitsync 1.2.0 → 1.2.2

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
@@ -7,7 +7,6 @@
7
7
 
8
8
  - 一键执行 `git add`、`git commit`、`git push`
9
9
  - 使用交互式命令行输入提交信息
10
- - 提交信息颜色随机显示
11
10
 
12
11
  ## 安装
13
12
 
package/package.json CHANGED
@@ -1,16 +1,21 @@
1
1
  {
2
2
  "name": "zen-gitsync",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "控制台输入g,回车,输入提交内容,自动执行git add+commit+push",
5
5
  "main": "index.js",
6
+ "type": "module",
6
7
  "scripts": {
7
- "g": "node ./src/gitCommit.js"
8
+ "g": "node ./src/gitCommit.js",
9
+ "g:y": "node ./src/gitCommit.js -y"
8
10
  },
9
11
  "files": [
10
12
  "src/gitCommit.js",
13
+ "src/utils/index.js",
11
14
  "package.json"
12
15
  ],
13
- "bin": { "g": "./src/gitCommit.js" },
16
+ "bin": {
17
+ "g": "./src/gitCommit.js"
18
+ },
14
19
  "repository": {
15
20
  "type": "git",
16
21
  "url": "git+https://github.com/xz333221/zen-gitsync.git"
@@ -21,5 +26,8 @@
21
26
  "bugs": {
22
27
  "url": "https://github.com/xz333221/zen-gitsync/issues"
23
28
  },
24
- "homepage": "https://github.com/xz333221/zen-gitsync#readme"
29
+ "homepage": "https://github.com/xz333221/zen-gitsync#readme",
30
+ "dependencies": {
31
+ "string-width": "^7.2.0"
32
+ }
25
33
  }
package/src/gitCommit.js CHANGED
@@ -1,7 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const {exec, execSync} = require('child_process')
4
- const os = require('os');
3
+ import {exec, execSync} from 'child_process'
4
+ import os from 'os'
5
+ import { coloredLog } from './utils/index.js';
6
+ import readline from 'readline'
7
+
5
8
 
6
9
  const judgePlatform = () => {
7
10
  // 判断是否是 Windows 系统
@@ -16,7 +19,7 @@ const judgePlatform = () => {
16
19
  };
17
20
 
18
21
  // 有时候有乱码呢123神奇
19
- const readline = require('readline')
22
+
20
23
  const rl = readline.createInterface({
21
24
  input: process.stdin,
22
25
  output: process.stdout
@@ -30,62 +33,6 @@ function rlPromisify(fn) {
30
33
 
31
34
  const question = rlPromisify(rl.question.bind(rl))
32
35
 
33
- const colors = [
34
- '\x1b[31m', // 红色
35
- '\x1b[32m', // 绿色
36
- '\x1b[33m', // 黄色
37
- '\x1b[34m', // 蓝色
38
- '\x1b[35m', // 紫色
39
- '\x1b[36m', // 青色
40
- ];
41
-
42
- function getRandomColor() {
43
- return `\x1b[0m`;
44
- // const randomIndex = Math.floor(Math.random() * colors.length);
45
- // return colors[randomIndex];
46
- }
47
-
48
- function resetColor() {
49
- return '\x1b[0m';
50
- }
51
-
52
- function coloredLog(...args) {
53
- const color = getRandomColor();
54
- // 获取控制台的宽度
55
- const terminalWidth = process.stdout.columns;
56
-
57
- // 创建与控制台宽度相同的横线
58
- const line = '-'.repeat(terminalWidth);
59
- let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
60
- console.log(line);
61
- _args.map((arg, i) => {
62
- let _color = color;
63
- let trim_arg = arg.trim();
64
- if (_args[0] === 'git diff' && arg.startsWith('-')) {
65
- _color = '\x1b[31m';
66
- }
67
- if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
68
- _color = '\x1b[31m';
69
- }
70
- if (_args[0] === 'git diff' && arg.startsWith('+')) {
71
- _color = '\x1b[32m';
72
- }
73
- if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
74
- _color = '\x1b[32m';
75
- }
76
- if (_args[0] === 'git diff' && arg.startsWith('@@ ')) {
77
- _color = '\x1b[36m';
78
- }
79
- if (i === 0) {
80
- console.log(`|\x1b[1m\x1b[34m ${arg}⬇️\x1b[22m\x1b[39m`);
81
- } else {
82
- console.log(`|${_color} ${arg}`, resetColor());
83
- }
84
- });
85
- console.log(line);
86
- }
87
-
88
-
89
36
  class GitCommit {
90
37
  constructor() {
91
38
  this.statusOutput = null
@@ -103,8 +50,14 @@ class GitCommit {
103
50
  }
104
51
  this.execSyncGitCommand('git diff')
105
52
 
106
- // 等待用户输入提交信息
107
- const commitMessage = await question('请输入提交信息:')
53
+ // 检查命令行参数,判断是否有 -y 参数
54
+ const autoCommit = process.argv.includes('-y');
55
+ let commitMessage = '提交'; // 默认提交信息
56
+
57
+ if (!autoCommit) {
58
+ // 如果没有 -y 参数,则等待用户输入提交信息
59
+ commitMessage = await question('请输入提交信息:');
60
+ }
108
61
 
109
62
  // 执行 git add .
110
63
  this.statusOutput.includes('(use "git add <file>') && this.execSyncGitCommand('git add .')
@@ -122,7 +75,7 @@ class GitCommit {
122
75
  // this.execSyncGitCommand(`git push`);
123
76
  this.execGitCommand('git push', {}, (error, stdout, stderr) => {
124
77
  console.log('提交完成。')
125
- this.execSyncGitCommand(`git log -n 1 --pretty=format:"%H %d %ad%n%B" --date=iso`)
78
+ this.execSyncGitCommand(`git log -n 1 --pretty=format:"%B%n%h %d%n%ad" --date=iso`)
126
79
  process.exit();
127
80
  })
128
81
  } catch (e) {
@@ -133,7 +86,7 @@ class GitCommit {
133
86
  execSyncGitCommand(command, options = {}) {
134
87
  try {
135
88
  let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
136
- cwd = process.argv[2] || cwd
89
+ // cwd = process.argv[2] || cwd
137
90
  const output = execSync(command, {encoding, maxBuffer, cwd})
138
91
  let result = output.trim()
139
92
  coloredLog(command, result)
@@ -146,7 +99,7 @@ class GitCommit {
146
99
 
147
100
  execGitCommand(command, options = {}, callback) {
148
101
  let {encoding = 'utf-8', maxBuffer = 30 * 1024 * 1024, cwd = process.cwd()} = options
149
- cwd = process.argv[2] || cwd
102
+ // cwd = process.argv[2] || cwd
150
103
  exec(command, {encoding, maxBuffer, cwd}, (error, stdout, stderr) => {
151
104
  if (error) {
152
105
  coloredLog(command, error)
@@ -0,0 +1,95 @@
1
+ // const chalk = require('chalk');
2
+ // const boxen = require('boxen');
3
+ // const message = chalk.blue('git diff') + '\n' +
4
+ // chalk.red('- line1') + '\n' +
5
+ // chalk.green('+ line2') + '\n' +
6
+ // chalk.cyan('@@ line diff @@');
7
+ //
8
+ // const options = {
9
+ // padding: 1,
10
+ // margin: 1,
11
+ // borderStyle: 'round', // 可以选择 'single', 'double', 'round' 等边框样式
12
+ // borderColor: 'yellow'
13
+ // };
14
+ //
15
+ // const boxedMessage = boxen(message, options);
16
+ // console.log(boxedMessage);
17
+ import stringWidth from 'string-width';
18
+
19
+ const colors = [
20
+ '\x1b[31m', // 红色
21
+ '\x1b[32m', // 绿色
22
+ '\x1b[33m', // 黄色
23
+ '\x1b[34m', // 蓝色
24
+ '\x1b[35m', // 紫色
25
+ '\x1b[36m', // 青色
26
+ ];
27
+
28
+ function getRandomColor() {
29
+ return `\x1b[0m`;
30
+ // const randomIndex = Math.floor(Math.random() * colors.length);
31
+ // return colors[randomIndex];
32
+ }
33
+
34
+ function resetColor() {
35
+ return '\x1b[0m';
36
+ }
37
+ const coloredLog = (...args) =>{
38
+ const color = getRandomColor();
39
+ // 获取控制台的宽度
40
+ const terminalWidth = process.stdout.columns;
41
+
42
+ // 创建与控制台宽度相同的横线
43
+ // const line = '-'.repeat(terminalWidth);
44
+ let str = '├─'
45
+ let str2 = '│'
46
+ const start_line = '┌' + '──'.repeat(terminalWidth / 2 - 1) + '┐';
47
+ const end_line = '└' + '──'.repeat(terminalWidth / 2 - 1) + '┘';
48
+ let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
49
+ console.log(start_line);
50
+ _args.map(async (arg, i) => {
51
+ let _color = color;
52
+ let trim_arg = arg.trim();
53
+ if (_args[0] === 'git diff' && arg.startsWith('-')) {
54
+ _color = '\x1b[31m';
55
+ }
56
+ if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
57
+ _color = '\x1b[31m';
58
+ }
59
+ if (_args[0] === 'git diff' && arg.startsWith('+')) {
60
+ _color = '\x1b[32m';
61
+ }
62
+ if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
63
+ _color = '\x1b[32m';
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(trim_arg.startsWith('modified:')){
74
+ fix2 = 6
75
+ }
76
+ if (i === 0) {
77
+ fix2 = 2
78
+ }
79
+ fix_end = ' '.repeat(terminalWidth - length - 4 - fix2)
80
+ fix_end += "│"
81
+ }
82
+ // console.log(`fix_end ==> `, fix_end)
83
+ if (i === 0) {
84
+ console.log(`│ \x1b[1m\x1b[34m> ${arg}\x1b[22m\x1b[39m${fix_end}`);
85
+ let mid = '├─' + '──'.repeat(terminalWidth / 2 - 2) + '─┤';
86
+ console.log(mid);
87
+ } else {
88
+ if(arg.trim().length > 0) {
89
+ console.log(`│${_color} ${arg}${resetColor()}${fix_end}`);
90
+ }
91
+ }
92
+ });
93
+ console.log(end_line);
94
+ }
95
+ export { coloredLog };