zen-gitsync 1.2.0 → 1.2.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/package.json +12 -4
- package/src/gitCommit.js +16 -63
- package/src/utils/index.js +91 -0
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zen-gitsync",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
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": {
|
|
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
|
-
|
|
4
|
-
|
|
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
|
-
|
|
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
|
|
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 .')
|
|
@@ -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,91 @@
|
|
|
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
|
+
const start_line = '┌' + '——'.repeat(terminalWidth / 2 - 1) + '┐';
|
|
45
|
+
const end_line = '└' + '——'.repeat(terminalWidth / 2 - 1) + '┘';
|
|
46
|
+
let _args = args.map(arg => arg.split('\n')).flat().filter(arg => arg.trim() !== '');
|
|
47
|
+
console.log(start_line);
|
|
48
|
+
_args.map(async (arg, i) => {
|
|
49
|
+
let _color = color;
|
|
50
|
+
let trim_arg = arg.trim();
|
|
51
|
+
if (_args[0] === 'git diff' && arg.startsWith('-')) {
|
|
52
|
+
_color = '\x1b[31m';
|
|
53
|
+
}
|
|
54
|
+
if (_args[0] === 'git status' && trim_arg.startsWith('new file:')) {
|
|
55
|
+
_color = '\x1b[31m';
|
|
56
|
+
}
|
|
57
|
+
if (_args[0] === 'git diff' && arg.startsWith('+')) {
|
|
58
|
+
_color = '\x1b[32m';
|
|
59
|
+
}
|
|
60
|
+
if (_args[0] === 'git status' && trim_arg.startsWith('modified:')) {
|
|
61
|
+
_color = '\x1b[32m';
|
|
62
|
+
}
|
|
63
|
+
if (_args[0] === 'git diff' && arg.startsWith('@@ ')) {
|
|
64
|
+
_color = '\x1b[36m';
|
|
65
|
+
}
|
|
66
|
+
// 测试边框
|
|
67
|
+
let fix_end = ''
|
|
68
|
+
let length = stringWidth(arg);
|
|
69
|
+
if (length < terminalWidth) {
|
|
70
|
+
let fix2 = 0
|
|
71
|
+
if(trim_arg.startsWith('modified:')){
|
|
72
|
+
fix2 = 6
|
|
73
|
+
}
|
|
74
|
+
if (i === 0) {
|
|
75
|
+
fix2 = 2
|
|
76
|
+
}
|
|
77
|
+
fix_end = ' '.repeat(terminalWidth - length - 4 - fix2)
|
|
78
|
+
fix_end += "|"
|
|
79
|
+
}
|
|
80
|
+
// console.log(`fix_end ==> `, fix_end)
|
|
81
|
+
if (i === 0) {
|
|
82
|
+
console.log(`| \x1b[1m\x1b[34m> ${arg}\x1b[22m\x1b[39m${fix_end}`);
|
|
83
|
+
} else {
|
|
84
|
+
if(arg.trim().length > 0) {
|
|
85
|
+
console.log(`|${_color} ${arg}${fix_end}`, resetColor());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
console.log(end_line);
|
|
90
|
+
}
|
|
91
|
+
export { coloredLog };
|